Search in sources :

Example 11 with QueryRoutingCHGraph

use of com.graphhopper.routing.querygraph.QueryRoutingCHGraph in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method getWeighting.

@Test
public void getWeighting() {
    graph.freeze();
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.<Snap>emptyList());
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    // maybe query CH graph should return query graph weighting instead?
    assertSame(weighting, queryCHGraph.getWeighting());
}
Also used : QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 12 with QueryRoutingCHGraph

use of com.graphhopper.routing.querygraph.QueryRoutingCHGraph in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method withShortcuts.

@Test
public void withShortcuts() {
    // 0-1-2
    // \-/
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(10));
    graph.freeze();
    assertEquals(2, graph.getEdges());
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    CHStorageBuilder chBuilder = new CHStorageBuilder(chStore);
    chBuilder.setIdentityLevels();
    chBuilder.addShortcutEdgeBased(0, 2, PrepareEncoder.getScFwdDir(), 20, 0, 1, 0, 1);
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.emptyList());
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    assertEquals(3, queryCHGraph.getNodes());
    assertEquals(3, queryCHGraph.getEdges());
    assertNodesConnected(queryCHGraph, 0, 1, true);
    assertNodesConnected(queryCHGraph, 1, 2, true);
    // the shortcut 0-2 is not visible from node 2
    // assertNodesConnected(queryCHGraph, 0, 2, false);
    RoutingCHEdgeIterator outIter = queryCHGraph.createOutEdgeExplorer().setBaseNode(0);
    assertNextShortcut(outIter, 0, 2, 0, 1);
    assertNextEdge(outIter, 0, 1, 0);
    assertEnd(outIter);
    RoutingCHEdgeIterator inIter = queryCHGraph.createInEdgeExplorer().setBaseNode(2);
    assertNextEdge(inIter, 2, 1, 1);
    assertEnd(inIter);
}
Also used : QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 13 with QueryRoutingCHGraph

use of com.graphhopper.routing.querygraph.QueryRoutingCHGraph in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method getWeight_withAccess.

@Test
public void getWeight_withAccess() {
    na.setNode(0, 50.00, 10.00);
    na.setNode(1, 50.00, 10.10);
    double dist = DistancePlaneProjection.DIST_PLANE.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1));
    EdgeIteratorState edge = graph.edge(0, 1).setDistance(dist);
    // we set the access flags, but do use direction dependent speeds to make sure we are testing whether or not the
    // access flags are respected and the weight calculation does not simply rely on the speed, see this forum issue
    // https://discuss.graphhopper.com/t/speed-and-access-when-setbothdirections-true-false/5695
    edge.set(encoder.getAccessEnc(), true, false);
    edge.set(encoder.getAverageSpeedEnc(), 60, 60);
    graph.freeze();
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    // without query graph
    // 0->1
    RoutingCHEdgeIterator iter = routingCHGraph.createOutEdgeExplorer().setBaseNode(0);
    assertNextEdge(iter, 0, 1, 0);
    assertEquals(428.8483, iter.getWeight(false), 1.e-4);
    assertEquals(Double.POSITIVE_INFINITY, iter.getWeight(true));
    assertEnd(iter);
    iter = routingCHGraph.createInEdgeExplorer().setBaseNode(1);
    assertNextEdge(iter, 1, 0, 0);
    assertEquals(Double.POSITIVE_INFINITY, iter.getWeight(false));
    assertEquals(428.8483, iter.getWeight(true), 1.e-4);
    assertEnd(iter);
    // single edges
    assertEquals(428.8483, routingCHGraph.getEdgeIteratorState(0, 1).getWeight(false), 1.e-4);
    assertEquals(Double.POSITIVE_INFINITY, routingCHGraph.getEdgeIteratorState(0, 1).getWeight(true));
    assertEquals(Double.POSITIVE_INFINITY, routingCHGraph.getEdgeIteratorState(0, 0).getWeight(false));
    assertEquals(428.8483, routingCHGraph.getEdgeIteratorState(0, 0).getWeight(true), 1.e-4);
    // with query graph
    // 0-x->1
    // 2
    Snap snap = new Snap(50.00, 10.05);
    snap.setClosestEdge(edge);
    snap.setWayIndex(0);
    snap.setSnappedPosition(Snap.Position.EDGE);
    snap.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.singletonList(snap));
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    iter = queryCHGraph.createOutEdgeExplorer().setBaseNode(0);
    assertNextEdge(iter, 0, 2, 1);
    assertEquals(214.4241, iter.getWeight(false), 1.e-4);
    assertEquals(Double.POSITIVE_INFINITY, iter.getWeight(true));
    assertEnd(iter);
    iter = queryCHGraph.createInEdgeExplorer().setBaseNode(1);
    assertNextEdge(iter, 1, 2, 2);
    assertEquals(Double.POSITIVE_INFINITY, iter.getWeight(false));
    assertEquals(214.4241, iter.getWeight(true), 1.e-4);
    assertEnd(iter);
    // at virtual node
    iter = queryCHGraph.createInEdgeExplorer().setBaseNode(2);
    assertNextEdge(iter, 2, 0, 1);
    assertEquals(Double.POSITIVE_INFINITY, iter.getWeight(false));
    assertEquals(214.4241, iter.getWeight(true), 1.e-4);
    assertNextEdge(iter, 2, 1, 2);
    assertEquals(214.4241, iter.getWeight(false), 1.e-4);
    assertEquals(Double.POSITIVE_INFINITY, iter.getWeight(true));
    assertEnd(iter);
    // single edges
    assertEquals(214.4241, queryCHGraph.getEdgeIteratorState(1, 2).getWeight(false), 1.e-4);
    assertEquals(Double.POSITIVE_INFINITY, queryCHGraph.getEdgeIteratorState(1, 2).getWeight(true));
    assertEquals(Double.POSITIVE_INFINITY, queryCHGraph.getEdgeIteratorState(1, 0).getWeight(false));
    assertEquals(214.4241, queryCHGraph.getEdgeIteratorState(1, 0).getWeight(true), 1.e-4);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 14 with QueryRoutingCHGraph

