Search in sources :

Example 1 with CHRoutingAlgorithmFactory

use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.

the class TrafficChangeWithNodeOrderingReusingTest method runPerformanceTest.

private static void runPerformanceTest(final GraphHopperStorage ghStorage, CHConfig chConfig, long seed, final int iterations) {
    final int numNodes = ghStorage.getNodes();
    RoutingCHGraph chGraph = ghStorage.createCHGraph(ghStorage.createCHStorage(chConfig), chConfig);
    final Random random = new Random(seed);
    LOGGER.info("Running performance test, seed = {}", seed);
    final double[] distAndWeight = { 0.0, 0.0 };
    MiniPerfTest performanceTest = new MiniPerfTest();
    performanceTest.setIterations(iterations).start(new MiniPerfTest.Task() {

        private long queryTime;

        @Override
        public int doCalc(boolean warmup, int run) {
            if (!warmup && run % 1000 == 0) {
                LOGGER.debug("Finished {} of {} runs. {}", run, iterations, run > 0 ? String.format(Locale.ROOT, " Time: %6.2fms", queryTime * 1.e-6 / run) : "");
            }
            if (run == iterations - 1) {
                String avg = fmt(queryTime * 1.e-6 / run);
                LOGGER.debug("Finished all ({}) runs, avg time: {}ms", iterations, avg);
            }
            int from = random.nextInt(numNodes);
            int to = random.nextInt(numNodes);
            long start = nanoTime();
            RoutingAlgorithm algo = new CHRoutingAlgorithmFactory(chGraph).createAlgo(new PMap());
            Path path = algo.calcPath(from, to);
            if (!warmup && !path.isFound())
                return 1;
            if (!warmup) {
                queryTime += nanoTime() - start;
                double distance = path.getDistance();
                double weight = path.getWeight();
                distAndWeight[0] += distance;
                distAndWeight[1] += weight;
            }
            return 0;
        }
    });
    if (performanceTest.getDummySum() > 0.5 * iterations) {
        throw new IllegalStateException("too many errors, probably something is wrong");
    }
    LOGGER.info("Total distance: {}, total weight: {}", distAndWeight[0], distAndWeight[1]);
    LOGGER.info("Average query time: {}ms", performanceTest.getMean());
}
Also used : PMap(com.graphhopper.util.PMap) MiniPerfTest(com.graphhopper.util.MiniPerfTest) Random(java.util.Random) CHRoutingAlgorithmFactory(com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)

Example 2 with CHRoutingAlgorithmFactory

use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.

the class TrafficChangeWithNodeOrderingReusingTest method checkCorrectness.

private static void checkCorrectness(GraphHopperStorage ghStorage, CHConfig chConfig, long seed, long numQueries) {
    LOGGER.info("checking correctness");
    RoutingCHGraph chGraph = ghStorage.createCHGraph(ghStorage.createCHStorage(chConfig), chConfig);
    Random rnd = new Random(seed);
    int numFails = 0;
    for (int i = 0; i < numQueries; ++i) {
        Dijkstra dijkstra = new Dijkstra(ghStorage, chConfig.getWeighting(), TraversalMode.NODE_BASED);
        RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(chGraph).createAlgo(new PMap());
        int from = rnd.nextInt(ghStorage.getNodes());
        int to = rnd.nextInt(ghStorage.getNodes());
        double dijkstraWeight = dijkstra.calcPath(from, to).getWeight();
        double chWeight = chAlgo.calcPath(from, to).getWeight();
        double error = Math.abs(dijkstraWeight - chWeight);
        if (error > 1) {
            System.out.println("failure from " + from + " to " + to + " dijkstra: " + dijkstraWeight + " ch: " + chWeight);
            numFails++;
        }
    }
    LOGGER.info("number of failed queries: " + numFails);
    assertEquals(0, numFails);
}
Also used : Random(java.util.Random) PMap(com.graphhopper.util.PMap) CHRoutingAlgorithmFactory(com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)

Example 3 with CHRoutingAlgorithmFactory

use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.

the class DijkstraBidirectionCHTest method createCHAlgo.

private RoutingAlgorithm createCHAlgo(RoutingCHGraph chGraph, boolean withSOD) {
    PMap opts = new PMap();
    if (!withSOD) {
        opts.putObject("stall_on_demand", false);
    }
    opts.putObject(ALGORITHM, DIJKSTRA_BI);
    return new CHRoutingAlgorithmFactory(chGraph).createAlgo(opts);
}
Also used : PMap(com.graphhopper.util.PMap) CHRoutingAlgorithmFactory(com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)

Example 4 with CHRoutingAlgorithmFactory

use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory 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 5 with CHRoutingAlgorithmFactory

use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.

the class LowLevelAPIExample method useContractionHierarchiesToMakeQueriesFaster.

public static void useContractionHierarchiesToMakeQueriesFaster() {
    // Creating and saving the graph
    FlagEncoder encoder = new CarFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    Weighting weighting = new FastestWeighting(encoder);
    CHConfig chConfig = CHConfig.nodeBased("my_profile", weighting);
    GraphHopperStorage graph = new GraphBuilder(em).setRAM(graphLocation, true).create();
    graph.flush();
    // Set node coordinates and build location index
    NodeAccess na = graph.getNodeAccess();
    graph.edge(0, 1).set(encoder.getAccessEnc(), true).set(encoder.getAverageSpeedEnc(), 10).setDistance(1020);
    na.setNode(0, 15.15, 20.20);
    na.setNode(1, 15.25, 20.21);
    // Prepare the graph for fast querying ...
    graph.freeze();
    PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chConfig);
    PrepareContractionHierarchies.Result pchRes = pch.doWork();
    RoutingCHGraph chGraph = graph.createCHGraph(pchRes.getCHStorage(), pchRes.getCHConfig());
    // create location index
    LocationIndexTree index = new LocationIndexTree(graph, graph.getDirectory());
    index.prepareIndex();
    // calculate a path with location index
    Snap fromSnap = index.findClosest(15.15, 20.20, EdgeFilter.ALL_EDGES);
    Snap toSnap = index.findClosest(15.25, 20.21, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, fromSnap, toSnap);
    BidirRoutingAlgorithm algo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap());
    Path path = algo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
    assert Helper.round(path.getDistance(), -2) == 1000;
}
Also used : Path(com.graphhopper.routing.Path) PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) PMap(com.graphhopper.util.PMap) Snap(com.graphhopper.storage.index.Snap) BidirRoutingAlgorithm(com.graphhopper.routing.BidirRoutingAlgorithm) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) CHRoutingAlgorithmFactory(com.graphhopper.routing.ch.CHRoutingAlgorithmFactory) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Aggregations

CHRoutingAlgorithmFactory (com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)5 PMap (com.graphhopper.util.PMap)5 PrepareContractionHierarchies (com.graphhopper.routing.ch.PrepareContractionHierarchies)2 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)2 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)2 Weighting (com.graphhopper.routing.weighting.Weighting)2 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)2 Snap (com.graphhopper.storage.index.Snap)2 Random (java.util.Random)2 BidirRoutingAlgorithm (com.graphhopper.routing.BidirRoutingAlgorithm)1 Path (com.graphhopper.routing.Path)1 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)1 MiniPerfTest (com.graphhopper.util.MiniPerfTest)1 ArrayList (java.util.ArrayList)1