SocialGroupMovementModel.java 11.5 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;

23

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.api.topology.movement.local.LocalMovementStrategy;
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.MovementGroupContainer;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.SocialMovementGroup;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.groupencounter.IGroupEncounterBehavior;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.groupforming.IGroupFormingBehavior;
Julian Zobel's avatar
wip    
Julian Zobel committed
39
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.IAttractionAssigmentStrategy;
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
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;

/**
 * 
 * @author Julian Zobel
 * @version 1.0, 28.01.2020
 */
public class SocialGroupMovementModel extends ModularMovementModel {

	protected MovementGroupContainer groupContainer;
	protected IGroupFormingBehavior groupFormingBehavior;
	protected IGroupEncounterBehavior groupEncounterBehavior;
	protected IAttractionPointHostCounter attractionPointHostCounter;
	
	private Set<SimLocationActuator> singleHosts = new LinkedHashSet<SimLocationActuator>();
	private int numberOfSingleHosts;
	
	@Override
	public void initialize() {

		if (!initialized) {
			groupContainer = MovementGroupContainer.getInstance();
			attractionPointHostCounter.initialize(this);
			groupFormingBehavior.initialize(this);
			groupEncounterBehavior.initialize(this);
			
			// Choose single hosts
			if(numberOfSingleHosts > 0) {
				do {
					singleHosts.add(getRandomActuator());
				} while(singleHosts.size() < numberOfSingleHosts);
			}
			
			super.initialize();
		}
	}

	/**
	 * Returns a random SimLocationActuator component from the set of moveable hosts.
	 * 
	 * @return SimLocationActuator
	 */
	private SimLocationActuator getRandomActuator() {
		
		int index = rand.nextInt(moveableHosts.size());
		int i = 0;
		for(SimLocationActuator actuator : moveableHosts) {
			if(i == index) {
				return actuator;
			}
			i++;
		}

		return null;
	}
	
	/**
	 * The movement model. 
	 * 1) Updates the number of hosts within each attraction point.
	 * 2) Creates and deletes groups, if necessary.
	 * 3) Applies encounter action to meeting groups.
	 * 4) Moves the hosts using doGroupMovement, movement style depends on role of the host (Leader, Participant, Single)
	 */
	@Override
Julian Zobel's avatar
wip    
Julian Zobel committed
106
	protected void move() {	
107
108
109
110
111
112
113
114
115
116
		
		// Update the number of hosts within each attraction point.
		attractionPointHostCounter.updateHostCount();

		// Check POIs if groups can be created. Delete groups, if destination reached.
		groupFormingBehavior.manageGroups();
		
		// Checks if groups encounter each other and applies a specified action to these groups.
		Set<SocialMovementGroup[]> encounteringGroups = groupEncounterBehavior.getEncounteringGroups();
		groupEncounterBehavior.handleEncounters(encounteringGroups);
117
118
	
		/*	
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
		 * Moves all nodes according to definition in group movement method
		 */
		for (SimLocationActuator component : moveableHosts) {
			assert currentTargets.containsKey(component);
			doGroupMovement(component, currentTargets.get(component));
		}
		Event.scheduleWithDelay(timeBetweenMoveOperation, this, null, EVENT_MOVE);
	}
		
	/**
	 * Applies movement of the host according to different circumstances:
	 * 
	 * Towards Destination: Host is not a group member || Group has met at meeting point successfully.
	 * Towards MeetingPoint: Host if group member and has not reached meeting point yet.
	 * No movement: Host reached meeting point, but still waits for group members to arrive.
	 * 
	 * @param SimLocationActuator The host to move.
	 * @param PositionVector Destination of the host.
	 */
	protected void doGroupMovement(SimLocationActuator host, PositionVector destination) {
		// Single Host Movement
Julian Zobel's avatar
wip    
Julian Zobel committed
140
		if(singleHosts.contains(host) || !groupContainer.isGroupMember(host)) {			
141
142
143
144
145
			doLocalMovement(host, destination);
		}
				
		// Group Related Movement
		else if(groupContainer.isGroupMember(host)){
146
147
			if(movesToGroupMeetingPoint(host)) {
				if(hostIsAtMeetingPoint(host)) {
148
149
150
151
152
153
154
					if(groupAtMP(host)) {
						SocialMovementGroup group = groupContainer.getGroupOfHost(host);
						AttractionPoint currAp = group.getLeader().getCurrentTargetAttractionPoint();
						PositionVector currDest = group.getDestination();
						group.setMeetingPoint(currDest);
						
						for(SimLocationActuator participant : group.getMembers()) {
Julian Zobel's avatar
wip    
Julian Zobel committed
155
							attractionAssigment.updateTargetAttractionPoint(participant, currAp);
156
157
158
159
160
161
162
163
164
165
166
167
						}
					}
				}
				// MP not reached. Move to MP.
				else {
					PositionVector mp = groupContainer.getGroupOfHost(host).getMeetingPoint();
					doLocalMovement(host, mp);
				}
			}
			// MP reached. Move to destination.
			else {
				if(groupContainer.isLeader(host)) {
Julian Zobel's avatar
wip    
Julian Zobel committed
168
					if(!groupContainer.isWaiting(groupContainer.getGroupOfHost(host))) {						
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
						doLocalMovement(host, destination);
					}
				}
				else {
					followLeader(host);
				}
			}
		}
	}
	
	/* 
	 * =====================================================================================================
	 * === MEETING POINT FUNCTIONS
	 * =====================================================================================================
	 */
	/**
	 * Checks if a host is currently on its way to a meeting point or to its destination.
	 * Returns true if moving towards meeting point, false else.
	 * 
	 * @param SimLocationActuator The host to be checked.
	 * @return Boolean
	 */
191
	private boolean movesToGroupMeetingPoint(SimLocationActuator host) {
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
		PositionVector mp = groupContainer.getGroupOfHost(host).getMeetingPoint();
		PositionVector dest = groupContainer.getGroupOfHost(host).getDestination();
		if(mp != dest) {
			return true;
		}
		return false;
	}

