Commit 74fef9c4 authored by Julian Zobel's avatar Julian Zobel
Browse files

used abstract transtion strategy for weifhtered and random in area stratgegy...

used abstract transtion strategy for weifhtered and random in area stratgegy (now both extend the abstract ITransition)
parent 39b4a677
......@@ -49,7 +49,7 @@ public abstract class AbstractAttractionBasedTransitionStrategy implements ITran
protected Map<SimLocationActuator, AttractionPoint> lastAssignments = new LinkedHashMap<>();
protected List<AttractionAssignmentListener> listeners = new LinkedList<>();
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
@Override
public AttractionPoint getAssignment(SimLocationActuator comp) {
......@@ -80,6 +80,12 @@ public abstract class AbstractAttractionBasedTransitionStrategy implements ITran
return Collections.unmodifiableSet(attractionPoints);
}
/**
* Notify all listeners of an updated attraction point assignment for the given component
*
* @param comp
* @param attractionPoint
*/
protected void notifyListenersOfAssignmentUpdate(SimLocationActuator comp,
AttractionPoint attractionPoint) {
listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
......
......@@ -24,18 +24,20 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.BasicAttractionPoint;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Binder;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ModularMovementModel;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
/**
* With this transition strategy, nodes are roaming around {@link AttractionPoint}s that have a radius. As the {@link ModularMovementModel}
* uses a Gauss function to add jitter and offsets to the movement, some nodes may also roam outside of the circle's radius (this is intended to make it more realistic)
*
* @author Julian Zobel
* @version 1.0, 24.01.2019
*/
public class InAreaRoamingTransitionStrategy extends AbstractAttractionBasedTransitionStrategy implements EventHandler {
public static enum roamingTransitionState {
......@@ -133,47 +135,5 @@ public class InAreaRoamingTransitionStrategy extends AbstractAttractionBasedTran
AttractionPoint assignment = candidates.get(rnd.nextInt(candidates.size()));
return assignment;
}
private AttractionPoint getNewRoamingPosition(AttractionPoint roamingAP)
{
double apRadius = roamingAP.getRadius();
double x = roamingAP.getLongitudeOrX();
double y = roamingAP.getLatitudeOrY();
double newX = -1;
double newY = -1;
PositionVector destination = null;
int tries = 0;
do
{
double calcRadius = rnd.nextDouble() * apRadius;
double calcAngle = rnd.nextDouble() * 360;
newX = x + Math.sin(calcAngle) * calcRadius;
newY = y + Math.cos(calcAngle) * calcRadius;
destination = new PositionVector(newX, newY);
// Check constraints
if (destination.getX() < 0.0
|| destination.getX() > Binder.getComponentOrNull(Topology.class).getWorldDimensions().getX()
|| destination.getY() < 0.0
|| destination.getY() > Binder.getComponentOrNull(Topology.class).getWorldDimensions().getY())
{
destination = null;
if (tries > 100) {
throw new AssertionError("Unable to find a valid target destination within <100 tries.");
}
}
tries++;
} while (destination == null);
Monitor.log(this.getClass(), Monitor.Level.DEBUG, "Next roaming position is " + newX + " / " + newY);
return new BasicAttractionPoint("ROAMING", destination);
}
}
package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.BasicAttractionPoint;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Binder;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
......@@ -25,28 +17,21 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
* to roam the area randomly till a new target area is assigned.
*
* @author Clemens Krug
*
* Made the transition strategy extend the {@link AbstractAttractionBasedTransitionStrategy} for a more generalized approach on
* the {@link ITransitionStrategy} implementation for attraction point based strategies.
*
* @author Julian Zobel
* @version 1.1, January 2019
*/
public class RandomInAreaTransitionStrategy implements ITransitionStrategy
{
private final Random random = Randoms
.getRandom(RandomInAreaTransitionStrategy.class);
private LinkedHashSet<AttractionPoint> aPoints = new LinkedHashSet<>();
/**
* These are the target area centers the clients have assigned.
*/
private Map<SimLocationActuator, AttractionPoint> assignments = new HashMap<>();
public class RandomInAreaTransitionStrategy extends AbstractAttractionBasedTransitionStrategy
{
/**
* These are the current spots inside the target area where the client is currently heading.
*/
private Map<SimLocationActuator, AttractionPoint> currentTarget = new HashMap<>();
private Map<SimLocationActuator, Integer> currentSearchRadius = new HashMap<>();
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
/**
* The radius the target area should have. Should be set via XML config file.
......@@ -54,66 +39,41 @@ public class RandomInAreaTransitionStrategy implements ITransitionStrategy
private int defaultRadius;
@Override
public AttractionPoint getAssignment(SimLocationActuator comp)
{
public AttractionPoint getAssignment(SimLocationActuator comp) {
return currentTarget.get(comp);
}
@Override
public void addAttractionAssignmentListener(AttractionAssignmentListener listener)
{
listeners.add(listener);
}
@Override
public void removeAttractionAssignmentListener(AttractionAssignmentListener listener)
{
listeners.remove(listener);
}
@Override
public void setAttractionPoints(Collection<AttractionPoint> attractionPoints) {
aPoints.addAll(attractionPoints);
}
@Override
public Set<AttractionPoint> getAllAttractionPoints() {
return aPoints;
}
}
@Override
public void addComponent(SimLocationActuator ms) {
if(!assignments.containsKey(ms))
{
AttractionPoint aPoint = aPoints.iterator().next();
AttractionPoint aPoint = attractionPoints.iterator().next();
assignments.put(ms, aPoint);
currentTarget.put(ms, nextRandomPosition(aPoint, defaultRadius));
currentSearchRadius.put(ms, defaultRadius);
}
listeners.forEach(listener -> listener.updatedAttractionAssignment(ms, currentTarget.get(ms)));
notifyListenersOfAssignmentUpdate(ms, currentTarget.get(ms));
}
@Override
public void reachedAttractionPoint(SimLocationActuator ms) {
currentTarget.put(ms, nextRandomPosition(assignments.get(ms), currentSearchRadius.get(ms)));
listeners.forEach(listener -> listener.updatedAttractionAssignment(ms, currentTarget.get(ms)));
notifyListenersOfAssignmentUpdate(ms, currentTarget.get(ms));
}
@Override
public void updateTargetAttractionPoint(SimLocationActuator comp, AttractionPoint attractionPoint) {
assignments.put(comp, attractionPoint);
currentTarget.put(comp, nextRandomPosition(attractionPoint, currentSearchRadius.get(comp)));
listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
notifyListenersOfAssignmentUpdate(comp, attractionPoint);
}
public void setSearchRadiusForComponent(SimLocationActuator ms, int radius)
{
public void setSearchRadiusForComponent(SimLocationActuator ms, int radius) {
currentSearchRadius.put(ms, radius);
}
public void setDefaultRadius(int radius)
{
public void setDefaultRadius(int radius) {
this.defaultRadius = radius;
}
......@@ -123,8 +83,7 @@ public class RandomInAreaTransitionStrategy implements ITransitionStrategy
* @param radius The radius of the area.
* @return A random position within the area.
*/
private BasicAttractionPoint nextRandomPosition(Location center, int radius)
{
private BasicAttractionPoint nextRandomPosition(Location center, int radius) {
assert radius > 0 : "An area radius must be specified for the RandomInAreaTransitionStrategy! Did you set the 'DefaultRadius' property for this transition?";
double x = center.getLongitudeOrX();
......@@ -138,8 +97,8 @@ public class RandomInAreaTransitionStrategy implements ITransitionStrategy
while(newX < 0.0 || newX > Binder.getComponentOrNull(Topology.class).getWorldDimensions().getX()
|| newY < 0.0 || newY > Binder.getComponentOrNull(Topology.class).getWorldDimensions().getY())
{
double calcRadius = random.nextDouble() * radius;
double calcAngle = random.nextDouble() * 360;
double calcRadius = rnd.nextDouble() * radius;
double calcAngle = rnd.nextDouble() * 360;
newX = x + Math.sin(calcAngle) * calcRadius;
newY = y + Math.cos(calcAngle) * calcRadius;
......@@ -151,6 +110,5 @@ public class RandomInAreaTransitionStrategy implements ITransitionStrategy
Monitor.log(this.getClass(), Monitor.Level.DEBUG, "Next random position is " + newX + " / " + newY);
return new BasicAttractionPoint("RNDPOS", new PositionVector(newX, newY));
}
}
......@@ -30,6 +30,8 @@ import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.joda.time.chrono.JulianChronology;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tudarmstadt.maki.simonstrator.api.Event;
......@@ -42,53 +44,22 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Attraction
*
* @author Bjoern Richerzhagen
* @version 1.0, Jan 16, 2017
*
* Made the transition strategy extend the {@link AbstractAttractionBasedTransitionStrategy} for a more generalized approach on
* the {@link ITransitionStrategy} implementation for attraction point based strategies.
*
* @author Julian Zobel
* @version 1.1, January 2019
*
*/
public class WeightedTransitionStrategy implements ITransitionStrategy, EventHandler {
private Set<AttractionPoint> aps = new LinkedHashSet<>();
private Map<SimLocationActuator, AttractionPoint> assignments = new LinkedHashMap<>();
private Map<SimLocationActuator, AttractionPoint> lastAssignments = new LinkedHashMap<>();
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
public class WeightedTransitionStrategy extends AbstractAttractionBasedTransitionStrategy implements EventHandler {
private long pauseTimeMin = 0;
private long pauseTimeMax = 0;
private Random rnd = Randoms.getRandom(WeightedTransitionStrategy.class);
private final static int EVENT_PAUSE_ENDED = 1;
@Override
public AttractionPoint getAssignment(SimLocationActuator comp) {
return assignments.get(comp);
}
@Override
public void addAttractionAssignmentListener(
AttractionAssignmentListener listener) {
listeners.add(listener);
}
@Override
public void removeAttractionAssignmentListener(
AttractionAssignmentListener listener) {
listeners.remove(listener);
}
@Override
public void setAttractionPoints(
Collection<AttractionPoint> attractionPoints) {
this.aps.addAll(attractionPoints);
}
@Override
public Set<AttractionPoint> getAllAttractionPoints() {
return Collections.unmodifiableSet(aps);
}
@Override
public void addComponent(SimLocationActuator ms) {
this.assignments.put(ms, getNewAssignment(ms));
......@@ -97,16 +68,16 @@ public class WeightedTransitionStrategy implements ITransitionStrategy, EventHan
private AttractionPoint getNewAssignment(SimLocationActuator component) {
double score = rnd.nextDouble();
List<AttractionPoint> candidates = new LinkedList<>();
for (AttractionPoint ap : aps) {
for (AttractionPoint ap : attractionPoints) {
if (ap.getWeight() >= score) {
candidates.add(ap);
}
}
if (candidates.isEmpty()) {
candidates.addAll(aps);
candidates.addAll(attractionPoints);
}
AttractionPoint assignment = candidates.get(rnd.nextInt(candidates.size()));
listeners.forEach(listener -> listener.updatedAttractionAssignment(component, assignment));
notifyListenersOfAssignmentUpdate(component, assignment);
return assignment;
}
......@@ -126,7 +97,7 @@ public class WeightedTransitionStrategy implements ITransitionStrategy, EventHan
@Override
public void reachedAttractionPoint(SimLocationActuator ms) {
// JZ: Added this to prevent multiple calls while pausing
// JZ: Added this to prevent multiple calls while pausing (otherwise most nodes will not wait more the minimum pause time)
if (!this.assignments.containsKey(ms)) {
return;
}
......@@ -134,14 +105,6 @@ public class WeightedTransitionStrategy implements ITransitionStrategy, EventHan
this.lastAssignments.put(ms, this.assignments.remove(ms));
Event.scheduleWithDelay(getPauseTime(ms), this, ms, EVENT_PAUSE_ENDED);
}
@Override
public void updateTargetAttractionPoint(SimLocationActuator comp,
AttractionPoint attractionPoint) {
this.lastAssignments.put(comp, this.assignments.remove(comp));
this.assignments.put(comp, attractionPoint);
listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
}
public void setMinPauseTime(long minPauseTime) {
if (minPauseTime < 0) {
......
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