Commit dc857437 authored by Julian Zobel's avatar Julian Zobel
Browse files

More refactoring for social group movement

parent 30bbfedd
......@@ -20,39 +20,25 @@
package de.tud.kom.p2psim.impl.topology.movement.modularosm;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Vector;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.api.topology.movement.local.LocalMovementStrategy;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.TopologyFactory;
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;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.mapvisualization.IMapVisualization;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.IAttractionAssigmentStrategy;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.IAttractionAssigmentStrategy.AttractionAssignmentListener;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tud.kom.p2psim.impl.util.Either;
import de.tudarmstadt.maki.simonstrator.api.Binder;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
/**
......@@ -94,8 +80,6 @@ public class SocialGroupMovementModel extends ModularMovementModel {
* Returns a random SimLocationActuator component from the set of moveable hosts.
*
* @return SimLocationActuator
*
* @author Marcel Verst
*/
private SimLocationActuator getRandomActuator() {
......@@ -117,8 +101,6 @@ public class SocialGroupMovementModel extends ModularMovementModel {
* 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)
*
* @author Marcel Verst
*/
@Override
protected void move() {
......@@ -132,8 +114,8 @@ public class SocialGroupMovementModel extends ModularMovementModel {
// Checks if groups encounter each other and applies a specified action to these groups.
Set<SocialMovementGroup[]> encounteringGroups = groupEncounterBehavior.getEncounteringGroups();
groupEncounterBehavior.handleEncounters(encounteringGroups);
/**
/*
* Moves all nodes according to definition in group movement method
*/
for (SimLocationActuator component : moveableHosts) {
......@@ -152,8 +134,6 @@ public class SocialGroupMovementModel extends ModularMovementModel {
*
* @param SimLocationActuator The host to move.
* @param PositionVector Destination of the host.
*
* @author Marcel Verst
*/
protected void doGroupMovement(SimLocationActuator host, PositionVector destination) {
// Single Host Movement
......@@ -163,8 +143,8 @@ public class SocialGroupMovementModel extends ModularMovementModel {
// Group Related Movement
else if(groupContainer.isGroupMember(host)){
if(movesToMP(host)) {
if(atMP(host)) {
if(movesToGroupMeetingPoint(host)) {
if(hostIsAtMeetingPoint(host)) {
if(groupAtMP(host)) {
SocialMovementGroup group = groupContainer.getGroupOfHost(host);
AttractionPoint currAp = group.getLeader().getCurrentTargetAttractionPoint();
......@@ -207,10 +187,8 @@ public class SocialGroupMovementModel extends ModularMovementModel {
*
* @param SimLocationActuator The host to be checked.
* @return Boolean
*
* @author Marcel Verst
*/
private boolean movesToMP(SimLocationActuator host) {
private boolean movesToGroupMeetingPoint(SimLocationActuator host) {
PositionVector mp = groupContainer.getGroupOfHost(host).getMeetingPoint();
PositionVector dest = groupContainer.getGroupOfHost(host).getDestination();
if(mp != dest) {
......@@ -224,14 +202,12 @@ public class SocialGroupMovementModel extends ModularMovementModel {
*
* @param SimLocationActuator The host to be checked.
* @return Boolean
*
* @author Marcel Verst
*/
private boolean groupAtMP(SimLocationActuator host) {
Set<SimLocationActuator> participants = groupContainer.getGroupMembers(host);
int counter = 0;
for(SimLocationActuator participant : participants) {
if(atMP(participant)) {
if(hostIsAtMeetingPoint(participant)) {
counter++;
}
}
......@@ -247,16 +223,15 @@ public class SocialGroupMovementModel extends ModularMovementModel {
* @param SimLocationActuator The host to be checked.
* @return Boolean
*
* @author Marcel Verst
*/
private boolean atMP(SimLocationActuator host) {
private boolean hostIsAtMeetingPoint(SimLocationActuator host) {
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);
if(distance <= 2) {
if(distance <= 5) {
return true;
}
return false;
......@@ -273,8 +248,6 @@ public class SocialGroupMovementModel extends ModularMovementModel {
* Host position adapts to leaders position by taking the leader position and adding a small offset
*
* @param SimLocationActuator The host to be moved.
*
* @author Marcel Verst
*/
private void followLeader(SimLocationActuator host) {
SimLocationActuator leader = groupContainer.getGroupOfHost(host).getLeader();
......@@ -357,6 +330,10 @@ public class SocialGroupMovementModel extends ModularMovementModel {
return singleHosts;
}
public Map<SimLocationActuator, PositionVector> getCurrentTargets(){
return currentTargets;
}
/**
* Only for visualization!
*
......@@ -366,8 +343,6 @@ public class SocialGroupMovementModel extends ModularMovementModel {
return new Vector<AttractionPoint>(IAttractionGenerator.attractionPoints);
}
public Map<SimLocationActuator, PositionVector> getCurrentTargets(){
return currentTargets;
}
}
......@@ -33,8 +33,7 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Attraction
* @version 1.1, 09 2018
*/
public interface IAttractionGenerator {
// TODO make this private in JAVA >= 9
static LinkedList<AttractionPoint> attractionPoints = new LinkedList<>();
public List<AttractionPoint> getAttractionPoints();
......
......@@ -33,7 +33,6 @@ import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractio
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.IAttractionAssigmentStrategy;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.util.Tuple;
import de.tudarmstadt.maki.simonstrator.api.Event;
......@@ -47,8 +46,8 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
/**
* Abstract class implementation for implementing GroupFormingStrategies.
*
* @author Marcel Verst
* @version 1.0, 22.11.2018
* @author Marcel Verst, Julian Zobel
* @version 1.1, 30.01.2020
*/
public abstract class AbstractGroupForming implements IGroupFormingBehavior {
protected Random rand = Randoms.getRandom(AbstractGroupForming.class);
......@@ -68,7 +67,7 @@ public abstract class AbstractGroupForming implements IGroupFormingBehavior {
protected long groupRejoinWaitTime;
protected long groupFormationSetupDelay;
protected long groupFormationDelay;
private long groupFormationDelay;
protected IAttractionPointHostCounter hostCounter;
......@@ -146,37 +145,65 @@ public abstract class AbstractGroupForming implements IGroupFormingBehavior {
}, null, 0);
}
/**
* 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;
}
protected abstract void triggerGroupFormation();
/**
* Creates a group with the given set of participants.
*
* @param Set<SimLocationActuator> The participants which want to form a group.
* Creates a group with the given set of members.
*
* @author Marcel Verst
* @param Set<SimLocationActuator> The members which want to form a group.
*/
protected void createGroup(Set<SimLocationActuator> candidates) {
SocialMovementGroup group = new SocialMovementGroup(candidates);
protected void createGroup(Set<SimLocationActuator> members) {
SocialMovementGroup group = new SocialMovementGroup(members);
// 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;
AttractionPoint nextDestination = null;
do {
poiDestination = getRandomAttractionPoint();
} while(poiDestination == leaderTarget);
nextDestination = getRandomAttractionPoint();
} while(nextDestination == leaderTarget);
// Add Offset to destination
PositionVector destination = new PositionVector(poiDestination.getLongitudeOrX(), poiDestination.getLatitudeOrY());
double apRadius = poiDestination.getRadius();
PositionVector destination = new PositionVector(nextDestination);
double apRadius = nextDestination.getRadius();
PositionVector offset = new PositionVector(rand.nextDouble() * apRadius / 3, rand.nextDouble() * apRadius / 3);
PositionVector offset = new PositionVector(rand.nextGaussian() * apRadius / 3, rand.nextGaussian() * apRadius / 3);
destination.plus(offset);
group.setDestination(destination);
setGroupMeetingPoint(group);
for(SimLocationActuator member : group.getMembers()) {
member.setTargetAttractionPoint(poiDestination);
member.setTargetAttractionPoint(nextDestination);
if(groupCon.hostWasGroupMember(member)) {
groupCon.removeLeftGroupAtTimeEntry(member);
}
......@@ -376,8 +403,7 @@ public abstract class AbstractGroupForming implements IGroupFormingBehavior {
// Update target for all group members to meeting point
for(SimLocationActuator participant : group.getMembers()) {
IAttractionAssigmentStrategy transition = movementModel.getAttractionAssignmentStrategy();
transition.updateTargetAttractionPoint(participant, meetingPoint);
movementModel.getAttractionAssignmentStrategy().updateTargetAttractionPoint(participant, meetingPoint);
}
}
......
......@@ -20,17 +20,12 @@
package de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.groupforming;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
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.SocialGroupMovementModel;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
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.transition.IAttractionAssigmentStrategy;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
......@@ -106,13 +101,10 @@ public class DefaultGroupForming extends AbstractGroupForming {
break;
}
int numberOfHostsInAttractionPoint = hostCounter.getHostCountOfAttractionPoint(apCandidate);
int groupSize = Math.min(Math.max(minGroupSize, rand.nextInt(maxGroupSize)), numberOfHostsInAttractionPoint);
if(groupSize > numberOfHostsInAttractionPoint) {
throw new UnsupportedOperationException("Math Genius you are not!");
}
int numberOfHostsInAttractionPoint = hostCounter.getHostCountOfAttractionPoint(apCandidate);
int groupSize = this.rndGroupSize(numberOfHostsInAttractionPoint);
Set<SimLocationActuator> groupCandidates = new LinkedHashSet<>();
Set<SimLocationActuator> hostsOfPoi = hostCounter.getHostsOfAttractionPoint(apCandidate);
......@@ -199,9 +191,9 @@ public class DefaultGroupForming extends AbstractGroupForming {
for(SocialMovementGroup group : groupsToRemove) {
removeGroup(group);
System.out.println("Removed group, schedule new formation in " + Time.getFormattedTime(groupFormationDelay) + " @ " + Time.getFormattedTime(Time.getCurrentTime() + groupFormationDelay));
//System.out.println("Removed group, schedule new formation in " + Time.getFormattedTime(groupFormationDelay) + " @ " + Time.getFormattedTime(Time.getCurrentTime() + groupFormationDelay));
Event.scheduleWithDelay(groupFormationDelay, new EventHandler() {
Event.scheduleWithDelay(rndGroupFormationDelay(), new EventHandler() {
@Override
public void eventOccurred(Object content, int type) {
triggerGroupFormation();
......
......@@ -37,9 +37,9 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Attraction
* @author Julian Zobel
* @version 1.0, 23.01.2019
*/
public abstract class AbstractAttractionBasedTransitionStrategy implements IAttractionAssigmentStrategy {
public abstract class AbstractAttractionBasedAssignmentStrategy implements IAttractionAssigmentStrategy {
protected Random rnd = Randoms.getRandom(AbstractAttractionBasedTransitionStrategy.class);
protected Random rnd = Randoms.getRandom(AbstractAttractionBasedAssignmentStrategy.class);
protected Map<SimLocationActuator, AttractionPoint> assignments = new LinkedHashMap<>();
......
......@@ -38,7 +38,7 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Attraction
* @author Martin Hellwig
* @version 1.0, 07.07.2015
*/
public class FixedAssignmentStrategy extends AbstractAttractionBasedTransitionStrategy {
public class FixedAssignmentStrategy extends AbstractAttractionBasedAssignmentStrategy {
private List<SimLocationActuator> comps = new LinkedList<SimLocationActuator>();
......
......@@ -38,7 +38,7 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
* @author Julian Zobel
* @version 1.0, 24.01.2019
*/
public class InAreaRoamingTransitionStrategy extends AbstractAttractionBasedTransitionStrategy implements EventHandler {
public class InAreaRoamingTransitionStrategy extends AbstractAttractionBasedAssignmentStrategy implements EventHandler {
public static enum roamingTransitionState {
PAUSE,
......
......@@ -19,13 +19,13 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
*
* @author Clemens Krug
*
* Made the transition strategy extend the {@link AbstractAttractionBasedTransitionStrategy} for a more generalized approach on
* Made the transition strategy extend the {@link AbstractAttractionBasedAssignmentStrategy} for a more generalized approach on
* the {@link IAttractionAssigmentStrategy} implementation for attraction point based strategies.
*
* @author Julian Zobel
* @version 1.1, January 2019
*/
public class RandomInAreaTransitionStrategy extends AbstractAttractionBasedTransitionStrategy
public class RandomInAreaTransitionStrategy extends AbstractAttractionBasedAssignmentStrategy
{
/**
* These are the current spots inside the target area where the client is currently heading.
......
......@@ -78,7 +78,7 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
*
*
*/
public class SocialTransitionStrategy extends AbstractAttractionBasedTransitionStrategy implements EventHandler {
public class SocialTransitionStrategy extends AbstractAttractionBasedAssignmentStrategy implements EventHandler {
private String socialId = null;
......
......@@ -32,14 +32,14 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
* @author Bjoern Richerzhagen
* @version 1.0, Jan 16, 2017
*
* Made the transition strategy extend the {@link AbstractAttractionBasedTransitionStrategy} for a more generalized approach on
* Made the transition strategy extend the {@link AbstractAttractionBasedAssignmentStrategy} for a more generalized approach on
* the {@link IAttractionAssigmentStrategy} implementation for attraction point based strategies.
*
* @author Julian Zobel
* @version 1.1, January 2019
*
*/
public class WeightedTransitionStrategy extends AbstractAttractionBasedTransitionStrategy implements EventHandler {
public class WeightedTransitionStrategy extends AbstractAttractionBasedAssignmentStrategy implements EventHandler {
@XMLConfigurableConstructor({ "defaultPauseTimeMin", "defaultPauseTimeMax" })
public WeightedTransitionStrategy(long defaultPauseTimeMin, long defaultPauseTimeMax) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment