Search in sources :

Example 11 with CHProfile

use of com.graphhopper.config.CHProfile in project graphhopper by graphhopper.

the class CHMeasurement method testPerformanceAutomaticNodeOrdering.

/**
 * Parses a given osm file, contracts the graph and runs random routing queries on it. This is useful to test
 * the node contraction heuristics with regards to the performance of the automatic graph contraction (the node
 * contraction order determines how many and which shortcuts will be introduced) and the resulting query speed.
 * The queries are compared with a normal AStar search for comparison and to ensure correctness.
 */
private static void testPerformanceAutomaticNodeOrdering(String[] args) {
    // example args:
    // map=berlin.pbf stats_file=stats.dat period_updates=0 lazy_updates=100 neighbor_updates=50 max_neighbor_updatse=3 contract_nodes=100 log_messages=20 edge_quotient_weight=100.0 orig_edge_quotient_weight=100.0 hierarchy_depth_weight=20.0 landmarks=0 cleanup=true turncosts=true threshold=0.1 seed=456 comp_iterations=10 perf_iterations=100 quick=false
    long start = nanoTime();
    PMap map = PMap.read(args);
    GraphHopperConfig ghConfig = new GraphHopperConfig(map);
    LOGGER.info("Running analysis with parameters {}", ghConfig);
    String osmFile = ghConfig.getString("map", "map-matching/files/leipzig_germany.osm.pbf");
    ghConfig.putObject("datareader.file", osmFile);
    final String statsFile = ghConfig.getString("stats_file", null);
    final int periodicUpdates = ghConfig.getInt("period_updates", 0);
    final int lazyUpdates = ghConfig.getInt("lazy_updates", 100);
    final int neighborUpdates = ghConfig.getInt("neighbor_updates", 50);
    final int maxNeighborUpdates = ghConfig.getInt("max_neighbor_updates", 3);
    final int contractedNodes = ghConfig.getInt("contract_nodes", 100);
    final int logMessages = ghConfig.getInt("log_messages", 20);
    final float edgeQuotientWeight = ghConfig.getFloat("edge_quotient_weight", 100.0f);
    final float origEdgeQuotientWeight = ghConfig.getFloat("orig_edge_quotient_weight", 100.0f);
    final float hierarchyDepthWeight = ghConfig.getFloat("hierarchy_depth_weight", 20.0f);
    final int pollFactorHeuristic = ghConfig.getInt("poll_factor_heur", 5);
    final int pollFactorContraction = ghConfig.getInt("poll_factor_contr", 200);
    final int landmarks = ghConfig.getInt("landmarks", 0);
    final boolean cleanup = ghConfig.getBool("cleanup", true);
    final boolean withTurnCosts = ghConfig.getBool("turncosts", true);
    final int uTurnCosts = ghConfig.getInt(Parameters.Routing.U_TURN_COSTS, 80);
    final double errorThreshold = ghConfig.getDouble("threshold", 0.1);
    final long seed = ghConfig.getLong("seed", 456);
    final int compIterations = ghConfig.getInt("comp_iterations", 100);
    final int perfIterations = ghConfig.getInt("perf_iterations", 1000);
    final boolean quick = ghConfig.getBool("quick", false);
    final GraphHopper graphHopper = new GraphHopper();
    String profile = "car_profile";
    if (withTurnCosts) {
        ghConfig.putObject("graph.flag_encoders", "car|turn_costs=true");
        ghConfig.setProfiles(Collections.singletonList(new Profile(profile).setVehicle("car").setWeighting("fastest").setTurnCosts(true).putHint(Parameters.Routing.U_TURN_COSTS, uTurnCosts)));
        ghConfig.setCHProfiles(Collections.singletonList(new CHProfile(profile)));
        if (landmarks > 0) {
            ghConfig.setLMProfiles(Collections.singletonList(new LMProfile(profile)));
            ghConfig.putObject("prepare.lm.landmarks", landmarks);
        }
    } else {
        ghConfig.putObject("graph.flag_encoders", "car");
        ghConfig.setProfiles(Collections.singletonList(new Profile(profile).setVehicle("car").setWeighting("fastest").setTurnCosts(false)));
    }
    ghConfig.putObject(PERIODIC_UPDATES, periodicUpdates);
    ghConfig.putObject(LAST_LAZY_NODES_UPDATES, lazyUpdates);
    ghConfig.putObject(NEIGHBOR_UPDATES, neighborUpdates);
    ghConfig.putObject(NEIGHBOR_UPDATES_MAX, maxNeighborUpdates);
    ghConfig.putObject(CONTRACTED_NODES, contractedNodes);
    ghConfig.putObject(LOG_MESSAGES, logMessages);
    if (withTurnCosts) {
        ghConfig.putObject(EDGE_QUOTIENT_WEIGHT, edgeQuotientWeight);
        ghConfig.putObject(ORIGINAL_EDGE_QUOTIENT_WEIGHT, origEdgeQuotientWeight);
        ghConfig.putObject(HIERARCHY_DEPTH_WEIGHT, hierarchyDepthWeight);
        ghConfig.putObject(MAX_POLL_FACTOR_HEURISTIC_EDGE, pollFactorHeuristic);
        ghConfig.putObject(MAX_POLL_FACTOR_CONTRACTION_EDGE, pollFactorContraction);
    } else {
        ghConfig.putObject(MAX_POLL_FACTOR_HEURISTIC_NODE, pollFactorHeuristic);
        ghConfig.putObject(MAX_POLL_FACTOR_CONTRACTION_NODE, pollFactorContraction);
    }
    LOGGER.info("Initializing graph hopper with args: {}", ghConfig);
    graphHopper.init(ghConfig);
    if (cleanup) {
        graphHopper.clean();
    }
    PMap results = new PMap(ghConfig.asPMap());
    StopWatch sw = new StopWatch();
    sw.start();
    graphHopper.importOrLoad();
    sw.stop();
    results.putObject("_prepare_time", sw.getSeconds());
    LOGGER.info("Import and preparation took {}s", sw.getMillis() / 1000);
    if (!quick) {
        runCompareTest(DIJKSTRA_BI, graphHopper, withTurnCosts, uTurnCosts, seed, compIterations, errorThreshold, results);
        runCompareTest(ASTAR_BI, graphHopper, withTurnCosts, uTurnCosts, seed, compIterations, errorThreshold, results);
    }
    if (!quick) {
        runPerformanceTest(DIJKSTRA_BI, graphHopper, withTurnCosts, seed, perfIterations, results);
    }
    runPerformanceTest(ASTAR_BI, graphHopper, withTurnCosts, seed, perfIterations, results);
    if (!quick && landmarks > 0) {
        runPerformanceTest("lm", graphHopper, withTurnCosts, seed, perfIterations, results);
    }
    graphHopper.close();
    Map<String, Object> resultMap = results.toMap();
    TreeSet<String> sortedKeys = new TreeSet<>(resultMap.keySet());
    for (String key : sortedKeys) {
        LOGGER.info(key + "=" + resultMap.get(key));
    }
    if (statsFile != null) {
        File f = new File(statsFile);
        boolean writeHeader = !f.exists();
        try (OutputStream os = new FileOutputStream(f, true);
            Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
            if (writeHeader)
                writer.write(getHeader(sortedKeys));
            writer.write(getStatLine(sortedKeys, resultMap));
        } catch (IOException e) {
            LOGGER.error("Could not write summary to file '{}'", statsFile, e);
        }
    }
    // output to be used by external caller
    StringBuilder sb = new StringBuilder();
    for (String key : sortedKeys) {
        sb.append(key).append(":").append(resultMap.get(key)).append(";");
    }
    sb.deleteCharAt(sb.lastIndexOf(";"));
    System.out.println(sb);
    LOGGER.info("Total time: {}s", fmt((nanoTime() - start) * 1.e-9));
}
Also used : CHProfile(com.graphhopper.config.CHProfile) GraphHopper(com.graphhopper.GraphHopper) CHProfile(com.graphhopper.config.CHProfile) Profile(com.graphhopper.config.Profile) LMProfile(com.graphhopper.config.LMProfile) GraphHopperConfig(com.graphhopper.GraphHopperConfig) LMProfile(com.graphhopper.config.LMProfile)

