SocialTransitionStrategy.java 13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.movement.modular.transition;

23
import java.util.*;
24
25
26
27
28
import java.util.Map.Entry;

import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.common.SimHostComponent;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
29
import de.tud.kom.p2psim.api.topology.Topology;
30
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
31
32
33
34
import de.tud.kom.p2psim.api.topology.social.SocialView;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.modular.attraction.AttractionPoint;
35
import de.tudarmstadt.maki.simonstrator.api.Binder;
36
37
38
39
40
41
42
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Randoms;

/**
 * This is a {@link TransitionStrategy} for the Social Case. It will be try to
 * build groups based on the {@link SocialView} information. For this, it tries
43
 * to assignment the given {@link SimLocationActuator} objects to the given
44
45
46
47
48
49
50
51
52
53
54
55
56
 * {@link AttractionPoint}s. For the {@link SocialView}, it is required a
 * {@link #socialId}, to find the right {@link SocialView}.
 * 
 * <br>
 * 
 * The Strategy has the parameter of {@link #socialFactor},
 * {@link #minPauseTime} and {@link #maxPauseTime}. The socialFactor should be a
 * value between 0 and 1. It gives the probability for a social based transition
 * or the transition to a random {@link AttractionPoint}. If the social based
 * transition is selected, then will be used a scoring to find the right
 * {@link AttractionPoint}. For that, it will be used only the AttractionPoints,
 * of the hosts, which are in the same SocialCluster or are SocialNeighbors. For
 * this AttractionPoints it will be find the highest scoring, which is to found
57
 * in {@link #score(SimLocationActuator, AttractionPoint, List, List, List, List)}
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 * .
 * 
 * <br>
 * 
 * After the finding of the next {@link AttractionPoint}, it will be scheduled
 * an Event, with a delay between min- and maxPauseTime. After this delay, it
 * will be tried to assign a new {@link AttractionPoint} like the described
 * above.
 * 
 * 
 * @author Christoph Muenker
 * @version 1.0, 02.07.2013
 */
