RandomInAreaTransitionStrategy.java 4.65 KB
Newer Older
1
2
package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition;

3
4
import java.util.HashMap;
import java.util.Map;
5
6
7
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;
8
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
9
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
10
11
12
13
14
15
import de.tudarmstadt.maki.simonstrator.api.Binder;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;

/**
Julian Zobel's avatar
wip    
Julian Zobel committed
16
 * This {@link IAttractionAssigmentStrategy} makes clients move around randomly in a specified area. You can specify the target
Clemens Krug's avatar
Clemens Krug committed
17
18
19
20
 * area center via the {@link #updateTargetAttractionPoint(SimLocationActuator, AttractionPoint)} method. The client will then start
 * to roam the area randomly till a new target area is assigned.
 *
 * @author Clemens Krug
21
 * 
22
 * Made the transition strategy extend the {@link AbstractAttractionBasedAssignmentStrategy} for a more generalized approach on 
Julian Zobel's avatar
wip    
Julian Zobel committed
23
 * the {@link IAttractionAssigmentStrategy} implementation for attraction point based strategies.
24
25
26
 * 
 * @author Julian Zobel
 * @version 1.1, January 2019
27
 */
28
public class RandomInAreaTransitionStrategy extends AbstractAttractionBasedAssignmentStrategy
29
{   
30
31
32
33
34
    public RandomInAreaTransitionStrategy() {
		super(0, 0);
	}

	/**
Clemens Krug's avatar
Clemens Krug committed
35
36
     * These are the current spots inside the target area where the client is currently heading.
     */
37
    private Map<SimLocationActuator, AttractionPoint> currentTarget = new HashMap<>();
Clemens Krug's avatar
Clemens Krug committed
38
    private Map<SimLocationActuator, Integer> currentSearchRadius = new HashMap<>();
39
  
Clemens Krug's avatar
Clemens Krug committed
40
41
42
    /**
     * The radius the target area should have. Should be set via XML config file.
     */
43
44
45
46
    private int defaultRadius;

    @Override
    public void addComponent(SimLocationActuator ms) {
Clemens Krug's avatar
Clemens Krug committed
47

48
49
        if(!assignments.containsKey(ms))
        {
50
            AttractionPoint aPoint = IAttractionGenerator.attractionPoints.iterator().next();
51
52
            assignments.put(ms, aPoint);
            currentTarget.put(ms, nextRandomPosition(aPoint, defaultRadius));
Clemens Krug's avatar
Clemens Krug committed
53
            currentSearchRadius.put(ms, defaultRadius);
54
        }
55
        notifyListenersOfAssignmentUpdate(ms,  currentTarget.get(ms));
56
57
58
    }

    @Override
59
    public void reachedAttractionPoint(SimLocationActuator ms, AttractionPoint attractionPoint) {
Clemens Krug's avatar
Clemens Krug committed
60
        currentTarget.put(ms, nextRandomPosition(assignments.get(ms), currentSearchRadius.get(ms)));
61
        notifyListenersOfAssignmentUpdate(ms,  currentTarget.get(ms));
62
63
64
65
66
    }

    @Override
    public void updateTargetAttractionPoint(SimLocationActuator comp, AttractionPoint attractionPoint) {
        assignments.put(comp, attractionPoint);
Clemens Krug's avatar
Clemens Krug committed
67
        currentTarget.put(comp, nextRandomPosition(attractionPoint, currentSearchRadius.get(comp)));
68
        notifyListenersOfAssignmentUpdate(comp, attractionPoint);
69
70
    }

71
    public void setSearchRadiusForComponent(SimLocationActuator ms, int radius) {
Clemens Krug's avatar
Clemens Krug committed
72
73
74
        currentSearchRadius.put(ms, radius);
    }

75
    public void setDefaultRadius(int radius) {
76
77
78
        this.defaultRadius = radius;
    }

79
80
81
82
83
84
    /**
     * Calculates a random point within a given circular area.
     * @param center The center of the area.
     * @param radius The radius of the area.
     * @return A random position within the area.
     */
85
    private BasicAttractionPoint nextRandomPosition(Location center, int radius) {
Clemens Krug's avatar
Clemens Krug committed
86
        assert radius > 0 : "An area radius must be specified for the RandomInAreaTransitionStrategy! Did you set the 'DefaultRadius' property for this transition?";
Clemens Krug's avatar
Clemens Krug committed
87

88
89
        double x = center.getLongitudeOrX();
        double y = center.getLatitudeOrY();
90
91
92
93
94
95
96
97
98

        double newX = -1;
        double newY = -1;

        int tries = 0;

        while(newX < 0.0 || newX > Binder.getComponentOrNull(Topology.class).getWorldDimensions().getX()
                || newY < 0.0 || newY > Binder.getComponentOrNull(Topology.class).getWorldDimensions().getY())
        {
99
100
            double calcRadius = rnd.nextDouble() * radius;
            double calcAngle = rnd.nextDouble() * 360;
101
102
103
104
105
106
107
108
109
110
111
112
113

            newX = x + Math.sin(calcAngle) * calcRadius;
            newY = y + Math.cos(calcAngle) * calcRadius;
            tries++;

            if(tries > 100) throw new AssertionError("Unable to find a valid target destination within <100 tries.");
        }

        Monitor.log(this.getClass(), Monitor.Level.DEBUG, "Next random position is " + newX + " / " + newY);

        return new BasicAttractionPoint("RNDPOS", new PositionVector(newX, newY));
    }
}