Example 12 with CHProfile

use of com.graphhopper.config.CHProfile in project graphhopper by graphhopper.

the class CHImportTest method main.

public static void main(String[] args) {
    System.out.println("running for args: " + Arrays.toString(args));
    PMap map = PMap.read(args);
    String vehicle = map.getString("vehicle", "car");
    GraphHopperConfig config = new GraphHopperConfig(map);
    config.putObject("datareader.file", map.getString("pbf", "map-matching/files/leipzig_germany.osm.pbf"));
    config.putObject("graph.location", map.getString("gh", "ch-import-test-gh"));
    config.setProfiles(Arrays.asList(new Profile(vehicle).setVehicle(vehicle).setWeighting("fastest")));
    config.setCHProfiles(Collections.singletonList(new CHProfile(vehicle)));
    config.putObject(CHParameters.PERIODIC_UPDATES, map.getInt("periodic", 0));
    config.putObject(CHParameters.LAST_LAZY_NODES_UPDATES, map.getInt("lazy", 100));
    config.putObject(CHParameters.NEIGHBOR_UPDATES, map.getInt("neighbor", 100));
    config.putObject(CHParameters.NEIGHBOR_UPDATES_MAX, map.getInt("neighbor_max", 2));
    config.putObject(CHParameters.CONTRACTED_NODES, map.getInt("contracted", 100));
    config.putObject(CHParameters.LOG_MESSAGES, map.getInt("logs", 20));
    config.putObject(CHParameters.EDGE_DIFFERENCE_WEIGHT, map.getDouble("edge_diff", 10));
    config.putObject(CHParameters.ORIGINAL_EDGE_COUNT_WEIGHT, map.getDouble("orig_edge", 1));
    config.putObject(CHParameters.MAX_POLL_FACTOR_HEURISTIC_NODE, map.getDouble("mpf_heur", 5));
    config.putObject(CHParameters.MAX_POLL_FACTOR_CONTRACTION_NODE, map.getDouble("mpf_contr", 200));
    GraphHopper hopper = new GraphHopper();
    hopper.init(config);
    if (map.getBool("use_country_rules", false))
        // note that using this requires a new import of the base graph!
        hopper.setCountryRuleFactory(new CountryRuleFactory());
    hopper.importOrLoad();
    runQueries(hopper, vehicle);
}
Also used : CHProfile(com.graphhopper.config.CHProfile) PMap(com.graphhopper.util.PMap) CountryRuleFactory(com.graphhopper.routing.util.countryrules.CountryRuleFactory) GraphHopper(com.graphhopper.GraphHopper) CHProfile(com.graphhopper.config.CHProfile) Profile(com.graphhopper.config.Profile) GraphHopperConfig(com.graphhopper.GraphHopperConfig)