	/**
	 * Returns true, if all group members of a host including the host itself reached the meeting point, false else.
	 * 
	 * @param SimLocationActuator The host to be checked.
	 * @return Boolean
	 */
	private boolean groupAtMP(SimLocationActuator host) {
		Set<SimLocationActuator> participants = groupContainer.getGroupMembers(host);
		int counter = 0;
		for(SimLocationActuator participant : participants) {
210
			if(hostIsAtMeetingPoint(participant)) {
211
212
213
				counter++;
			}
		}
Julian Zobel's avatar
wip    
Julian Zobel committed
214
		if(counter == participants.size()) {			
215
216
217
218
219
220
221
222
223
224
225
226
			return true;
		}
		return false;
	}

	/**
	 * Returns true if the host reached its current meeting point, false else.
	 * 
	 * @param SimLocationActuator The host to be checked.
	 * @return Boolean
	 * 
	 */
227
	private boolean hostIsAtMeetingPoint(SimLocationActuator host) {
228
229
230
231
232
233
		
		PositionVector currMP = groupContainer.getGroupOfHost(host).getMeetingPoint();
		
		// Setting the required distance to the meeting point to maximum of 2 meters
		// This is equivalent to a distance of group members in real life
		double distance = host.getLastLocation().distanceTo(currMP);
234
		if(distance <= 5) {			
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
			return true;
		}
		return false;
	}

	/* 
	 * =====================================================================================================
	 * === MOVEMENT FUNCTIONS
	 * =====================================================================================================
	 */
	

	/**
	 * Host position adapts to leaders position by taking the leader position and adding a small offset
	 * 
	 * @param SimLocationActuator The host to be moved.
	 */
	private void followLeader(SimLocationActuator host) {
		SimLocationActuator leader = groupContainer.getGroupOfHost(host).getLeader();
Julian Zobel's avatar
wip    
Julian Zobel committed
254
255
256
257
258
		
		if(leader.getCurrentTargetAttractionPoint() == null) {
			System.out.println();
		}
		
259
260
261
262
		AttractionPoint leaderDestination = leader.getCurrentTargetAttractionPoint();
		AttractionPoint hostDestination = host.getCurrentTargetAttractionPoint();
		
		// Update target attraction point if not already done
Julian Zobel's avatar
wip    
Julian Zobel committed
263
264
		if(leaderDestination != null && hostDestination != leaderDestination) {
			attractionAssigment.updateTargetAttractionPoint(host, leaderDestination);
265
266
		}
		
Julian Zobel's avatar
wip    
Julian Zobel committed
267
268
		int o = 10;
		
269
270
		// Assign small offset to the host depending on the leaders position.
		PositionVector leaderPos = leader.getRealPosition();
Julian Zobel's avatar
wip    
Julian Zobel committed
271
		PositionVector offset = new PositionVector(rand.nextInt(o), rand.nextInt(o));
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
		PositionVector newPos = leaderPos.plus(offset);

		// Update location of host, which will be around the leaders location.
		host.updateCurrentLocation(newPos);
	}


	/* 
	 * =====================================================================================================
	 * === GETTER AND SETTER FUNCTIONS
	 * =====================================================================================================
	 */
	
	public void setGroupFormingBehavior(IGroupFormingBehavior defaultGroupForming) {
		if (defaultGroupForming == null) {
			throw new ConfigurationException(
					"GroupFormingStrategy is missing in ModularMovementModel!");
		}
		this.groupFormingBehavior = defaultGroupForming;
	}

	public void setGroupEncounterBehavior(IGroupEncounterBehavior defaultGroupEncounterBehavior) {
		if (defaultGroupEncounterBehavior == null) {
			throw new ConfigurationException(
					"GroupEncounterStrategy is missing in ModularMovementModel!");
		}
		this.groupEncounterBehavior = defaultGroupEncounterBehavior;
	}

	public void setAttractionPointHostCounter(IAttractionPointHostCounter attractionPointHostCounter) {
		if (attractionPointHostCounter == null) {
			throw new ConfigurationException(
					"HostCountStrategy is missing in ModularMovementModel!");
		}
		this.attractionPointHostCounter = attractionPointHostCounter;
	}

Julian Zobel's avatar
wip    
Julian Zobel committed
309
310
311
312
313
314
	public IAttractionAssigmentStrategy getAttractionAssignmentStrategy() {
		return attractionAssigment;
	}
	
	public IGroupFormingBehavior getGroupFormingBehavior() {
		return groupFormingBehavior;
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
	}

	public LocalMovementStrategy getMovementStrategy() {
		return localMovementStrategy;
	}

	public IAttractionPointHostCounter getAttractionPointHostCounter() {
		return attractionPointHostCounter;
	}
	
	public void setNumberOfSingleHosts(int numberOfSingleHosts) {
		this.numberOfSingleHosts = numberOfSingleHosts;
	}
	
	public Set<SimLocationActuator> getSingleHosts(){
		return singleHosts;
	}	

333
334
335
336
	public Map<SimLocationActuator, PositionVector> getCurrentTargets(){
		return currentTargets;
	}
	
337
338
339
340
341
342
343
344
345
	/**
	 * Only for visualization!
	 * 
	 * @return
	 */
	public List<AttractionPoint> getAttractionPoints() {
		return new Vector<AttractionPoint>(IAttractionGenerator.attractionPoints);
	}
	
346
	
347
348

}