private PrepareContractionHierarchies createPrepareContractionHierarchies(GraphHopperStorage g, CHGraph lg, Weighting w) { g.freeze(); return new PrepareContractionHierarchies(lg, w, tMode); }
/** * For landmarks it is required to always call this method: either it creates the landmark data or it loads it. */ protected void loadOrPrepareLM() { boolean tmpPrepare = lmFactoryDecorator.isEnabled() && !lmFactoryDecorator.getPreparations().isEmpty(); if (tmpPrepare) { ensureWriteAccess(); ghStorage.freeze(); if (lmFactoryDecorator.loadOrDoWork(ghStorage.getProperties())) ghStorage.getProperties().put(Landmark.PREPARE + "done", true); } }
protected void prepareCH() { boolean tmpPrepare = chFactoryDecorator.isEnabled(); if (tmpPrepare) { ensureWriteAccess(); ghStorage.freeze(); chFactoryDecorator.prepare(ghStorage.getProperties()); ghStorage.getProperties().put(CH.PREPARE + "done", true); } }
@Override public RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, AlgorithmOptions opts) { ghStorage.freeze(); PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage( ghStorage, opts.getWeighting(), TraversalMode.NODE_BASED); ch.doWork(); return ch; }
@Test public void testContractNode_directed_shortcutRequired() { // 0 --> 1 --> 2 final EdgeIteratorState edge1 = graph.edge(0, 1, 1, false); final EdgeIteratorState edge2 = graph.edge(1, 2, 2, false); graph.freeze(); setMaxLevelOnAllNodes(); createNodeContractor().contractNode(1); checkShortcuts(expectedShortcut(0, 2, edge1, edge2, true, false)); }
@Test public void testContractNode_bidirected_shortcutsRequired() { // 0 -- 1 -- 2 final EdgeIteratorState edge1 = graph.edge(0, 1, 1, true); final EdgeIteratorState edge2 = graph.edge(1, 2, 2, true); graph.freeze(); setMaxLevelOnAllNodes(); createNodeContractor().contractNode(1); checkShortcuts(expectedShortcut(0, 2, edge2, edge1, true, true)); }
private void createExampleGraph() { //5-1-----2 // \ __/| // 0 | // / | // 4-----3 // graph.edge(0, 1, 1, true); graph.edge(0, 2, 1, true); graph.edge(0, 4, 3, true); graph.edge(1, 2, 3, true); graph.edge(2, 3, 1, true); graph.edge(4, 3, 2, true); graph.edge(5, 1, 2, true); graph.freeze(); }
@Test public void testContractNode_directed_withWitness() { // 0 --> 1 --> 2 // \_________/ graph.edge(0, 1, 1, false); graph.edge(1, 2, 2, false); graph.edge(0, 2, 1, false); graph.freeze(); setMaxLevelOnAllNodes(); createNodeContractor().contractNode(1); checkNoShortcuts(); }
@Test public void testContractNode_directed_shortcutRequired_reverse() { // 0 <-- 1 <-- 2 final EdgeIteratorState edge1 = graph.edge(2, 1, 1, false); final EdgeIteratorState edge2 = graph.edge(1, 0, 2, false); graph.freeze(); setMaxLevelOnAllNodes(); createNodeContractor().contractNode(1); checkShortcuts(expectedShortcut(0, 2, edge1, edge2, false, true)); }
@Test public void testMultiplePreparationsIdenticalView() { CarFlagEncoder tmpCarEncoder = new CarFlagEncoder(); BikeFlagEncoder tmpBikeEncoder = new BikeFlagEncoder(); EncodingManager tmpEncodingManager = new EncodingManager(tmpCarEncoder, tmpBikeEncoder); // FastestWeighting would lead to different shortcuts due to different default speeds for bike and car Weighting carWeighting = new ShortestWeighting(tmpCarEncoder); Weighting bikeWeighting = new ShortestWeighting(tmpBikeEncoder); List<Weighting> chWeightings = Arrays.asList(carWeighting, bikeWeighting); GraphHopperStorage ghStorage = new GraphHopperStorage(chWeightings, dir, tmpEncodingManager, false, new GraphExtension.NoOpExtension()).create(1000); initShortcutsGraph(ghStorage); ghStorage.freeze(); for (Weighting w : chWeightings) { checkPath(ghStorage, w, 7, 5, IntArrayList.from(3, 9, 14, 16, 13, 12)); } }
@Test public void testPrios() { graph = createGHStorage(); CHGraph g = getGraph(graph); g.getNodeAccess().ensureNode(30); graph.freeze(); assertEquals(0, g.getLevel(10)); g.setLevel(10, 100); assertEquals(100, g.getLevel(10)); g.setLevel(30, 100); assertEquals(100, g.getLevel(30)); }
@Test public void testShortcutMergeBug() { // We refer to this real world situation http://www.openstreetmap.org/#map=19/52.71205/-1.77326 // assume the following graph: // // ---1---->----2-----3 // \--------/ // // where there are two roads from 1 to 2 and the directed road has a smaller weight // leading to two shortcuts sc1 (unidir) and sc2 (bidir) where the second should NOT be rejected due to the larger weight final EdgeIteratorState edge1to2bidirected = graph.edge(1, 2, 1, true); final EdgeIteratorState edge1to2directed = graph.edge(1, 2, 1, false); final EdgeIteratorState edge2to3 = graph.edge(2, 3, 1, true); graph.freeze(); setMaxLevelOnAllNodes(); NodeContractor nodeContractor = createNodeContractor(); nodeContractor.contractNode(2); checkShortcuts( expectedShortcut(1, 3, edge2to3, edge1to2bidirected, false, true), expectedShortcut(1, 3, edge1to2directed, edge2to3, true, false) ); }
@Test public void testSave_and_Freeze() throws IOException { graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), true).create(defaultSize); graph.edge(1, 0); graph.freeze(); graph.flush(); graph.close(); graph = newGHStorage(new MMapDirectory(defaultGraphLoc), true); assertTrue(graph.loadExisting()); assertEquals(2, graph.getNodes()); assertTrue(graph.isFrozen()); }
@Test public void testMultiplePreparationsDifferentView() { CarFlagEncoder tmpCarEncoder = new CarFlagEncoder(); BikeFlagEncoder tmpBikeEncoder = new BikeFlagEncoder(); EncodingManager tmpEncodingManager = new EncodingManager(tmpCarEncoder, tmpBikeEncoder); Weighting carWeighting = new FastestWeighting(tmpCarEncoder); Weighting bikeWeighting = new FastestWeighting(tmpBikeEncoder); List<Weighting> chWeightings = Arrays.asList(carWeighting, bikeWeighting); GraphHopperStorage ghStorage = new GraphHopperStorage(chWeightings, dir, tmpEncodingManager, false, new GraphExtension.NoOpExtension()).create(1000); initShortcutsGraph(ghStorage); EdgeIteratorState edge = GHUtility.getEdge(ghStorage, 9, 14); edge.setFlags(tmpBikeEncoder.setAccess(edge.getFlags(), false, false)); ghStorage.freeze(); checkPath(ghStorage, carWeighting, 7, 5, IntArrayList.from(3, 9, 14, 16, 13, 12)); // detour around blocked 9,14 checkPath(ghStorage, bikeWeighting, 9, 5, IntArrayList.from(3, 10, 14, 16, 13, 12)); }
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 testSimpleShortcutCreationAndTraversal() { graph = createGHStorage(); graph.edge(1, 3, 10, true); graph.edge(3, 4, 10, true); graph.freeze(); CHGraph lg = graph.getGraph(CHGraph.class); lg.shortcut(1, 4).setWeight(3).setFlags(carEncoder.setProperties(10, true, true)); EdgeExplorer vehicleOutExplorer = lg.createEdgeExplorer(DefaultEdgeFilter.outEdges(carEncoder)); // iteration should result in same nodes even if reusing the iterator assertEquals(GHUtility.asSet(3, 4), GHUtility.getNeighbors(vehicleOutExplorer.setBaseNode(1))); assertEquals(GHUtility.asSet(3, 4), GHUtility.getNeighbors(vehicleOutExplorer.setBaseNode(1))); }
@Test public void testAddShortcutSkippedEdgesWriteRead_writeWithCHEdgeIterator() { graph = createGHStorage(); final EdgeIteratorState edge1 = graph.edge(1, 3, 10, true); final EdgeIteratorState edge2 = graph.edge(3, 4, 10, true); graph.freeze(); CHGraph lg = graph.getGraph(CHGraph.class); CHEdgeIteratorState shortcut = lg.shortcut(1, 4); shortcut.setSkippedEdges(edge1.getEdge(), edge2.getEdge()); AllCHEdgesIterator iter = lg.getAllEdges(); iter.next(); iter.next(); iter.next(); assertTrue(iter.isShortcut()); assertEquals(edge1.getEdge(), iter.getSkippedEdge1()); assertEquals(edge2.getEdge(), iter.getSkippedEdge2()); }
@Test public void testAddShortcutSkippedEdgesWriteRead() { graph = createGHStorage(); final EdgeIteratorState edge1 = graph.edge(1, 3, 10, true); final EdgeIteratorState edge2 = graph.edge(3, 4, 10, true); graph.freeze(); CHGraph lg = graph.getGraph(CHGraph.class); lg.shortcut(1, 4); AllCHEdgesIterator iter = lg.getAllEdges(); iter.next(); iter.next(); iter.next(); assertTrue(iter.isShortcut()); iter.setSkippedEdges(edge1.getEdge(), edge2.getEdge()); assertEquals(edge1.getEdge(), iter.getSkippedEdge1()); assertEquals(edge2.getEdge(), iter.getSkippedEdge2()); }
@Test public void testDirectedGraph() { GraphHopperStorage g = createGHStorage(); CHGraph lg = g.getGraph(CHGraph.class); g.edge(5, 4, 3, false); g.edge(4, 5, 10, false); g.edge(2, 4, 1, false); g.edge(5, 2, 1, false); g.edge(3, 5, 1, false); g.edge(4, 3, 1, false); g.freeze(); int oldCount = GHUtility.count(lg.getAllEdges()); assertEquals(6, oldCount); PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g, lg); prepare.doWork(); assertEquals(2, prepare.getShortcuts()); assertEquals(oldCount + 2, GHUtility.count(lg.getAllEdges())); RoutingAlgorithm algo = prepare.createAlgo(lg, new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode)); Path p = algo.calcPath(4, 2); assertEquals(3, p.getDistance(), 1e-6); assertEquals(IntArrayList.from(4, 3, 5, 2), p.calcNodes()); }
@Test public void testGetWeightIfAdvancedEncoder() { FlagEncoder customEncoder = new Bike2WeightFlagEncoder(); EncodingManager em = new EncodingManager(customEncoder); FastestWeighting weighting = new FastestWeighting(customEncoder); GraphHopperStorage ghStorage = new GraphBuilder(em).setCHGraph(weighting).create(); ghStorage.edge(0, 2); ghStorage.freeze(); CHGraphImpl lg = (CHGraphImpl) ghStorage.getGraph(CHGraph.class, weighting); CHEdgeIteratorState sc1 = lg.shortcut(0, 1); long flags = customEncoder.setProperties(10, false, true); sc1.setFlags(flags); sc1.setWeight(100.123); assertEquals(100.123, lg.getEdgeIteratorState(sc1.getEdge(), sc1.getAdjNode()).getWeight(), 1e-3); assertEquals(100.123, lg.getEdgeIteratorState(sc1.getEdge(), sc1.getBaseNode()).getWeight(), 1e-3); assertEquals(100.123, ((CHEdgeIteratorState) GHUtility.getEdge(lg, sc1.getBaseNode(), sc1.getAdjNode())).getWeight(), 1e-3); assertEquals(100.123, ((CHEdgeIteratorState) GHUtility.getEdge(lg, sc1.getAdjNode(), sc1.getBaseNode())).getWeight(), 1e-3); sc1 = lg.shortcut(1, 0); assertTrue(sc1.isShortcut()); sc1.setFlags(PrepareEncoder.getScDirMask()); sc1.setWeight(1.011011); assertEquals(1.011011, sc1.getWeight(), 1e-3); }