Commit f7c16e22 authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Fist evaluation version for geographical relevance

parent bdd6e83e
......@@ -26,6 +26,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
......@@ -38,6 +39,7 @@ import de.tud.kom.p2psim.impl.vehicular.DefaultVehicleInformationComponent;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
......@@ -50,6 +52,8 @@ import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
public class VehicleMovementModel implements MovementModel, EventHandler {
private Random _knownRoutesRandom = Randoms.getRandom(getClass());
private static final Location DEFAULT_LOCATION = new PositionVector(Double.NaN, Double.NaN);
private boolean _reuseComponents = false;
......@@ -88,6 +92,8 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
private List<Location> intersections;
private double _percentageOfKnownRoutes = 1;
/**
* Constructor for the movement model using the sumo TraCI API
* @param timeBetweenMoveOperations The time between two movement operations.
......@@ -143,6 +149,13 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
this.height = height;
}
/**
* @param pPercentageOfKnownRoutes the percentageOfKnownRoutes to set
*/
public void setPercentageOfKnownRoutes(double pPercentageOfKnownRoutes) {
_percentageOfKnownRoutes = pPercentageOfKnownRoutes;
}
public void setReuseComponents(boolean pReuseComponents) {
_reuseComponents = pReuseComponents;
......@@ -358,7 +371,13 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
vehicularHostComponent = simLocationActuator.getHost().getComponent(VehicleInformationComponent.class);
} catch (ComponentNotAvailableException e) {
Host host = simLocationActuator.getHost();
vehicularHostComponent = new DefaultVehicleInformationComponent(host, _controller, _extractor);
boolean routeKnown;
if (_knownRoutesRandom.nextDouble() < _percentageOfKnownRoutes) {
routeKnown = true;
} else {
routeKnown = false;
}
vehicularHostComponent = new DefaultVehicleInformationComponent(host, _controller, _extractor, routeKnown);
host.registerComponent(vehicularHostComponent);
}
vehicularHostComponent.setVehicleID(vehicle);
......
......@@ -21,17 +21,20 @@
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
public class VehicleInformationContainer {
private Location _position;
private double _heading;
private double _speed;
private RoadNetworkRoute _route;
public VehicleInformationContainer(Location pPosition, double pHeading,
double pSpeed) {
double pSpeed, RoadNetworkRoute pRoute) {
_position = pPosition;
_heading = pHeading;
_speed = pSpeed;
_route = pRoute;
}
public Location getPosition() {
......@@ -45,4 +48,8 @@ public class VehicleInformationContainer {
public double getSpeed() {
return _speed;
}
public RoadNetworkRoute getRoute() {
return _route;
}
}
\ No newline at end of file
......@@ -7,7 +7,6 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
......@@ -75,9 +74,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
private double _endX;
private double _endY;
private Map<Double, Map<String, VehicleInformationContainer>> _positonsByTimestamp = new HashMap<>();
private int _futureInformation = 0;
private Map<String, VehicleInformationContainer> _positons = new HashMap<>();
private boolean _initalized = false;
......@@ -120,10 +117,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
try {
_connection.runServer();
for (int i = 0; i < _futureInformation; i++) {
nextStep();
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
......@@ -149,14 +142,15 @@ public class TraciSimulationController implements VehicleController, SimulationS
@Override
public Location getVehiclePosition(String pVehicleID) {
return getVehiclePosition(_step, pVehicleID);
return _positons.get(pVehicleID).getPosition();
}
@Override
public Location getVehiclePosition(double pStep, String pVehicleID) {
Map<String, VehicleInformationContainer> map = _positonsByTimestamp.get(pStep);
return map.get(pVehicleID).getPosition();
if (pStep == _step) {
return getVehiclePosition(pVehicleID);
}
throw new UnsupportedOperationException("Future locations is not supported anymore!");
}
@Override
......@@ -214,18 +208,18 @@ public class TraciSimulationController implements VehicleController, SimulationS
_connection.do_timestep();
try {
synchronized (_positons) {
_positons.clear();
int temp = (Integer) _connection.do_job_get(Simulation.getCurrentTime());
_step = temp / 1000.0 - _futureInformation;
_step = temp / 1000.0;
if (_start == -1) {
_start = _step + _futureInformation;
_start = _step;
}
double newStep = _step + _futureInformation;
if (!_positonsByTimestamp.containsKey(newStep)) {
Map<String, VehicleInformationContainer> vehiclePositions = new HashMap<>();
_positonsByTimestamp.put(newStep, vehiclePositions);
List<String> allVehicles = requestAllVehicles();
for (String vehicle : allVehicles) {
......@@ -233,16 +227,14 @@ public class TraciSimulationController implements VehicleController, SimulationS
if (position != null) {
double heading = requestVehicleHeading(vehicle);
double speed = requestVehicleSpeed(vehicle);
RoadNetworkRoute route = requestVehicleRoute(vehicle);
VehicleInformationContainer informationContainer = new VehicleInformationContainer(position, heading, speed);
VehicleInformationContainer informationContainer = new VehicleInformationContainer(position, heading, speed, route);
vehiclePositions.put(vehicle, informationContainer);
}
}
}
if (_positonsByTimestamp.containsKey(_step - 1)) {
_positonsByTimestamp.remove(_step - 1);
_positons = vehiclePositions;
}
} catch (Exception e) {
e.printStackTrace();
......@@ -364,9 +356,10 @@ public class TraciSimulationController implements VehicleController, SimulationS
@Override
public List<String> getAllVehicles(double pStep) {
Map<String, VehicleInformationContainer> map = _positonsByTimestamp.get(pStep);
return new ArrayList<>(map.keySet());
if (pStep == _step) {
return new ArrayList<>(_positons.keySet());
}
throw new UnsupportedOperationException("Future locations is not supported anymore!");
}
public RoadNetworkEdge getCurrentEdge(String pVehicleID) {
......@@ -387,10 +380,12 @@ public class TraciSimulationController implements VehicleController, SimulationS
return edge;
}
@Override
public RoadNetworkRoute getCurrentRoute(String pVehicleID) {
public RoadNetworkRoute requestVehicleRoute(String pVehicleID) {
obtainRoadNetwork();
synchronized (_positons) {
List<RoadNetworkEdge> streets = new ArrayList<>();
SumoCommand routeCommand = Vehicle.getRoute(pVehicleID);
Object object = requestObject(routeCommand);
SumoStringList streetList = (SumoStringList) object;
......@@ -401,7 +396,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
return null;
}
List<RoadNetworkEdge> streets = new ArrayList<>();
boolean add = false;
for (String street : streetList) {
if (street.equals(currentEdge.getEdgeID())) {
......@@ -419,6 +413,15 @@ public class TraciSimulationController implements VehicleController, SimulationS
return new RoadNetworkRoute(streets);
}
}
@Override
public RoadNetworkRoute getCurrentRoute(String pVehicleID) {
if (_positons.containsKey(pVehicleID)) {
return _positons.get(pVehicleID).getRoute();
}
return null;
}
public Object requestObject(SumoCommand routeCommand) {
Object object = null;
......@@ -454,9 +457,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
@Override
public double getMaximumAvailablePrediction() {
double max = Collections.max(_positonsByTimestamp.keySet());
return max;
return getStep();
}
@Override
......@@ -801,6 +802,20 @@ public class TraciSimulationController implements VehicleController, SimulationS
edge.setMaxSpeed(pMaxSpeed);
}
@Override
public boolean isEdgeUsable(String pEdgeID) {
if (_observedAreaSet) {
List<Location> laneShape = getLaneShape(_roadNetwork.getEdge(pEdgeID).getLanes().get(0).getLaneID());
for (Location location : laneShape) {
if (_startX <= location.getLongitude() && location.getLongitude() <= _endX && _startY <= location.getLatitude() && location.getLatitude() <= _endY) {
return true;
}
}
return false;
}
return true;
}
/**
* @return the startX
*/
......
......@@ -58,10 +58,10 @@ public class VehicleDataInformationHandler extends DefaultHandler {
if (_observedAreaSet) {
if (_startX <= lon && lon <= _endX && _startY <= lat && lat <= _endY) {
_nextVehiclePositions.put(id, new VehicleInformationContainer(new PositionVector(lon - _startX, lat - _startY, 0), heading, speed));
_nextVehiclePositions.put(id, new VehicleInformationContainer(new PositionVector(lon - _startX, lat - _startY, 0), heading, speed, null));
}
} else {
_nextVehiclePositions.put(id, new VehicleInformationContainer(new PositionVector(lon, lat, 0), heading, speed));
_nextVehiclePositions.put(id, new VehicleInformationContainer(new PositionVector(lon, lat, 0), heading, speed, null));
}
}
}
......
......@@ -50,7 +50,7 @@ public class DefaultVehicleInformationComponent implements VehicleInformationCom
private String vehicleID;
public DefaultVehicleInformationComponent(Host host, VehicleController controller, SimulationSetupExtractor extractor) {
public DefaultVehicleInformationComponent(Host host, VehicleController controller, SimulationSetupExtractor extractor, boolean pRouteKnown) {
this.host = host;
this.controller = controller;
......@@ -66,7 +66,10 @@ public class DefaultVehicleInformationComponent implements VehicleInformationCom
throws InformationNotAvailableException {
if (pNodeID == getHost().getId()) {
if (isValid()) {
return getCurrentRoute().getStart();
RoadNetworkRoute route = getCurrentRoute();
if (route != null) {
return route.getStart();
}
}
}
return null;
......@@ -83,6 +86,34 @@ public class DefaultVehicleInformationComponent implements VehicleInformationCom
}
});
if (pRouteKnown) {
siSComponent.provide().nodeState(SiSTypes.ROUTE, new SiSDataCallback<RoadNetworkRoute>() {
@Override
public RoadNetworkRoute getValue(INodeID pNodeID,
SiSProviderHandle pProviderHandle)
throws InformationNotAvailableException {
if (pNodeID == getHost().getId()) {
if (isValid()) {
return getCurrentRoute();
}
}
return null;
}
@Override
public Set<INodeID> getObservedNodes() {
return INodeID.getSingleIDSet(getHost().getId());
}
@Override
public SiSInfoProperties getInfoProperties() {
return new SiSInfoProperties();
}
});
}
} catch (ComponentNotAvailableException e) {
// Nothing to do!
}
......
......@@ -26,29 +26,46 @@ import java.util.List;
import java.util.Map;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetworkComponent.NetInterfaceName;
import de.tudarmstadt.maki.simonstrator.api.component.transport.ConnectivityListener;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.VehicleInformationComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.CachingComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.invalidation.CacheInvalidationStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.JamInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
public class DefaultCachingComponent implements CachingComponent, ConnectivityListener {
public class DefaultCachingComponent
implements CachingComponent, ConnectivityListener {
private Map<Class<? extends Object>, List<PointInformation>> _cache = new HashMap<>();
private Map<Integer, Integer> _lastColorValues = new HashMap<>();
private Host _host;
private CacheInvalidationStrategy _invalidationStrategy;
private CacheReplacementStrategy _replacementStrategy;
private CacheDecisionStrategy _decisionStrategy;
public DefaultCachingComponent(Host pHost, CacheInvalidationStrategy pInvalidationStrategy, CacheReplacementStrategy pReplacementStrategy, CacheDecisionStrategy pDecisionStrategy) {
private double[] informationRatios = new double[] {1, 0.75, 0.5, 0.25, 0};
public DefaultCachingComponent(Host pHost,
CacheInvalidationStrategy pInvalidationStrategy,
CacheReplacementStrategy pReplacementStrategy,
CacheDecisionStrategy pDecisionStrategy) {
_host = pHost;
if (_host != null) {
_host.getNetworkComponent().getByName(NetInterfaceName.WIFI).addConnectivityListener(this);
_host.getNetworkComponent().getByName(NetInterfaceName.WIFI)
.addConnectivityListener(this);
}
_invalidationStrategy = pInvalidationStrategy;
......@@ -57,7 +74,8 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
}
@Override
public <T extends PointInformation> List<T> getDecidedCacheEntries(Class<T> pCacheEntryClass) {
public <T extends PointInformation> List<T> getDecidedCacheEntries(
Class<T> pCacheEntryClass) {
List<T> cacheEntries = getCacheEntries(pCacheEntryClass);
if (cacheEntries == null) {
......@@ -75,8 +93,10 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
}
List<T> decidedInformation = new ArrayList<>();
for (List<PointInformation> similarEntries : similarCacheEntries.values()) {
PointInformation correctInformation = _decisionStrategy.decideOnCorrectInformation(similarEntries);
for (List<PointInformation> similarEntries : similarCacheEntries
.values()) {
PointInformation correctInformation = _decisionStrategy
.decideOnCorrectInformation(similarEntries);
decidedInformation.add((T) correctInformation);
}
......@@ -85,9 +105,11 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
}
@Override
public <T extends PointInformation> List<T> getCacheEntries(Class<T> pCacheEntryClass) {
public <T extends PointInformation> List<T> getCacheEntries(
Class<T> pCacheEntryClass) {
if (_cache.containsKey(pCacheEntryClass)) {
List<? extends PointInformation> cacheEntries = _cache.get(pCacheEntryClass);
List<? extends PointInformation> cacheEntries = _cache
.get(pCacheEntryClass);
List<T> results = new ArrayList<>();
for (int i = 0; i < cacheEntries.size(); i++) {
......@@ -153,7 +175,8 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
public Object getEdgeOrPosition(PointInformation information) {
if (information.hasAttribute(AvailableInformationAttributes.EDGE)) {
return information.getAttribute(AvailableInformationAttributes.EDGE);
return information
.getAttribute(AvailableInformationAttributes.EDGE);
} else {
return information.getLocation();
}
......@@ -163,4 +186,99 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
return _decisionStrategy;
}
@Override
public String getNodeDescription() {
return "";
}
@Override
public int getNodeColorDimensions() {
return 2;
}
@Override
public String[] getNodeColorDimensionDescriptions() {
return new String[] {"Route Information", "Number of jam information"};
}
@Override
public String[] getNodeColorDescriptions(int pDimension) {
if (pDimension == 0) {
String[] labels = new String[informationRatios.length];
for (int i = 0; i < informationRatios.length; i++) {
labels[i] = informationRatios[i] * 100 + "%";
}
return labels;
} else if (pDimension == 1) {
String[] numbers = new String[10];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = String.valueOf(i);
}
return numbers;
}
return new String[0];
}
@Override
public int getNodeColor(int pDimension) {
if (pDimension == 0) {
List<JamInformation> decidedCacheEntries = getDecidedCacheEntries(JamInformation.class);
try {
VehicleInformationComponent vehicleInformationComponent = getHost().getComponent(VehicleInformationComponent.class);
if (vehicleInformationComponent.isValid()) {
RoadNetworkRoute currentRoute = vehicleInformationComponent.getCurrentRoute();
if (currentRoute != null) {
int count = 0;
int active = 0;
for (RoadNetworkEdge edge : currentRoute.getRoute()) {
if (edge.isActive()) {
if (decidedCacheEntries != null) {
for (JamInformation jamInformation : decidedCacheEntries) {
if (jamInformation.getEdge().equals(edge) && jamInformation.getValue()) {
count++;
break;
}
}
}
active++;
}
}
if (active != 0) {
double ratio = count / ((double) active);
for (int i = 0; i < informationRatios.length; i++) {
if (informationRatios[i] <= ratio) {
_lastColorValues.put(pDimension, i);
break;
}
}
} else {
_lastColorValues.put(pDimension, 0);
}
}
}
} catch (ComponentNotAvailableException e) {
}
if (_lastColorValues.containsKey(pDimension)) {
return _lastColorValues.get(pDimension);
}
return 0;
}
if (pDimension == 1) {
List<JamInformation> decidedCacheEntries = getDecidedCacheEntries(JamInformation.class);
if (decidedCacheEntries != null) {
_lastColorValues.put(pDimension, decidedCacheEntries.size());
}
if (_lastColorValues.containsKey(pDimension)) {
return Math.min(9, _lastColorValues.get(pDimension));
}
return 0;
}
return 0;
}
@Override
public boolean isActive() {
return getHost().getNetworkComponent().getByName(NetInterfaceName.MOBILE).isUp()
|| getHost().getNetworkComponent().getByName(NetInterfaceName.WIFI).isUp();
}
}
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