Search in sources :

Example 1 with CHEdgeIteratorState

use of com.graphhopper.util.CHEdgeIteratorState in project graphhopper by graphhopper.

the class DijkstraBidirectionCHTest method testPathRecursiveUnpacking.

@Test
public void testPathRecursiveUnpacking() {
    // use an encoder where it is possible to store 2 weights per edge        
    FlagEncoder encoder = new Bike2WeightFlagEncoder();
    EncodingManager em = new EncodingManager(encoder);
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    GraphHopperStorage ghStorage = createGHStorage(em, Arrays.asList(weighting), false);
    CHGraphImpl g2 = (CHGraphImpl) ghStorage.getGraph(CHGraph.class, weighting);
    g2.edge(0, 1, 1, true);
    EdgeIteratorState iter1_1 = g2.edge(0, 2, 1.4, false);
    EdgeIteratorState iter1_2 = g2.edge(2, 5, 1.4, false);
    g2.edge(1, 2, 1, true);
    g2.edge(1, 3, 3, true);
    g2.edge(2, 3, 1, true);
    g2.edge(4, 3, 1, true);
    g2.edge(2, 5, 1.4, true);
    g2.edge(3, 5, 1, true);
    g2.edge(5, 6, 1, true);
    g2.edge(4, 6, 1, true);
    g2.edge(6, 7, 1, true);
    EdgeIteratorState iter2_2 = g2.edge(5, 7);
    iter2_2.setDistance(1.4).setFlags(encoder.setProperties(10, true, false));
    ghStorage.freeze();
    // simulate preparation
    CHEdgeIteratorState iter2_1 = g2.shortcut(0, 5);
    iter2_1.setDistance(2.8).setFlags(encoder.setProperties(10, true, false));
    iter2_1.setSkippedEdges(iter1_1.getEdge(), iter1_2.getEdge());
    CHEdgeIteratorState tmp = g2.shortcut(0, 7);
    tmp.setDistance(4.2).setFlags(encoder.setProperties(10, true, false));
    tmp.setSkippedEdges(iter2_1.getEdge(), iter2_2.getEdge());
    g2.setLevel(1, 0);
    g2.setLevel(3, 1);
    g2.setLevel(4, 2);
    g2.setLevel(6, 3);
    g2.setLevel(2, 4);
    g2.setLevel(5, 5);
    g2.setLevel(7, 6);
    g2.setLevel(0, 7);
    AlgorithmOptions opts = new AlgorithmOptions(Parameters.Algorithms.DIJKSTRA_BI, weighting);
    Path p = new PrepareContractionHierarchies(new GHDirectory("", DAType.RAM_INT), ghStorage, g2, weighting, TraversalMode.NODE_BASED).createAlgo(g2, opts).calcPath(0, 7);
    assertEquals(Helper.createTList(0, 2, 5, 7), p.calcNodes());
    assertEquals(1064, p.getTime());
    assertEquals(4.2, p.getDistance(), 1e-5);
}
Also used : ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) Test(org.junit.Test)

Example 2 with CHEdgeIteratorState

use of com.graphhopper.util.CHEdgeIteratorState in project graphhopper by graphhopper.

the class DijkstraBidirectionCHTest method testPathRecursiveUnpacking.

