Search in sources :

Example 1 with DijkstraBidirectionCH

use of com.graphhopper.routing.DijkstraBidirectionCH in project graphhopper by graphhopper.

the class NodeBasedNodeContractorTest method testNodeContraction_shortcutWeightRounding.

/**
 * similar to the previous test, but using the fastest weighting
 */
@Test
public void testNodeContraction_shortcutWeightRounding() {
    CarFlagEncoder encoder = new CarFlagEncoder();
    EncodingManager encodingManager = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(encodingManager).create();
    // 0 ------------> 4
    // \             /
    // 1 --> 2 --> 3
    double fac = 60 / 3.6;
    double[] distances = { fac * 4.019, fac * 1.006, fac * 1.004, fac * 1.006, fac * 1.004 };
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 4).setDistance(distances[0]));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 1).setDistance(distances[1]));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(distances[2]));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 3).setDistance(distances[3]));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 4).setDistance(distances[4]));
    graph.freeze();
    Weighting weighting = new FastestWeighting(encoder);
    CHConfig chConfig = CHConfig.nodeBased("p1", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    setMaxLevelOnAllNodes(chStore);
    // perform CH contraction
    contractInOrder(graph, chStore, chConfig, 1, 3, 2, 0, 4);
    // first we compare dijkstra with CH to make sure they produce the same results
    int from = 0;
    int to = 4;
    Dijkstra dikstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);
    Path dijkstraPath = dikstra.calcPath(from, to);
    DijkstraBidirectionCH ch = new DijkstraBidirectionCH(graph.createCHGraph(chStore, chConfig));
    Path chPath = ch.calcPath(from, to);
    assertEquals(dijkstraPath.calcNodes(), chPath.calcNodes());
    assertEquals(dijkstraPath.getDistance(), chPath.getDistance(), 1.e-6);
    assertEquals(dijkstraPath.getWeight(), chPath.getWeight(), 1.e-6);
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) Path(com.graphhopper.routing.Path) DijkstraBidirectionCH(com.graphhopper.routing.DijkstraBidirectionCH) Dijkstra(com.graphhopper.routing.Dijkstra) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with DijkstraBidirectionCH

use of com.graphhopper.routing.DijkstraBidirectionCH in project graphhopper by graphhopper.

the class NodeBasedNodeContractorTest method testNodeContraction_shortcutDistanceRounding.

@Test
public void testNodeContraction_shortcutDistanceRounding() {
    assertTrue(weighting instanceof ShortestWeighting, "this test was constructed assuming we are using the ShortestWeighting");
    // 0 ------------> 4
    // \             /
    // 1 --> 2 --> 3
    double[] distances = { 4.019, 1.006, 1.004, 1.006, 1.004 };
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 4).setDistance(distances[0]));
    EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 1).setDistance(distances[1]));
    EdgeIteratorState edge2 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(distances[2]));
    EdgeIteratorState edge3 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 3).setDistance(distances[3]));
    EdgeIteratorState edge4 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 4).setDistance(distances[4]));
    freeze();
    setMaxLevelOnAllNodes();
    // make sure that distances do not get changed in storage (they might get truncated)
    AllEdgesIterator iter = graph.getAllEdges();
    double[] storedDistances = new double[iter.length()];
    int count = 0;
    while (iter.next()) {
        storedDistances[count++] = iter.getDistance();
    }
    assertArrayEquals(distances, storedDistances, 1.e-6);
    // perform CH contraction
    contractInOrder(1, 3, 2, 0, 4);
    // first we compare dijkstra with CH to make sure they produce the same results
    int from = 0;
    int to = 4;
    Dijkstra dikstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);
    Path dijkstraPath = dikstra.calcPath(from, to);
    RoutingCHGraph lg = graph.createCHGraph(store, chConfig);
    DijkstraBidirectionCH ch = new DijkstraBidirectionCH(lg);
    Path chPath = ch.calcPath(from, to);
    assertEquals(dijkstraPath.calcNodes(), chPath.calcNodes());
    assertEquals(dijkstraPath.getDistance(), chPath.getDistance(), 1.e-6);
    assertEquals(dijkstraPath.getWeight(), chPath.getWeight(), 1.e-6);
    // on a more detailed level we check that the right shortcuts were added
    // contracting nodes 1&3 will always introduce shortcuts, but contracting node 2 should not because going from
    // 0 to 4 directly via edge 4 is cheaper. however, if shortcut distances get truncated it appears as if going
    // via node 2 is better. here we check that this does not happen.
    checkShortcuts(expectedShortcut(2, 0, edge2, edge1, false, true), expectedShortcut(2, 4, edge3, edge4, true, false));
}
Also used : Path(com.graphhopper.routing.Path) AllEdgesIterator(com.graphhopper.routing.util.AllEdgesIterator) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) DijkstraBidirectionCH(com.graphhopper.routing.DijkstraBidirectionCH) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Dijkstra(com.graphhopper.routing.Dijkstra) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Dijkstra (com.graphhopper.routing.Dijkstra)2 DijkstraBidirectionCH (com.graphhopper.routing.DijkstraBidirectionCH)2 Path (com.graphhopper.routing.Path)2 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 AllEdgesIterator (com.graphhopper.routing.util.AllEdgesIterator)1 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)1 EncodingManager (com.graphhopper.routing.util.EncodingManager)1 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)1 Weighting (com.graphhopper.routing.weighting.Weighting)1 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)1