AbstractGroupForming.java 12.2 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
44
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
80
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
147
148
149
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
/*
 * 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.groups.groupforming;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.SocialGroupMovementModel;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.BasicAttractionPoint;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.hostcount.IAttractionPointHostCounter;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.SocialMovementGroup;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.MovementGroupContainer;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.ITransitionStrategy;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.util.Tuple;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;

/**
 * Abstract class implementation for implementing GroupFormingStrategies.
 * 
 * @author Marcel Verst
 * @version 1.0, 22.11.2018
 */
public abstract class AbstractGroupForming implements IGroupFormingBehavior {
	protected Random rand = Randoms.getRandom(AbstractGroupForming.class);	
	
	protected SocialGroupMovementModel movementModel;	
	protected MovementGroupContainer groupCon;	
	
	// Holds time information of nodes for the groups (stay duration, last update)
	protected Map<INodeID, Tuple<Long,Long>> stayDuration;

	// GROUP VARIABLES FROM CONFIGS
	protected boolean enableGroups;
	protected int minGroupSize;
	protected int maxGroupSize;
	protected double probabilityToJoin;
	protected int maxNumberOfGroups;
	protected long waitTime;
	protected long setupTime;

	protected IAttractionPointHostCounter hostCounter;

	/**
	 * Creates a group with the given set of participants.
	 * 
	 * @param Set<SimLocationActuator> The participants which want to form a group.
	 * 
	 * @author Marcel Verst
	 */
	public void createGroup(Set<SimLocationActuator> candidates) {
		SocialMovementGroup group = new SocialMovementGroup(candidates);
		// Guarantee that the new destination of a group is not equal to the current AttractionPoint where the group is created
		// This would be the case if the destination of the group leader is the current position of the group.
		AttractionPoint leaderTarget = group.getLeader().getCurrentTargetAttractionPoint();
		AttractionPoint poiDestination = null;
		do {
			poiDestination = getRandomAttractionPoint();
		} while(poiDestination == leaderTarget);
		
		// Add Offset to destination
		PositionVector destination = new PositionVector(poiDestination.getLongitudeOrX(), poiDestination.getLatitudeOrY());
		double apRadius = poiDestination.getRadius();
		
		PositionVector offset = new PositionVector(rand.nextDouble() * apRadius / 3, rand.nextDouble() * apRadius / 3);
		destination.plus(offset);
		
		group.setDestination(destination);
		setMP(group);

		for(SimLocationActuator member : group.getMembers()) {
			member.setTargetAttractionPoint(poiDestination);
			groupCon.addHasBeenInGroupEntry(member, true);
			if(groupCon.getLeftGroupAtTime().containsKey(member)) {
//				System.out.println(groupCon.getLeftGroupAtTime().get(member));
				groupCon.removeLeftGroupAtTimeEntry(member);
			}
		}

		groupCon.addGroup(group);
	}

	/**
	 * Returns a random {@link AttractionPoint} out of the set of all AttractionPoints
	 * 
	 * @return AttractionPoint
	 * 
	 * @author Marcel Verst
	 */
	protected AttractionPoint getRandomAttractionPoint() {		
		int item = rand.nextInt(IAttractionGenerator.attractionPoints.size());		
		return IAttractionGenerator.attractionPoints.get(item);
	}

	/**
	 * Removes a group.
	 * 
	 * @param SocialMovementGroup The group to be removed.
	 * 
	 * @author Marcel Verst
	 */
	public void removeGroup(SocialMovementGroup group) {
		groupCon.removeGroup(group);
	}

	/**
	 * Returns the first AttractionPoint which contains the host with the greatest stayDuration of all hosts located in AttractionPoints.
	 * 
	 * @return AttractionPoint
	 * 
	 * @author Marcel Verst, Julian Zobel
	 */
	protected AttractionPoint getAttractionPointWithOldestHost() {
		AttractionPoint result = null;
		long maxDuration = 0;
		
		for(AttractionPoint ap : IAttractionGenerator.attractionPoints) {
			for(SimLocationActuator host :  hostCounter.getHostsOfAttractionPoint(ap)) {
				INodeID id = host.getHost().getId();
				long duration = stayDuration.get(id).getA();
				
				if(duration > maxDuration) {					
					result = ap;
					maxDuration = duration;
				}
			}
		}
		
		return result;
	}
	
	/**
	 * Returns an Array of SimLocationActuators sorted in ascending order of stayDuration values.
	 * 
	 * @param Set<SimLocationActuator> The candidates to sort.
	 * @return SimLocationActuator[]
	 * 
	 * @author Marcel Verst
	 */
	public SimLocationActuator[] getHostsSortedByStayDurationAscending(Set<SimLocationActuator> candidates) {
		int size = candidates.size();
		SimLocationActuator[] youngestHostsSorted = new SimLocationActuator[size];

		for(int i = 0; i < size; i++) {
			long largestDuration = Integer.MAX_VALUE;
			SimLocationActuator youngesHost = null;
			for(SimLocationActuator candidate : candidates) {
				INodeID id = candidate.getHost().getId();
				long duration = stayDuration.get(id).getA();
				if(duration <= largestDuration) {
					largestDuration = duration;
					youngesHost = candidate;
				}
			}
			youngestHostsSorted[i] = youngesHost;
			candidates.remove(youngesHost);
		}
		return youngestHostsSorted;
	}
	