public class SocialTransitionStrategy implements TransitionStrategy,
		EventHandler {

	private String socialId = null;

	private SocialView socialView;

78
	private List<SimLocationActuator> comps = new Vector<SimLocationActuator>();
79
80
81

	private List<AttractionPoint> aPoints = new Vector<AttractionPoint>();

82
	private Map<SimLocationActuator, AttractionPoint> assignments = new HashMap<SimLocationActuator, AttractionPoint>();
83

84
	private Map<SimLocationActuator, Set<AttractionPoint>> favoritePlaces = new HashMap<SimLocationActuator, Set<AttractionPoint>>();
85

86
	private Map<SimLocationActuator, SimHost> mapMsHost = new HashMap<SimLocationActuator, SimHost>();
87

88
	private Map<SimHost, SimLocationActuator> mapHostMs = new HashMap<SimHost, SimLocationActuator>();
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

	private double minPauseTime = Simulator.MINUTE_UNIT * 0.5;

	private double maxPauseTime = Simulator.MINUTE_UNIT * 100;

	private double socialFactor = 0.8;

	private long numberOfFavoritePlaces = 4;

	private Random rand;

	private PositionVector worldDimension;

	private boolean init = false;

	public SocialTransitionStrategy() {
		this.rand = Randoms.getRandom(SocialTransitionStrategy.class);
	}

	private void init() {
		if (!init) {
			if (socialId == null) {
				throw new ConfigurationException(
						"SocialId is not set, to find the needed SocialView!");
			}

115
116
			socialView = Binder.getComponentOrNull(Topology.class)
					.getSocialView(socialId);
117
118
119
120
121
122
123
124
125
126
127

			if (socialView == null) {
				throw new ConfigurationException(
						"Cannot find the right socialView. Is the socialId correct?");
			}

			if (minPauseTime > maxPauseTime) {
				throw new ConfigurationException(
						"MinPauseTime should be smaller then maxPauseTime.");
			}

128
129
			worldDimension = Binder.getComponentOrNull(Topology.class)
					.getWorldDimensions();
130
131
132
133
134
135
136
137
138
			init = true;
		}
	}

	public void setSocialId(String socialId) {
		this.socialId = socialId;
	}

	@Override
139
140
	public Map<SimLocationActuator, AttractionPoint> getAssignments() {
		return new HashMap<SimLocationActuator, AttractionPoint>(assignments);
141
142
143
144
145
146
147
148
149
	}

	@Override
	public void setAttractionPoints(List<AttractionPoint> attractionPoints) {
		init();
		aPoints.addAll(attractionPoints);
	}

	@Override
150
	public void addComponent(SimLocationActuator ms) {
151
152
153
154
155
156
157
158
		comps.add(ms);
		mappingHost(ms);
		// assign the ms to an attractionPoint, which is near to the ms
		// position.
		// TODO: needed? We do Transition as next, and this will delete the
		// assignment..
		AttractionPoint nearest = aPoints.iterator().next();
		for (AttractionPoint aPoint : aPoints) {
159
160
161
			if (nearest.getRealPosition()
					.distanceTo(ms.getRealPosition()) > aPoint.getRealPosition()
							.distanceTo(ms.getRealPosition())) {
162
163
164
165
166
167
168
169
170
171
				nearest = aPoint;
			}
		}
		assignments.put(ms, nearest);

		assignFavoritePlaces(ms);

		doTransition(ms);
	}

172
	public void doTransition(SimLocationActuator ms) {
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
		List<AttractionPoint> apFavorites = getFavoritePlaces(ms);
		List<AttractionPoint> apFriends = getFriendsPlaces(ms);
		List<AttractionPoint> apClusters = getClusterPlaces(ms);
		List<AttractionPoint> apRandom = getRandomPlaces(ms,
				(int) Math.max(aPoints.size() * 0.2, 5));

		AttractionPoint ap = null;
		if (rand.nextDouble() < socialFactor) {
			ap = findHighestScore(ms, apFavorites, apFriends, apClusters,
					apRandom);
		} else {
			List<AttractionPoint> aps = new ArrayList<AttractionPoint>();
			aps.addAll(apRandom);
			aps.addAll(apFavorites);
			ap = aps.get(rand.nextInt(apRandom.size()));
		}
		assignments.put(ms, ap);

		Event.scheduleWithDelay(getPauseTime(), this, ms, 0);
	}

194
	private AttractionPoint findHighestScore(SimLocationActuator ms,
195
196
			List<AttractionPoint> apFavorites, List<AttractionPoint> apFriends,
			List<AttractionPoint> apClusters, List<AttractionPoint> apRandom) {
197
		Set<AttractionPoint> aps = new LinkedHashSet<AttractionPoint>();
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
		aps.addAll(apFavorites);
		aps.addAll(apFriends);
		aps.addAll(apClusters);
		aps.addAll(apRandom);

		double maxScore = 0;
		AttractionPoint maxAp = null;
		for (AttractionPoint ap : aps) {
			double score = score(ms, ap, apFavorites, apFriends, apClusters,
					apRandom);
			// System.out.println(score);
			if (score > maxScore) {
				maxScore = score;
				maxAp = ap;
			}
		}
		return maxAp;
	}

	/**
218
	 * Score the given AttractionPoint for the given {@link SimLocationActuator}. <br>
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	 * (clusterScore/#NodesInAp + friendsScore + 1/#NodesInAp) * socialFactor +
	 * (distanceScore + penalty) + (1-socialFactor) <br>
	 * 
	 * clusterScore = 1 if one is in the same cluster in this AP<br>
	 * friendsScore = 1 if one friend is in the same AP<br>
	 * penalty = -1 if AP the actually AP is <br>
	 * distance = 1 - (distance / maxDistance)
	 * 
	 * @param ms
	 * @param ap
	 * @param apFavorites
	 * @param apFriends
	 * @param apClusters
	 * @param apRandom
	 * @return
	 */
235
	private double score(SimLocationActuator ms, AttractionPoint ap,
236
237
			List<AttractionPoint> apFavorites, List<AttractionPoint> apFriends,
			List<AttractionPoint> apClusters, List<AttractionPoint> apRandom) {
238
		double distance = ms.getRealPosition().distanceTo(ap.getRealPosition());
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
		double distanceScore = 1 - (distance / worldDimension.getLength());

		double clusterScore = 0;
		double friendsScore = 0;
		if (apClusters.contains(ap)) {
			if (occurence(ap, apClusters) > 3) {
				// occurence give the number of other peers in this AP
				clusterScore = 1.0 / (occurence(ap, apClusters) - 1);
			} else {
				clusterScore = 1.0;
			}
		}
		if (apFriends.contains(ap)) {
			if (occurence(ap, apFriends) > 3) {
				// occurence give the number of other peers in this AP
				friendsScore = 1.0 / (occurence(ap, apFriends) - 1);
			} else {
				friendsScore = 1.0;
			}
		}
		// penalty for distance
		double penalty = 0;
		if (ap.equals(assignments.get(ms))) {
			penalty = -1;
		}

		return (clusterScore / assignedToAp(ap) + friendsScore + 1.0 / assignedToAp(ap))
				* socialFactor

				+ (distanceScore + penalty) * (1 - socialFactor);
	}

	// counts the number of the AttractionPoint in the list
	private int occurence(AttractionPoint ap, List<AttractionPoint> aps) {
		int i = 0;
		for (AttractionPoint a : aps) {
			if (a.equals(ap)) {
				i++;
			}
		}
		return i;
	}

	private int assignedToAp(AttractionPoint ap) {
		int i = 1;
284
		for (Entry<SimLocationActuator, AttractionPoint> entry : assignments
285
286
287
288
289
290
291
292
				.entrySet()) {
			if (entry.getValue().equals(ap)) {
				i++;
			}
		}
		return i;
	}

293
	private List<AttractionPoint> getRandomPlaces(SimLocationActuator ms,
294
			int number) {
295
		Collections.shuffle(aPoints, rand);
296
297
298
299
300
301
302
303
304
		List<AttractionPoint> result = new Vector<AttractionPoint>();
		Iterator<AttractionPoint> iAP = aPoints.iterator();
		for (int i = 0; i < number && iAP.hasNext(); i++) {
			result.add(iAP.next());
		}

		return result;
	}

305
	private List<AttractionPoint> getClusterPlaces(SimLocationActuator ms) {
306
307
308
309
		List<AttractionPoint> result = new Vector<AttractionPoint>();
		SimHost msHost = mapMsHost.get(ms);

		for (SimHost host : socialView.getCluster(msHost)) {
310
			SimLocationActuator temp = mapHostMs.get(host);
311
312
313
314
315
316
317
318
			if (assignments.get(temp) != null) {
				result.add(assignments.get(temp));
			}
		}

		return result;
	}

319
	private List<AttractionPoint> getFriendsPlaces(SimLocationActuator ms) {
320
321
322
323
		List<AttractionPoint> result = new Vector<AttractionPoint>();
		SimHost msHost = mapMsHost.get(ms);

		for (SimHost host : socialView.getNeighbors(msHost)) {
324
			SimLocationActuator temp = mapHostMs.get(host);
325
326
327
328
329
330
331
332
			if (assignments.get(temp) != null) {
				result.add(assignments.get(temp));
			}
		}

		return result;
	}

333
	private List<AttractionPoint> getFavoritePlaces(SimLocationActuator ms) {
334
335
336
		return new Vector<AttractionPoint>(favoritePlaces.get(ms));
	}

337
	private AttractionPoint getRandomPlace(SimLocationActuator ms) {
338
		Collections.shuffle(aPoints, rand);
339
340
341
		return aPoints.iterator().next();
	}

342
	private AttractionPoint getNearestPlace(SimLocationActuator ms,
343
344
345
346
347
348
349
			List<AttractionPoint> aps) {
		if (aps.isEmpty()) {
			return null;
		}
		AttractionPoint nearest = aps.iterator().next();

		for (AttractionPoint aPoint : aps) {
350
351
352
			if (nearest.getRealPosition()
					.distanceTo(ms.getRealPosition()) > aPoint.getRealPosition()
							.distanceTo(ms.getRealPosition())
353
354
355
356
357
358
359
					&& !assignments.get(ms).equals(aPoint)) {
				nearest = aPoint;
			}
		}
		return nearest;
	}

360
	private AttractionPoint getNearestFavoritePlace(SimLocationActuator ms) {
361
362
363
364
		Set<AttractionPoint> fps = favoritePlaces.get(ms);
		AttractionPoint nearest = fps.iterator().next();

		for (AttractionPoint aPoint : fps) {
365
366
367
			if (nearest.getRealPosition()
					.distanceTo(ms.getRealPosition()) > aPoint.getRealPosition()
							.distanceTo(ms.getRealPosition())
368
369
370
371
372
373
374
					&& !assignments.get(ms).equals(aPoint)) {
				nearest = aPoint;
			}
		}
		return nearest;
	}

375
	private void assignFavoritePlaces(SimLocationActuator ms) {
376
		Set<AttractionPoint> msFavoritePlaces = new LinkedHashSet<AttractionPoint>();
377
378
379
380
381
382
383
384
385
386
387
		LinkedList<AttractionPoint> temp = new LinkedList<AttractionPoint>(
				aPoints);
		Collections.shuffle(temp, rand);
		for (int i = 0; i < numberOfFavoritePlaces; i++) {
			if (!temp.isEmpty()) {
				msFavoritePlaces.add(temp.removeFirst());
			}
		}
		favoritePlaces.put(ms, msFavoritePlaces);
	}

388
	private void mappingHost(SimLocationActuator ms) {
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
		SimHostComponent comp = (SimHostComponent) ms;
		SimHost host = comp.getHost();

		assert host != null;

		mapHostMs.put(host, ms);
		mapMsHost.put(ms, host);
	}

	protected long getPauseTime() {
		return (long) ((rand.nextDouble() * (maxPauseTime - minPauseTime)) + minPauseTime);
	}

	public void setMinPauseTime(long minPauseTime) {
		if (minPauseTime < 0) {
			throw new ConfigurationException(
					"MinPauseTime should be bigger then 0!");
		}
		this.minPauseTime = minPauseTime;
	}

	public void setMaxPauseTime(long maxPauseTime) {
		if (maxPauseTime < 0) {
			throw new ConfigurationException(
					"MaxPauseTime should be bigger then 0!");
		}
		this.maxPauseTime = maxPauseTime;
	}

	@Override
	public void eventOccurred(Object se, int type) {
420
		doTransition((SimLocationActuator) se);
421
422
423
424
425
426
427
428
429
430
431
	}

	public void setSocialFactor(double socialFactor) {
		if (socialFactor < 0 || socialFactor > 1) {
			throw new ConfigurationException(
					"socialFactor should be between 0 and 1!");
		}
		this.socialFactor = socialFactor;
	}

}