@Test
public void testPathRecursiveUnpacking() {
    // use an encoder where it is possible to store 2 weights per edge
    FlagEncoder encoder = new Bike2WeightFlagEncoder();
    EncodingManager em = new EncodingManager(encoder);
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    GraphHopperStorage ghStorage = createGHStorage(em, Arrays.asList(weighting), false);
    CHGraphImpl g2 = (CHGraphImpl) ghStorage.getGraph(CHGraph.class, weighting);
    g2.edge(0, 1, 1, true);
    EdgeIteratorState iter1_1 = g2.edge(0, 2, 1.4, false);
    EdgeIteratorState iter1_2 = g2.edge(2, 5, 1.4, false);
    g2.edge(1, 2, 1, true);
    g2.edge(1, 3, 3, true);
    g2.edge(2, 3, 1, true);
    g2.edge(4, 3, 1, true);
    g2.edge(2, 5, 1.4, true);
    g2.edge(3, 5, 1, true);
    g2.edge(5, 6, 1, true);
    g2.edge(4, 6, 1, true);
    g2.edge(6, 7, 1, true);
    EdgeIteratorState iter2_2 = g2.edge(5, 7);
    iter2_2.setDistance(1.4).setFlags(encoder.setProperties(10, true, false));
    ghStorage.freeze();
    // simulate preparation
    CHEdgeIteratorState iter2_1 = g2.shortcut(0, 5);
    iter2_1.setDistance(2.8).setFlags(encoder.setProperties(10, true, false));
    iter2_1.setSkippedEdges(iter1_1.getEdge(), iter1_2.getEdge());
    CHEdgeIteratorState tmp = g2.shortcut(0, 7);
    tmp.setDistance(4.2).setFlags(encoder.setProperties(10, true, false));
    tmp.setSkippedEdges(iter2_1.getEdge(), iter2_2.getEdge());
    g2.setLevel(1, 0);
    g2.setLevel(3, 1);
    g2.setLevel(4, 2);
    g2.setLevel(6, 3);
    g2.setLevel(2, 4);
    g2.setLevel(5, 5);
    g2.setLevel(7, 6);
    g2.setLevel(0, 7);
    AlgorithmOptions opts = new AlgorithmOptions(Parameters.Algorithms.DIJKSTRA_BI, weighting);
    Path p = new PrepareContractionHierarchies(new GHDirectory("", DAType.RAM_INT), ghStorage, g2, weighting, TraversalMode.NODE_BASED).createAlgo(g2, opts).calcPath(0, 7);
    assertEquals(Helper.createTList(0, 2, 5, 7), p.calcNodes());
    assertEquals(1064, p.getTime());
    assertEquals(4.2, p.getDistance(), 1e-5);
}
Also used : PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) Test(org.junit.Test)

Example 3 with CHEdgeIteratorState

use of com.graphhopper.util.CHEdgeIteratorState in project graphhopper by graphhopper.

the class Path4CH method expandEdge.

private void expandEdge(CHEdgeIteratorState mainEdgeState, boolean reverse) {
    if (!mainEdgeState.isShortcut()) {
        distance += mainEdgeState.getDistance();
        time += weighting.calcMillis(mainEdgeState, reverse, EdgeIterator.NO_EDGE);
        addEdge(mainEdgeState.getEdge());
        return;
    }
    int skippedEdge1 = mainEdgeState.getSkippedEdge1();
    int skippedEdge2 = mainEdgeState.getSkippedEdge2();
    int from = mainEdgeState.getBaseNode(), to = mainEdgeState.getAdjNode();
    // get properties like speed of the edge in the correct direction
    if (reverse) {
        int tmp = from;
        from = to;
        to = tmp;
    }
    // getEdgeProps could possibly return an empty edge if the shortcut is available for both directions
    if (reverseOrder) {
        CHEdgeIteratorState edgeState = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge1, to);
        boolean empty = edgeState == null;
        if (empty)
            edgeState = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge2, to);
        expandEdge(edgeState, false);
        if (empty)
            edgeState = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge1, from);
        else
            edgeState = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge2, from);
        expandEdge(edgeState, true);
    } else {
        CHEdgeIteratorState iter = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge1, from);
        boolean empty = iter == null;
        if (empty)
            iter = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge2, from);
        expandEdge(iter, true);
        if (empty)
            iter = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge1, to);
        else
            iter = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge2, to);
        expandEdge(iter, false);
    }
}
Also used : CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState)

Example 4 with CHEdgeIteratorState

use of com.graphhopper.util.CHEdgeIteratorState in project graphhopper by graphhopper.

the class LocationIndexTreeCHTest method testCHGraph.

