@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; }
return this; algoEntry.setAlgorithmOptions(AlgorithmOptions.start(opts).weighting(new TurnWeighting(opts.getWeighting(), (TurnCostExtension) queryGraph.getExtension())).build());
algorithm(algoStr).traversalMode(tMode).weighting(weighting). maxVisitedNodes(maxVisitedNodesForRequest). hints(hints).
AlgorithmOptions opts = AlgorithmOptions.start().weighting(weighting).traversalMode(tm). build();
AlgorithmOptions opts = AlgorithmOptions.start().weighting(defaultOpts.getWeighting()).build(); GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(opts.getWeighting()), true); initEleGraph(graph); AlgorithmOptions fakeOpts = AlgorithmOptions.start().weighting(fakeWeighting).build(); graph = createGHStorage(encodingManager, Arrays.asList(fakeOpts.getWeighting()), true); initEleGraph(graph);
weighting(new TurnWeighting(new FastestWeighting(carEncoder), tcs) { @Override public double calcTurnWeight(int edgeFrom, int nodeVia, int edgeTo) {
@Test public void testBasicTurnRestriction() { GraphHopperStorage g = createStorage(createEncodingManager(true)); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); initTurnRestrictions(g, tcs, carEncoder); Path p = createAlgo(g, AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 40)). traversalMode(TraversalMode.EDGE_BASED_2DIR).build()). calcPath(5, 1); assertEquals(IntArrayList.from(new int[]{5, 2, 3, 4, 7, 6, 3, 1}), p.calcNodes()); // test 7-6-5 and reverse p = createAlgo(g, AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 40)). traversalMode(TraversalMode.EDGE_BASED_1DIR).build()). calcPath(5, 7); assertEquals(IntArrayList.from(new int[]{5, 6, 7}), p.calcNodes()); p = createAlgo(g, AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 40)). traversalMode(TraversalMode.EDGE_BASED_1DIR).build()). calcPath(7, 5); assertEquals(IntArrayList.from(new int[]{7, 6, 3, 2, 5}), p.calcNodes()); }
@Test public void testUTurns() { GraphHopperStorage g = createStorage(createEncodingManager(true)); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); long tflags = carEncoder.getTurnFlags(true, 0); // force u-turn via lowering the cost for it EdgeIteratorState e3_6 = getEdge(g, 3, 6); e3_6.setDistance(0.1); getEdge(g, 3, 2).setDistance(864); getEdge(g, 1, 0).setDistance(864); tcs.addTurnInfo(getEdge(g, 7, 6).getEdge(), 6, getEdge(g, 6, 5).getEdge(), tflags); tcs.addTurnInfo(getEdge(g, 4, 3).getEdge(), 3, e3_6.getEdge(), tflags); AlgorithmOptions opts = AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 50)). traversalMode(TraversalMode.EDGE_BASED_2DIR_UTURN).build(); Path p = createAlgo(g, opts).calcPath(7, 5); assertEquals(IntArrayList.from(new int[]{7, 6, 3, 6, 5}), p.calcNodes()); // no u-turn for 6-3 opts = AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 100)). traversalMode(TraversalMode.EDGE_BASED_2DIR_UTURN).build(); tcs.addTurnInfo(getEdge(g, 6, 3).getEdge(), 3, getEdge(g, 3, 6).getEdge(), tflags); p = createAlgo(g, opts).calcPath(7, 5); assertEquals(IntArrayList.from(new int[]{7, 6, 3, 2, 5}), p.calcNodes()); }
@Test public void testBaseGraphMultipleVehicles() { AlgorithmOptions footOptions = AlgorithmOptions.start(). weighting(new FastestWeighting(footEncoder)).build(); AlgorithmOptions carOptions = AlgorithmOptions.start(). weighting(new FastestWeighting(carEncoder)).build(); GraphHopperStorage g = createGHStorage(encodingManager, Arrays.asList(footOptions.getWeighting(), carOptions.getWeighting()), false); initFootVsCar(g); // do CH preparation for car RoutingAlgorithmFactory contractedFactory = createFactory(g, carOptions); // use contracted graph Path p1 = contractedFactory.createAlgo(getGraph(g, carOptions.getWeighting()), carOptions).calcPath(0, 7); assertEquals(IntArrayList.from(0, 4, 6, 7), p1.calcNodes()); assertEquals(p1.toString(), 15000, p1.getDistance(), 1e-6); // use base graph for solving normal Dijkstra via car Path p2 = new RoutingAlgorithmFactorySimple().createAlgo(g, carOptions).calcPath(0, 7); assertEquals(IntArrayList.from(0, 4, 6, 7), p2.calcNodes()); assertEquals(p2.toString(), 15000, p2.getDistance(), 1e-6); assertEquals(p2.toString(), 2700 * 1000, p2.getTime()); // use base graph for solving normal Dijkstra via foot Path p3 = new RoutingAlgorithmFactorySimple().createAlgo(g, footOptions).calcPath(0, 7); assertEquals(p3.toString(), 17000, p3.getDistance(), 1e-6); assertEquals(p3.toString(), 12240 * 1000, p3.getTime()); assertEquals(IntArrayList.from(0, 4, 5, 7), p3.calcNodes()); }
@Test public void testBasicTurnCosts() { GraphHopperStorage g = createStorage(createEncodingManager(false)); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); Path p = createAlgo(g, AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 40)). traversalMode(TraversalMode.EDGE_BASED_1DIR).build()). calcPath(5, 1); // no restriction and costs EdgeIteratorState e3_6 = getEdge(g, 5, 6); e3_6.setDistance(2); assertEquals(IntArrayList.from(new int[]{5, 2, 3, 1}), p.calcNodes()); // now introduce some turn costs long tflags = carEncoder.getTurnFlags(false, 2); tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags); p = createAlgo(g, AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 40)). traversalMode(TraversalMode.EDGE_BASED_1DIR).build()). calcPath(5, 1); assertEquals(IntArrayList.from(new int[]{5, 6, 3, 1}), p.calcNodes()); }
private void runTestWithDirectionDependentEdgeSpeed( int speed, int revSpeed, int from, int to, IntArrayList expectedPath, FlagEncoder encoder) { EncodingManager encodingManager = new EncodingManager(encoder); FastestWeighting weighting = new FastestWeighting(encoder); AlgorithmOptions algoOpts = AlgorithmOptions.start().weighting(weighting).build(); GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false); EdgeIteratorState edge = graph.edge(0, 1, 2, true); long flags = edge.getFlags(); flags = encoder.setSpeed(flags, speed); flags = encoder.setReverseSpeed(flags, revSpeed); edge.setFlags(flags); graph.edge(1, 2, 1, true); CHGraph chGraph = graph.getGraph(CHGraph.class); for (int i = 0; i < 3; ++i) { chGraph.setLevel(i, i); } graph.freeze(); RoutingAlgorithm algo = createCHAlgo(graph, chGraph, true, algoOpts); Path p = algo.calcPath(from, to); assertEquals(3, p.getDistance(), 1.e-3); assertEquals(p.toString(), expectedPath, p.calcNodes()); }
@Test public void testBlockANode() { GraphHopperStorage g = createStorage(createEncodingManager(true)); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); blockNode3(g, tcs, carEncoder); for (int i=0; i<=7; i++) { if (i==3) continue; for (int j=0; j<=7; j++) { if (j==3) continue; Path p = createAlgo(g, AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 40)). traversalMode(TraversalMode.EDGE_BASED_2DIR).build()). calcPath(i, j); assertTrue(p.isFound()); // We can go from everywhere to everywhere else without using node 3 for (IntCursor node : p.calcNodes()) { assertNotEquals(p.calcNodes().toString(), 3, node.value); } } } }
@Test public void testMultipleVehicles_issue548() { FastestWeighting footWeighting = new FastestWeighting(footEncoder); AlgorithmOptions footOptions = AlgorithmOptions.start(). weighting(footWeighting).build(); FastestWeighting carWeighting = new FastestWeighting(carEncoder); AlgorithmOptions carOptions = AlgorithmOptions.start(). weighting(carWeighting).build(); GraphHopperStorage ghStorage = createGHStorage(encodingManager, Arrays.asList(footOptions.getWeighting(), carOptions.getWeighting()), false); initFootVsCar(ghStorage); // normal path would be 0-4-6-7 but block 4-6 GHUtility.getEdge(ghStorage, 4, 6).setFlags(carEncoder.setProperties(20, false, false)); RoutingAlgorithm algoFoot = createFactory(ghStorage, footOptions). createAlgo(getGraph(ghStorage, footWeighting), footOptions); RoutingAlgorithm algoCar = createFactory(ghStorage, carOptions). createAlgo(getGraph(ghStorage, carWeighting), carOptions); Path p1 = algoCar.calcPath(0, 7); assertEquals(IntArrayList.from(new int[]{0, 1, 5, 6, 7}), p1.calcNodes()); assertEquals(p1.toString(), 26000, p1.getDistance(), 1e-6); }
@Test public void testTwoWeightsPerEdge() { FlagEncoder encoder = new Bike2WeightFlagEncoder(); EncodingManager em = new EncodingManager(encoder); AlgorithmOptions opts = AlgorithmOptions.start(). weighting(new FastestWeighting(encoder)).build(); GraphHopperStorage graph = createGHStorage(em, Arrays.asList(opts.getWeighting()), true); initEleGraph(graph); // force the other path GHUtility.getEdge(graph, 0, 3).setFlags(encoder.setProperties(10, false, true)); // for two weights per edge it happened that Path (and also the Weighting) read the wrong side // of the speed and read 0 => infinity weight => overflow of millis => negative millis! Path p = createAlgo(graph, opts). calcPath(0, 10); assertEquals(85124371, p.getTime()); assertEquals(425622, p.getDistance(), 1); assertEquals(85124.4, p.getWeight(), 1); }
/** * 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; }
@Test public void testCalcFastestPath() { GraphHopperStorage graphShortest = createGHStorage(false); initDirectedAndDiffSpeed(graphShortest, carEncoder); Path p1 = createAlgo(graphShortest, defaultOpts). calcPath(0, 3); assertEquals(IntArrayList.from(new int[]{0, 1, 5, 2, 3}), p1.calcNodes()); assertEquals(p1.toString(), 402.3, p1.getDistance(), .1); assertEquals(p1.toString(), 144823, p1.getTime()); AlgorithmOptions opts = AlgorithmOptions.start(). weighting(new FastestWeighting(carEncoder)).build(); GraphHopperStorage graphFastest = createGHStorage(encodingManager, Arrays.asList(opts.getWeighting()), false); initDirectedAndDiffSpeed(graphFastest, carEncoder); Path p2 = createAlgo(graphFastest, opts). calcPath(0, 3); assertEquals(IntArrayList.from(new int[]{0, 4, 6, 7, 5, 3}), p2.calcNodes()); assertEquals(p2.toString(), 1261.7, p2.getDistance(), 0.1); assertEquals(p2.toString(), 111442, p2.getTime()); }
@Test public void testBaseGraph() { CarFlagEncoder carFE = new CarFlagEncoder(); EncodingManager em = new EncodingManager(carFE); AlgorithmOptions opts = AlgorithmOptions.start(). weighting(new ShortestWeighting(carFE)).build(); GraphHopperStorage ghStorage = createGHStorage(em, Arrays.asList(opts.getWeighting()), false); initDirectedAndDiffSpeed(ghStorage, carFE); // do CH preparation for car createFactory(ghStorage, opts); // use base graph for solving normal Dijkstra Path p1 = new RoutingAlgorithmFactorySimple().createAlgo(ghStorage, defaultOpts).calcPath(0, 3); assertEquals(IntArrayList.from(0, 1, 5, 2, 3), p1.calcNodes()); assertEquals(p1.toString(), 402.29, p1.getDistance(), 1e-2); assertEquals(p1.toString(), 144823, p1.getTime()); }
protected AlgorithmOptions createAlgoOptions() { return AlgorithmOptions.start(). weighting(new ShortestWeighting(carEncoder)).build(); }
Path calcPathViaQuery(Weighting weighting, GraphHopperStorage ghStorage, double fromLat, double fromLon, double toLat, double toLon) { LocationIndex index = new LocationIndexTree(ghStorage, new RAMDirectory()); index.prepareIndex(); QueryResult from = index.findClosest(fromLat, fromLon, EdgeFilter.ALL_EDGES); QueryResult to = index.findClosest(toLat, toLon, EdgeFilter.ALL_EDGES); // correct order for CH: in factory do prepare and afterwards wrap in query graph AlgorithmOptions opts = AlgorithmOptions.start().weighting(weighting).build(); RoutingAlgorithmFactory factory = createFactory(ghStorage, opts); QueryGraph qGraph = new QueryGraph(getGraph(ghStorage, weighting)).lookup(from, to); return factory.createAlgo(qGraph, opts). calcPath(from.getClosestNode(), to.getClosestNode()); }
@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()); }