Commit eff5d429 authored by Julian Zobel's avatar Julian Zobel 🦄
Browse files

Merge branch 'master' into 'cherry-pick-7698d9d7'

# Conflicts:
#   src/de/tud/kom/p2psim/impl/analyzer/metric/output/MetricOutputDAO.java
#   src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java
parents 1c7f20ec 37020b44
......@@ -39,8 +39,8 @@ import javax.swing.JComponent;
import javax.swing.JMenu;
import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tudarmstadt.maki.simonstrator.api.Binder;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
......
/*
* 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.transition;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Random;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.IAttractionBasedMovementAnalyzer;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
/**
* Abstract base class for Transition Strategies ({@link IAttractionAssigmentStrategy}s) using {@link IAttractionPoint}s.
*
* @author Julian Zobel
* @version 1.0, 23.01.2019
*/
public abstract class AbstractAttractionBasedAssignmentStrategy implements IAttractionAssigmentStrategy {
protected Random rnd = Randoms.getRandom(AbstractAttractionBasedAssignmentStrategy.class);
protected IAttractionProvider attractionProvider;
protected LinkedHashMap<SimLocationActuator, IAttractionPoint> assignments = new LinkedHashMap<>();
protected LinkedHashMap<SimLocationActuator, IAttractionPoint> lastAssignments = new LinkedHashMap<>();
private LinkedList<AttractionAssignmentListener> listeners = new LinkedList<>();
protected long defaultPauseTimeMin = 0;
protected long defaultPauseTimeMax = 0;
protected final static int EVENT_PAUSE_ENDED = 1;
public AbstractAttractionBasedAssignmentStrategy(long defaultPauseTimeMin, long defaultPauseTimeMax) {
assert defaultPauseTimeMax >= 0 && defaultPauseTimeMin >= 0;
assert defaultPauseTimeMax >= defaultPauseTimeMin;
this.defaultPauseTimeMax = defaultPauseTimeMax;
this.defaultPauseTimeMin = defaultPauseTimeMin;
}
public void setAttractionProvider(IAttractionProvider attractionProvider) {
this.attractionProvider = attractionProvider;
}
@Override
public IAttractionPoint getAssignment(SimLocationActuator host) {
return assignments.get(host);
}
@Override
public IAttractionPoint getLastAssignment(SimLocationActuator host) {
return lastAssignments.get(host);
}
@Override
public void addAttractionAssignmentListener(
AttractionAssignmentListener listener) {
listeners.add(listener);
}
@Override
public void removeAttractionAssignmentListener(
AttractionAssignmentListener listener) {
listeners.remove(listener);
}
/**
* Notify all listeners of an updated attraction point assignment for the given component
*
* @param comp
* @param attractionPoint
*/
protected void notifyListenersOfAssignmentUpdate(SimLocationActuator comp,
IAttractionPoint attractionPoint) {
listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
// also iInform analyzer of assigment update
if(Monitor.hasAnalyzer(IAttractionBasedMovementAnalyzer.class)) {
Monitor.getOrNull(IAttractionBasedMovementAnalyzer.class).onUpdateAttractionAssignment(comp, attractionPoint);
}
}
@Override
public void updateTargetAttractionPoint(SimLocationActuator comp,
IAttractionPoint attractionPoint) {
if(assignments.containsKey(comp)) {
this.lastAssignments.put(comp, this.assignments.remove(comp));
}
this.assignments.put(comp, attractionPoint);
notifyListenersOfAssignmentUpdate(comp, attractionPoint);
}
private long getRandomUniformDistributionPauseTime(IAttractionPoint attractionPoint) {
// if attraction point has own pause time use it, otherwise use default values
if(attractionPoint != null && attractionPoint.hasPauseTime()) {
return (long) (rnd.nextDouble() * (attractionPoint.getPauseTimeMax() - attractionPoint.getPauseTimeMin())) + attractionPoint.getPauseTimeMin();
}
else {
return (long) (rnd.nextDouble() * (defaultPauseTimeMax - defaultPauseTimeMin)) + defaultPauseTimeMin;
}
}
public long getPauseTime(IAttractionPoint attractionPoint) {
long pause = getRandomUniformDistributionPauseTime(attractionPoint);
//System.out.println(Time.getFormattedTime(pause));
return pause;
}
/**
* Get a new assignment for this component, that is not the last assignment
*
* @param host
* @return
*/
protected IAttractionPoint getNewAttractionPointAssignment(SimLocationActuator host) {
double score = rnd.nextDouble();
LinkedList<IAttractionPoint> candidates = new LinkedList<>();
for (IAttractionPoint ap : attractionProvider.getAttractionPoints()) {
// skip null values (should never occur!)
if(ap == null) {
continue;
}
// fast skip forward if the attraction point would not be considered due to its score
if (score >= ap.getWeight()) {
continue;
}
// skip attraction points that are currently assigned to the host
if(assignments.containsKey(host) && ap.equals(assignments.get(host))) {
continue;
}
// add the attraction point to the list of candidates, if no attraction point was
// previously visited, or if currently no attraction point is assigned and the last
// assignment was not this attraction point (this prevents that, if the current attraction
// point was already set to the last assignment, the last visited attraction point is also considered)
if(lastAssignments.get(host) == null ||
(!assignments.containsKey(host) && !ap.equals(lastAssignments.get(host)))) {
candidates.add(ap);
}
}
// fallback: if no attraction point is considered, randomly select one, except the current assignment
if (candidates.isEmpty()) {
candidates.addAll(attractionProvider.getAttractionPoints());
if(assignments.containsKey(host) && candidates.size() > 1) {
candidates.remove(assignments.get(host));
}
}
IAttractionPoint assignment = candidates.get(rnd.nextInt(candidates.size()));
return assignment;
}
@Override
public boolean hostInAttractionPointArea(SimLocationActuator host) {
for(IAttractionPoint ap : attractionProvider.getAttractionPoints()) {
if(ap.distanceTo(host.getRealPosition()) <= ap.getRadius()) {
return true;
}
}
return false;
}
}
/*
* 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.transition;
import java.util.LinkedHashMap;
import java.util.Map;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ISocialGroupMovementAnalyzer;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ModularMovementModel;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
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.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
/**
* With this transition strategy, nodes are roaming around {@link IAttractionPoint}s, without transitions between them. 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 10.02.2020
*/
public class AttractionPointRoamingStrategy extends AbstractAttractionBasedAssignmentStrategy implements EventHandler {
public static enum roamingTransitionState {
PAUSE,
ROAMING
}
protected Map<SimLocationActuator, roamingTransitionState> roamingStates = new LinkedHashMap<>();
@XMLConfigurableConstructor({ "defaultPauseTimeMin", "defaultPauseTimeMax" })
public AttractionPointRoamingStrategy(long defaultPauseTimeMin, long defaultPauseTimeMax) {
super(defaultPauseTimeMin, defaultPauseTimeMax);
}
@Override
public void addComponent(SimLocationActuator comp) {
this.roamingStates.put(comp, null);
IAttractionPoint nextAP = getNewAttractionPointAssignment(comp);
updateTargetAttractionPoint(comp, nextAP);
}
@Override
public void updateTargetAttractionPoint(SimLocationActuator comp,
IAttractionPoint attractionPoint) {
this.roamingStates.put(comp, null);
super.updateTargetAttractionPoint(comp, attractionPoint);
}
@Override
public void reachedAttractionPoint(SimLocationActuator comp, IAttractionPoint attractionPoint) {
if(roamingStates.get(comp) == roamingTransitionState.PAUSE) {
return;
}
if(Monitor.hasAnalyzer(ISocialGroupMovementAnalyzer.class)) {
Monitor.getOrNull(ISocialGroupMovementAnalyzer.class).onNodeReachedAttractionPoint(comp, attractionPoint);
}
this.roamingStates.put(comp, roamingTransitionState.PAUSE);
// schedule roaming
long roamingPauseTime = (long) (rnd.nextDouble() * Time.MINUTE * 5) + Time.MINUTE * 5;
Event.scheduleWithDelay(roamingPauseTime, this, comp, 0);
}
private void roamAroundAttractionPoint(SimLocationActuator comp) {
if(roamingStates.get(comp) == roamingTransitionState.PAUSE) {
IAttractionPoint currentAttractionPoint = this.assignments.get(comp);
this.roamingStates.put(comp, roamingTransitionState.ROAMING);
notifyListenersOfAssignmentUpdate(comp, currentAttractionPoint);
}
}
@Override
public void eventOccurred(Object content, int type) {
SimLocationActuator comp = (SimLocationActuator) content;
IAttractionPoint currentAttractionPoint = this.assignments.get(comp);
// if the attraction point was removed in the meantime, go directly to transit state
if(currentAttractionPoint == null || !attractionProvider.getAttractionPoints().contains(currentAttractionPoint)) {
this.addComponent(comp);
}
else {
this.roamAroundAttractionPoint(comp);
}
}
}
\ No newline at end of file
......@@ -20,74 +20,41 @@
package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.common.SimHostComponent;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ModularMovementModel;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.ITransitionStrategy.AttractionAssignmentListener;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
/**
* A {@link TransitionStrategy} for a case in which nodes are affiliated to an {@link AttractionPoint} only
* A {@link TransitionStrategy} for a case in which nodes are affiliated to an {@link IAttractionPoint} only
* in the beginning. No further transition will take place.
*
* @author Martin Hellwig
* @version 1.0, 07.07.2015
*/
public class FixedAssignmentStrategy implements ITransitionStrategy {
public class FixedAssignmentStrategy extends AbstractAttractionBasedAssignmentStrategy {
private List<SimLocationActuator> comps = new LinkedList<SimLocationActuator>();
private LinkedHashSet<AttractionPoint> aPoints = new LinkedHashSet<AttractionPoint>();
private Map<SimLocationActuator, AttractionPoint> assignments = new LinkedHashMap<SimLocationActuator, AttractionPoint>();
public FixedAssignmentStrategy() {
super(0, 0);
}
private List<SimLocationActuator> comps = new LinkedList<SimLocationActuator>();
private Map<SimLocationActuator, SimHost> mappingMSHost = new LinkedHashMap<SimLocationActuator, SimHost>();
private Map<SimHost, SimLocationActuator> mappingHostMS = new LinkedHashMap<SimHost, SimLocationActuator>();
private Map<String, AttractionPoint> mappingGroupIdAP = new LinkedHashMap<String, AttractionPoint>();
private Map<AttractionPoint, String> mappingAPGroupId = new LinkedHashMap<AttractionPoint, String>();
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
@Override
public AttractionPoint getAssignment(SimLocationActuator comp) {
return assignments.get(comp);
}
@Override
public void setAttractionPoints(Collection<AttractionPoint> attractionPoints) {
aPoints.addAll(attractionPoints);
}
@Override
public Set<AttractionPoint> getAllAttractionPoints() {
return aPoints;
}
private Map<String, IAttractionPoint> mappingGroupIdAP = new LinkedHashMap<String, IAttractionPoint>();
@Override
public void addAttractionAssignmentListener(
AttractionAssignmentListener listener) {
listeners.add(listener);
}
@Override
public void removeAttractionAssignmentListener(
AttractionAssignmentListener listener) {
listeners.remove(listener);
}
private Map<IAttractionPoint, String> mappingAPGroupId = new LinkedHashMap<IAttractionPoint, String>();
@Override
public void addComponent(SimLocationActuator ms) {
comps.add(ms);
......@@ -95,14 +62,14 @@ public class FixedAssignmentStrategy implements ITransitionStrategy {
// No assignments been done before.
if (assignments.isEmpty()) {
AttractionPoint aPoint = aPoints.iterator().next();
IAttractionPoint aPoint = attractionProvider.getAttractionPoints().iterator().next();
assignments.put(ms, aPoint);
mappingGroupId(ms, aPoint);
}
// GroupId is not mapped.
else if (!mappingGroupIdAP.containsKey(mappingMSHost.get(ms)
.getProperties().getGroupID())) {
for (AttractionPoint actAP : aPoints) {
for (IAttractionPoint actAP : attractionProvider.getAttractionPoints()) {
if (!mappingAPGroupId.containsKey(actAP)) {
assignments.put(ms, actAP);
mappingGroupId(ms, actAP);
......@@ -113,27 +80,27 @@ public class FixedAssignmentStrategy implements ITransitionStrategy {
// GroupId is already mapped.
else if (mappingGroupIdAP.containsKey(mappingMSHost.get(ms)
.getProperties().getGroupID())) {
AttractionPoint aPoint = mappingGroupIdAP.get(mappingMSHost.get(ms)
IAttractionPoint aPoint = mappingGroupIdAP.get(mappingMSHost.get(ms)
.getProperties().getGroupID());
assignments.put(ms, aPoint);
} else {
throw new Error("Should not happen.");
}
listeners.forEach(listener -> listener.updatedAttractionAssignment(ms, assignments.get(ms)));
notifyListenersOfAssignmentUpdate(ms, assignments.get(ms));
}
@Override
public void reachedAttractionPoint(SimLocationActuator ms) {
public void reachedAttractionPoint(SimLocationActuator ms, IAttractionPoint attractionPoint) {
// don't care, as no further assignment takes place.
}
@Override
public void updateTargetAttractionPoint(SimLocationActuator comp,
AttractionPoint attractionPoint) {
IAttractionPoint attractionPoint) {
assignments.put(comp, attractionPoint);
listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
notifyListenersOfAssignmentUpdate(comp, attractionPoint);
}
private void mappingHost(SimLocationActuator ms) {
......@@ -146,7 +113,7 @@ public class FixedAssignmentStrategy implements ITransitionStrategy {
mappingMSHost.put(ms, host);
}
private void mappingGroupId(SimLocationActuator ms, AttractionPoint AP) {
private void mappingGroupId(SimLocationActuator ms, IAttractionPoint AP) {
SimHostComponent comp = (SimHostComponent) ms;
SimHost host = comp.getHost();
......@@ -160,13 +127,13 @@ public class FixedAssignmentStrategy implements ITransitionStrategy {
/**
* Used by the MobilityModel (M1) of the {@link ModularMovementModel} to get
* the groupId of the affiliated nodes to that {@link AttractionPoint}. Once
* the groupId of the affiliated nodes to that {@link IAttractionPoint}. Once
* the groupId is known nodes can be set <b>offline</b> or <b>online</b>.
*
* @param attractionPoint
* @return
*/
public String getGroupIdOfAttractionPoint(AttractionPoint attractionPoint) {
public String getGroupIdOfAttractionPoint(IAttractionPoint attractionPoint) {
return mappingAPGroupId.get(attractionPoint);
}
}
......@@ -20,19 +20,22 @@
package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition;
import java.util.Collection;
import java.util.Set;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
/**
* This is the interface for the Transition Strategy.<br>
*
* @author Martin Hellwig
* @version 1.0, 03.07.2015
*
*
*
* @author Julian Zobel
* @version 1.1, 27.01.2020
*/
public interface ITransitionStrategy {
public interface IAttractionAssigmentStrategy {
/**
* Currently assigned attraction point.
......@@ -40,33 +43,22 @@ public interface ITransitionStrategy {
* @param comp
* @return
*/
public AttractionPoint getAssignment(SimLocationActuator comp);
public IAttractionPoint getAssignment(SimLocationActuator host);
public IAttractionPoint getLastAssignment(SimLocationActuator host);
public void addAttractionAssignmentListener(AttractionAssignmentListener listener);
public void removeAttractionAssignmentListener(AttractionAssignmentListener listener);
public void setAttractionProvider(IAttractionProvider attractionProvider);
/**
* Should be called first, to add the Attraction Points for the assignment!
*
* @param attractionPoints
*/
public void setAttractionPoints(
Collection<AttractionPoint> attractionPoints);
/**
* Return a set of all attraction points
*
* @return
*/
public Set<AttractionPoint> getAllAttractionPoints();
/**
* Add the object and assign the MS to an {@link AttractionPoint}.
* Add the object and assign the MS to an {@link IAttractionPoint}.
*
* @param ms
*/
public void addComponent(SimLocationActuator ms);
public void addComponent(SimLocationActuator host);
/**
* Notify the TransitionStrategy, that the component has reached an
......@@ -74,7 +66,7 @@ public interface ITransitionStrategy {
*
* @param ms
*/
public void reachedAttractionPoint(SimLocationActuator ms);
public void reachedAttractionPoint(SimLocationActuator host, IAttractionPoint attractionPoint);
/**
* Updates the target attraction point of a component
......@@ -82,7 +74,7 @@ public interface ITransitionStrategy {
* @param attractionPoint
*/
public void updateTargetAttractionPoint(SimLocationActuator comp,
AttractionPoint attractionPoint);
IAttractionPoint attractionPoint);
public interface AttractionAssignmentListener {
......@@ -93,9 +85,12 @@ public interface ITransitionStrategy {
* @param component
* @param newAssignment
*/
public void updatedAttractionAssignment(SimLocationActuator component,
AttractionPoint newAssignment);
public void updatedAttractionAssignment(SimLocationActuator host,
IAttractionPoint newAssignment);
}
public boolean hostInAttractionPointArea(SimLocationActuator host);
public long getPauseTime(IAttractionPoint attractionPoint);
}
/*
* 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.transition;
import java.util.LinkedHashMap;
import java.util.Map;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ISocialGroupMovementAnalyzer;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ModularMovementModel;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
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.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
/**
* With this transition strategy, nodes are roaming around {@link IAttractionPoint}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 AbstractAttractionBasedAssignmentStrategy implements EventHandler {
public static enum roamingTransitionState {
PAUSE,
ROAMING,
TRANSITION
}
protected Map<SimLocationActuator, roamingTransitionState> roamingStates = new LinkedHashMap<>();
protected final static int EVENT_ROAMING_PAUSE_ENDED = 2;
private boolean useGaussianDistributedPauseTime = false;
@XMLConfigurableConstructor({ "defaultPauseTimeMin", "defaultPauseTimeMax" })
public InAreaRoamingTransitionStrategy(long defaultPauseTimeMin, long defaultPauseTimeMax) {
super(defaultPauseTimeMin, defaultPauseTimeMax);
}
@Override
public void addComponent(SimLocationActuator comp) {
this.roamingStates.put(comp, roamingTransitionState.TRANSITION);
IAttractionPoint nextAP = getNewAttractionPointAssignment(comp);
updateTargetAttractionPoint(comp, nextAP);
}
@Override
public void reachedAttractionPoint(SimLocationActuator comp, IAttractionPoint attractionPoint) {
if(roamingStates.get(comp) == roamingTransitionState.PAUSE) {
return;
}
if(Monitor.hasAnalyzer(ISocialGroupMovementAnalyzer.class)) {
Monitor.getOrNull(ISocialGroupMovementAnalyzer.class).onNodeReachedAttractionPoint(comp, attractionPoint);
}
// 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(attractionPoint), this, comp, EVENT_PAUSE_ENDED);
}
this.roamingStates.put(comp, roamingTransitionState.PAUSE);
// schedule roaming
//Event.scheduleWithDelay(gaussianDistributionPauseTime(5 * Time.MINUTE, Time.MINUTE), this, comp, EVENT_ROAMING_PAUSE_ENDED);
Event.scheduleWithDelay(15 * Time.SECOND, this, comp, EVENT_ROAMING_PAUSE_ENDED);
}
@Override
public long getPauseTime(IAttractionPoint attractionPoint) {
if(useGaussianDistributedPauseTime) {
return gaussianDistributionPauseTime(defaultPauseTimeMax - defaultPauseTimeMin, 0.5*(defaultPauseTimeMax - defaultPauseTimeMin));
}
else {
return super.getPauseTime(attractionPoint);
}
}
/**
* Use a gaussian distribution for the pause time interval generation, using a mean value and a standard deviation
*
* @return
*/
private long gaussianDistributionPauseTime(double mean, double std) {
double x = rnd.nextGaussian() * std + mean;
if(x <= 0) return gaussianDistributionPauseTime(mean, std*0.9);
return (long) x;
}
private void roamAroundAttractionPoint(SimLocationActuator comp) {
if(roamingStates.get(comp) != roamingTransitionState.TRANSITION) {
IAttractionPoint currentAttractionPoint = this.assignments.get(comp);
if(currentAttractionPoint == null) {
System.err.println("AP roaming failed: no AP");
}
if(currentAttractionPoint.getRadius() > 0)
{
this.roamingStates.put(comp, roamingTransitionState.ROAMING);
updateTargetAttractionPoint(comp, currentAttractionPoint);
//notifyListenersOfAssignmentUpdate(comp, currentAttractionPoint);
}
}
}
@Override
public void eventOccurred(Object content, int type) {
if(type == EVENT_PAUSE_ENDED) {
SimLocationActuator comp = (SimLocationActuator) content;
// if the transit was triggered beforehand (e.g., attraction point moved), then do nothing.
if(roamingStates.get(comp) != roamingTransitionState.TRANSITION) {
this.addComponent(comp);
}
}
else if(type == EVENT_ROAMING_PAUSE_ENDED) {
SimLocationActuator comp = (SimLocationActuator) content;
IAttractionPoint currentAttractionPoint = this.assignments.get(comp);
// if the attraction point was removed in the meantime, go directly to transit state
if(currentAttractionPoint == null || !attractionProvider.getAttractionPoints().contains(currentAttractionPoint)) {
this.addComponent(comp);
}
else {
this.roamAroundAttractionPoint(comp);
}
}
}
public void setGaussianPauseTime(boolean useGaussian) {
useGaussianDistributedPauseTime = useGaussian;
}
}
package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
import java.util.*;
/**
* This {@link ITransitionStrategy} works with manual assignments. Initially components will be assigned
* This {@link IAttractionAssigmentStrategy} works with manual assignments. Initially components will be assigned
* one of the attraction points, but at any further point, the set list of attraction points doesn't really matter.
* New target can be assigned by using the {@link #updateTargetAttractionPoint(SimLocationActuator, AttractionPoint)} method,
* New target can be assigned by using the {@link #updateTargetAttractionPoint(SimLocationActuator, IAttractionPoint)} method,
* otherwise the components will stop moving upon reaching their target.
*
* @author Clemens Krug
*/
public class ManualAssignmentStrategy implements ITransitionStrategy
public class ManualAssignmentStrategy extends AbstractAttractionBasedAssignmentStrategy
{
private LinkedHashSet<AttractionPoint> aPoints = new LinkedHashSet<>();
public ManualAssignmentStrategy() {
super(0, 0);
}
private Map<SimLocationActuator, AttractionPoint> assignments = new HashMap<>();
private LinkedHashSet<IAttractionPoint> aPoints = new LinkedHashSet<>();
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
@Override
public AttractionPoint getAssignment(SimLocationActuator comp)
{
return assignments.get(comp);
}
@Override
public void addAttractionAssignmentListener(AttractionAssignmentListener listener)
{
listeners.add(listener);
}
private Map<SimLocationActuator, IAttractionPoint> assignments = new HashMap<>();
@Override
public void removeAttractionAssignmentListener(AttractionAssignmentListener listener)
{
listeners.remove(listener);
}
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
@Override
public void setAttractionPoints(Collection<AttractionPoint> attractionPoints) {
public void setAttractionPoints(Collection<IAttractionPoint> 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();
IAttractionPoint aPoint = aPoints.iterator().next();
assignments.put(ms, aPoint);
}
listeners.forEach(listener -> listener.updatedAttractionAssignment(ms, assignments.get(ms)));
}
@Override
public void reachedAttractionPoint(SimLocationActuator ms) {
public void reachedAttractionPoint(SimLocationActuator ms, IAttractionPoint attractionPoint) {
//Nothing to do.
}
@Override
public void updateTargetAttractionPoint(SimLocationActuator comp, AttractionPoint attractionPoint) {
public void updateTargetAttractionPoint(SimLocationActuator comp, IAttractionPoint attractionPoint) {
assignments.put(comp, attractionPoint);
listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
}
}
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.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.BasicAttractionPoint;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
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.IAttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
/**
* This {@link ITransitionStrategy} makes clients move around randomly in a specified area. You can specify the target
* area center via the {@link #updateTargetAttractionPoint(SimLocationActuator, AttractionPoint)} method. The client will then start
* This {@link IAttractionAssigmentStrategy} makes clients move around randomly in a specified area. You can specify the target
* area center via the {@link #updateTargetAttractionPoint(SimLocationActuator, IAttractionPoint)} method. The client will then start
* to roam the area randomly till a new target area is assigned.
*
* @author Clemens Krug
*
* 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 AbstractAttractionBasedAssignmentStrategy
{
public RandomInAreaTransitionStrategy() {
super(0, 0);
}
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<>();
/**
/**
* 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, IAttractionPoint> 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.
*/
private int defaultRadius;
@Override
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();
IAttractionPoint aPoint = attractionProvider.getAttractionPoints().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) {
public void reachedAttractionPoint(SimLocationActuator ms, IAttractionPoint attractionPoint) {
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) {
public void updateTargetAttractionPoint(SimLocationActuator comp, IAttractionPoint 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,12 +82,11 @@ 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.getLongitude();
double y = center.getLatitude();
double x = center.getLongitudeOrX();
double y = center.getLatitudeOrY();
double newX = -1;
double newY = -1;
......@@ -138,8 +96,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 +109,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));
}
}
......@@ -21,38 +21,33 @@
package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.Vector;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.common.SimHostComponent;
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.api.topology.social.SocialView;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.ITransitionStrategy.AttractionAssignmentListener;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionProvider;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
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.component.sensor.location.AttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
/**
* This is a {@link TransitionStrategy} for the Social Case. It will be try to
* build groups based on the {@link SocialView} information. For this, it tries
* to assignment the given objects to the given {@link AttractionPoint}s. For
* to assignment the given objects to the given {@link IAttractionPoint}s. For
* the {@link SocialView}, it is required a {@link #socialId}, to find the right
* {@link SocialView}.
*
......@@ -61,65 +56,58 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Attraction
* The Strategy has the parameter of {@link #socialFactor},
* {@link #minPauseTime} and {@link #maxPauseTime}. The socialFactor should be a
* value between 0 and 1. It gives the probability for a social based transition
* or the transition to a random {@link AttractionPoint}. If the social based
* or the transition to a random {@link IAttractionPoint}. If the social based
* transition is selected, then will be used a scoring to find the right
* {@link AttractionPoint}. For that, it will be used only the AttractionPoints,
* {@link IAttractionPoint}. For that, it will be used only the AttractionPoints,
* of the hosts, which are in the same SocialCluster or are SocialNeighbors. For
* this AttractionPoints it will be find the highest scoring, which is to found
* in score. .
*
* <br>
*
* After the finding of the next {@link AttractionPoint}, it will be scheduled
* After the finding of the next {@link IAttractionPoint}, it will be scheduled
* an Event, with a delay between min- and maxPauseTime. After this delay, it
* will be tried to assign a new {@link AttractionPoint} like the described
* will be tried to assign a new {@link IAttractionPoint} like the described
* above.
*
*
*
* @author Christoph Muenker
* @version 1.0, 02.07.2013
*
* <hr>
*
*
*
*/
public class SocialTransitionStrategy
implements ITransitionStrategy, EventHandler {
public class SocialTransitionStrategy extends AbstractAttractionBasedAssignmentStrategy implements EventHandler {
private String socialId = null;
private SocialView socialView;
private List<SimLocationActuator> comps = new Vector<SimLocationActuator>();
private LinkedHashSet<AttractionPoint> aPoints = new LinkedHashSet<AttractionPoint>();
private Map<SimLocationActuator, AttractionPoint> assignments = new HashMap<SimLocationActuator, AttractionPoint>();
private List<SimLocationActuator> comps = new Vector<SimLocationActuator>();
private Set<SimLocationActuator> arrivedAtAttractionPoint = new LinkedHashSet<>();
private Map<SimLocationActuator, Set<AttractionPoint>> favoritePlaces = new HashMap<SimLocationActuator, Set<AttractionPoint>>();
private Map<SimLocationActuator, Set<IAttractionPoint>> favoritePlaces = new HashMap<SimLocationActuator, Set<IAttractionPoint>>();
private Map<SimLocationActuator, SimHost> mapMsHost = new HashMap<SimLocationActuator, SimHost>();
private Map<SimHost, SimLocationActuator> mapHostMs = new HashMap<SimHost, SimLocationActuator>();
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
private double minPauseTime = Simulator.MINUTE_UNIT * 0.5;
private double maxPauseTime = Simulator.MINUTE_UNIT * 100;
private double socialFactor = 0.8;
private long numberOfFavoritePlaces = 4;
private Random rand;
private PositionVector worldDimension;
private boolean init = false;
public SocialTransitionStrategy() {
this.rand = Randoms.getRandom(SocialTransitionStrategy.class);
@XMLConfigurableConstructor({ "defaultPauseTimeMin", "defaultPauseTimeMax" })
public SocialTransitionStrategy(long defaultPauseTimeMin, long defaultPauseTimeMax) {
super(defaultPauseTimeMin, defaultPauseTimeMax);
}
private void init() {
if (!init) {
if (socialId == null) {
......@@ -135,7 +123,7 @@ public class SocialTransitionStrategy
"Cannot find the right socialView. Is the socialId correct?");
}
if (minPauseTime > maxPauseTime) {
if (defaultPauseTimeMin > defaultPauseTimeMax) {
throw new ConfigurationException(
"MinPauseTime should be smaller then maxPauseTime.");
}
......@@ -149,36 +137,7 @@ public class SocialTransitionStrategy
public void setSocialId(String socialId) {
this.socialId = socialId;
}
@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) {
init();
aPoints.addAll(attractionPoints);
}
@Override
public Set<AttractionPoint> getAllAttractionPoints() {
return aPoints;
}
@Override
public void addComponent(SimLocationActuator ms) {
comps.add(ms);
......@@ -187,8 +146,8 @@ public class SocialTransitionStrategy
// position.
// TODO: needed? We do Transition as next, and this will delete the
// assignment..
AttractionPoint nearest = aPoints.iterator().next();
for (AttractionPoint aPoint : aPoints) {
IAttractionPoint nearest = attractionProvider.getAttractionPoints().iterator().next();
for (IAttractionPoint aPoint : attractionProvider.getAttractionPoints()) {
if (nearest.distanceTo(ms.getRealPosition()) > aPoint
.distanceTo(ms.getRealPosition())) {
nearest = aPoint;
......@@ -203,56 +162,54 @@ public class SocialTransitionStrategy
@Override
public void updateTargetAttractionPoint(SimLocationActuator comp,
AttractionPoint attractionPoint) {
IAttractionPoint attractionPoint) {
arrivedAtAttractionPoint.remove(comp);
assignments.put(comp, attractionPoint);
listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
notifyListenersOfAssignmentUpdate(comp, attractionPoint);
}
@Override
public void reachedAttractionPoint(SimLocationActuator ms) {
public void reachedAttractionPoint(SimLocationActuator ms, IAttractionPoint attractionPoint) {
if (!arrivedAtAttractionPoint.contains(ms)) {
Event.scheduleWithDelay(getPauseTime(), this, ms, 0);
Event.scheduleWithDelay(getPauseTime(attractionPoint), this, ms, 0);
arrivedAtAttractionPoint.add(ms);
}
}
public void doTransition(SimLocationActuator ms) {
List<AttractionPoint> apFavorites = getFavoritePlaces(ms);
List<AttractionPoint> apFriends = getFriendsPlaces(ms);
List<AttractionPoint> apClusters = getClusterPlaces(ms);
List<AttractionPoint> apRandom = getRandomPlaces(ms,
(int) Math.max(aPoints.size() * 0.2, 5));
AttractionPoint ap = null;
if (rand.nextDouble() < socialFactor) {
List<IAttractionPoint> apFavorites = getFavoritePlaces(ms);
List<IAttractionPoint> apFriends = getFriendsPlaces(ms);
List<IAttractionPoint> apClusters = getClusterPlaces(ms);
List<IAttractionPoint> apRandom = getRandomPlaces(ms,
(int) Math.max(attractionProvider.getAttractionPoints().size() * 0.2, 5));
IAttractionPoint ap = null;
if (rnd.nextDouble() < socialFactor) {
ap = findHighestScore(ms, apFavorites, apFriends, apClusters,
apRandom);
} else {
List<AttractionPoint> aps = new ArrayList<AttractionPoint>();
List<IAttractionPoint> aps = new ArrayList<IAttractionPoint>();
aps.addAll(apRandom);
aps.addAll(apFavorites);
ap = aps.get(rand.nextInt(apRandom.size()));
ap = aps.get(rnd.nextInt(apRandom.size()));
}
assignments.put(ms, ap);
arrivedAtAttractionPoint.remove(ms);
for (AttractionAssignmentListener listener : listeners) {
listener.updatedAttractionAssignment(ms, ap);
}
notifyListenersOfAssignmentUpdate(ms, ap);
}
private AttractionPoint findHighestScore(SimLocationActuator ms,
List<AttractionPoint> apFavorites, List<AttractionPoint> apFriends,
List<AttractionPoint> apClusters, List<AttractionPoint> apRandom) {
Set<AttractionPoint> aps = new LinkedHashSet<AttractionPoint>();
private IAttractionPoint findHighestScore(SimLocationActuator ms,
List<IAttractionPoint> apFavorites, List<IAttractionPoint> apFriends,
List<IAttractionPoint> apClusters, List<IAttractionPoint> apRandom) {
Set<IAttractionPoint> aps = new LinkedHashSet<IAttractionPoint>();
aps.addAll(apFavorites);
aps.addAll(apFriends);
aps.addAll(apClusters);
aps.addAll(apRandom);
double maxScore = 0;
AttractionPoint maxAp = null;
for (AttractionPoint ap : aps) {
IAttractionPoint maxAp = null;
for (IAttractionPoint ap : aps) {
double score = score(ms, ap, apFavorites, apFriends, apClusters,
apRandom);
// System.out.println(score);
......@@ -282,9 +239,9 @@ public class SocialTransitionStrategy
* @param apRandom
* @return
*/
private double score(SimLocationActuator ms, AttractionPoint ap,
List<AttractionPoint> apFavorites, List<AttractionPoint> apFriends,
List<AttractionPoint> apClusters, List<AttractionPoint> apRandom) {
private double score(SimLocationActuator ms, IAttractionPoint ap,
List<IAttractionPoint> apFavorites, List<IAttractionPoint> apFriends,
List<IAttractionPoint> apClusters, List<IAttractionPoint> apRandom) {
double distance = ms.getRealPosition().distanceTo(ap);
double distanceScore = 1 - (distance / worldDimension.getLength());
......@@ -319,9 +276,9 @@ public class SocialTransitionStrategy
}
// counts the number of the AttractionPoint in the list
private int occurence(AttractionPoint ap, List<AttractionPoint> aps) {
private int occurence(IAttractionPoint ap, List<IAttractionPoint> aps) {
int i = 0;
for (AttractionPoint a : aps) {
for (IAttractionPoint a : aps) {
if (a.equals(ap)) {
i++;
}
......@@ -329,9 +286,9 @@ public class SocialTransitionStrategy
return i;
}
private int assignedToAp(AttractionPoint ap) {
private int assignedToAp(IAttractionPoint ap) {
int i = 1;
for (Entry<SimLocationActuator, AttractionPoint> entry : assignments
for (Entry<SimLocationActuator, IAttractionPoint> entry : assignments
.entrySet()) {
if (entry.getValue().equals(ap)) {
i++;
......@@ -340,15 +297,15 @@ public class SocialTransitionStrategy
return i;
}
private List<AttractionPoint> getRandomPlaces(SimLocationActuator ms,
private List<IAttractionPoint> getRandomPlaces(SimLocationActuator ms,
int number) {
List<AttractionPoint> result = new Vector<AttractionPoint>(aPoints);
Collections.shuffle(result, rand);
List<IAttractionPoint> result = new Vector<IAttractionPoint>(attractionProvider.getAttractionPoints());
Collections.shuffle(result, rnd);
return result.subList(0, Math.min(result.size(), number));
}
private List<AttractionPoint> getClusterPlaces(SimLocationActuator ms) {
List<AttractionPoint> result = new Vector<AttractionPoint>();
private List<IAttractionPoint> getClusterPlaces(SimLocationActuator ms) {
List<IAttractionPoint> result = new Vector<IAttractionPoint>();
SimHost msHost = mapMsHost.get(ms);
for (SimHost host : socialView.getCluster(msHost)) {
......@@ -361,8 +318,8 @@ public class SocialTransitionStrategy
return result;
}
private List<AttractionPoint> getFriendsPlaces(SimLocationActuator ms) {
List<AttractionPoint> result = new Vector<AttractionPoint>();
private List<IAttractionPoint> getFriendsPlaces(SimLocationActuator ms) {
List<IAttractionPoint> result = new Vector<IAttractionPoint>();
SimHost msHost = mapMsHost.get(ms);
for (SimHost host : socialView.getNeighbors(msHost)) {
......@@ -375,15 +332,15 @@ public class SocialTransitionStrategy
return result;
}
private List<AttractionPoint> getFavoritePlaces(SimLocationActuator ms) {
return new Vector<AttractionPoint>(favoritePlaces.get(ms));
private List<IAttractionPoint> getFavoritePlaces(SimLocationActuator ms) {
return new Vector<IAttractionPoint>(favoritePlaces.get(ms));
}
private void assignFavoritePlaces(SimLocationActuator ms) {
Set<AttractionPoint> msFavoritePlaces = new LinkedHashSet<AttractionPoint>();
LinkedList<AttractionPoint> temp = new LinkedList<AttractionPoint>(
aPoints);
Collections.shuffle(temp, rand);
Set<IAttractionPoint> msFavoritePlaces = new LinkedHashSet<IAttractionPoint>();
LinkedList<IAttractionPoint> temp = new LinkedList<IAttractionPoint>(
attractionProvider.getAttractionPoints());
Collections.shuffle(temp, rnd);
for (int i = 0; i < numberOfFavoritePlaces; i++) {
if (!temp.isEmpty()) {
msFavoritePlaces.add(temp.removeFirst());
......@@ -401,28 +358,7 @@ public class SocialTransitionStrategy
mapHostMs.put(host, ms);
mapMsHost.put(ms, host);
}
protected long getPauseTime() {
return (long) ((rand.nextDouble() * (maxPauseTime - minPauseTime))
+ minPauseTime);
}
public void setMinPauseTime(long minPauseTime) {
if (minPauseTime < 0) {
throw new ConfigurationException(
"MinPauseTime should be bigger then 0!");
}
this.minPauseTime = minPauseTime;
}
public void setMaxPauseTime(long maxPauseTime) {
if (maxPauseTime < 0) {
throw new ConfigurationException(
"MaxPauseTime should be bigger then 0!");
}
this.maxPauseTime = maxPauseTime;
}
@Override
public void eventOccurred(Object se, int type) {
if (arrivedAtAttractionPoint.contains(se)) {
......
......@@ -20,143 +20,57 @@
package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
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.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
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.component.sensor.location.AttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
/**
* A simplified transition strategy taking only the weights of an AP into account.
*
* @author Bjoern Richerzhagen
* @version 1.0, Jan 16, 2017
*
* 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 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<>();
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);
public class WeightedTransitionStrategy extends AbstractAttractionBasedAssignmentStrategy implements EventHandler {
@XMLConfigurableConstructor({ "defaultPauseTimeMin", "defaultPauseTimeMax" })
public WeightedTransitionStrategy(long defaultPauseTimeMin, long defaultPauseTimeMax) {
super(defaultPauseTimeMin, defaultPauseTimeMax);
}
@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));
}
private AttractionPoint getNewAssignment(SimLocationActuator component) {
double score = rnd.nextDouble();
List<AttractionPoint> candidates = new LinkedList<>();
for (AttractionPoint ap : aps) {
if (ap.getWeight() >= score) {
candidates.add(ap);
}
}
if (candidates.isEmpty()) {
candidates.addAll(aps);
}
AttractionPoint assignment = candidates.get(rnd.nextInt(candidates.size()));
listeners.forEach(listener -> listener.updatedAttractionAssignment(component, assignment));
return assignment;
}
private long getPauseTime(SimLocationActuator component) {
return (long) (rnd.nextDouble() * (pauseTimeMax-pauseTimeMin)) + pauseTimeMin;
IAttractionPoint nextAP = getNewAttractionPointAssignment(ms);
updateTargetAttractionPoint(ms, nextAP);
}
@Override
public void eventOccurred(Object content, int type) {
assert type == EVENT_PAUSE_ENDED;
SimLocationActuator comp = (SimLocationActuator) content;
if (!this.assignments.containsKey(comp)) {
// maybe someone assigned a new AP in the meantime. ignore.
this.assignments.put(comp, getNewAssignment(comp));
this.assignments.put(comp, getNewAttractionPointAssignment(comp));
}
}
@Override
public void reachedAttractionPoint(SimLocationActuator ms) {
// JZ: Added this to prevent multiple calls while pausing
public void reachedAttractionPoint(SimLocationActuator ms, IAttractionPoint attractionPoint) {
// 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;
}
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) {
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;
Event.scheduleWithDelay(getPauseTime(attractionPoint), this, ms, EVENT_PAUSE_ENDED);
}
}
......@@ -52,4 +52,8 @@ public class VehicleInformationContainer {
public RoadNetworkRoute getRoute() {
return _route;
}
public void setRoute(RoadNetworkRoute pRoute) {
_route = pRoute;
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
public class RoadSideUnitInformationHandler {
......@@ -27,8 +27,8 @@ public class RoadSideUnitInformationHandler {
List<Location> result = new ArrayList<>();
for (Location position : _positions) {
if (_startX <= position.getLongitude() && position.getLongitude() <= _endX && _startY <= position.getLatitude() && position.getLatitude() <= _endY) {
result.add(new PositionVector(position.getLongitude() - _startX, position.getLatitude() - _startY, 0));
if (_startX <= position.getLongitudeOrX() && position.getLongitudeOrX() <= _endX && _startY <= position.getLatitudeOrY() && position.getLatitudeOrY() <= _endY) {
result.add(new PositionVector(position.getLongitudeOrX() - _startX, position.getLatitudeOrY() - _startY, 0));
}
}
......
......@@ -12,13 +12,15 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import de.tud.kom.p2psim.api.simengine.SimulatorObserver;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleInformationContainer;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.EdgeController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
......@@ -31,6 +33,8 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.Road
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.SerializableRoadNetwork;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoAdditionalRouteAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoExitAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.paths.VehiclePathTracker;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.paths.VehiclePathTrackerFactory;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.DijkstraAlgorithm;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.RoutingAlgorithm;
import de.tudresden.sumo.cmd.Edge;
......@@ -54,7 +58,9 @@ import it.polito.appeal.traci.SumoTraciConnection;
*
*/
public class TraciSimulationController implements VehicleController, SimulationSetupExtractor, EdgeController, SimulatorObserver {
private static final File TEMP_FILE = new File(new File(System.getProperty("java.io.tmpdir")), "road_network.tmp");
private static final File TEMP_FILE = new File(new File(System.getProperty("user.home") + "/.simonstrator"), "road_network.tmp");
private static final boolean TRAIN_PATH_PROBABILITIES = false;
private Random _random = Randoms.getRandom(getClass());
......@@ -75,6 +81,8 @@ public class TraciSimulationController implements VehicleController, SimulationS
private double _endX;
private double _endY;
private int _startTime = 0;
private Map<String, VehicleInformationContainer> _positons = new HashMap<>();
private boolean _initalized = false;
......@@ -87,16 +95,23 @@ public class TraciSimulationController implements VehicleController, SimulationS
private RoutingAlgorithm _algorithm = new DijkstraAlgorithm();
public static synchronized TraciSimulationController createSimulationController(String pSumoExe, String pConfigFile) {
private int _timestepRatio;
public static synchronized TraciSimulationController createSimulationController(String pSumoExe, String pConfigFile, int pTimestepRatio) {
if (!CONTROLLER.containsKey(pConfigFile)) {
CONTROLLER.put(pConfigFile, new TraciSimulationController(pSumoExe, pConfigFile));
CONTROLLER.put(pConfigFile, new TraciSimulationController(pSumoExe, pConfigFile, pTimestepRatio));
}
return CONTROLLER.get(pConfigFile);
}
private TraciSimulationController(String pSumoExe, String pConfigFile) {
private TraciSimulationController(String pSumoExe, String pConfigFile, int pTimestepRatio) {
_sumoExe = pSumoExe;
_configFile = pConfigFile;
_timestepRatio = pTimestepRatio;
}
public void setStartTime(int pStartTime) {
_startTime = pStartTime;
}
public static VehicleController getSimulationController() {
......@@ -104,7 +119,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
}
@Override
public synchronized void init() {
public synchronized void init(long pTimeScale) {
if (!_initalized) {
Random random = Randoms.getRandom("SUMO");
......@@ -115,6 +130,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
* prevent vehicles form teleporting (http://sumo.dlr.de/wiki/Simulation/Why_Vehicles_are_teleporting)
*/
_connection.addOption("time-to-teleport", Integer.toString(-1));
_connection.addOption("step-length", String.valueOf(pTimeScale / (double)Time.SECOND));
try {
_connection.runServer();
......@@ -127,6 +143,26 @@ public class TraciSimulationController implements VehicleController, SimulationS
Simulator.getInstance().addObserver(this);
_initalized = true;
for (int i = 0; i < _startTime; i++) {
System.out.println("Pre-Start Setup: " + i + " of " + _startTime + " steps done.");
if (TRAIN_PATH_PROBABILITIES) {
Map<String, VehicleInformationContainer> positions = nextStep();
for (Entry<String, VehicleInformationContainer> entry : positions.entrySet()) {
VehiclePathTrackerFactory.getVehiclePathTracker().setEdge(entry.getKey(), entry.getValue().getRoute().getStart(), i * pTimeScale);
}
} else {
try {
_connection.do_timestep();
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
if (TRAIN_PATH_PROBABILITIES) {
VehiclePathTrackerFactory.getVehiclePathTracker().disableTracking();
}
}
}
......@@ -146,6 +182,13 @@ public class TraciSimulationController implements VehicleController, SimulationS
return _positons.get(pVehicleID).getPosition();
}
@Override
public double getVehicleLength(String pVehicleID) {
SumoCommand lengthCommand = Vehicle.getLength(pVehicleID);
Object requestObject = requestObject(lengthCommand);
return (Double) requestObject;
}
@Override
public double getVehicleHeading(String pVehicleID) {
return _positons.get(pVehicleID).getHeading();
......@@ -176,10 +219,10 @@ public class TraciSimulationController implements VehicleController, SimulationS
if (_observedAreaSet) {
if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) {
result.add(new PositionVector(sumoPosition.x - _startX, sumoPosition.y - _startY, 0));
result.add(new PositionVector(sumoPosition.x - _startX, sumoPosition.y - _startY));
}
} else {
result.add(new PositionVector(sumoPosition.x, sumoPosition.y, 0));
result.add(new PositionVector(sumoPosition.x, sumoPosition.y));
}
}
......@@ -205,54 +248,63 @@ public class TraciSimulationController implements VehicleController, SimulationS
return result;
}
@Override
public boolean nextStep() {
if (Simulator.getEndTime() == Simulator.getCurrentTime()) {
return false;
}
try {
_connection.do_timestep();
private Map<String, VehicleInformationContainer> nextStep() {
try {
for (int i = 0; i < _timestepRatio; i++) {
_connection.do_timestep();
}
try {
synchronized (_positons) {
_positons.clear();
try {
Map<String, VehicleInformationContainer> vehiclePositions = new HashMap<>();
int temp = (Integer) _connection.do_job_get(Simulation.getCurrentTime());
List<String> allVehicles = requestAllVehicles();
for (String vehicle : allVehicles) {
Location position = requestVehiclePosition(vehicle);
if (position != null) {
double heading = requestVehicleHeading(vehicle);
double speed = requestVehicleSpeed(vehicle);
RoadNetworkRoute route = requestVehicleRoute(vehicle);
_step = temp / 1000.0;
VehicleInformationContainer informationContainer = new VehicleInformationContainer(position, heading, speed, route);
if (_start == -1) {
_start = _step;
}
vehiclePositions.put(vehicle, informationContainer);
}
}
return vehiclePositions;
} catch (Exception e) {
throw new AssertionError(e);
}
} catch (Exception e) {
throw new AssertionError(e);
}
}
Map<String, VehicleInformationContainer> vehiclePositions = new HashMap<>();
@Override
public boolean nextStep(long pTimeScale) {
if (Simulator.getEndTime() == Simulator.getCurrentTime()) {
return false;
}
Map<String, VehicleInformationContainer> vehiclePositions = nextStep();
List<String> allVehicles = requestAllVehicles();
for (String vehicle : allVehicles) {
Location position = requestVehiclePosition(vehicle);
if (position != null) {
double heading = requestVehicleHeading(vehicle);
double speed = requestVehicleSpeed(vehicle);
RoadNetworkRoute route = requestVehicleRoute(vehicle);
try {
int temp = (Integer) _connection.do_job_get(Simulation.getCurrentTime());
VehicleInformationContainer informationContainer = new VehicleInformationContainer(position, heading, speed, route);
_step = temp / (pTimeScale / (double)Time.MILLISECOND);
vehiclePositions.put(vehicle, informationContainer);
}
}
_positons = vehiclePositions;
}
} catch (Exception e) {
e.printStackTrace();
}
if (_start == -1) {
_start = _step;
}
} catch (Exception e) {
throw new AssertionError(e);
}
return true;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
if (vehiclePositions != null) {
_positons = vehiclePositions;
return true;
} else {
_positons.clear();
return false;
}
return false;
}
private Location requestVehiclePosition(String pVehicleID) {
......@@ -272,7 +324,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
if (_observedAreaSet) {
if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) {
return new PositionVector(sumoPosition.x - _startX, sumoPosition.y - _startY, 0);
return new PositionVector(sumoPosition.x - _startX, sumoPosition.y - _startY);
} else {
double diffX = _startX - sumoPosition.x;
if (diffX < 0 || sumoPosition.x - _endX < diffX) {
......@@ -297,7 +349,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
return null;
}
} else {
return new PositionVector(sumoPosition.x, sumoPosition.y, 0);
return new PositionVector(sumoPosition.x, sumoPosition.y);
}
}
......@@ -314,7 +366,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
Object angleObject = requestObject(angleCommand);
if (angleObject != null) {
return (Double) angleObject;
}
......@@ -408,7 +459,11 @@ public class TraciSimulationController implements VehicleController, SimulationS
add = true;
}
if (add) {
streets.add(_roadNetwork.getEdge(street));
RoadNetworkEdge edge = _roadNetwork.getEdge(street);
streets.add(edge);
if (!edge.isUsable()) {
break;
}
}
}
......@@ -484,8 +539,12 @@ public class TraciSimulationController implements VehicleController, SimulationS
SumoCommand netBoundaryCommand = Simulation.getNetBoundary();
try {
SumoBoundingBox netBoundary = (SumoBoundingBox) _connection.do_job_get(netBoundaryCommand);
return Math.max(netBoundary.x_max - netBoundary.x_min, 10);
SumoBoundingBox netBoundary = (SumoBoundingBox) _connection.do_job_get(netBoundaryCommand);
if (_observedAreaSet) {
return Math.max(Math.min(netBoundary.x_max, _endX) - Math.max(netBoundary.x_min, _startX), 10);
} else {
return Math.max(netBoundary.x_max - netBoundary.x_min, 10);
}
} catch (Exception e) {
//Nothing to do
}
......@@ -498,7 +557,11 @@ public class TraciSimulationController implements VehicleController, SimulationS
try {
SumoBoundingBox netBoundary = (SumoBoundingBox) _connection.do_job_get(netBoundaryCommand);
return Math.max(netBoundary.y_max - netBoundary.y_min, 10);
if (_observedAreaSet) {
return Math.max(Math.min(netBoundary.y_max, _endY) - Math.max(netBoundary.y_min, _startY), 10);
} else {
return Math.max(netBoundary.y_max - netBoundary.y_min, 10);
}
} catch (Exception e) {
//Nothing to do
}
......@@ -610,6 +673,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
routeEdges.add(edge.getEdgeID());
}
execute(Vehicle.setRoute(pVehicle, routeEdges));
_positons.get(pVehicle).setRoute(pRoute);
}
@Override
......@@ -707,12 +771,14 @@ public class TraciSimulationController implements VehicleController, SimulationS
_roadNetwork = new RoadNetwork(roadNetwork, this, true);
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(TEMP_FILE));
if (TEMP_FILE.getParentFile().exists() || TEMP_FILE.getParentFile().mkdirs()) {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(TEMP_FILE));
outputStream.writeObject(new SerializableRoadNetwork(_roadNetwork));
outputStream.writeObject(new SerializableRoadNetwork(_roadNetwork));
outputStream.flush();
outputStream.close();
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
//Nothing to do
e.printStackTrace();
......@@ -749,19 +815,19 @@ public class TraciSimulationController implements VehicleController, SimulationS
public List<Location> getLaneShape(String pLaneID) {
List<Location> positions = new ArrayList<>();
boolean set = true;
boolean set = false;
SumoCommand laneShapeCommand = Lane.getShape(pLaneID);
SumoGeometry geometry = (SumoGeometry)requestObject(laneShapeCommand);
for (SumoPosition2D location : geometry.coords) {
if (!isObservedAreaSet()) {
positions.add(new PositionVector(location.x, location.y));
set = true;
} else {
if (_startX <= location.x && location.x <= _endX && _startY <= location.y && location.y <= _endY) {
positions.add(new PositionVector(location.x - _startX, location.y - _startY));
} else {
set = true;
}
positions.add(new PositionVector(location.x - _startX, location.y - _startY));
}
}
......@@ -786,8 +852,8 @@ public class TraciSimulationController implements VehicleController, SimulationS
int count = 0;
for (Location position : positions) {
x += position.getLongitude();
y += position.getLatitude();
x += position.getLongitudeOrX();
y += position.getLatitudeOrY();
count++;
}
......@@ -830,8 +896,11 @@ public class TraciSimulationController implements VehicleController, SimulationS
length += (double) object;
}
return length / (_roadNetwork.getEdge(pEdgeID).getLaneAmount());
if ((_roadNetwork.getEdge(pEdgeID).getLaneAmount()) != 0) {
return length / (_roadNetwork.getEdge(pEdgeID).getLaneAmount());
} else {
return 0.0;
}
}
@Override
......@@ -839,10 +908,12 @@ public class TraciSimulationController implements VehicleController, SimulationS
if (_observedAreaSet) {
List<RoadNetworkLane> lanes = _roadNetwork.getEdge(pEdgeID).getLanes();
if (lanes.size() > 0) {
List<Location> laneShape = getLaneShape(lanes.get(0).getLaneID());
if (laneShape.size() > 1) {
return true;
}
for (RoadNetworkLane lane : lanes) {
List<Location> laneShape = getLaneShape(lane.getLaneID());
if (laneShape.size() > 1) {
return true;
}
}
}
return false;
}
......
......@@ -4,7 +4,7 @@ import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
public class SimulationSetupInformationHandler extends DefaultHandler {
......
......@@ -6,8 +6,8 @@ import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleInformationContainer;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
public class VehicleDataInformationHandler extends DefaultHandler {
private boolean _next = true;
......
......@@ -12,6 +12,7 @@ import javax.xml.parsers.SAXParserFactory;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleInformationContainer;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.csv.RoadSideUnitInformationHandler;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.VehicleController;
......@@ -49,7 +50,7 @@ public class XMLSimulationController implements VehicleController, SimulationSet
}
@Override
public void init() {
public void init(long timeBetweenMoveOperations) {
if (!_initalized) {
if (_vehicleDataPath != null) {
......@@ -57,7 +58,7 @@ public class XMLSimulationController implements VehicleController, SimulationSet
thread.start();
for (int i = 0; i < _futureInformation; i++) {
nextStep();
nextStep(Time.SECOND);
}
}
if (_roadSideUnitDataPath != null) {
......@@ -77,7 +78,7 @@ public class XMLSimulationController implements VehicleController, SimulationSet
}
@Override
public boolean nextStep() {
public boolean nextStep(long pTimeScale) {
_vehicleDataInformationHandler.readNext();
_step = _vehicleDataInformationHandler.getStep() - _futureInformation;
......@@ -171,6 +172,11 @@ public class XMLSimulationController implements VehicleController, SimulationSet
return -1;
}
@Override
public double getVehicleLength(String pVehicleID) {
throw new UnsupportedOperationException("This method is not supported for " + getClass().getSimpleName());
}
@Override
public RoadNetworkRoute getCurrentRoute(String pVehicleID) {
throw new UnsupportedOperationException("This method is not supported for " + getClass().getSimpleName());
......
......@@ -39,7 +39,7 @@ import de.tud.kom.p2psim.api.topology.waypoints.WaypointModel;
import de.tud.kom.p2psim.api.util.geo.maps.Map;
import de.tud.kom.p2psim.impl.scenario.simcfg2.annotations.Configure;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.util.Tuple;
import de.tud.kom.p2psim.impl.util.geo.maps.MapChangeListener;
import de.tud.kom.p2psim.impl.util.geo.maps.MapLoader;
......
......@@ -32,7 +32,7 @@ import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.Polygon;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
/**
* This Obstacle is build from a list of coordinates that form a Polygon. The
......
......@@ -27,7 +27,7 @@ import com.google.common.collect.Lists;
import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.obstacles.Obstacle;
import de.tud.kom.p2psim.api.topology.obstacles.ObstacleModel;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponent;
......
......@@ -31,7 +31,7 @@ import java.util.Vector;
import java.util.zip.GZIPInputStream;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
public class BonnMotionPositionDistribution extends PositionDistribution {
......
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