diff --git a/debug-tool/proto/sample/roadnetwork_sample.proto b/debug-tool/proto/sample/roadnetwork_sample.proto index e7c6172b49ce4baddf9b3a003dce405f10dab1e0..3b0b387d336047ca7c8f09f252fca817befc5ea3 100644 Binary files a/debug-tool/proto/sample/roadnetwork_sample.proto and b/debug-tool/proto/sample/roadnetwork_sample.proto differ diff --git a/routing-server/pom.xml b/routing-server/pom.xml index 372b7c3f0149a73e6cf5b346f0673cf8b180c22a..e7c8281667303ff226aabcc61d860a36725cdeca 100644 --- a/routing-server/pom.xml +++ b/routing-server/pom.xml @@ -50,7 +50,7 @@ <dependency> <groupId>de.fuberlin.navigator</groupId> <artifactId>proto</artifactId> - <version>1.00.04</version> + <version>1.00.05</version> </dependency> </dependencies> diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/controller/RoutingController.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/controller/RoutingController.java index b729a99f55a7ad6e51df63960eefa2c544950d48..e2b2d8bec0548bbb12a69927996537a7ab071d1c 100644 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/controller/RoutingController.java +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/controller/RoutingController.java @@ -3,6 +3,7 @@ package de.fuberlin.navigator.routingserver.controller; import java.io.IOException; import java.util.List; +import org.jgrapht.GraphPath; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; @@ -18,6 +19,7 @@ import de.fuberlin.navigator.protos.map_builder.Coordinates; import de.fuberlin.navigator.routingserver.model.AdressRequest; import de.fuberlin.navigator.routingserver.model.Point; import de.fuberlin.navigator.routingserver.model.RoutingRequest; +import de.fuberlin.navigator.routingserver.model.shortestPath.ExtendedEdge; import de.fuberlin.navigator.routingserver.utility.ApplyShortestPath; import de.fuberlin.navigator.routingserver.utility.MapMatcher; @@ -38,10 +40,13 @@ public class RoutingController { log("/route", RequestMethod.POST, MediaType.APPLICATION_JSON, routingRequest); // route generation - List<Point> path = ApplyShortestPath.getShortestPath(routingRequest); + //route generation + GraphPath<Long, ExtendedEdge> path = ApplyShortestPath.getShortestPathInTime(routingRequest); + List<Point> coordinateList = ApplyShortestPath.covertToCoordinates(path.getVertexList()); + ArrayNode arrayNode = mapper.createArrayNode(); - for (Point coordinate : path) { + for (Point coordinate : coordinateList) { arrayNode.add( mapper.createArrayNode() .add(coordinate.getLat()) diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/RoutingRequest.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/RoutingRequest.java index f12a37c1b3a98916cdb13966cdc8f08024ad9351..57bc20899f2e57db999bcf10ed14300f37b4631a 100644 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/RoutingRequest.java +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/RoutingRequest.java @@ -16,12 +16,24 @@ public class RoutingRequest { private Set<WeatherTag> forbidden; + private int startDayAfterToday; + private int endDayAfterToday; + public RoutingRequest(Point startNode, Point endNode, Set<WeatherTag> forbidden) { this.startNode = startNode; this.endNode = endNode; this.forbidden = forbidden; } + public RoutingRequest(Point startNode, Point endNode, Set<WeatherTag> forbidden, int startDayAfterToday, int endDayAfterToday) { + this.startNode = startNode; + this.endNode = endNode; + this.forbidden = forbidden; + this.startDayAfterToday = startDayAfterToday; + this.endDayAfterToday = endDayAfterToday; + } + + public Point getStartNode() { return startNode; } @@ -30,6 +42,18 @@ public class RoutingRequest { return endNode; } + public Set<WeatherTag> getForbidden() { + return forbidden; + } + + public int getStartDayAfterToday() { + return startDayAfterToday; + } + + public int getEndDayAfterToday() { + return endDayAfterToday; + } + public void setStartNode(Point startNode) { this.startNode = startNode; } @@ -38,6 +62,15 @@ public class RoutingRequest { this.endNode = endNode; } + public void setStartDayAfterToday(int startDayAfterToday) { + this.startDayAfterToday = startDayAfterToday; + } + + public void setEndDayAfterToday(int endDayAfterToday) { + this.endDayAfterToday = endDayAfterToday; + } + + @Override public String toString() { String startNodeStr = "\"startNode\": " + startNode.toString(); diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/shortestPath/ExtendedEdge.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/shortestPath/ExtendedEdge.java index 068e0af9058479529df7970ab0c30b243eb0345b..5cbad2ded0c525926e6cd2b7978ba40ddbc992a9 100644 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/shortestPath/ExtendedEdge.java +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/shortestPath/ExtendedEdge.java @@ -4,12 +4,12 @@ import org.jgrapht.graph.DefaultWeightedEdge; public class ExtendedEdge extends DefaultWeightedEdge { - private Integer id; + private Long id; /** * Constructs an edge with id */ - public ExtendedEdge(Integer id) + public ExtendedEdge(Long id) { this.id = id; } @@ -17,7 +17,7 @@ public class ExtendedEdge extends DefaultWeightedEdge { /** * Gets the id associated with this edge. */ - public Integer getId() + public Long getId() { return id; } @@ -28,3 +28,9 @@ public class ExtendedEdge extends DefaultWeightedEdge { + + + + + + diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/shortestPath/Heuristic.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/shortestPath/Heuristic.java index 3b91d02fb064f2380bf799fed226f36c00390b41..a318ad574cc875e1545c7a6dffff9d6429aa6a63 100644 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/shortestPath/Heuristic.java +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/model/shortestPath/Heuristic.java @@ -1,48 +1,53 @@ package de.fuberlin.navigator.routingserver.model.shortestPath; -import org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic; +import org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic; import com.graphhopper.util.DistanceCalcEarth; import de.fuberlin.navigator.protos.map_builder.Node; -import de.fuberlin.navigator.routingserver.utility.NetworkReader; +import de.fuberlin.navigator.routingserver.utility.ProtobufToGraph; + + + -public class Heuristic { +public class Heuristic { public static void main(String[] args) { System.out.println(heuristic); } - // create an instance of AStarAdmissibleHeuristic - public static AStarAdmissibleHeuristic<Integer> heuristic = new AStarAdmissibleHeuristic<Integer>() { + // create an instance of AStarAdmissibleHeuristic + public static AStarAdmissibleHeuristic<Long> heuristic = new AStarAdmissibleHeuristic<Long>() { + @Override - public double getCostEstimate(Integer source, Integer target) { + public double getCostEstimate(Long source, Long target) { double lat1 = getLocationX(source); double lon1 = getLocationY(source); double lat2 = getLocationX(target); double lon2 = getLocationY(target); + DistanceCalcEarth distCalc = new DistanceCalcEarth(); - + // calculate haversian distance between current node and goal - double distance = distCalc.calcDist(lat1, lon1, lat2, lon2); - + double distance = distCalc.calcDist(lat1,lon1,lat2,lon2); + return distance; } // helper methods to get latitude and longitude of a vertex - private double getLocationX(int vertex) { - Node node = NetworkReader.readRoadNetwork().getNodesMap().get((long) vertex); + private double getLocationX(long vertex) { + Node node = ProtobufToGraph.getNodeMap().get(vertex); return node.getPosition().getLat(); } - private double getLocationY(int vertex) { - Node node = NetworkReader.readRoadNetwork().getNodesMap().get((long) vertex); + private double getLocationY(long vertex) { + Node node = ProtobufToGraph.getNodeMap().get(vertex); return node.getPosition().getLon(); } - }; - + + }; } diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/ApplyShortestPath.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/ApplyShortestPath.java index 9886b4784da88aa794fdad9934aee4d2c5a68449..31571039053836ec1526ccfb6bd3aa40dd611c05 100644 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/ApplyShortestPath.java +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/ApplyShortestPath.java @@ -1,95 +1,140 @@ package de.fuberlin.navigator.routingserver.utility; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; +import java.util.Map.Entry; -import org.jgrapht.Graph; +import org.jgrapht.graph.SimpleDirectedWeightedGraph; import org.jgrapht.GraphPath; import org.jgrapht.alg.shortestpath.AStarShortestPath; import de.fuberlin.navigator.protos.map_builder.Node; -import de.fuberlin.navigator.protos.map_builder.RoadNetwork; import de.fuberlin.navigator.routingserver.model.Point; import de.fuberlin.navigator.routingserver.model.RoutingRequest; import de.fuberlin.navigator.routingserver.model.WeatherTag; import de.fuberlin.navigator.routingserver.model.shortestPath.ExtendedEdge; import de.fuberlin.navigator.routingserver.model.shortestPath.Heuristic; + + public class ApplyShortestPath { - public static Graph<Integer, ExtendedEdge> graph = new ProtobufToGraph(NetworkReader.readRoadNetwork()) - .getGraphFromProto(); + public static void main(String[] args){ - public static ProtobufToGraph roadnetwork = new ProtobufToGraph(NetworkReader.readRoadNetwork());; + // test routing algorithm - public static void main(String[] args) { + Set<WeatherTag> weatherTags = new HashSet<>(){{ + add(WeatherTag.findByCode("rain")); + }}; - System.out.println("Graph: " + graph); + double lat1 = 51.762146; + double lon1 = 14.286304; + double lat2 = 51.747814; + double lon2 = 14.357037; - Set<WeatherTag> weatherTags = new HashSet<>() { - { - add(WeatherTag.findByCode("rain")); - } - }; + RoutingRequest routingRequest = new RoutingRequest(new Point(lat1,lon1), new Point(lat2, lon2), weatherTags, 0, 6); - RoutingRequest routingRequest = new RoutingRequest(new Point(10.0, 30.0), new Point(12, 33), - weatherTags); + // find the shortest path + GraphPath<Long, ExtendedEdge> path = getShortestPathInTime(routingRequest); - // find the shortest path from vertex 1 to vertex - RoadNetwork roadNetwork = NetworkReader.readRoadNetwork(); - ProtobufToGraph protobufToGrad = new ProtobufToGraph(roadNetwork); - List<Point> path = getShortestPath(routingRequest); + List<Point> coordinateList = covertToCoordinates(path.getVertexList()); + // duration of path in minutes + int duration = calculateDuration(path); + + // length of path in meters + double length = calculateLength(path); + + + System.out.println("List of Point: "+Arrays.deepToString(coordinateList.toArray())); + System.out.println("duration: " + duration + " min"); + System.out.println("length of path: " + length + " meter"); } - public static List<Point> getShortestPath(RoutingRequest routingRequest) { + public static GraphPath<Long, ExtendedEdge> getShortestPathInTime(RoutingRequest routingRequest){ + int start = routingRequest.getStartDayAfterToday(); + int end = routingRequest.getEndDayAfterToday(); + + for (int i = start; i <= end; i++ ){ + GraphPath<Long, ExtendedEdge> path = getShortestPath(routingRequest, i); + if (path != null) { + System.out.println("Path found in " +i+ " days!"); + return path; + } + System.out.println("No path with desired conditions could be found in " +i+ " days!"); + } - int source = coordinatesToID(routingRequest.getStartNode()); - int target = coordinatesToID(routingRequest.getEndNode()); + return null; - // create an instance of AStarShortestPath - AStarShortestPath<Integer, ExtendedEdge> aStar = new TurnRestrictedAStar<>(graph, Heuristic.heuristic); + } - // find the shortest path - GraphPath<Integer, ExtendedEdge> path = aStar.getPath(source, target); - // print the path and its weight - System.out.println("Shortest path from " + source + " to " + target + ": " + path.getVertexList() + " (weight: " - + path.getWeight() + ")"); + public static GraphPath<Long, ExtendedEdge> getShortestPath(RoutingRequest routingRequest, int dayAfterToday){ - // convert path consisting of node ids into path of coordinates - List<Point> coordinateList = new ArrayList<>(); - for (Integer nodeId : path.getVertexList()) { - double lat = roadnetwork.getNodeMap().get((long) nodeId).getPosition().getLat(); - double lon = roadnetwork.getNodeMap().get((long) nodeId).getPosition().getLon(); - coordinateList.add(new Point(lat, lon)); - } + SimpleDirectedWeightedGraph graph = ProtobufToGraph.getGraphFromProto(dayAfterToday); - System.out.println("List of coordinates: " + Arrays.deepToString(coordinateList.toArray())); + long source = CoordinatesToID(routingRequest.getStartNode()); + long target = CoordinatesToID(routingRequest.getEndNode()); - return coordinateList; + // create an instance of AStarShortestPath + AStarShortestPath<Long, ExtendedEdge> aStar = new TurnRestrictedAStar<>(graph, Heuristic.heuristic, routingRequest.getForbidden(), dayAfterToday); + + // find the shortest path + GraphPath<Long, ExtendedEdge> path = aStar.getPath(source, target); + + return path; } - public static int coordinatesToID(Point coordinates) { - // map coordinates of node to node ID - double lat = coordinates.getLat(); - double lon = coordinates.getLon(); + private static long CoordinatesToID(Point Point){ + // map Point of start and end point to node ID + + double lat = Point.getLat(); + double lon = Point.getLon(); double epsilon = 0.0001; // 14m difference - for (Map.Entry<Long, Node> node : roadnetwork.getNodeMap().entrySet()) { - if (Math.abs(lat - node.getValue().getPosition().getLat()) < epsilon) { - if (Math.abs(lon - node.getValue().getPosition().getLon()) < epsilon) { - return (int) node.getValue().getId(); + for (Entry<Long, Node> node : ProtobufToGraph.getNodeMap().entrySet()) { + if (Math.abs(lat-node.getValue().getPosition().getLat()) < epsilon) { + if (Math.abs(lon-node.getValue().getPosition().getLon()) < epsilon) { + return (long) node.getValue().getOsmId(); } } } + System.out.println("No start or end point found!"); return 0; } -} \ No newline at end of file + public static double calculateLength(GraphPath<Long, ExtendedEdge> path){ + double length = 0; + for (ExtendedEdge edge : path.getEdgeList()) { + double edgeLen = ProtobufToGraph.getRoadnetwork().getSegmentsMap().get((long)edge.getId()).getLength(); + length += edgeLen; + } + + return length; + } + + public static int calculateDuration(GraphPath<Long, ExtendedEdge> path){ + double durationH = path.getWeight(); + int durationMin = (int) (durationH * 60); + return durationMin; + } + + + public static List<Point> covertToCoordinates(List<Long> vertexList){ + // convert path consisting of node ids into path of Point + List<Point> coordinateList = new ArrayList<>(); + for (Long nodeId : vertexList) { + double lat = ProtobufToGraph.getNodeMap().get(nodeId).getPosition().getLat(); + double lon = ProtobufToGraph.getNodeMap().get(nodeId).getPosition().getLon(); + coordinateList.add(new Point(lat, lon)); + } + return coordinateList; + } + +} diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/MetricReader.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/MetricReader.java new file mode 100644 index 0000000000000000000000000000000000000000..bf5c1cb3f02c3455ae11fcb917ded2fcb0ac919b --- /dev/null +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/MetricReader.java @@ -0,0 +1,45 @@ +package de.fuberlin.navigator.routingserver.utility; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import de.fuberlin.navigator.protos.metric_builder.Metrics; +import de.fuberlin.navigator.protos.metric_builder.Metrics.Builder; + +public class MetricReader { + + static Metrics metrics = readMetrics(); + public static void main(String[] args) throws IOException { + + // testing + int dayAfterToday = 0; + System.out.println(MetricReader.metrics.getMetricsMap().get(dayAfterToday).getMetricsPerDayMap()); + + } + + // read from metrics file + public static Metrics readMetrics(){ + Builder builder = Metrics.newBuilder(); + + + String PathToFile = "routing-server/src/main/resources/metrics.proto"; + + try { + FileInputStream input = new FileInputStream(PathToFile); + Metrics metrics = builder.mergeFrom(input).build(); + return metrics; + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + + + + } + +} + diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/NetworkReader.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/NetworkReader.java index 9fb59de0cc80cd1f36613ef02586b6a3d75bbcb2..ce6667709bdc7fce4e49b9f1073ca8da12d04cc0 100644 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/NetworkReader.java +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/NetworkReader.java @@ -3,7 +3,6 @@ package de.fuberlin.navigator.routingserver.utility; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; - import de.fuberlin.navigator.protos.map_builder.RoadNetwork; import de.fuberlin.navigator.protos.map_builder.RoadNetwork.Builder; @@ -11,14 +10,16 @@ import de.fuberlin.navigator.protos.map_builder.RoadNetwork.Builder; public class NetworkReader{ public static void main(String[] args) throws IOException { + // testing System.out.println(readRoadNetwork()); + } // read from network file and print content public static RoadNetwork readRoadNetwork(){ Builder builder = RoadNetwork.newBuilder(); - String PathToFile = "routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/exampleNetwork/roadnetwork_protobuf_output"; + String PathToFile = "debug-tool/proto/sample/roadnetwork_sample.proto"; try { FileInputStream input = new FileInputStream(PathToFile); @@ -37,3 +38,4 @@ public class NetworkReader{ } } + diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/ProtobufToGraph.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/ProtobufToGraph.java index f28c9f9fc5751a4daf77c8e0486af7dbcc11609e..e3cbb6dcda4e08c347c758bfccba2868dceb9557 100644 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/ProtobufToGraph.java +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/ProtobufToGraph.java @@ -2,60 +2,75 @@ package de.fuberlin.navigator.routingserver.utility; import java.util.Map; -import org.jgrapht.Graph; + import org.jgrapht.graph.AbstractBaseGraph; import org.jgrapht.graph.SimpleDirectedWeightedGraph; import de.fuberlin.navigator.protos.map_builder.Node; import de.fuberlin.navigator.protos.map_builder.RoadNetwork; import de.fuberlin.navigator.protos.map_builder.Segment; +import de.fuberlin.navigator.protos.metric_builder.Metric; import de.fuberlin.navigator.routingserver.model.shortestPath.ExtendedEdge; -public class ProtobufToGraph { - // private static Map<Integer, Node> nodeMap = new HashMap<>(); - private final RoadNetwork roadnetwork; - public ProtobufToGraph(RoadNetwork roadNetwork) { - this.roadnetwork = roadNetwork; - } - public RoadNetwork getRoadnetwork() { - return this.roadnetwork; - } +public class ProtobufToGraph { + + public static final String getSegmentsMap = null; + private static RoadNetwork roadnetwork = NetworkReader.readRoadNetwork(); - public Map<Long, Node> getNodeMap() { - return this.roadnetwork.getNodesMap(); + public static RoadNetwork getRoadnetwork() { + return roadnetwork; } - public static void main(String[] args) { - RoadNetwork network = NetworkReader.readRoadNetwork(); - ProtobufToGraph test = new ProtobufToGraph(network); - System.out.println(test.getGraphFromProto()); + public static Map<Long, Node> getNodeMap() { + return roadnetwork.getNodesMap(); } + + public static SimpleDirectedWeightedGraph getGraphFromProto(int dayAfterToday){ - public Graph<Integer, ExtendedEdge> getGraphFromProto() { // create a new directed weighted graph - Graph<Integer, ExtendedEdge> graph = new SimpleDirectedWeightedGraph<>(ExtendedEdge.class); + SimpleDirectedWeightedGraph<Long, ExtendedEdge> graph = new SimpleDirectedWeightedGraph<>(ExtendedEdge.class); // add vertices to the graph - for (Map.Entry<Long, Node> node : this.roadnetwork.getNodesMap().entrySet()) { - graph.addVertex((int) node.getValue().getId()); - // create a map of nodes by ID - // nodeMap.put((int) node.getValue().getId(), node.getValue()); + for (Map.Entry<Long,Node> node : roadnetwork.getNodesMap().entrySet()) { + graph.addVertex(node.getValue().getOsmId()); } + // add edges to the graph - for (Map.Entry<Long, Segment> edge : roadnetwork.getSegmentsMap().entrySet()) { - int source = (int) edge.getValue().getStartNode(); - int target = (int) edge.getValue().getEndNode(); - double weight = edge.getValue().getLength(); - ExtendedEdge e = new ExtendedEdge((int) edge.getValue().getId()); - graph.addEdge(source, target, e); - ((AbstractBaseGraph<Integer, ExtendedEdge>) graph).setEdgeWeight(e, weight); + for (Map.Entry<Long,Segment> edge : roadnetwork.getSegmentsMap().entrySet()) { + long source = edge.getValue().getStartNode(); + long target = edge.getValue().getEndNode(); + + double weight = calculateWeight(edge.getValue(), dayAfterToday); + + ExtendedEdge e1 = new ExtendedEdge(edge.getValue().getId()); + ExtendedEdge e2 = new ExtendedEdge(edge.getValue().getId()); + if (source != target){ + graph.addEdge(source, target, e1); + ((AbstractBaseGraph<Long, ExtendedEdge>) graph).setEdgeWeight(e1, weight); + + if (!edge.getValue().getOneWay()){ + graph.addEdge(target, source, e2); + ((AbstractBaseGraph<Long, ExtendedEdge>) graph).setEdgeWeight(e2, weight); + } + } + + } return graph; } -} + + private static double calculateWeight(Segment edge, int dayAfterToday){ + Metric metric = MetricReader.metrics.getMetricsMap().get(dayAfterToday).getMetricsPerDayMap().get(edge.getId()); + double maxSpeed = metric.getTravelSpeed(); + double lengthKm = edge.getLength()/1000; + double weight = lengthKm/maxSpeed; + return weight; + + } +} diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/TurnRestrictedAStar.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/TurnRestrictedAStar.java index ddc91692cfc14ea63c001eda6f5a78998b6528cf..dc5cd7d50ee39dc2d5d4f0572b87f47a249aa7eb 100644 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/TurnRestrictedAStar.java +++ b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/TurnRestrictedAStar.java @@ -7,34 +7,47 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import org.jgrapht.DirectedGraph; -import org.jgrapht.Graph; -import org.jgrapht.GraphPath; -import org.jgrapht.Graphs; -import org.jgrapht.UndirectedGraph; -import org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic; + import org.jgrapht.alg.shortestpath.AStarShortestPath; -import org.jgrapht.graph.GraphWalk; -import org.jgrapht.util.FibonacciHeap; -import org.jgrapht.util.FibonacciHeapNode; + + +import org.jgrapht.*; +import org.jgrapht.alg.interfaces.*; +import org.jgrapht.graph.*; +import org.jgrapht.util.*; import de.fuberlin.navigator.protos.map_builder.Restriction; +import de.fuberlin.navigator.protos.metric_builder.Metric; +import de.fuberlin.navigator.protos.metric_builder.WeatherCondition; +import de.fuberlin.navigator.routingserver.model.WeatherTag; import de.fuberlin.navigator.routingserver.model.shortestPath.ExtendedEdge; + public class TurnRestrictedAStar<V, E> extends AStarShortestPath<V, E> { /* - * Sub class of AStarShortestPath to check for turn restrictions - * only code added is the method 'isTurnRestricted' and - * a check in method 'expandNode' (line 105) to filter + * Sub class of AStarShortestPath to check for turn and weather restrictions. + * Overriding 'getPath' method to add restriction checks in 'expandNode' + * only code added are the methods 'isTurnRestricted' and 'isForbiddenWeather' + * and corresponding checks in method 'expandNode' (line 120, 128) to filter * out affected edges */ + private Set<WeatherTag> forbidden; + private int dayAfterToday; + public TurnRestrictedAStar(Graph<V, E> graph, AStarAdmissibleHeuristic<V> admissibleHeuristic) { super(graph, admissibleHeuristic); } - private void initialize(AStarAdmissibleHeuristic<V> admissibleHeuristic) { + public TurnRestrictedAStar(Graph<V, E> graph, AStarAdmissibleHeuristic<V> admissibleHeuristic, Set<WeatherTag> forbidden, int dayAfterToday) { + super(graph, admissibleHeuristic); + this.forbidden = forbidden; + this.dayAfterToday = dayAfterToday; + } + + private void initialize(AStarAdmissibleHeuristic<V> admissibleHeuristic) + { this.admissibleHeuristic = admissibleHeuristic; openList = new FibonacciHeap<>(); vertexToHeapNodeMap = new HashMap<>(); @@ -44,11 +57,13 @@ public class TurnRestrictedAStar<V, E> extends AStarShortestPath<V, E> { numberOfExpandedNodes = 0; } + @Override - public GraphPath<V, E> getPath(V sourceVertex, V targetVertex) { + public GraphPath<V, E> getPath(V sourceVertex, V targetVertex) + { if (!graph.containsVertex(sourceVertex) || !graph.containsVertex(targetVertex)) { throw new IllegalArgumentException( - "Source or target vertex not contained in the graph!"); + "Source or target vertex not contained in the graph!"); } if (sourceVertex.equals(targetVertex)) { @@ -79,7 +94,9 @@ public class TurnRestrictedAStar<V, E> extends AStarShortestPath<V, E> { return createEmptyPath(sourceVertex, targetVertex); } - private void expandNode(FibonacciHeapNode<V> currentNode, V endVertex) { + + private void expandNode(FibonacciHeapNode<V> currentNode, V endVertex) + { numberOfExpandedNodes++; Set<E> outgoingEdges = null; @@ -98,15 +115,22 @@ public class TurnRestrictedAStar<V, E> extends AStarShortestPath<V, E> { // add turn restriction check E previous = cameFrom.get(currentNode.getData()); - if (previous != null) { - if (isTurnRestricted(previous, edge)) { + if (previous!=null){ + if(isTurnRestricted(previous,edge)){ continue; } } + // add forbidden weather check + if(isForbiddenWeather(edge)){ + continue; + } + + double gScore_current = gScoreMap.get(currentNode.getData()); double tentativeGScore = gScore_current + graph.getEdgeWeight(edge); - double fScore = tentativeGScore + admissibleHeuristic.getCostEstimate(successor, endVertex); + double fScore = + tentativeGScore + admissibleHeuristic.getCostEstimate(successor, endVertex); if (vertexToHeapNodeMap.containsKey(successor)) { // We re-encountered a vertex. It's // either in the open or closed list. @@ -135,19 +159,9 @@ public class TurnRestrictedAStar<V, E> extends AStarShortestPath<V, E> { } } - private boolean isTurnRestricted(E edge1, E edge2) { - for (Restriction restriction : NetworkReader.readRoadNetwork().getTurnRestrictionsList()) { - if (restriction.getFromId() == ((ExtendedEdge) edge1).getId() - && restriction.getToId() == ((ExtendedEdge) edge2).getId()) { - return true; - - } - } - return false; - - } - private GraphPath<V, E> buildGraphPath(V startVertex, V targetVertex, double pathLength) { + private GraphPath<V, E> buildGraphPath(V startVertex, V targetVertex, double pathLength) + { List<E> edgeList = new ArrayList<>(); List<V> vertexList = new ArrayList<>(); vertexList.add(targetVertex); @@ -163,4 +177,38 @@ public class TurnRestrictedAStar<V, E> extends AStarShortestPath<V, E> { return new GraphWalk<>(graph, startVertex, targetVertex, vertexList, edgeList, pathLength); } + private boolean isTurnRestricted(E edge1, E edge2){ + for (Restriction restriction : ProtobufToGraph.getRoadnetwork().getTurnRestrictionsList()) { + if (restriction.getFromId() == ((ExtendedEdge) edge1).getId() && restriction.getToId() == ((ExtendedEdge) edge2).getId()){ + return true; + + } + } + return false; + + } + + private boolean isForbiddenWeather(E edge){ + Metric metric = MetricReader.metrics.getMetricsMap().get(dayAfterToday).getMetricsPerDayMap().get((long)((ExtendedEdge) edge).getId()); + List<WeatherCondition> weather = metric.getAdditionalFactorsList(); + + if (forbidden.contains(WeatherTag.ICE) && weather.contains(WeatherCondition.WEATHER_ICE)){ + return true; + } + if (forbidden.contains(WeatherTag.RAIN) && weather.contains(WeatherCondition.WEATHER_RAIN)){ + return true; + } + if (forbidden.contains(WeatherTag.SNOW) && weather.contains(WeatherCondition.WEATHER_SNOW)){ + return true; + } + if (forbidden.contains(WeatherTag.WIND) && weather.contains(WeatherCondition.WEATHER_WIND)){ + return true; + } + return false; + + } + + + } + diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/exampleNetwork/CreateExampleNetwork.java b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/exampleNetwork/CreateExampleNetwork.java deleted file mode 100644 index 278e706808a71dab93f39c5b8992cb7c2f4527ae..0000000000000000000000000000000000000000 --- a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/exampleNetwork/CreateExampleNetwork.java +++ /dev/null @@ -1,128 +0,0 @@ -package de.fuberlin.navigator.routingserver.utility.exampleNetwork; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import de.fuberlin.navigator.protos.map_builder.Coordinates; - -import de.fuberlin.navigator.protos.map_builder.Node; -import de.fuberlin.navigator.protos.map_builder.Restriction; -import de.fuberlin.navigator.protos.map_builder.RoadCategory; -import de.fuberlin.navigator.protos.map_builder.RoadNetwork; -import de.fuberlin.navigator.protos.map_builder.Segment; - -import java.io.FileOutputStream; -import java.io.IOException; - - -public class CreateExampleNetwork { - - static String PathToFile = "routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/exampleNetwork/roadnetwork_protobuf_output"; - - public static void main(String[] args) throws IOException { - - //build example network - Coordinates location = createLocation(10, 30); - List<Coordinates> coordinates = new ArrayList<>(); - coordinates.add(location); - - Segment segment1 = createSegment(1, 854, coordinates, 2, 1, 2, RoadCategory.ROAD_CATEGORY_HIGHWAY); - //Segment segment2 = createSegment(2, "855", location, 5, 1, 4, RoadCategory.ROAD_CATEGORY_HIGHWAY); - Segment segment3 = createSegment(3, 856, coordinates, 2, 1, 3, RoadCategory.ROAD_CATEGORY_HIGHWAY); - Segment segment4 = createSegment(4, 857, coordinates, 2, 2, 4, RoadCategory.ROAD_CATEGORY_HIGHWAY); - Segment segment5 = createSegment(5, 858, coordinates, 3, 3, 4, RoadCategory.ROAD_CATEGORY_HIGHWAY); - - Map<Long,Segment> segments = new HashMap<>(); - segments.put((long) 1, segment1); - //segments.add(segment2); - segments.put((long) 3, segment3); - segments.put((long) 4, segment4); - segments.put((long) 5, segment5); - - Node node1 = createNode(1, 2365, createLocation(10, 30)); - Node node2 = createNode(2, 2366, createLocation(11, 31)); - Node node3 = createNode(3, 2367, createLocation(11, 30)); - Node node4 = createNode(4, 2368, createLocation(12, 33)); - - Map<Long, Node> nodes = new HashMap<>(); - nodes.put((long) 1, node1); - nodes.put((long) 2, node2); - nodes.put((long) 3, node3); - nodes.put((long) 4, node4); - - - Restriction restriction = createRestriction(1, 4); - List<Restriction> restrictions= new ArrayList<>(); - restrictions.add(restriction); - - RoadNetwork roadnetwork = createRoadNetwork(nodes, segments, restrictions); - - - // write network to file - try(FileOutputStream output = new FileOutputStream(PathToFile)){ - roadnetwork.writeTo(output); - } - System.out.println("Saved road network information with following data to disk: \n" + roadnetwork); - } - - - - - - static Node createNode(int id, int osmid, Coordinates location) { - Node.Builder builder = Node.newBuilder(); - Node Node = builder - .setId(id) - .setOsmId(osmid) - .setPosition(location) - .build(); - return Node; - } - - static Coordinates createLocation(float lat, float lon){ - Coordinates.Builder builder = Coordinates.newBuilder(); - Coordinates Coordinates = builder - .setLat(lat) - .setLon(lon) - .build(); - return Coordinates; - } - - static Segment createSegment(int id, int osmid, List<Coordinates> location, double length, int start, int end, RoadCategory category) { - Segment.Builder builder = Segment.newBuilder(); - Segment Segment = builder - .setId(id) - .setOsmId(osmid) - .addAllGeometry(location) - .setLength(length) - .setStartNode(start) - .setEndNode(end) - .setCategory(category) - .build(); - return Segment; - } - - static Restriction createRestriction(int id1, int id2) { - Restriction.Builder builder = Restriction.newBuilder(); - Restriction Restriction = builder - .setFromId(id1) - .setToId(id2) - .build(); - return Restriction; - } - - - static RoadNetwork createRoadNetwork(Map<Long, Node> nodes, Map<Long, Segment> segments, List<Restriction> restrictions) { - RoadNetwork.Builder builder = RoadNetwork.newBuilder(); - RoadNetwork RoadNetwork = builder - .putAllNodes(nodes) - .putAllSegments(segments) - .addAllTurnRestrictions(restrictions) - .build(); - return RoadNetwork; - } - - -} \ No newline at end of file diff --git a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/exampleNetwork/roadnetwork_protobuf_output b/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/exampleNetwork/roadnetwork_protobuf_output deleted file mode 100644 index e7c6172b49ce4baddf9b3a003dce405f10dab1e0..0000000000000000000000000000000000000000 Binary files a/routing-server/src/main/java/de/fuberlin/navigator/routingserver/utility/exampleNetwork/roadnetwork_protobuf_output and /dev/null differ diff --git a/routing-server/src/main/resources/metrics.proto b/routing-server/src/main/resources/metrics.proto new file mode 100644 index 0000000000000000000000000000000000000000..c8d137d396317dfc5c3ffc38f28513902cd96eac Binary files /dev/null and b/routing-server/src/main/resources/metrics.proto differ