/** * @param x1 the actual "from" x coordinate (in respect to the "dense grid") * @param y1 the actual "from" y coordinate (ditto) * @param x2 the actual "to" x coordinate (ditto) * @param y2 the actual "to" y coordinate (ditto) * @return the list of edges representing the shortest path from P1 to P2 */ public List<WeightedEdge> getShortestPath(int x1, int y1, int x2, int y2) { // int x1 = 158, y1 = 109, x2 = 108, y2 = 489; int row1 = y1 / unitH, col1 = x1 / unitW, row2 = y2 / unitH, col2 = x2 / unitW; DijkstraShortestPath d = new DijkstraShortestPath(g, cellContainer[row1][col1], cellContainer[row2][col2]); return d.getPathEdgeList(); }
/** * @return shortest path between two connected spot, using Dijkstra's algorithm. The edge weights, * if any, are ignored here, meaning that the returned path is the shortest in terms of number * of edges. * <p>Return <code>null</code> if the two spots are not connected by a track, or if one of the * spot do not belong to the graph, or if the {@link #graph} field is <code>null</code>. * @param source the spot to start the path with * @param target the spot to stop the path with */ public List<DefaultWeightedEdge> dijkstraShortestPath(final Spot source, final Spot target) { if (null == graph) { return null; } final AsUnweightedGraph<Spot, DefaultWeightedEdge> unWeightedGrah = new AsUnweightedGraph<Spot, DefaultWeightedEdge>(graph); final DijkstraShortestPath<Spot, DefaultWeightedEdge> pathFinder = new DijkstraShortestPath<Spot, DefaultWeightedEdge>(unWeightedGrah, source, target); final List<DefaultWeightedEdge> path = pathFinder.getPathEdgeList(); return path; }
/** * Apply selected routing algorithm to the graph. * * @param nodes Nodes used to calculate path. * @param algorithm Algorithm used to compute the path, * RoutingGraph.Algorithm.ROUTING_ALG_DIJKSTRA or * RoutingGraph.Algorithm.ROUTING_ALG_BELLMANFORD * @return new path. */ public List<OsmEdge> applyAlgorithm(List<Node> nodes, Algorithm algorithm) { List<OsmEdge> path = new ArrayList<>(); Graph<Node, OsmEdge> g; double totalWeight = 0; RoutingLayer layer = (RoutingLayer) Main.map.mapView.getActiveLayer(); RoutingModel routingModel = layer.getRoutingModel(); if (graph == null || routingModel.getOnewayChanged()) this.createGraph(); logger.debug("apply algorithm between nodes "); for (Node node : nodes) { logger.debug(node.getId()); } logger.debug("-----------------------------------"); // Assign the graph to g g = graph; switch (algorithm) { case ROUTING_ALG_DIJKSTRA: logger.debug("Using Dijkstra algorithm"); DijkstraShortestPath<Node, OsmEdge> routingk = null; for (int index = 1; index < nodes.size(); ++index) { routingk = new DijkstraShortestPath<>(g, nodes.get(index - 1), nodes.get(index)); if (routingk.getPathEdgeList() == null) { logger.debug("no path found!"); break; } path.addAll(routingk.getPathEdgeList()); totalWeight += routingk.getPathLength(); } break; case ROUTING_ALG_BELLMANFORD: logger.debug("Using Bellman Ford algorithm"); for (int index = 1; index < nodes.size(); ++index) { path = BellmanFordShortestPath.findPathBetween( rgDelegator, nodes.get(index - 1), nodes.get(index)); if (path == null) { logger.debug("no path found!"); return null; } } break; default: logger.debug("Wrong algorithm"); break; } logger.debug("shortest path found: " + path + "\nweight: " + totalWeight); return path; }
public List<Graph> getAllNodeCoverage() { Node initial = getInitialState(); Set<Node> toVisit = new HashSet<>(nodes); toVisit.remove(initial); Set<Node> left = new HashSet<>(nodes); left.remove(initial); List<Graph> toReturn = new ArrayList<>(); for (Node visiting : toVisit) { if (left.contains(visiting)) { DijkstraShortestPath<Node, Edge> pathFinder = new DijkstraShortestPath<>(myGraph, initial, visiting); List<Edge> path = pathFinder.getPathEdgeList(); for (Edge e : path) { left.remove(e.getDest()); } toReturn.add(buildGraph(path)); } } return toReturn; }
private WeightedMultigraph<Vertex, LabeledWeightedEdge> step3( WeightedMultigraph<Vertex, LabeledWeightedEdge> g2) { logger.debug("<enter"); WeightedMultigraph<Vertex, LabeledWeightedEdge> g3 = new WeightedMultigraph<Vertex, LabeledWeightedEdge>(LabeledWeightedEdge.class); Set<LabeledWeightedEdge> edges = g2.edgeSet(); DijkstraShortestPath<Vertex, LabeledWeightedEdge> path; Vertex source, target; for (LabeledWeightedEdge edge : edges) { source = edge.getSource(); target = edge.getTarget(); path = new DijkstraShortestPath<Vertex, LabeledWeightedEdge>(this.graph, source, target); List<LabeledWeightedEdge> pathEdges = path.getPathEdgeList(); if (pathEdges == null) continue; for (int i = 0; i < pathEdges.size(); i++) { if (g3.edgeSet().contains(pathEdges.get(i))) continue; source = pathEdges.get(i).getSource(); target = pathEdges.get(i).getTarget(); if (!g3.vertexSet().contains(source)) g3.addVertex(source); if (!g3.vertexSet().contains(target)) g3.addVertex(target); g3.addEdge(source, target, pathEdges.get(i)); } } logger.debug("exit>"); return g3; }
public Queue<Vector2> getPathToPoint(Vector2 origin, Vector2 destination) { LinkedList<Vector2> steps = new LinkedList<Vector2>(); Vector2 originRounded = getClosestNode(Math.round(origin.x), Math.round(origin.y)); Vector2 destinationRounded = getClosestNode(Math.round(destination.x), Math.round(destination.y)); try { List<DefaultWeightedEdge> list = DijkstraShortestPath.findPathBetween(movementGraph, originRounded, destinationRounded); for (DefaultWeightedEdge edge : list) steps.add(movementGraph.getEdgeSource(edge)); steps.add(movementGraph.getEdgeTarget(list.get(list.size() - 1))); } catch (Exception e) { logger.warning( "Error pathfinding for origin=" + originRounded + " destination=" + destinationRounded + " with message: " + e.getMessage()); } return steps; }