AbstractGroupForming.java 10.9 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.modularosm.groups.groupforming;

Julian Zobel's avatar
wip    
Julian Zobel committed
23
import java.util.LinkedHashMap;
24
25
26
27
import java.util.Map;
import java.util.Random;
import java.util.Set;

Julian Zobel's avatar
wip    
Julian Zobel committed
28
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
29
30
31
32
33
34
35
36
37
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.util.PositionVector;
import de.tud.kom.p2psim.impl.util.Tuple;
Julian Zobel's avatar
wip    
Julian Zobel committed
38
39
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
40
41
42
43
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;
Julian Zobel's avatar
wip    
Julian Zobel committed
44
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
45
46
47
48

/**
 * Abstract class implementation for implementing GroupFormingStrategies.
 * 
49
50
 * @author Marcel Verst, Julian Zobel
 * @version 1.1, 30.01.2020
51
52
53
54
55
56
57
58
59
 */
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;
Julian Zobel's avatar
wip    
Julian Zobel committed
60
	
61
62
63
64
65
	// GROUP VARIABLES FROM CONFIGS
	protected boolean enableGroups;
	protected int minGroupSize;
	protected int maxGroupSize;
	protected double probabilityToJoin;
Julian Zobel's avatar
wip    
Julian Zobel committed
66
67
68
69
	protected int maxNumberOfGroups;	
	
	protected long groupRejoinWaitTime;
	protected long groupFormationSetupDelay;
70
	private long groupFormationDelay;
71
72
73

	protected IAttractionPointHostCounter hostCounter;

Julian Zobel's avatar
wip    
Julian Zobel committed
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
	@XMLConfigurableConstructor({"enableGroups", "maxNumberOfGroups", "minGroupSize", "maxGroupSize", "probabilityToJoin", "groupFormationSetupDelay", "groupFormationDelay","groupRejoinWaitTime"})
	public AbstractGroupForming(boolean enableGroups, int maxNumberOfGroups, int minGroupSize, int maxGroupSize, double probabilityToJoin, long groupFormationSetupDelay, long groupFormationDelay, long groupRejoinWaitTime) {
		
		this.enableGroups = enableGroups;
		
		if(enableGroups) {
			if (probabilityToJoin < 0 || probabilityToJoin > 1.0) {
				throw new ConfigurationException(
						"GroupForming: probabilityToJoin must be between 0.0 and 1.0!");
			}
			
			if (maxNumberOfGroups <= 0) {
				throw new ConfigurationException(
						"GroupForming: maxNumberOfGroups must be >= 1!");
			}
			
			if (minGroupSize < 0) {
				throw new ConfigurationException(
						"GroupForming: minGroupSize must be >= 1!");
			}
			
			if (maxGroupSize < minGroupSize) {
				throw new ConfigurationException(
						"GroupForming: maxGroupSize cannot be bigger than minGroupSize!");
			}
			
			if (groupFormationSetupDelay <= 0) {
				throw new ConfigurationException(
						"GroupForming: groupFormationSetupDelay must be > 0!");
			}
			
			if (groupFormationDelay < 0) {
				throw new ConfigurationException(
						"GroupForming: groupFormationDelay must be >= 1!");
			}
			
			if (groupRejoinWaitTime < 0) {
				throw new ConfigurationException(
						"GroupForming: groupRejoinWaitTime must be >= 1!");
			}
		}
		
		this.maxNumberOfGroups = maxNumberOfGroups;
		this.minGroupSize = minGroupSize;
		this.maxGroupSize = maxGroupSize;
		this.probabilityToJoin = probabilityToJoin;		
		this.groupFormationSetupDelay = groupFormationSetupDelay;
		this.groupFormationDelay = groupFormationDelay;
		this.groupRejoinWaitTime = groupRejoinWaitTime;
	}
	
	@Override
	public void initialize(SocialGroupMovementModel movementModel) {
		this.movementModel = movementModel;
		
		groupCon = MovementGroupContainer.getInstance();		

		// Initialize instances of other classes		
		this.hostCounter = movementModel.getAttractionPointHostCounter();
		
		stayDuration = new LinkedHashMap<INodeID, Tuple<Long,Long>>();

		for(SimLocationActuator host : movementModel.getAllLocationActuators()) {
			stayDuration.put(host.getHost().getId(), new Tuple<Long, Long>(0L, Time.getCurrentTime()));			
		}
		
		Event.scheduleWithDelay(groupFormationSetupDelay, new EventHandler() {			
			@Override
			public void eventOccurred(Object content, int type) {
				triggerGroupFormation();				
			}
		}, null, 0);
	}
	
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
	/**
	 * Returns a time delay between removal and creation of a group. A random part of up to half of the 
	 * stated formation delay is added to/substracted from the formation delay.
	 * 
	 * @return
	 */
	protected long rndGroupFormationDelay() {		
		return (long) ((rand.nextDouble() + 0.5) * groupFormationDelay);		
	}
	
	/**
	 * Returns a random integer value between the minimum and maximumn group size boundaries 
	 * with an additional upper boundary. 
	 *  
	 * @return
	 */
	protected int rndGroupSize(int max) {			
		return Math.min(rndGroupSize(), max);
	}
	
	/**
	 * Returns a random integer value between the minimum and maximumn group size boundaries.
	 *  
	 * @return
	 */
	protected int rndGroupSize() {
		return rand.nextInt(maxGroupSize - minGroupSize) + minGroupSize;
	}
	
