@Override public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { RoutingAlgorithm ra; String algoStr = opts.getAlgorithm(); if (DIJKSTRA_BI.equalsIgnoreCase(algoStr)) { ra = new DijkstraBidirectionRef(g, opts.getWeighting(), opts.getTraversalMode()); } else if (DIJKSTRA.equalsIgnoreCase(algoStr)) { ra = new Dijkstra(g, opts.getWeighting(), opts.getTraversalMode()); AStarBidirection aStarBi = new AStarBidirection(g, opts.getWeighting(), opts.getTraversalMode()); aStarBi.setApproximation(getApproximation(ASTAR_BI, opts, g.getNodeAccess())); ra = aStarBi; ra = new DijkstraOneToMany(g, opts.getWeighting(), opts.getTraversalMode()); AStar aStar = new AStar(g, opts.getWeighting(), opts.getTraversalMode()); aStar.setApproximation(getApproximation(ASTAR, opts, g.getNodeAccess())); ra = aStar; AlternativeRoute altRouteAlgo = new AlternativeRoute(g, opts.getWeighting(), opts.getTraversalMode()); altRouteAlgo.setMaxPaths(opts.getHints().getInt(MAX_PATHS, 2)); altRouteAlgo.setMaxWeightFactor(opts.getHints().getDouble(MAX_WEIGHT, 1.4)); altRouteAlgo.setMaxShareFactor(opts.getHints().getDouble(MAX_SHARE, 0.6)); altRouteAlgo.setMinPlateauFactor(opts.getHints().getDouble("alternative_route.min_plateau_factor", 0.2)); altRouteAlgo.setMaxExplorationFactor(opts.getHints().getDouble("alternative_route.max_exploration_factor", 1)); ra = altRouteAlgo; ra.setMaxVisitedNodes(opts.getMaxVisitedNodes()); return ra;
@Override public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts) { pathList = new ArrayList<>(queryResults.size() - 1); AvoidEdgesWeighting avoidPathWeighting = new AvoidEdgesWeighting(algoOpts.getWeighting()); avoidPathWeighting.setEdgePenaltyFactor(5); algoOpts = AlgorithmOptions.start(algoOpts). algorithm(Parameters.Algorithms.ASTAR_BI). weighting(avoidPathWeighting).build(); algoOpts.getHints().put(Algorithms.AStarBi.EPSILON, 2); long visitedNodesSum = 0L; QueryResult start = queryResults.get(0); for (int qrIndex = 1; qrIndex < queryResults.size(); qrIndex++) { RoutingAlgorithm algo = algoFactory.createAlgo(queryGraph, algoOpts); // instead getClosestNode (which might be a virtual one and introducing unnecessary tails of the route) // use next tower node -> getBaseNode or getAdjNode // Later: remove potential route tail QueryResult startQR = queryResults.get(qrIndex - 1); int startNode = (startQR == start) ? startQR.getClosestNode() : startQR.getClosestEdge().getBaseNode(); QueryResult endQR = queryResults.get(qrIndex); int endNode = (endQR == start) ? endQR.getClosestNode() : endQR.getClosestEdge().getBaseNode(); Path path = algo.calcPath(startNode, endNode); visitedNodesSum += algo.getVisitedNodes(); pathList.add(path); // it is important to avoid previously visited nodes for future paths avoidPathWeighting.addEdges(path.calcEdges()); } ghResponse.getHints().put("visited_nodes.sum", visitedNodesSum); ghResponse.getHints().put("visited_nodes.average", (float) visitedNodesSum / (queryResults.size() - 1)); return pathList; }
public Weighting getWeighting() { assertNotNull(weighting, "weighting"); return weighting; }
@Override public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { RoutingAlgorithm algo = tmpFactory.createAlgo(g, opts); if (algo instanceof AStarBidirection) { return new DebugAStarBi(g, opts.getWeighting(), opts.getTraversalMode(), mg). setApproximation(((AStarBidirection) algo).getApproximation()); } else if (algo instanceof AStar) { return new DebugAStar(g, opts.getWeighting(), opts.getTraversalMode(), mg); } else if (algo instanceof DijkstraBidirectionRef) { return new DebugDijkstraBidirection(g, opts.getWeighting(), opts.getTraversalMode(), mg); } else if (algo instanceof Dijkstra) { return new DebugDijkstraSimple(g, opts.getWeighting(), opts.getTraversalMode(), mg); } return algo; } };
private AbstractBidirAlgo doCreateAlgo(Graph graph, AlgorithmOptions opts) { if (ASTAR_BI.equals(opts.getAlgorithm())) { return new AStarBidirectionCH(graph, prepareWeighting, traversalMode) .setApproximation(RoutingAlgorithmFactorySimple.getApproximation(ASTAR_BI, opts, graph.getNodeAccess())); } else if (DIJKSTRA_BI.equals(opts.getAlgorithm())) { if (opts.getHints().getBool("stall_on_demand", true)) { return new DijkstraBidirectionCH(graph, prepareWeighting, traversalMode); } else { return new DijkstraBidirectionCHNoSOD(graph, prepareWeighting, traversalMode); } } else { throw new IllegalArgumentException("Algorithm " + opts.getAlgorithm() + " not supported for Contraction Hierarchies. Try with ch.disable=true"); } }
public static WeightApproximator getApproximation(String prop, AlgorithmOptions opts, NodeAccess na) { String approxAsStr = opts.getHints().get(prop + ".approximation", "BeelineSimplification"); double epsilon = opts.getHints().getDouble(prop + ".epsilon", 1); BeelineWeightApproximator approx = new BeelineWeightApproximator(na, opts.getWeighting()); approx.setEpsilon(epsilon); if ("BeelineSimplification".equals(approxAsStr)) approx.setDistanceCalc(Helper.DIST_PLANE); else if ("BeelineAccurate".equals(approxAsStr)) approx.setDistanceCalc(Helper.DIST_EARTH); else throw new IllegalArgumentException("Approximation " + approxAsStr + " not found in " + RoutingAlgorithmFactorySimple.class.getName()); return approx; } }
@Test public void testCalcFootPath() { AlgorithmOptions opts = AlgorithmOptions.start(). weighting(new ShortestWeighting(footEncoder)).build(); GraphHopperStorage ghStorage = createGHStorage(encodingManager, Arrays.asList(opts.getWeighting()), false); initFootVsCar(ghStorage); Path p1 = createAlgo(ghStorage, opts). calcPath(0, 7); assertEquals(p1.toString(), 17000, p1.getDistance(), 1e-6); assertEquals(p1.toString(), 12240 * 1000, p1.getTime()); assertEquals(IntArrayList.from(new int[]{0, 4, 5, 7}), p1.calcNodes()); }
queryGraph.lookup(queryList); AlgorithmOptions opts = algoEntry.getAlgorithmOptions(); FlagEncoder encoder = opts.getWeighting().getFlagEncoder(); if (encoder.supports(TurnWeighting.class)) { if (!opts.getTraversalMode().isEdgeBased()) { errors.add("Cannot use TurnWeighting with a node based traversal"); return this; algoEntry.setAlgorithmOptions(AlgorithmOptions.start(opts).weighting(new TurnWeighting(opts.getWeighting(), (TurnCostExtension) queryGraph.getExtension())).build());
protected AlgorithmOptions createAlgoOptions() { return AlgorithmOptions.start(). weighting(new ShortestWeighting(carEncoder)).build(); }
public MapMatching(GraphHopper graphHopper, AlgorithmOptions algoOptions) { final double headingTimePenalty = algoOptions.getHints().getDouble( Parameters.Routing.HEADING_PENALTY, Parameters.Routing.DEFAULT_HEADING_PENALTY); uTurnDistancePenalty = headingTimePenalty * PENALTY_CONVERSION_VELOCITY; for (Entry<String, String> entry : algoOptions.getHints().toMap().entrySet()) { hints.put(entry.getKey(), entry.getValue()); if (algoOptions.hasWeighting()) { vehicle = algoOptions.getWeighting().getFlagEncoder().toString(); } else { vehicle = graphHopper.getEncodingManager().fetchEdgeEncoders().get(0).toString(); weighting = new FastestWeighting(graphHopper.getEncodingManager().getEncoder(vehicle), algoOptions.getHints()); RoutingAlgorithmFactory routingAlgorithmFactory = graphHopper.getAlgorithmFactory(hints); if (routingAlgorithmFactory instanceof PrepareContractionHierarchies) { this.maxVisitedNodes = algoOptions.getMaxVisitedNodes();
/** * This method clones the specified AlgorithmOption object with the possibility for further * changes. */ public static Builder start(AlgorithmOptions opts) { Builder b = new Builder(); if (opts.algorithm != null) b.algorithm(opts.getAlgorithm()); if (opts.traversalMode != null) b.traversalMode(opts.getTraversalMode()); if (opts.weighting != null) b.weighting(opts.getWeighting()); if (opts.maxVisitedNodes >= 0) b.maxVisitedNodes(opts.maxVisitedNodes); if (!opts.hints.isEmpty()) b.hints(opts.hints); return b; }
@Override public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { return new DijkstraOneToMany(g, opts.getWeighting(), traversalMode); } };
/** * This method clones the specified AlgorithmOption object with the possibility for further * changes. */ public static Builder start( AlgorithmOptions opts ) { Builder b = new Builder(); if (opts.algorithm != null) b.algorithm(opts.getAlgorithm()); if (opts.flagEncoder != null) b.flagEncoder(opts.getFlagEncoder()); if (opts.traversalMode != null) b.traversalMode(opts.getTraversalMode()); if (opts.weighting != null) b.weighting(opts.getWeighting()); if (opts.maxVisitedNodes >= 0) b.maxVisitedNodes(opts.maxVisitedNodes); return b; }
.setVehicle(hints.getVehicle()).setWeighting(hints.getWeighting()); AlgorithmOptions defaultOpts = AlgorithmOptions.start(new AlgorithmOptions("", weighting, tMode)).hints(defaultHints).build(); List<AlgoHelperEntry> prepare = new ArrayList<>(); prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(ASTAR).build(), idx, "astar|beeline|" + addStr + weighting)); prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA).build(), idx, "dijkstra|" + addStr + weighting)); AlgorithmOptions astarbiOpts = AlgorithmOptions.start(defaultOpts).algorithm(ASTAR_BI).build(); astarbiOpts.getHints().put(ASTAR_BI + ".approximation", "BeelineSimplification"); AlgorithmOptions dijkstrabiOpts = AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA_BI).build(); prepare.add(new AlgoHelperEntry(ghStorage, astarbiOpts, idx, "astarbi|beeline|" + addStr + weighting)); prepare.add(new AlgoHelperEntry(ghStorage, dijkstrabiOpts, idx, "dijkstrabi|" + addStr + weighting)); prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(astarbiOpts).hints(lmHints).build(), idx, "astarbi|landmarks|" + weighting) { @Override public RoutingAlgorithmFactory createRoutingFactory() { AlgorithmOptions.start(dijkstrabiOpts).hints(chHints).build(), idx, "dijkstrabi|ch|prepare|" + hints.getWeighting()) { @Override public RoutingAlgorithmFactory createRoutingFactory() { AlgorithmOptions.start(astarbiOpts).hints(chHints).build(), idx, "astarbi|ch|prepare|" + hints.getWeighting()) { @Override public RoutingAlgorithmFactory createRoutingFactory() {
queryGraph.lookup(queryList); AlgorithmOptions opts = algoEntry.opts; FlagEncoder encoder = opts.getFlagEncoder(); if (encoder.supports(TurnWeighting.class)) algoEntry.setAlgorithmOptions(AlgorithmOptions.start(opts).weighting(new TurnWeighting(opts.getWeighting(), opts.getFlagEncoder(), (TurnCostExtension) queryGraph.getExtension())).build());
@Override public RoutingAlgorithm createAlgo( Graph graph, AlgorithmOptions opts ) { AbstractBidirAlgo algo; if (ASTAR_BI.equals(opts.getAlgorithm())) { algo = createAStarBidirection(graph); } else if (DIJKSTRA_BI.equals(opts.getAlgorithm())) { algo = createDijkstraBidirection(graph); } else { throw new UnsupportedOperationException("Algorithm " + opts.getAlgorithm() + " not supported for Contraction Hierarchies"); } algo.setMaxVisitedNodes(opts.getMaxVisitedNodes()); algo.setEdgeFilter(levelFilter); return algo; }
@Override public RoutingAlgorithm createAlgo(Graph graph, AlgorithmOptions opts) { AbstractBidirAlgo algo = doCreateAlgo(graph, opts); algo.setEdgeFilter(new LevelEdgeFilter(prepareGraph)); algo.setMaxVisitedNodes(opts.getMaxVisitedNodes()); return algo; }
public RoutingAlgorithm getDecoratedAlgorithm(Graph qGraph, RoutingAlgorithm algo, AlgorithmOptions opts) { int activeLM = Math.max(1, opts.getHints().getInt(Landmark.ACTIVE_COUNT, defaultActiveLandmarks)); if (algo instanceof AStar) { if (!lms.isInitialized()) throw new IllegalStateException("Initialize landmark storage before creating algorithms"); double epsilon = opts.getHints().getDouble(Parameters.Algorithms.AStar.EPSILON, 1); AStar astar = (AStar) algo; astar.setApproximation(new LMApproximator(qGraph, this.graph.getNodes(), lms, activeLM, lms.getFactor(), false). throw new IllegalStateException("Initialize landmark storage before creating algorithms"); double epsilon = opts.getHints().getDouble(Parameters.Algorithms.AStarBi.EPSILON, 1); AStarBidirection astarbi = (AStarBidirection) algo; astarbi.setApproximation(new LMApproximator(qGraph, this.graph.getNodes(), lms, activeLM, lms.getFactor(), false). throw new IllegalStateException("Initialize landmark storage before creating algorithms"); double epsilon = opts.getHints().getDouble(Parameters.Algorithms.AStarBi.EPSILON, 1); AlternativeRoute altRoute = (AlternativeRoute) algo; altRoute.setApproximation(new LMApproximator(qGraph, this.graph.getNodes(), lms, activeLM, lms.getFactor(), false).
@Test public void testWithCoordinates() { Weighting weighting = new ShortestWeighting(carEncoder); GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false); graph.edge(0, 1, 2, true).setWayGeometry(Helper.createPointList(1.5, 1)); graph.edge(2, 3, 2, true).setWayGeometry(Helper.createPointList(0, 1.5)); graph.edge(3, 4, 2, true).setWayGeometry(Helper.createPointList(0, 2)); // duplicate but the second edge is longer graph.edge(0, 2, 1.2, true); graph.edge(0, 2, 1.5, true).setWayGeometry(Helper.createPointList(0.5, 0)); graph.edge(1, 3, 1.3, true).setWayGeometry(Helper.createPointList(0.5, 1.5)); graph.edge(1, 4, 1, true); updateDistancesFor(graph, 0, 1, 0.6); updateDistancesFor(graph, 1, 1, 1.5); updateDistancesFor(graph, 2, 0, 0); updateDistancesFor(graph, 3, 0, 1); updateDistancesFor(graph, 4, 0, 2); AlgorithmOptions opts = new AlgorithmOptions(DIJKSTRA_BI, weighting); RoutingAlgorithmFactory prepare = createFactory(graph, opts); Path p = prepare.createAlgo(getGraph(graph, opts.getWeighting()), opts).calcPath(4, 0); assertEquals(IntArrayList.from(new int[]{4, 1, 0}), p.calcNodes()); assertEquals(Helper.createPointList(0, 2, 1, 1.5, 1.5, 1, 1, 0.6), p.calcPoints()); assertEquals(274128, p.calcPoints().calcDistance(new DistanceCalcEarth()), 1); p = prepare.createAlgo(getGraph(graph, opts.getWeighting()), opts).calcPath(2, 1); assertEquals(IntArrayList.from(new int[]{2, 0, 1}), p.calcNodes()); assertEquals(Helper.createPointList(0, 0, 1, 0.6, 1.5, 1, 1, 1.5), p.calcPoints()); assertEquals(279482, p.calcPoints().calcDistance(new DistanceCalcEarth()), 1); }