Example 13 with CHProfile

use of com.graphhopper.config.CHProfile in project graphhopper by graphhopper.

the class GraphHopperTest method testNodeBasedCHOnlyButTurnCostForNonCH.

@Test
public void testNodeBasedCHOnlyButTurnCostForNonCH() {
    final String profile1 = "car_profile_tc";
    final String profile2 = "car_profile_notc";
    final String weighting = "fastest";
    // before edge-based CH was added a common case was to use edge-based without CH and CH for node-based
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setOSMFile(MOSCOW).setProfiles(Arrays.asList(new Profile(profile1).setVehicle("car").setWeighting(weighting).setTurnCosts(true), new Profile(profile2).setVehicle("car").setWeighting(weighting).setTurnCosts(false))).setStoreOnFlush(true);
    hopper.getCHPreparationHandler().setCHProfiles(new CHProfile(profile2));
    hopper.importOrLoad();
    GHRequest req = new GHRequest(55.813357, 37.5958585, 55.811042, 37.594689);
    // without CH, turn turn costs on and off
    req.putHint(CH.DISABLE, true);
    req.setProfile(profile1);
    assertEquals(1044, hopper.route(req).getBest().getDistance(), 1);
    req.setProfile(profile2);
    assertEquals(400, hopper.route(req).getBest().getDistance(), 1);
    // with CH, turn turn costs on and off, since turn costs not supported for CH throw an error
    req.putHint(CH.DISABLE, false);
    req.setProfile(profile2);
    assertEquals(400, hopper.route(req).getBest().getDistance(), 1);
    req.setProfile(profile1);
    GHResponse rsp = hopper.route(req);
    assertEquals(1, rsp.getErrors().size());
    String expected = "Cannot find CH preparation for the requested profile: 'car_profile_tc'" + "\nYou can try disabling CH using ch.disable=true" + "\navailable CH profiles: [car_profile_notc]";
    assertTrue(rsp.getErrors().toString().contains(expected), "unexpected error:\n" + rsp.getErrors().toString() + "\nwhen expecting an error containing:\n" + expected);
}
Also used : CHProfile(com.graphhopper.config.CHProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with CHProfile

use of com.graphhopper.config.CHProfile in project graphhopper by graphhopper.

the class GraphHopperTest method connectionNotFound.

@Test
public void connectionNotFound() {
    final String profile = "profile";
    final String vehicle = "car";
    final String weighting = "fastest";
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setOSMFile(BAYREUTH).setProfiles(new Profile("profile").setVehicle(vehicle).setWeighting(weighting)).setStoreOnFlush(true);
    hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("profile"));
    hopper.setMinNetworkSize(0);
    hopper.importOrLoad();
    // here from and to both snap to small subnetworks that are disconnected from the main graph and
    // since we set min network size to 0 we expect a connection not found error
    GHPoint from = new GHPoint(49.97964, 11.539593);
    GHPoint to = new GHPoint(50.029247, 11.582851);
    GHRequest req = new GHRequest(from, to).setProfile(profile);
    GHResponse res = hopper.route(req);
    assertEquals("[com.graphhopper.util.exceptions.ConnectionNotFoundException: Connection between locations not found]", res.getErrors().toString());
}
Also used : CHProfile(com.graphhopper.config.CHProfile) GHPoint(com.graphhopper.util.shapes.GHPoint) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with CHProfile