Julian Zobel's avatar
wip    
Julian Zobel committed
177
178
	protected abstract void triggerGroupFormation();
	
179
	/**
180
	 * Creates a group with the given set of members.
181
	 * 
182
	 * @param Set<SimLocationActuator> The members which want to form a group.
183
	 */
184
185
	protected void createGroup(Set<SimLocationActuator> members) {		
		SocialMovementGroup group = new SocialMovementGroup(members);
186
	
187
188
189
		movementModel.getAttractionAssignmentStrategy().addComponent(group.getLeader());
		
		AttractionPoint nextDestination =  movementModel.getAttractionAssignmentStrategy().getAssignment(group.getLeader());
190
191
					
		// Add Offset to group destination
192
193
194
		PositionVector destination = movementModel.addGaussianOffsetToPosition(new PositionVector(nextDestination),
				nextDestination.getRadius() / 3);
				
195
		group.setDestination(destination);
Julian Zobel's avatar
wip    
Julian Zobel committed
196
		setGroupMeetingPoint(group);
197
198

		for(SimLocationActuator member : group.getMembers()) {
199
			member.setTargetAttractionPoint(nextDestination);			
200
			if(groupCon.hostWasGroupMember(member)) {
201
202
203
				groupCon.removeLeftGroupAtTimeEntry(member);
			}
		}
Julian Zobel's avatar
wip    
Julian Zobel committed
204
		
205
206
207
208
209
210
211
212
213
		groupCon.addGroup(group);
	}

	/**
	 * Removes a group.
	 * 
	 * @param SocialMovementGroup The group to be removed.
	 * 
	 */
214
215
	@Override
	public void removeGroup(SocialMovementGroup group) {
216
		groupCon.removeGroup(group);
217
218
219
220
221
222
223
		
		Event.scheduleWithDelay(rndGroupFormationDelay(), new EventHandler() {			
			@Override
			public void eventOccurred(Object content, int type) {
				triggerGroupFormation();				
			}
		}, null, 0);
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
	}

	/**
	 * Returns the first AttractionPoint which contains the host with the greatest stayDuration of all hosts located in AttractionPoints.
	 * 
	 * @return AttractionPoint
	 * 
	 */
	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;
	}
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
	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 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
	 */
	protected boolean wantsToJoin() {
		return (rand.nextDouble() <= probabilityToJoin);
	}

	/**
	 * Checks if a host has waited long enough before he is allowed to join groups again.
276
	 * Returns true, if the host has waited long enough, false otherwise.
277
278
279
280
	 * 
	 * @param SimLocationActuator The host to be checked.
	 * @return boolean
	 */
281
282
	protected boolean groupRejoinTimerExpired(SimLocationActuator host) {		
		return groupCon.getTimeSinceHostLeftLastGroup(host) >= groupRejoinWaitTime;		
283
284
285
286
287
288
289
290
291
	}

	/**
	 * 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
	 */
	protected boolean beenInGroup(SimLocationActuator host) {
292
		return groupCon.hostWasGroupMember(host);
293
294
295
296
297
298
299
300
301
302
303
304
305
	}

	/* 
	 * =====================================================================================================
	 * === 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.
	 */
Julian Zobel's avatar
wip    
Julian Zobel committed
306
	protected void setGroupMeetingPoint(SocialMovementGroup group) {
307
308
		PositionVector leaderPos = group.getLeader().getRealPosition();
		
309
310
		BasicAttractionPoint meetingPoint = 
				new BasicAttractionPoint("Group Movement Meeting Point of Leader #"+group.getLeader().getHost().getId(), leaderPos);
311
312
313
314
315
	
		group.setMeetingPoint(meetingPoint);

		// Update target for all group members to meeting point
		for(SimLocationActuator participant : group.getMembers()) {
316
			movementModel.getAttractionAssignmentStrategy().updateTargetAttractionPoint(participant, meetingPoint);
317
		}
318
	}	
Julian Zobel's avatar
wip    
Julian Zobel committed
319
320
321
322
323
	
	@Override
	public int getMinGroupSize() {
		return minGroupSize;
	}
324

Julian Zobel's avatar
wip    
Julian Zobel committed
325
326
327
328
	@Override
	public int getMaxGroupSize() {
		return maxGroupSize;
	}
329
330

}