use of com.graphhopper.routing.querygraph.QueryRoutingCHGraph in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method getTurnCost.

@Test
public void getTurnCost() {
    // /-----\
    // 0-x-1-x-2
    // 3   4
    na.setNode(0, 50.00, 10.00);
    na.setNode(1, 50.00, 10.10);
    na.setNode(2, 50.00, 10.20);
    EdgeIteratorState edge1 = addEdge(graph, 0, 1);
    EdgeIteratorState edge2 = addEdge(graph, 1, 2);
    DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(encoder.toString()));
    graph.getTurnCostStorage().set(turnCostEnc, 0, 1, 1, 5);
    graph.freeze();
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    CHStorageBuilder chBuilder = new CHStorageBuilder(chStore);
    chBuilder.setIdentityLevels();
    chBuilder.addShortcutEdgeBased(0, 2, PrepareEncoder.getScFwdDir(), 20, 0, 1, 0, 1);
    // without virtual nodes
    assertEquals(5, routingCHGraph.getTurnWeight(0, 1, 1));
    // with virtual nodes
    Snap snap1 = new Snap(50.00, 10.05);
    snap1.setClosestEdge(edge1);
    snap1.setWayIndex(0);
    snap1.setSnappedPosition(Snap.Position.EDGE);
    snap1.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    Snap snap2 = new Snap(50.00, 10.15);
    snap2.setClosestEdge(edge2);
    snap2.setWayIndex(0);
    snap2.setSnappedPosition(Snap.Position.EDGE);
    snap2.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    QueryGraph queryGraph = QueryGraph.create(graph, Arrays.asList(snap1, snap2));
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    assertEquals(5, queryCHGraph.getTurnWeight(0, 1, 1));
    // take a look at edges 3->1 and 1->4, their original edge ids are 3 and 4 (not 4 and 5)
    assertNodesConnected(queryCHGraph, 3, 1, true);
    assertNodesConnected(queryCHGraph, 1, 4, true);
    int expectedEdge31 = 3;
    int expectedEdge14 = 4;
    RoutingCHEdgeIterator iter = queryCHGraph.createOutEdgeExplorer().setBaseNode(3);
    assertNextEdge(iter, 3, 0, 2);
    assertNextEdge(iter, 3, 1, expectedEdge31);
    assertEnd(iter);
    iter = queryCHGraph.createOutEdgeExplorer().setBaseNode(1);
    assertNextEdge(iter, 1, 3, 3);
    assertNextEdge(iter, 1, 4, expectedEdge14);
    assertEnd(iter);
    // check the turn weight between these edges
    assertEquals(5, queryCHGraph.getTurnWeight(expectedEdge31, 1, expectedEdge14));
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 15 with QueryRoutingCHGraph