	/**
	 * Returns an Array of SimLocationActuators sorted in descending order of stayDuration values.
	 * 
	 * @param Set<SimLocationActuator> The candidates to sort.
	 * @return SimLocationActuator[]
	 * 
	 * @author Marcel Verst
	 */
	public SimLocationActuator[] getHostsSortedByStayDurationDescending(Set<SimLocationActuator> candidates) {
		int size = candidates.size();
		SimLocationActuator[] oldestHostsSorted = new SimLocationActuator[size];

		for(int i = 0; i < size; i++) {
			long largestDuration = 0;
			SimLocationActuator oldestHost = null;
			for(SimLocationActuator candidate : candidates) {
				if(!groupCon.isGroupMember(candidate)) {
					INodeID id = candidate.getHost().getId();
					long duration = stayDuration.get(id).getA();
					if(duration >= largestDuration) {
						largestDuration = duration;
						oldestHost = candidate;
					}
				}
			}
			if(oldestHost != null) {
				oldestHostsSorted[i] = oldestHost;
			}
			candidates.remove(oldestHost);
		}
		return oldestHostsSorted;
	}
	
	protected AttractionPoint getAttractionPointWithMostHosts() {
		AttractionPoint apCandidate = null;
		int size = 0;
		for(AttractionPoint ap : IAttractionGenerator.attractionPoints) {
			int numberOfHostsInAP = hostCounter.getHostCountOfAttractionPoint(ap);
			if(numberOfHostsInAP > size) {
				apCandidate = ap;
				size = numberOfHostsInAP;
			}
		}
		return apCandidate;
	}
	
	

	/**
	 * Checks for a set of candidates which candidate is able to join a group. Returns a subset of group candidates which
	 * are able to form a group with other candidates.
	 * 
	 * @param Set<SimLocationActuator> Set of candidates.
	 * @return Set<SimLocationActuator> Set of group candidates.
	 * 
	 * @author Marcel Verst
	 */
	public Set<SimLocationActuator> getGroupCandidates(Set<SimLocationActuator> candidates){
		Set<SimLocationActuator> groupCandidates = new LinkedHashSet<>();

		for(SimLocationActuator candidate : candidates) {
			// Candidate can only be a group candidate if he is not yet in a group
			if(!groupCon.isGroupMember(candidate)){
				// Further check if the candidate even wants to join (based on probability)
				if(wantsToJoin()) {
					/*
					 * Candidate is not in a group yet and wants to be in a group:
					 * Check if the candidate has been in a group before.
					 */
					// If not, add the candidate to the set of group candidates.
					if(!beenInGroup(candidate)) {
						groupCandidates.add(candidate);
					}
					// Host has already been in a group. Check if the host has waited long enough to join another group.
					else { 
						if(waitedLongEnough(candidate, Time.getCurrentTime() / 1000000)) {
							groupCandidates.add(candidate);
						}
					}
				}
				else {
					/*
					 * The host does not want to join. This decision is counted as an action which has the impact,
					 * that the host can not perform any other action until he waited for a specified amount of time.
					 * This procedure ensures that the host is not asked again every second if he wants to join a group.
					 */
					groupCon.addHasBeenInGroupEntry(candidate, true);
					groupCon.addLeftGroupAtTimeEntry(candidate, Time.getCurrentTime() / 1000000);
				}
			}
		}
		return groupCandidates;
	}

	/**
	 * Checks if a host wants to join a group or not based on a given probability.
	 * Returns true if willing to join, false else.
	 * 
	 * @return boolean
	 * 
	 * @author Marcel Verst
	 */
	protected boolean wantsToJoin() {
		return (rand.nextDouble() <= probabilityToJoin);
	}

	/**
	 * Checks if a host has waited long enough before he is allowed to join groups again.
	 * Returns true, if the host has waited long enough, false else.
	 * 
	 * @param SimLocationActuator The host to be checked.
	 * @param int The current time.
	 * @return boolean
	 * 
	 * @author Marcel Verst
	 */
	protected boolean waitedLongEnough(SimLocationActuator host, long time) {
		return (getWaitingTime(host, time) >= waitTime);
	}

	/**
	 * Returns the time a host has waited since he left a group the last time.
	 * 
	 * @param SimLocacationActuator The host to be checked.
	 * @param Long The current time.
	 * @return Long
	 * 
	 * @author Marcel Verst
	 */
	private long getWaitingTime(SimLocationActuator host, long currentTime) {
		return currentTime - groupCon.getLeftGroupAtTime().get(host);
	}

	/**
	 * Returns true, if a host has been in a group before at least once. False else.
	 * 
	 * @param SimLocacationActuator The host to be checked.
	 * @return Boolean
	 * 
	 * @author Marcel Verst
	 */
	protected boolean beenInGroup(SimLocationActuator host) {
		if(!groupCon.getHasBeenInAGroup().containsKey(host)) {
			groupCon.addHasBeenInGroupEntry(host, false);
		}
		return groupCon.getHasBeenInAGroup().get(host);
	}

	/* 
	 * =====================================================================================================
	 * === MEETING POINT FUNCTIONS
	 * =====================================================================================================
	 */
	/**
	 * Sets the meeting point of a group after the group has been created. Before walking towards the destination,
	 * the group members first have to meet at the meeting point.
	 * 
	 * @param SocialMovementGroup The group to set the meeting point.
	 * 
	 * @author Marcel Verst, Julian Zobel
	 */
	public void setMP(SocialMovementGroup group) {
		PositionVector leaderPos = group.getLeader().getRealPosition();
		
		BasicAttractionPoint meetingPoint = new BasicAttractionPoint("Group Movement Meeting Point of Leader #"+group.getLeader().getHost().getId(), leaderPos);
	
		group.setMeetingPoint(meetingPoint);

		// Update target for all group members to meeting point
		for(SimLocationActuator participant : group.getMembers()) {
			ITransitionStrategy transition = movementModel.getTransition();
			transition.updateTargetAttractionPoint(participant, meetingPoint);
		}
	}


}