Commit 93e07276 authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Added functionality to simulation controller

    - Road network extraction
    - Vehicle rerouting
parent 670194fd
/*
* 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.vehicular.sumo.simulation.controller.roadnetwork;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class RoadNetwork {
private Map<String, RoadNetworkEdge> _roadNetwork;
public RoadNetwork(Map<String, RoadNetworkEdge> pRoadNetwork) {
_roadNetwork = pRoadNetwork;
}
public List<RoadNetworkEdge> getAvailableEdges() {
return new ArrayList<>(_roadNetwork.values());
}
public RoadNetworkEdge getEdge(String pRouteID) {
return _roadNetwork.get(pRouteID);
}
}
/*
* 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.vehicular.sumo.simulation.controller.roadnetwork;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class RoadNetworkEdge {
private String _edgeID;
private List<RoadNetworkEdge> _accessibleEdges = new ArrayList<>();
public RoadNetworkEdge(String pEdgeID) {
_edgeID = pEdgeID;
}
public String getEdgeID() {
return _edgeID;
}
public void addConnectedEdge(RoadNetworkEdge pEdge) {
if (!_accessibleEdges.contains(pEdge)) {
_accessibleEdges.add(pEdge);
}
}
public List<RoadNetworkEdge> getAccessibleEdges() {
return Collections.unmodifiableList(_accessibleEdges);
}
@Override
public String toString() {
return _edgeID;
}
}
/*
* 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.vehicular.sumo.simulation.controller.roadnetwork;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class RoadNetworkRoute {
private List<RoadNetworkEdge> _route;
public RoadNetworkRoute(List<RoadNetworkEdge> pRoute) {
_route = pRoute;
}
public RoadNetworkRoute(List<String> pRoute, RoadNetwork pNetwork) {
_route = new ArrayList<>();
for (String pRouteID : pRoute) {
RoadNetworkEdge edge = pNetwork.getEdge(pRouteID);
_route.add(edge);
}
}
public List<RoadNetworkEdge> getRoute() {
return Collections.unmodifiableList(_route);
}
@Override
public boolean equals(Object pObj) {
if (pObj instanceof RoadNetworkRoute) {
List<RoadNetworkEdge> route = ((RoadNetworkRoute) pObj).getRoute();
if (route.size() == _route.size()) {
for (int i = 0; i < route.size(); i++) {
if (!route.get(i).equals(_route.get(i))) {
return false;
}
}
return true;
}
}
return false;
}
public boolean containsEdge(String pEdgeID) {
for (RoadNetworkEdge roadNetworkEdge : _route) {
if (roadNetworkEdge.getEdgeID().equals(pEdgeID)) {
return true;
}
}
return false;
}
public boolean containsEdge(RoadNetworkEdge pEdge) {
return _route.contains(pEdge);
}
public RoadNetworkEdge getStart() {
return _route.get(0);
}
public RoadNetworkEdge getDestination() {
return _route.get(_route.size() - 1);
}
@Override
public String toString() {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < _route.size(); i++) {
if (i != 0) {
buffer.append(" --> ");
}
buffer.append(_route.get(i));
}
return buffer.toString();
}
}
/*
* 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.vehicular.sumo.simulation.controller.roadnetwork.exception;
import java.util.List;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkEdge;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkRoute;
public class NoAdditionalRouteAvailableException extends Exception {
public NoAdditionalRouteAvailableException(RoadNetworkEdge pStart, RoadNetworkEdge pDestination, List<RoadNetworkRoute> pRoutes) {
super("No " + (pRoutes.size() > 0?"additional ":"") + "route available between " + pStart.getEdgeID() + " and " + pDestination.getEdgeID() + (pRoutes.size()>0?" known routes are:" + printRoutes(pRoutes) + "\n":""));
}
private static String printRoutes(List<RoadNetworkRoute> pRoutes) {
StringBuffer stringBuffer = new StringBuffer();
for (RoadNetworkRoute roadNetworkRoute : pRoutes) {
stringBuffer.append("\n\t").append(roadNetworkRoute);
}
return stringBuffer.toString();
}
}
/*
* 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.vehicular.sumo.simulation.controller.roadnetwork.exception;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkEdge;
public class NoExitAvailableException extends Exception {
public NoExitAvailableException(RoadNetworkEdge pStart, RoadNetworkEdge pDestination) {
super("No departure available between " + pStart.getEdgeID() + " and " + pDestination.getEdgeID());
}
}
/*
* 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.vehicular.sumo.simulation.controller.roadnetwork.routing;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetwork;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkEdge;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkRoute;
public class BreathFirstSearchRoutingAlgorithm implements RoutingAlgorithm {
@Override
public RoadNetworkRoute findRoute(RoadNetwork pNetwork, RoadNetworkEdge pCurrentPosition, RoadNetworkEdge pDestination,
List<RoadNetworkRoute> pKnownRoutes) {
return findRoute(pNetwork, pCurrentPosition, pDestination, pKnownRoutes, new ArrayList<>());
}
@Override
public RoadNetworkRoute findRoute(RoadNetwork pNetwork, RoadNetworkEdge pCurrentPosition, RoadNetworkEdge pDestination,
List<RoadNetworkRoute> pKnownRoutes, List<RoadNetworkEdge> pEdgesToAvoid) {
List<RoadNetworkEdge> visitedEdges = new ArrayList<>();
Queue<PathInformation> buffer = new LinkedList<>();
PathInformation rootPathInformation = new PathInformation(pCurrentPosition, null);
buffer.add(rootPathInformation);
visitedEdges.add(pCurrentPosition);
outer:while (buffer.size() > 0) {
PathInformation pathInformation = buffer.poll();
RoadNetworkEdge edge = pathInformation.getEdge();
if (pEdgesToAvoid.contains(edge)) {
continue;
}
List<RoadNetworkEdge> accessibleEdges = edge.getAccessibleEdges();
for (RoadNetworkEdge roadNetworkEdge : accessibleEdges) {
if (roadNetworkEdge.equals(pDestination)) {
List<RoadNetworkEdge> routeList = new ArrayList<>();
extractRoute(routeList, new PathInformation(roadNetworkEdge, pathInformation));
RoadNetworkRoute route = new RoadNetworkRoute(routeList);
for (RoadNetworkEdge edgeToAvoid : pEdgesToAvoid) {
if (route.containsEdge(edgeToAvoid)) {
continue outer;
}
}
if (!pKnownRoutes.contains(route)) {
return route;
}
} else if (!visitedEdges.contains(roadNetworkEdge)) {
PathInformation childPathInformation = new PathInformation(roadNetworkEdge, pathInformation);
buffer.add(childPathInformation);
visitedEdges.add(roadNetworkEdge);
}
}
}
return null;
}
private void extractRoute(List<RoadNetworkEdge> pEdges, PathInformation pPathInformation) {
if (pPathInformation.getPreviousEdge() != null) {
extractRoute(pEdges, pPathInformation.getPreviousEdge());
}
pEdges.add(pPathInformation.getEdge());
}
private class PathInformation {
private final RoadNetworkEdge _edge;
private final PathInformation _previousEdge;
public PathInformation(RoadNetworkEdge pEdge,
PathInformation pPreviousEdge) {
_edge = pEdge;
_previousEdge = pPreviousEdge;
}
public RoadNetworkEdge getEdge() {
return _edge;
}
public PathInformation getPreviousEdge() {
return _previousEdge;
}
@Override
public boolean equals(Object pObj) {
if (pObj instanceof PathInformation) {
return ((PathInformation) pObj).getEdge().equals(_edge);
}
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.vehicular.sumo.simulation.controller.roadnetwork.routing;
import java.util.List;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetwork;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkEdge;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkRoute;
public interface RoutingAlgorithm {
RoadNetworkRoute findRoute(RoadNetwork pNetwork, RoadNetworkEdge pCurrentPosition, RoadNetworkEdge pDestination, List<RoadNetworkRoute> pKnownRoutes);
RoadNetworkRoute findRoute(RoadNetwork pNetwork, RoadNetworkEdge pCurrentPosition, RoadNetworkEdge pDestination, List<RoadNetworkRoute> pKnownRoutes, List<RoadNetworkEdge> pEdgesToAvoid);
}
......@@ -8,15 +8,23 @@ import java.util.Map;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.LocationUtils;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Position;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Route;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.information.Street;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.SimulationSetupExtractor;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleController;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetwork;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkEdge;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.RoadNetworkRoute;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.exception.NoAdditionalRouteAvailableException;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.exception.NoExitAvailableException;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.routing.BreathFirstSearchRoutingAlgorithm;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.roadnetwork.routing.RoutingAlgorithm;
import de.tudresden.sumo.cmd.Junction;
import de.tudresden.sumo.cmd.Lane;
import de.tudresden.sumo.cmd.Simulation;
import de.tudresden.sumo.cmd.Vehicle;
import de.tudresden.sumo.util.SumoCommand;
import de.tudresden.ws.container.SumoBoundingBox;
import de.tudresden.ws.container.SumoLink;
import de.tudresden.ws.container.SumoLinkList;
import de.tudresden.ws.container.SumoPosition2D;
import de.tudresden.ws.container.SumoStringList;
import it.polito.appeal.traci.SumoTraciConnection;
......@@ -41,7 +49,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
private double _endY;
private Map<Double, Map<String, Position>> _positonsByTimestamp = new HashMap<>();
private int _futureInformation = 10;
private int _futureInformation = 0;
private boolean _initalized = false;
......@@ -49,6 +57,10 @@ public class TraciSimulationController implements VehicleController, SimulationS
private Map<String, Double> _vehiclesOutOfRange = new HashMap<>();
private RoadNetwork _roadNetwork;
private RoutingAlgorithm _algorithm = new BreathFirstSearchRoutingAlgorithm();
public static synchronized TraciSimulationController createSimulationController(String pSumoExe, String pConfigFile) {
if (!CONTROLLER.containsKey(pConfigFile)) {
CONTROLLER.put(pConfigFile, new TraciSimulationController(pSumoExe, pConfigFile));
......@@ -276,18 +288,19 @@ public class TraciSimulationController implements VehicleController, SimulationS
return new ArrayList<>(map.keySet());
}
public Route getRoute(String pVehicleID) {
public RoadNetworkRoute getRoute(String pVehicleID) {
SumoCommand routeCommand = Vehicle.getRoute(pVehicleID);
Object object = requestObject(routeCommand);
SumoStringList streetList = (SumoStringList) object;
Route route = new Route();
List<Street> streets = route.getStreets();
obtainRoadNetwork();
List<RoadNetworkEdge> streets = new ArrayList<>();
for (String street : streetList) {
streets.add(new Street(street));
streets.add(_roadNetwork.getEdge(street));
}
return route;
return new RoadNetworkRoute(streets);
}
public Object requestObject(SumoCommand routeCommand) {
......@@ -302,6 +315,16 @@ public class TraciSimulationController implements VehicleController, SimulationS
return object;
}
public void execute(SumoCommand routeCommand) {
try {
_connection.do_job_set(routeCommand);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public double getStep() {
return _step;
......@@ -355,4 +378,126 @@ public class TraciSimulationController implements VehicleController, SimulationS
return -1;
}
public boolean providesRoadInformation() {
return true;
}
public RoadNetworkEdge getVehicleDestination(String pVehicleID) {
RoadNetworkRoute roadNetworkRoute = getRoute(pVehicleID);
return roadNetworkRoute.getDestination();
}
public RoadNetwork getRoadNetwork() {
obtainRoadNetwork();
return _roadNetwork;
}
public RoadNetworkRoute findNewRoute(String pVehicle) throws NoAdditionalRouteAvailableException {
List<RoadNetworkRoute> routes = new ArrayList<>();
RoadNetworkRoute route = getRoute(pVehicle);
routes.add(route);
RoadNetworkRoute findRoute = _algorithm.findRoute(_roadNetwork, route.getStart(), route.getDestination(), routes);
if (findRoute == null) {
throw new NoAdditionalRouteAvailableException(route.getStart(), route.getDestination(), routes);
}
return findRoute;
}
public RoadNetworkRoute findNewRoute(String pVehicle, List<RoadNetworkEdge> pEdgesToAvoid, boolean pKeepDestination) throws NoAdditionalRouteAvailableException, NoExitAvailableException {
if (pKeepDestination) {
List<RoadNetworkRoute> routes = new ArrayList<>();
RoadNetworkRoute route = getRoute(pVehicle);
routes.add(route);
RoadNetworkRoute findRoute = _algorithm.findRoute(_roadNetwork, route.getStart(), route.getDestination(), routes, pEdgesToAvoid);
if (findRoute == null) {
throw new NoAdditionalRouteAvailableException(route.getStart(), route.getDestination(), routes);
}
return findRoute;
} else {
RoadNetworkRoute route = getRoute(pVehicle);
List<RoadNetworkEdge> routeList = route.getRoute();
RoadNetworkEdge lastEdge = null;
boolean search = false;
outer:for (int i = routeList.size() - 1; i >= 0; i--) {
if (search) {
List<RoadNetworkEdge> accessibleEdges = routeList.get(i).getAccessibleEdges();
if (accessibleEdges.size() > 1) {
for (int j = 0; j < accessibleEdges.size(); j++) {
if (!accessibleEdges.get(j).equals(lastEdge)) {
List<RoadNetworkEdge> edges = new ArrayList<>();
for (int k = 0; k <= i; k++) {
edges.add(routeList.get(k));
}
edges.add(accessibleEdges.get(j));
for (RoadNetworkEdge roadNetworkEdge : edges) {
if (pEdgesToAvoid.contains(roadNetworkEdge)) {
continue outer;
}
}
return new RoadNetworkRoute(edges);
}
}
}
}
search = true;
lastEdge = routeList.get(i);
}
throw new NoExitAvailableException(route.getStart(), route.getDestination());
}
}
public void rerouteVehicle(String pVehicle, RoadNetworkRoute pRoute) {
SumoStringList routeEdges = new SumoStringList();
for (RoadNetworkEdge edge : pRoute.getRoute()) {
routeEdges.add(edge.getEdgeID());
}
execute(Vehicle.setRoute(pVehicle, routeEdges));
}
public void obtainRoadNetwork() {
if (_roadNetwork == null) {
SumoCommand laneIDCommand = Lane.getIDList();
Map<String, RoadNetworkEdge> roadNetwork = new HashMap<>();
SumoStringList laneIDStringList = (SumoStringList) requestObject(laneIDCommand);
for (String laneID : laneIDStringList) {
SumoCommand edgeIDCommand = Lane.getEdgeID(laneID);
SumoLinkList linkStringList = (SumoLinkList) requestObject(Lane.getLinks(laneID));
if (linkStringList.size() > 0) {
String edgeID = (String) requestObject(edgeIDCommand);
if (!roadNetwork.containsKey(edgeID)) {
roadNetwork.put(edgeID, new RoadNetworkEdge(edgeID));
}
RoadNetworkEdge edge = roadNetwork.get(edgeID);
for (SumoLink link : linkStringList) {
String notInternalLane = link.notInternalLane;
String connectedEdge = (String) requestObject(Lane.getEdgeID(notInternalLane));
if (!roadNetwork.containsKey(connectedEdge)) {
roadNetwork.put(connectedEdge, new RoadNetworkEdge(connectedEdge));
}
edge.addConnectedEdge(roadNetwork.get(connectedEdge));
}
}
}
_roadNetwork = new RoadNetwork(roadNetwork);
}
}
}
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