use of com.graphhopper.routing.querygraph.QueryRoutingCHGraph in project graphhopper by graphhopper.

the class RandomCHRoutingTest method runRandomTest.

private void runRandomTest(Fixture f, Random rnd, int numVirtualNodes) {
    LocationIndexTree locationIndex = new LocationIndexTree(f.graph, f.dir);
    locationIndex.prepareIndex();
    f.freeze();
    PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(f.graph, f.chConfig);
    PrepareContractionHierarchies.Result res = pch.doWork();
    RoutingCHGraph chGraph = f.graph.createCHGraph(res.getCHStorage(), res.getCHConfig());
    int numQueryGraph = 25;
    for (int j = 0; j < numQueryGraph; j++) {
        // add virtual nodes and edges, because they can change the routing behavior and/or produce bugs, e.g.
        // when via-points are used
        List<Snap> snaps = createRandomSnaps(f.graph.getBounds(), locationIndex, rnd, numVirtualNodes, false, EdgeFilter.ALL_EDGES);
        QueryGraph queryGraph = QueryGraph.create(f.graph, snaps);
        int numQueries = 100;
        int numPathsNotFound = 0;
        List<String> strictViolations = new ArrayList<>();
        for (int i = 0; i < numQueries; i++) {
            int from = rnd.nextInt(queryGraph.getNodes());
            int to = rnd.nextInt(queryGraph.getNodes());
            Weighting w = queryGraph.wrapWeighting(f.weighting);
            // using plain dijkstra instead of bidirectional, because of #1592
            RoutingAlgorithm refAlgo = new Dijkstra(queryGraph, w, f.traversalMode);
            Path refPath = refAlgo.calcPath(from, to);
            double refWeight = refPath.getWeight();
            QueryRoutingCHGraph routingCHGraph = new QueryRoutingCHGraph(chGraph, queryGraph);
            RoutingAlgorithm algo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap().putObject("stall_on_demand", true));
            Path path = algo.calcPath(from, to);
            if (refPath.isFound() && !path.isFound())
                fail("path not found for " + from + "->" + to + ", expected weight: " + refWeight);
            assertEquals(refPath.isFound(), path.isFound());
            if (!path.isFound()) {
                numPathsNotFound++;
                continue;
            }
            double weight = path.getWeight();
            if (Math.abs(refWeight - weight) > 1.e-2) {
                LOGGER.warn("expected: " + refPath.calcNodes());
                LOGGER.warn("given:    " + path.calcNodes());
                fail("wrong weight: " + from + "->" + to + ", dijkstra: " + refWeight + " vs. ch: " + path.getWeight());
            }
            if (Math.abs(path.getDistance() - refPath.getDistance()) > 1.e-1) {
                strictViolations.add("wrong distance " + from + "->" + to + ", expected: " + refPath.getDistance() + ", given: " + path.getDistance());
            }
            if (Math.abs(path.getTime() - refPath.getTime()) > 50) {
                strictViolations.add("wrong time " + from + "->" + to + ", expected: " + refPath.getTime() + ", given: " + path.getTime());
            }
        }
        if (numPathsNotFound > 0.9 * numQueries) {
            fail("Too many paths not found: " + numPathsNotFound + "/" + numQueries);
        }
        if (strictViolations.size() > 0.05 * numQueries) {
            fail("Too many strict violations: " + strictViolations.size() + "/" + numQueries + "\n" + Helper.join("\n", strictViolations));
        }
    }
}
Also used : PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) ArrayList(java.util.ArrayList) PMap(com.graphhopper.util.PMap) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) CHRoutingAlgorithmFactory(com.graphhopper.routing.ch.CHRoutingAlgorithmFactory) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Aggregations

QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)16 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)16 Snap (com.graphhopper.storage.index.Snap)12 Test (org.junit.jupiter.api.Test)11 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)7 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)5 Path (com.graphhopper.routing.Path)3 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)2 Weighting (com.graphhopper.routing.weighting.Weighting)2 PMap (com.graphhopper.util.PMap)2 IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)1 GraphHopper (com.graphhopper.GraphHopper)1 GraphHopperConfig (com.graphhopper.GraphHopperConfig)1 GHBitSet (com.graphhopper.coll.GHBitSet)1 GHTBitSet (com.graphhopper.coll.GHTBitSet)1 CHProfile (com.graphhopper.config.CHProfile)1 LMProfile (com.graphhopper.config.LMProfile)1