use of com.graphhopper.config.CHProfile in project graphhopper by graphhopper.

the class GraphHopperTest method testCompareAlgos.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testCompareAlgos(boolean turnCosts) {
    final String profile = "car";
    final String vehicle = "car";
    final String weighting = "fastest";
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setOSMFile(MOSCOW).setProfiles(new Profile(profile).setVehicle(vehicle).setWeighting(weighting).setTurnCosts(turnCosts));
    hopper.getCHPreparationHandler().setCHProfiles(new CHProfile(profile));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile(profile));
    hopper.importOrLoad();
    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    for (int i = 0; i < 100; i++) {
        BBox bounds = hopper.getGraphHopperStorage().getBounds();
        double lat1 = bounds.minLat + rnd.nextDouble() * (bounds.maxLat - bounds.minLat);
        double lat2 = bounds.minLat + rnd.nextDouble() * (bounds.maxLat - bounds.minLat);
        double lon1 = bounds.minLon + rnd.nextDouble() * (bounds.maxLon - bounds.minLon);
        double lon2 = bounds.minLon + rnd.nextDouble() * (bounds.maxLon - bounds.minLon);
        GHRequest req = new GHRequest(lat1, lon1, lat2, lon2);
        req.setProfile(profile);
        req.getHints().putObject(CH.DISABLE, false).putObject(Landmark.DISABLE, true);
        ResponsePath pathCH = hopper.route(req).getBest();
        req.getHints().putObject(CH.DISABLE, true).putObject(Landmark.DISABLE, false);
        ResponsePath pathLM = hopper.route(req).getBest();
        req.getHints().putObject(CH.DISABLE, true).putObject(Landmark.DISABLE, true);
        ResponsePath path = hopper.route(req).getBest();
        String failMessage = "seed: " + seed + ", i=" + i;
        assertEquals(path.hasErrors(), pathCH.hasErrors(), failMessage);
        assertEquals(path.hasErrors(), pathLM.hasErrors(), failMessage);
        if (!path.hasErrors()) {
            assertEquals(path.getDistance(), pathCH.getDistance(), 0.1, failMessage);
            assertEquals(path.getDistance(), pathLM.getDistance(), 0.1, failMessage);
            assertEquals(path.getTime(), pathCH.getTime(), failMessage);
            assertEquals(path.getTime(), pathLM.getTime(), failMessage);
        }
    }
}
Also used : CHProfile(com.graphhopper.config.CHProfile) BBox(com.graphhopper.util.shapes.BBox) LMProfile(com.graphhopper.config.LMProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) GHPoint(com.graphhopper.util.shapes.GHPoint) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CHProfile (com.graphhopper.config.CHProfile)66 Profile (com.graphhopper.config.Profile)63 LMProfile (com.graphhopper.config.LMProfile)58 Test (org.junit.jupiter.api.Test)39 CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)16 PMap (com.graphhopper.util.PMap)10 GHPoint (com.graphhopper.util.shapes.GHPoint)9 GraphHopperServerConfiguration (com.graphhopper.application.GraphHopperServerConfiguration)8 GraphHopperServerTestConfiguration (com.graphhopper.application.util.GraphHopperServerTestConfiguration)8 GraphHopper (com.graphhopper.GraphHopper)6 CHProfileSelectorTest (com.graphhopper.routing.ch.CHProfileSelectorTest)4 LMProfileSelectorTest (com.graphhopper.routing.lm.LMProfileSelectorTest)4 GraphHopperConfig (com.graphhopper.GraphHopperConfig)3 ProfileResolver (com.graphhopper.routing.ProfileResolver)3 CountryRuleFactory (com.graphhopper.routing.util.countryrules.CountryRuleFactory)2 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)2 JtsModule (com.bedatadriven.jackson.datatype.jts.JtsModule)1 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1