Search in sources :

Example 41 with PMap

use of com.graphhopper.util.PMap 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 42 with PMap

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

the class CHProfileSelectorTest method assertCHProfileSelectionError.

private String assertCHProfileSelectionError(String expectedError, List<Profile> profiles, List<CHProfile> chProfiles, String vehicle, String weighting, Boolean edgeBased, Integer uTurnCosts) {
    PMap hintsMap = createHintsMap(vehicle, weighting, edgeBased, uTurnCosts);
    try {
        new ProfileResolver(encodingManager, profiles, chProfiles, Collections.<LMProfile>emptyList()).selectProfileCH(hintsMap);
        fail("There should have been an error");
        return "";
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains(expectedError), "There should have been an error message containing:\n'" + expectedError + "'\nbut was:\n'" + e.getMessage() + "'");
        return e.getMessage();
    }
}
Also used : ProfileResolver(com.graphhopper.routing.ProfileResolver) PMap(com.graphhopper.util.PMap) LMProfile(com.graphhopper.config.LMProfile)

Example 43 with PMap

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

the class EdgeBasedNodeContractorTest method createNodeContractor.

private EdgeBasedNodeContractor createNodeContractor() {
    CHPreparationGraph.TurnCostFunction turnCostFunction = CHPreparationGraph.buildTurnCostFunctionFromTurnCostStorage(graph, weighting);
    CHPreparationGraph prepareGraph = CHPreparationGraph.edgeBased(graph.getNodes(), graph.getEdges(), turnCostFunction);
    CHPreparationGraph.buildFromGraph(prepareGraph, graph, weighting);
    EdgeBasedNodeContractor nodeContractor = new EdgeBasedNodeContractor(prepareGraph, chBuilder, new PMap());
    nodeContractor.initFromGraph();
    return nodeContractor;
}
Also used : PMap(com.graphhopper.util.PMap)

Example 44 with PMap

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

the class NodeBasedNodeContractorTest method createNodeContractor.

private static NodeContractor createNodeContractor(GraphHopperStorage g, CHStorage store, CHConfig chConfig) {
    CHPreparationGraph prepareGraph = CHPreparationGraph.nodeBased(g.getNodes(), g.getEdges());
    CHPreparationGraph.buildFromGraph(prepareGraph, g, g.createCHGraph(store, chConfig).getWeighting());
    NodeContractor nodeContractor = new NodeBasedNodeContractor(prepareGraph, new CHStorageBuilder(store), new PMap());
    nodeContractor.initFromGraph();
    return nodeContractor;
}
Also used : PMap(com.graphhopper.util.PMap)

Example 45 with PMap

use of com.graphhopper.util.PMap 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)

Aggregations

PMap (com.graphhopper.util.PMap)50 Test (org.junit.jupiter.api.Test)27 LMProfile (com.graphhopper.config.LMProfile)12 Profile (com.graphhopper.config.Profile)12 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)12 CHProfile (com.graphhopper.config.CHProfile)10 ArrayList (java.util.ArrayList)10 GraphHopper (com.graphhopper.GraphHopper)6 MapMatching (com.graphhopper.matching.MapMatching)5 MatchResult (com.graphhopper.matching.MatchResult)5 CHProfileSelectorTest (com.graphhopper.routing.ch.CHProfileSelectorTest)5 CHRoutingAlgorithmFactory (com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)5 LMProfileSelectorTest (com.graphhopper.routing.lm.LMProfileSelectorTest)5 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)5 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)5 Gpx (com.graphhopper.jackson.Gpx)4 ReaderWay (com.graphhopper.reader.ReaderWay)4 ProfileResolver (com.graphhopper.routing.ProfileResolver)4 Weighting (com.graphhopper.routing.weighting.Weighting)4 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)4