SocialTransitionStrategy.java 12.1 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * 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.modularosm.transition;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.Vector;

import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.common.SimHostComponent;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.movement.MovementSupported;
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.Topology;
44
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.AttractionPoint;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
 * to assignment the given {@link MovementSupported} objects to the given
 * {@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
 * in {@link #score(MovementSupported, AttractionPoint, List, List, List, List)}
 * .
 * 
 * <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
 */
80
public class SocialTransitionStrategy implements ITransitionStrategy,
81
82
83
84
85
86
87
88
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
		EventHandler {

	private String socialId = null;

	private SocialView socialView;

	private List<MovementSupported> comps = new Vector<MovementSupported>();

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

	private Map<MovementSupported, AttractionPoint> assignments = new HashMap<MovementSupported, AttractionPoint>();

	private Map<MovementSupported, Set<AttractionPoint>> favoritePlaces = new HashMap<MovementSupported, Set<AttractionPoint>>();

	private Map<MovementSupported, SimHost> mapMsHost = new HashMap<MovementSupported, SimHost>();

	private Map<SimHost, MovementSupported> mapHostMs = new HashMap<SimHost, MovementSupported>();

	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!");
			}

			socialView = Topology.getTopology().getSocialView(socialId);

			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.");
			}

			worldDimension = Topology.getWorldDimension();
			init = true;
		}
	}

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

	@Override
	public Map<MovementSupported, AttractionPoint> getAssignments() {
147
148
149
		// FIXME why new? return new HashMap<MovementSupported,
		// AttractionPoint>(assignments);
		return assignments;
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
	}

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

	@Override
	public void addComponent(MovementSupported ms) {
		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) {
			if (nearest.getRealPosition().getDistance(ms.getRealPosition()) > aPoint
					.getRealPosition().getDistance(ms.getRealPosition())) {
				nearest = aPoint;
			}
		}
		assignments.put(ms, nearest);

		assignFavoritePlaces(ms);

		doTransition(ms);
	}

	public void doTransition(MovementSupported ms) {
		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);
	}

	private AttractionPoint findHighestScore(MovementSupported ms,
			List<AttractionPoint> apFavorites, List<AttractionPoint> apFriends,
			List<AttractionPoint> apClusters, List<AttractionPoint> apRandom) {
		Set<AttractionPoint> aps = new HashSet<AttractionPoint>();
		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;
	}

	/**
	 * Score the given AttractionPoint for the given {@link MovementSupported}. <br>
	 * (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
	 */
	private double score(MovementSupported ms, AttractionPoint ap,
			List<AttractionPoint> apFavorites, List<AttractionPoint> apFriends,
			List<AttractionPoint> apClusters, List<AttractionPoint> apRandom) {
		double distance = ms.getRealPosition()
				.getDistance(ap.getRealPosition());
		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;
		for (Entry<MovementSupported, AttractionPoint> entry : assignments
				.entrySet()) {
			if (entry.getValue().equals(ap)) {
				i++;
			}
		}
		return i;
	}

	private List<AttractionPoint> getRandomPlaces(MovementSupported ms,
			int number) {
		Collections.shuffle(aPoints);
		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;
	}

	private List<AttractionPoint> getClusterPlaces(MovementSupported ms) {
		List<AttractionPoint> result = new Vector<AttractionPoint>();
		SimHost msHost = mapMsHost.get(ms);

		for (SimHost host : socialView.getCluster(msHost)) {
			MovementSupported temp = mapHostMs.get(host);
			if (assignments.get(temp) != null) {
				result.add(assignments.get(temp));
			}
		}

		return result;
	}

	private List<AttractionPoint> getFriendsPlaces(MovementSupported ms) {
		List<AttractionPoint> result = new Vector<AttractionPoint>();
		SimHost msHost = mapMsHost.get(ms);

		for (SimHost host : socialView.getNeighbors(msHost)) {
			MovementSupported temp = mapHostMs.get(host);
			if (assignments.get(temp) != null) {
				result.add(assignments.get(temp));
			}
		}

		return result;
	}

	private List<AttractionPoint> getFavoritePlaces(MovementSupported ms) {
		return new Vector<AttractionPoint>(favoritePlaces.get(ms));
	}

	private void assignFavoritePlaces(MovementSupported ms) {
		Set<AttractionPoint> msFavoritePlaces = new HashSet<AttractionPoint>();
		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);
	}

	private void mappingHost(MovementSupported ms) {
		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) {
		doTransition((MovementSupported) se);
	}

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

}