Commit 3c3c3c92 authored by Julian Zobel's avatar Julian Zobel
Browse files

Added the possibility to add own pause times to Attraction Points and moved...

Added the possibility to add own pause times to Attraction Points and moved logic form the basic attraction points to the AttractionPointIMPL (needs renaming I think).
parent af160d3c
......@@ -36,25 +36,41 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Attraction
* @author Christoph Muenker, Bjoern Richerzhagen, Julian Zobel
* @version 1.0, 02.07.2013
* @version 1.1, 26.10.2018 - since the class was very similar to {@link BasicAttractionPoint}, made this class an extension
* @version 1.2, 24.01.2020 - added pause time interval, weight, and radius exclusively to this class, because {@link BasicAttractionPoint} should be just a named point.
*/
public class AttractionPointImpl extends BasicAttractionPoint {
protected static Random rnd = Randoms.getRandom(AttractionPoint.class);
protected static Map<String, AttractionPointImpl> instances = new LinkedHashMap<>();
public AttractionPointImpl(String name, PositionVector posVec) {
super(name, posVec);
protected long pauseTimeMin = 0;
protected long pauseTimeMax = 0;
protected double weight = 0;
protected double radius = 0;
public AttractionPointImpl(String name, PositionVector posVec) {
super(name, posVec);
if (instances.containsKey(name)) {
throw new AssertionError("Name "+name+" already in use for an attraction point.");
}
instances.put(name, this);
instances.put(name, this);
}
public AttractionPointImpl(String name, PositionVector posVec, double weight, double radius, long pauseTimeMin, long pauseTimeMax) {
this(name, posVec);
assert weight >= 0 && weight <= 1.0;
assert radius >= 0;
this.weight = weight;
this.radius = radius;
this.setPauseTime(pauseTimeMin, pauseTimeMax);
}
@Override
public AttractionPoint clone(String newName) {
return new AttractionPointImpl(name, this);
return new AttractionPointImpl(name, this, weight, radius, pauseTimeMin, pauseTimeMax);
}
@Override
......@@ -81,9 +97,71 @@ public class AttractionPointImpl extends BasicAttractionPoint {
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
} else if (!name.equals(other.name)) {
return false;
} else if(weight != other.weight) {
return false;
} else if(radius != other.radius) {
return false;
} else if(pauseTimeMin != other.pauseTimeMin) {
return false;
} else if(pauseTimeMax != other.pauseTimeMax) {
return false;
}
return true;
}
@Override
public double getWeight() {
return weight;
}
@Override
public double getRadius() {
return radius;
}
@Override
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public long getPauseTimeMin() {
return pauseTimeMin;
}
@Override
public long getPauseTimeMax() {
return pauseTimeMax;
}
@Override
public void setPauseTime(long pauseTimeMin, long pauseTimeMax) {
assert pauseTimeMin >= 0 && pauseTimeMax >= 0;
assert pauseTimeMax >= pauseTimeMin;
this.pauseTimeMin = pauseTimeMin;
this.pauseTimeMax = pauseTimeMax;
}
@Override
public boolean hasPauseTime() {
return true;
}
@Override
public boolean hasWeight() {
return true;
}
@Override
public boolean hasRadius() {
return true;
}
}
......@@ -11,13 +11,11 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
public class BasicAttractionPoint extends PositionVector implements AttractionPoint
{
protected String name;
protected double weight = 0;
protected double radius = 0;
public BasicAttractionPoint(String name, PositionVector pos) {
super(pos);
this.name = name;
}
}
@XMLConfigurableConstructor({ "name", "x", "y" })
public BasicAttractionPoint(String name, double x, double y) {
......@@ -28,29 +26,10 @@ public class BasicAttractionPoint extends PositionVector implements AttractionPo
public String getName() {
return name;
}
@Override
public double getWeight() {
return weight;
}
@Override
public double getRadius() {
return radius;
}
@Override
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public AttractionPoint clone(String newName) {
return new BasicAttractionPoint(newName, this);
}
}
}
......@@ -129,27 +129,41 @@ public class JSONAttractionGenerator implements IAttractionGenerator {
double lat = allPOI.getJSONObject(i).getDouble("lat");
double lon = allPOI.getJSONObject(i).getDouble("lon");
AttractionPointImpl ap;
// check that the point is within the simulation boundaries
if(lat > latLeft && lat < latRight &&
lon > lonLeft && lon < lonRight) {
// initialize the attraction point with basic information, will be filled now...
ap = new AttractionPointImpl(barname, transformGPSWindowToOwnWorld(lat, lon));
attractionPoints.add(ap);
// the following is allowed to fail.
ap.setWeight(allPOI.getJSONObject(i).getJSONObject("tags").getDouble("weight"));
double radius = allPOI.getJSONObject(i).getJSONObject("tags").getDouble("radius");
if(maximumRadius == -1) {
ap.setRadius(radius);
}
else {
ap.setRadius(Math.min(maximumRadius, radius));
// the following setters are allowed to fail
// AP weight
if(allPOI.getJSONObject(i).getJSONObject("tags").has("weight")) {
ap.setWeight(allPOI.getJSONObject(i).getJSONObject("tags").getDouble("weight"));
}
// AP radius
if(allPOI.getJSONObject(i).getJSONObject("tags").has("radius")) {
double radius = allPOI.getJSONObject(i).getJSONObject("tags").getDouble("radius");
if(maximumRadius == -1) {
ap.setRadius(radius);
}
else {
ap.setRadius(Math.min(maximumRadius, radius));
}
}
if(allPOI.getJSONObject(i).getJSONObject("tags").has("pauseTimeMin") && allPOI.getJSONObject(i).getJSONObject("tags").has("pauseTimeMax")) {
ap.setPauseTime( allPOI.getJSONObject(i).getJSONObject("tags").getLong("pauseTimeMin"), allPOI.getJSONObject(i).getJSONObject("tags").getLong("pauseTimeMax"));
}
}
}
catch (JSONException e) {
//This bar had no name defined, so there was an error. Not so bad
System.out.println(e);
}
}
}
......
......@@ -51,9 +51,8 @@ public abstract class AbstractAttractionBasedTransitionStrategy implements ITran
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
protected long pauseTimeMin = 0;
protected long pauseTimeMax = 0;
protected long defaultPauseTimeMin = 0;
protected long defaultPauseTimeMax = 0;
protected final static int EVENT_PAUSE_ENDED = 1;
......@@ -95,25 +94,24 @@ public abstract class AbstractAttractionBasedTransitionStrategy implements ITran
notifyListenersOfAssignmentUpdate(comp, attractionPoint);
}
protected long getPauseTime() {
return (long) (rnd.nextDouble() * (pauseTimeMax-pauseTimeMin)) + pauseTimeMin;
protected long getRandomUniformDistributionPauseTime(AttractionPoint attractionPoint) {
// if attraction point has own pause time use it, otherwise use default values
if(attractionPoint.hasPauseTime()) {
return (long) (rnd.nextDouble() * (attractionPoint.getPauseTimeMax() - attractionPoint.getPauseTimeMin())) + attractionPoint.getPauseTimeMin();
}
else {
return (long) (rnd.nextDouble() * (defaultPauseTimeMax - defaultPauseTimeMin)) + defaultPauseTimeMin;
}
}
public void setMinPauseTime(long minPauseTime) {
if (minPauseTime < 0) {
throw new ConfigurationException(
"MinPauseTime should be >= 0!");
}
this.pauseTimeMin = minPauseTime;
}
public void setMaxPauseTime(long maxPauseTime) {
if (maxPauseTime < 0) {
throw new ConfigurationException(
"MaxPauseTime should be >= 0!");
}
this.pauseTimeMax = maxPauseTime;
public void setDefaultPauseTime(long defaultPauseTimeMin, long defaultPauseTimeMax) {
assert defaultPauseTimeMax >= 0 && defaultPauseTimeMin >= 0;
assert defaultPauseTimeMax >= defaultPauseTimeMin;
this.defaultPauseTimeMax = defaultPauseTimeMax;
this.defaultPauseTimeMin = defaultPauseTimeMin;
}
}
......@@ -70,7 +70,7 @@ public class InAreaRoamingTransitionStrategy extends AbstractAttractionBasedTran
// start roaming if the AP was reached
if(roamingStates.get(comp) == roamingTransitionState.TRANSITION) {
// schedule the end of the roaming phase, which will make a new transition
Event.scheduleWithDelay(getPauseTime(), this, comp, EVENT_PAUSE_ENDED);
Event.scheduleWithDelay(getRandomUniformDistributionPauseTime(), this, comp, EVENT_PAUSE_ENDED);
}
this.roamingStates.put(comp, roamingTransitionState.PAUSE);
......@@ -80,12 +80,12 @@ public class InAreaRoamingTransitionStrategy extends AbstractAttractionBasedTran
}
@Override
protected long getPauseTime() {
protected long getRandomUniformDistributionPauseTime() {
if(useGaussianDistributedPauseTime) {
return gaussianDistributionPauseTime(pauseTimeMax-pauseTimeMin, 0.5*(pauseTimeMax-pauseTimeMin));
}
else {
return super.getPauseTime();
return super.getRandomUniformDistributionPauseTime();
}
}
......
......@@ -166,7 +166,7 @@ public class SocialTransitionStrategy extends AbstractAttractionBasedTransitionS
@Override
public void reachedAttractionPoint(SimLocationActuator ms) {
if (!arrivedAtAttractionPoint.contains(ms)) {
Event.scheduleWithDelay(getPauseTime(), this, ms, 0);
Event.scheduleWithDelay(getRandomUniformDistributionPauseTime(), this, ms, 0);
arrivedAtAttractionPoint.add(ms);
}
}
......
......@@ -82,7 +82,7 @@ public class WeightedTransitionStrategy extends AbstractAttractionBasedTransitio
}
this.lastAssignments.put(ms, this.assignments.remove(ms));
Event.scheduleWithDelay(getPauseTime(), this, ms, EVENT_PAUSE_ENDED);
Event.scheduleWithDelay(getRandomUniformDistributionPauseTime(), this, ms, EVENT_PAUSE_ENDED);
}
}
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