Search in sources :

Example 51 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class DirectionResolverOnQueryGraphTest method assertUnrestricted.

private void assertUnrestricted(double lat, double lon) {
    Snap snap = snapCoordinate(lat, lon);
    queryGraph = QueryGraph.create(graph, snap);
    DirectionResolver resolver = new DirectionResolver(queryGraph, this::isAccessible);
    assertEquals(unrestricted(), resolver.resolveDirections(snap.getClosestNode(), snap.getQueryPoint()));
}
Also used : Snap(com.graphhopper.storage.index.Snap)

Example 52 with Snap

use of com.graphhopper.storage.index.Snap 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)

Example 53 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class DirectionResolverOnQueryGraphTest method closeToTowerNode_issue2443.

@Test
public void closeToTowerNode_issue2443() {
    // 0x-1
    addNode(0, 51.986000, 19.255000);
    addNode(1, 51.985500, 19.254000);
    DistancePlaneProjection distCalc = new DistancePlaneProjection();
    addEdge(0, 1, true).setDistance(distCalc.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1)));
    init();
    double lat = 51.9855003;
    double lon = 19.2540003;
    Snap snap = snapCoordinate(lat, lon);
    queryGraph = QueryGraph.create(graph, snap);
    DirectionResolver resolver = new DirectionResolver(queryGraph, this::isAccessible);
    DirectionResolverResult result = resolver.resolveDirections(snap.getClosestNode(), snap.getQueryPoint());
    assertEquals(0, result.getInEdgeRight());
    assertEquals(0, result.getOutEdgeRight());
    assertEquals(0, result.getInEdgeLeft());
    assertEquals(0, result.getOutEdgeRight());
}
Also used : Snap(com.graphhopper.storage.index.Snap) Test(org.junit.jupiter.api.Test)

Example 54 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class CHTurnCostTest method testRouteViaVirtualNode.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void testRouteViaVirtualNode(String algo) {
    // 3
    // 0-x-1-2
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 1).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(0));
    updateDistancesFor(graph, 0, 0.00, 0.00);
    updateDistancesFor(graph, 1, 0.02, 0.02);
    updateDistancesFor(graph, 2, 0.03, 0.03);
    graph.freeze();
    automaticPrepareCH();
    LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
    index.prepareIndex();
    Snap snap = index.findClosest(0.01, 0.01, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, snap);
    assertEquals(3, snap.getClosestNode());
    assertEquals(0, snap.getClosestEdge().getEdge());
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(0, 2);
    assertTrue(path.isFound(), "it should be possible to route via a virtual node, but no path found");
    assertEquals(IntArrayList.from(0, 3, 1, 2), path.calcNodes());
    assertEquals(DistancePlaneProjection.DIST_PLANE.calcDist(0.00, 0.00, 0.03, 0.03), path.getDistance(), 1.e-1);
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 55 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class CHTurnCostTest method testRouteViaVirtualNode_withAlternative.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void testRouteViaVirtualNode_withAlternative(String algo) {
    // 3
    // 0-x-1
    // \  |
    // \-2
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 0).setDistance(1));
    updateDistancesFor(graph, 0, 0.01, 0.00);
    updateDistancesFor(graph, 1, 0.01, 0.02);
    updateDistancesFor(graph, 2, 0.00, 0.02);
    graph.freeze();
    automaticPrepareCH();
    LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
    index.prepareIndex();
    Snap snap = index.findClosest(0.01, 0.01, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, snap);
    assertEquals(3, snap.getClosestNode());
    assertEquals(0, snap.getClosestEdge().getEdge());
    QueryRoutingCHGraph routingCHGraph = new QueryRoutingCHGraph(chGraph, queryGraph);
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(1, 0);
    assertEquals(IntArrayList.from(1, 3, 0), path.calcNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Snap (com.graphhopper.storage.index.Snap)77 Test (org.junit.jupiter.api.Test)39 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)31 GHPoint (com.graphhopper.util.shapes.GHPoint)22 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)20 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)13 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)12 Weighting (com.graphhopper.routing.weighting.Weighting)11 ArrayList (java.util.ArrayList)11 Path (com.graphhopper.routing.Path)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)8 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)6 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)6 LocationIndex (com.graphhopper.storage.index.LocationIndex)6 IntArrayList (com.carrotsearch.hppc.IntArrayList)5 GraphHopper (com.graphhopper.GraphHopper)5 Profile (com.graphhopper.config.Profile)5 ValueSource (org.junit.jupiter.params.provider.ValueSource)5 DefaultSnapFilter (com.graphhopper.routing.util.DefaultSnapFilter)4