@Test
public void testCHGraph() {
    GraphHopperStorage ghStorage = createGHStorage(new RAMDirectory(), encodingManager, false);
    CHGraph lg = ghStorage.getGraph(CHGraph.class);
    // 0
    // 1
    // 2
    // 3
    // 4
    NodeAccess na = ghStorage.getNodeAccess();
    na.setNode(0, 1, 0);
    na.setNode(1, 0.5, 0);
    na.setNode(2, 0, 0);
    na.setNode(3, -1, 1);
    na.setNode(4, -2, 2);
    EdgeIteratorState iter1 = ghStorage.edge(0, 1, 10, true);
    EdgeIteratorState iter2 = ghStorage.edge(1, 2, 10, true);
    EdgeIteratorState iter3 = ghStorage.edge(2, 3, 14, true);
    EdgeIteratorState iter4 = ghStorage.edge(3, 4, 14, true);
    // create shortcuts
    ghStorage.freeze();
    FlagEncoder car = encodingManager.getEncoder("car");
    long flags = car.setProperties(60, true, true);
    CHEdgeIteratorState iter5 = lg.shortcut(0, 2);
    iter5.setDistance(20).setFlags(flags);
    iter5.setSkippedEdges(iter1.getEdge(), iter2.getEdge());
    CHEdgeIteratorState iter6 = lg.shortcut(2, 4);
    iter6.setDistance(28).setFlags(flags);
    iter6.setSkippedEdges(iter3.getEdge(), iter4.getEdge());
    CHEdgeIteratorState tmp = lg.shortcut(0, 4);
    tmp.setDistance(40).setFlags(flags);
    tmp.setSkippedEdges(iter5.getEdge(), iter6.getEdge());
    LocationIndex index = createIndex(ghStorage, -1);
    assertEquals(2, findID(index, 0, 0.5));
}
Also used : CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) Test(org.junit.Test)

Example 5 with CHEdgeIteratorState

use of com.graphhopper.util.CHEdgeIteratorState in project graphhopper by graphhopper.

the class NodeContractorTest method testFindShortcuts_Roundabout.

@Test
public void testFindShortcuts_Roundabout() {
    // 1 -- 3 -- 4 ---> 5 ---> 6 -- 7
    // \           /
    // <--- 8 <---
    final EdgeIteratorState iter1to3 = graph.edge(1, 3, 1, true);
    final EdgeIteratorState iter3to4 = graph.edge(3, 4, 1, true);
    final EdgeIteratorState iter4to5 = graph.edge(4, 5, 1, false);
    final EdgeIteratorState iter5to6 = graph.edge(5, 6, 1, false);
    final EdgeIteratorState iter6to8 = graph.edge(6, 8, 2, false);
    final EdgeIteratorState iter8to4 = graph.edge(8, 4, 1, false);
    graph.edge(6, 7, 1, true);
    graph.freeze();
    CHEdgeIteratorState sc1to4 = lg.shortcut(1, 4);
    sc1to4.setFlags(PrepareEncoder.getScDirMask());
    sc1to4.setWeight(2);
    sc1to4.setDistance(2);
    sc1to4.setSkippedEdges(iter1to3.getEdge(), iter3to4.getEdge());
    long f = PrepareEncoder.getScFwdDir();
    CHEdgeIteratorState sc4to6 = lg.shortcut(4, 6);
    sc4to6.setFlags(f);
    sc4to6.setWeight(2);
    sc4to6.setDistance(2);
    sc4to6.setSkippedEdges(iter4to5.getEdge(), iter5to6.getEdge());
    CHEdgeIteratorState sc6to4 = lg.shortcut(6, 4);
    sc6to4.setFlags(f);
    sc6to4.setWeight(3);
    sc6to4.setDistance(3);
    sc6to4.setSkippedEdges(iter6to8.getEdge(), iter8to4.getEdge());
    setMaxLevelOnAllNodes();
    lg.setLevel(3, 3);
    lg.setLevel(5, 5);
    lg.setLevel(7, 7);
    lg.setLevel(8, 8);
    Shortcut manualSc1 = expectedShortcut(1, 4, iter1to3, iter3to4, true, true);
    Shortcut manualSc2 = expectedShortcut(4, 6, iter4to5, iter5to6, true, false);
    Shortcut manualSc3 = expectedShortcut(4, 6, iter6to8, iter8to4, false, true);
    checkShortcuts(manualSc1, manualSc2, manualSc3);
    // after 'manual contraction' of nodes 3, 5, 8 the graph looks like:
    // 1 -- 4 -->-- 6 -- 7
    // \      |
    // --<----
    // contract node 4!
    NodeContractor nodeContractor = createNodeContractor();
    nodeContractor.contractNode(4);
    checkShortcuts(manualSc1, manualSc2, manualSc3, // there should be two different shortcuts for both directions!
    expectedShortcut(1, 6, sc1to4, sc4to6, true, false), expectedShortcut(1, 6, sc6to4, sc1to4, false, true));
}
Also used : CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) Test(org.junit.Test)

Aggregations

CHEdgeIteratorState (com.graphhopper.util.CHEdgeIteratorState)5 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)4 Test (org.junit.Test)4 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)2 PrepareContractionHierarchies (com.graphhopper.routing.ch.PrepareContractionHierarchies)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1