@Test public void testQueryGraphAndFastest() { Weighting weighting = new FastestWeighting(carEncoder); GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false); initDirectedAndDiffSpeed(graph, carEncoder); Path p = calcPathViaQuery(weighting, graph, 0.002, 0.0005, 0.0017, 0.0031); assertEquals(IntArrayList.from(new int[]{8, 1, 5, 3, 9}), p.calcNodes()); assertEquals(602.98, p.getDistance(), 1e-1); }
@Test public void testMaxVisitedNodes() { GraphHopperStorage graph = createGHStorage(false); initBiGraph(graph); RoutingAlgorithm algo = createAlgo(graph); Path p = algo.calcPath(0, 4); assertTrue(p.isFound()); algo = createAlgo(graph); algo.setMaxVisitedNodes(3); p = algo.calcPath(0, 4); assertFalse(p.isFound()); }
@Before public void setUp() { carEncoder = encodingManager.getEncoder("car"); footEncoder = encodingManager.getEncoder("foot"); defaultOpts = createAlgoOptions(); }
protected GraphHopperStorage createMatrixGraph() { return createMatrixAlikeGraph(createGHStorage(false)); } }
protected final RoutingAlgorithm createAlgo(GraphHopperStorage ghStorage, AlgorithmOptions opts) { return createFactory(ghStorage, opts).createAlgo(getGraph(ghStorage, opts.getWeighting()), opts); }
Path calcPath(GraphHopperStorage ghStorage, int fromNode1, int fromNode2, int toNode1, int toNode2) { // lookup two edges: fromNode1-fromNode2 and toNode1-toNode2 QueryResult from = newQR(ghStorage, fromNode1, fromNode2); QueryResult to = newQR(ghStorage, toNode1, toNode2); RoutingAlgorithmFactory factory = createFactory(ghStorage, defaultOpts); QueryGraph qGraph = new QueryGraph(getGraph(ghStorage, defaultOpts.getWeighting())).lookup(from, to); return factory.createAlgo(qGraph, defaultOpts).calcPath(from.getClosestNode(), to.getClosestNode()); }
GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(opts.getWeighting()), true); initEleGraph(graph); Path p = createAlgo(graph, opts).calcPath(0, 10); graph = createGHStorage(encodingManager, Arrays.asList(fakeOpts.getWeighting()), true); initEleGraph(graph); QueryResult from = newQR(graph, 3, 0); QueryResult to = newQR(graph, 10, 9); RoutingAlgorithmFactory factory = createFactory(graph, fakeOpts); QueryGraph qGraph = new QueryGraph(getGraph(graph, fakeWeighting)).lookup(from, to); p = factory.createAlgo(qGraph, fakeOpts).calcPath(from.getClosestNode(), to.getClosestNode()); assertEquals(IntArrayList.from(new int[]{12, 0, 1, 2, 11, 7, 10, 13}), p.calcNodes());
@Test public void testCannotCalculateSP() { GraphHopperStorage graph = createGHStorage(false); graph.edge(0, 1, 1, false); graph.edge(1, 2, 1, false); Path p = createAlgo(graph).calcPath(0, 2); assertEquals(p.toString(), 3, p.calcNodes().size()); }
@Test public void testRekeyBugOfIntBinHeap() { // using Dijkstra + IntBinHeap then rekey loops endlessly GraphHopperStorage matrixGraph = createMatrixGraph(); Path p = createAlgo(matrixGraph).calcPath(36, 91); assertEquals(12, p.calcNodes().size()); IntIndexedContainer list = p.calcNodes(); if (!IntArrayList.from(new int[]{36, 46, 56, 66, 76, 86, 85, 84, 94, 93, 92, 91}).equals(list) && !IntArrayList.from(new int[]{36, 46, 56, 66, 76, 86, 85, 84, 83, 82, 92, 91}).equals(list)) { assertTrue("wrong locations: " + list.toString(), false); } assertEquals(66f, p.getDistance(), 1e-3); testBug1(matrixGraph); testCorrectWeight(matrixGraph); }
@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 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); }
@Test public void testCalcIfEmptyWay() { Path p = createAlgo(createTestStorage()).calcPath(0, 0); assertEquals(p.calcNodes().toString(), 1, p.calcNodes().size()); assertEquals(p.toString(), 0, p.getDistance(), 1e-4); }
@Test public void testViaEdges_BiGraph() { GraphHopperStorage graph = createGHStorage(false); initBiGraph(graph); // 0-7 to 4-3 Path p = calcPathViaQuery(graph, 0.0009, 0, 0.001, 0.001105); assertEquals(p.toString(), IntArrayList.from(new int[]{10, 7, 6, 8, 3, 9}), p.calcNodes()); assertEquals(p.toString(), 324.11, p.getDistance(), 0.01); // 0-1 to 2-3 p = calcPathViaQuery(graph, 0.001, 0.0001, 0.010, 0.0011); assertEquals(p.toString(), IntArrayList.from(new int[]{0, 7, 6, 8, 3, 9}), p.calcNodes()); assertEquals(p.toString(), 1335.35, p.getDistance(), 0.01); }
@Test public void testViaEdges_SpecialCases() { GraphHopperStorage graph = createGHStorage(false); // 0->1\ // | 2 // 4<-3/ graph.edge(0, 1, 7, false); graph.edge(1, 2, 7, true); graph.edge(2, 3, 7, true); graph.edge(3, 4, 7, false); graph.edge(4, 0, 7, true); updateDistancesFor(graph, 4, 0, 0); updateDistancesFor(graph, 0, 0.00010, 0); updateDistancesFor(graph, 1, 0.00010, 0.0001); updateDistancesFor(graph, 2, 0.00005, 0.00015); updateDistancesFor(graph, 3, 0, 0.0001); // 0-1 to 3-4 Path p = calcPathViaQuery(graph, 0.00010, 0.00001, 0, 0.00009); assertEquals(IntArrayList.from(new int[]{5, 1, 2, 3, 6}), p.calcNodes()); assertEquals(p.toString(), 26.81, p.getDistance(), .1); // overlapping edges: 2-3 and 3-2 p = calcPathViaQuery(graph, 0.000049, 0.00014, 0.00001, 0.0001); assertEquals(IntArrayList.from(new int[]{5, 6}), p.calcNodes()); assertEquals(p.toString(), 6.2, p.getDistance(), .1); // 'from' and 'to' edge share one node '2': 1-2 to 3-2 p = calcPathViaQuery(graph, 0.00009, 0.00011, 0.00001, 0.00011); assertEquals(p.toString(), IntArrayList.from(new int[]{6, 2, 5}), p.calcNodes()); assertEquals(p.toString(), 12.57, p.getDistance(), .1); }
@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()); }
@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 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); }
@Test public void testViaEdges_FromEqualsTo() { GraphHopperStorage ghStorage = createTestStorage(); // identical tower nodes Path p = calcPathViaQuery(ghStorage, 0.001, 0.000, 0.001, 0.000); assertTrue(p.isFound()); assertEquals(IntArrayList.from(new int[]{0}), p.calcNodes()); // assertEquals(1, p.calcPoints().size()); assertEquals(p.toString(), 0, p.getDistance(), 1e-4); // identical query points on edge p = calcPath(ghStorage, 0, 1, 0, 1); assertTrue(p.isFound()); assertEquals(IntArrayList.from(new int[]{8}), p.calcNodes()); // assertEquals(1, p.calcPoints().size()); assertEquals(p.toString(), 0, p.getDistance(), 1e-4); // very close p = calcPathViaQuery(ghStorage, 0.00092, 0, 0.00091, 0); assertEquals(IntArrayList.from(new int[]{8, 9}), p.calcNodes()); assertEquals(p.toString(), 1.11, p.getDistance(), .1); }
protected GraphHopperStorage createTestStorage() { GraphHopperStorage graph = createGHStorage(false); graph.edge(0, 1, 7, true); graph.edge(0, 4, 6, true); graph.edge(1, 4, 2, true); graph.edge(1, 5, 8, true); graph.edge(1, 2, 2, true); graph.edge(2, 5, 5, true); graph.edge(2, 3, 2, true); graph.edge(3, 5, 2, true); graph.edge(3, 7, 10, true); graph.edge(4, 6, 4, true); graph.edge(4, 5, 7, true); graph.edge(5, 6, 2, true); graph.edge(5, 7, 1, true); EdgeIteratorState edge6_7 = graph.edge(6, 7, 5, true); updateDistancesFor(graph, 0, 0.0010, 0.00001); updateDistancesFor(graph, 1, 0.0008, 0.0000); updateDistancesFor(graph, 2, 0.0005, 0.0001); updateDistancesFor(graph, 3, 0.0006, 0.0002); updateDistancesFor(graph, 4, 0.0009, 0.0001); updateDistancesFor(graph, 5, 0.0007, 0.0001); updateDistancesFor(graph, 6, 0.0009, 0.0002); updateDistancesFor(graph, 7, 0.0008, 0.0003); edge6_7.setDistance(5 * edge6_7.getDistance()); return graph; }
@Test public void testViaEdges_WithCoordinates() { GraphHopperStorage ghStorage = createTestStorage(); Path p = calcPath(ghStorage, 0, 1, 2, 3); assertEquals(IntArrayList.from(new int[]{8, 1, 2, 9}), p.calcNodes()); assertEquals(p.toString(), 56.7, p.getDistance(), .1); }