Search in sources :

Example 46 with Weighting

use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.

the class GraphHopperTest method testCreateWeightingHintsMerging.

@Test
public void testCreateWeightingHintsMerging() {
    final String profile = "profile";
    final String vehicle = "mtb";
    final String weighting = "shortest";
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setOSMFile(MONACO).setProfiles(new Profile(profile).setVehicle(vehicle).setWeighting(weighting).setTurnCosts(true).putHint(U_TURN_COSTS, 123));
    hopper.importOrLoad();
    // if we do not pass u_turn_costs with the request hints we get those from the profile
    Weighting w = hopper.createWeighting(hopper.getProfiles().get(0), new PMap());
    assertEquals(123.0, w.calcTurnWeight(5, 6, 5));
    // we can overwrite the u_turn_costs given in the profile
    w = hopper.createWeighting(hopper.getProfiles().get(0), new PMap().putObject(U_TURN_COSTS, 46));
    assertEquals(46.0, w.calcTurnWeight(5, 6, 5));
}
Also used : Weighting(com.graphhopper.routing.weighting.Weighting) 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 47 with Weighting

use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.

the class GraphHopperTest method issue2306_2.

@Test
public void issue2306_2() {
    // This is the same test as above, but without increasing the search radius.
    // As I am writing this, we find _no_ match here. But since the search radius
    // is a meta-parameter that could go away at some point, I say that _if_ we find a match,
    // it should be a close one. (And not a far away one, as happened in issue2306.)
    final String profile = "profile";
    final String vehicle = "car";
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setOSMFile("../map-matching/files/leipzig_germany.osm.pbf").setProfiles(new Profile("profile").setVehicle(vehicle).setWeighting("fastest")).setMinNetworkSize(200);
    hopper.importOrLoad();
    Weighting weighting = hopper.createWeighting(hopper.getProfile(profile), new PMap());
    EdgeFilter edgeFilter = new DefaultSnapFilter(weighting, hopper.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profile)));
    Snap snap = hopper.getLocationIndex().findClosest(51.229248, 12.328892, edgeFilter);
    if (snap.isValid()) {
        assertTrue(snap.getQueryDistance() < 3_000);
    }
}
Also used : Weighting(com.graphhopper.routing.weighting.Weighting) Snap(com.graphhopper.storage.index.Snap) 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 48 with Weighting

use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.

the class Measurement method measureRouting.

private void measureRouting(final GraphHopper hopper, final QuerySettings querySettings) {
    final Graph g = hopper.getGraphHopperStorage();
    final AtomicLong maxDistance = new AtomicLong(0);
    final AtomicLong minDistance = new AtomicLong(Long.MAX_VALUE);
    final AtomicLong distSum = new AtomicLong(0);
    final AtomicLong airDistSum = new AtomicLong(0);
    final AtomicLong altCount = new AtomicLong(0);
    final AtomicInteger failedCount = new AtomicInteger(0);
    final DistanceCalc distCalc = new DistanceCalcEarth();
    String profileName = querySettings.edgeBased ? "profile_tc" : "profile_no_tc";
    Weighting weighting = hopper.createWeighting(hopper.getProfile(profileName), new PMap());
    final EdgeFilter edgeFilter = new DefaultSnapFilter(weighting, hopper.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profileName)));
    final EdgeExplorer edgeExplorer = g.createEdgeExplorer(edgeFilter);
    final AtomicLong visitedNodesSum = new AtomicLong(0);
    final AtomicLong maxVisitedNodes = new AtomicLong(0);
    final Random rand = new Random(seed);
    final NodeAccess na = g.getNodeAccess();
    MiniPerfTest miniPerf = new MiniPerfTest().setIterations(querySettings.count).start((warmup, run) -> {
        GHRequest req = new GHRequest(querySettings.points);
        IntArrayList nodes = new IntArrayList(querySettings.points);
        // we try a few times to find points that do not lie within our blocked area
        for (int i = 0; i < 5; i++) {
            nodes.clear();
            List<GHPoint> points = new ArrayList<>();
            List<String> pointHints = new ArrayList<>();
            int tries = 0;
            while (nodes.size() < querySettings.points) {
                int node = rand.nextInt(maxNode);
                if (++tries > g.getNodes())
                    throw new RuntimeException("Could not find accessible points");
                // probe location. it could be a pedestrian area or an edge removed in the subnetwork removal process
                if (GHUtility.count(edgeExplorer.setBaseNode(node)) == 0)
                    continue;
                nodes.add(node);
                points.add(new GHPoint(na.getLat(node), na.getLon(node)));
                if (querySettings.withPointHints) {
                    // we add some point hint to make sure the name similarity filter has to do some actual work
                    pointHints.add("probably_not_found");
                }
            }
            req.setPoints(points);
            req.setPointHints(pointHints);
            if (querySettings.blockArea == null)
                break;
            try {
                req.getHints().putObject(BLOCK_AREA, querySettings.blockArea);
                // run this method to check if creating the blocked area is possible
                GraphEdgeIdFinder.createBlockArea(hopper.getGraphHopperStorage(), hopper.getLocationIndex(), req.getPoints(), req.getHints(), edgeFilter);
                break;
            } catch (IllegalArgumentException ex) {
                if (i >= 4)
                    throw new RuntimeException("Give up after 5 tries. Cannot find points outside of the block_area " + querySettings.blockArea + " - too big block_area or map too small? Request:" + req);
            }
        }
        req.setProfile(profileName);
        req.getHints().putObject(CH.DISABLE, !querySettings.ch).putObject("stall_on_demand", querySettings.sod).putObject(Landmark.DISABLE, !querySettings.lm).putObject(Landmark.ACTIVE_COUNT, querySettings.activeLandmarks).putObject("instructions", querySettings.withInstructions);
        if (querySettings.alternative)
            req.setAlgorithm(ALT_ROUTE);
        if (querySettings.pathDetails)
            req.setPathDetails(Arrays.asList(Parameters.Details.AVERAGE_SPEED, Parameters.Details.EDGE_ID, Parameters.Details.STREET_NAME));
        if (!querySettings.simplify)
            req.getHints().putObject(Parameters.Routing.WAY_POINT_MAX_DISTANCE, 0);
        GHResponse rsp;
        try {
            rsp = hopper.route(req);
        } catch (Exception ex) {
            // 'not found' can happen if import creates more than one subnetwork
            throw new RuntimeException("Error while calculating route! nodes: " + nodes + ", request:" + req, ex);
        }
        if (rsp.hasErrors()) {
            if (!warmup)
                failedCount.incrementAndGet();
            if (rsp.getErrors().get(0).getMessage() == null)
                rsp.getErrors().get(0).printStackTrace();
            else if (!toLowerCase(rsp.getErrors().get(0).getMessage()).contains("not found")) {
                if (stopOnError)
                    throw new RuntimeException("errors should NOT happen in Measurement! " + req + " => " + rsp.getErrors());
                else
                    logger.error("errors should NOT happen in Measurement! " + req + " => " + rsp.getErrors());
            }
            return 0;
        }
        ResponsePath responsePath = rsp.getBest();
        if (!warmup) {
            long visitedNodes = rsp.getHints().getLong("visited_nodes.sum", 0);
            visitedNodesSum.addAndGet(visitedNodes);
            if (visitedNodes > maxVisitedNodes.get()) {
                maxVisitedNodes.set(visitedNodes);
            }
            long dist = (long) responsePath.getDistance();
            distSum.addAndGet(dist);
            GHPoint prev = req.getPoints().get(0);
            for (GHPoint point : req.getPoints()) {
                airDistSum.addAndGet((long) distCalc.calcDist(prev.getLat(), prev.getLon(), point.getLat(), point.getLon()));
                prev = point;
            }
            if (dist > maxDistance.get())
                maxDistance.set(dist);
            if (dist < minDistance.get())
                minDistance.set(dist);
            if (querySettings.alternative)
                altCount.addAndGet(rsp.getAll().size());
        }
        return responsePath.getPoints().size();
    });
    int count = querySettings.count - failedCount.get();
    if (count == 0)
        throw new RuntimeException("All requests failed, something must be wrong: " + failedCount.get());
    // if using non-bidirectional algorithm make sure you exclude CH routing
    String algoStr = (querySettings.ch && !querySettings.edgeBased) ? Algorithms.DIJKSTRA_BI : Algorithms.ASTAR_BI;
    if (querySettings.ch && !querySettings.sod) {
        algoStr += "_no_sod";
    }
    String prefix = querySettings.prefix;
    put(prefix + ".guessed_algorithm", algoStr);
    put(prefix + ".failed_count", failedCount.get());
    put(prefix + ".distance_min", minDistance.get());
    put(prefix + ".distance_mean", (float) distSum.get() / count);
    put(prefix + ".air_distance_mean", (float) airDistSum.get() / count);
    put(prefix + ".distance_max", maxDistance.get());
    put(prefix + ".visited_nodes_mean", (float) visitedNodesSum.get() / count);
    put(prefix + ".visited_nodes_max", (float) maxVisitedNodes.get());
    put(prefix + ".alternative_rate", (float) altCount.get() / count);
    print(prefix, miniPerf);
}
Also used : IntArrayList(com.carrotsearch.hppc.IntArrayList) GHPoint(com.graphhopper.util.shapes.GHPoint) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Weighting(com.graphhopper.routing.weighting.Weighting) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntArrayList(com.carrotsearch.hppc.IntArrayList) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 49 with Weighting

use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.

the class GraphHopper method createLMConfigs.

private List<LMConfig> createLMConfigs(List<LMProfile> lmProfiles) {
    List<LMConfig> lmConfigs = new ArrayList<>();
    for (LMProfile lmProfile : lmProfiles) {
        if (lmProfile.usesOtherPreparation())
            continue;
        Profile profile = profilesByName.get(lmProfile.getProfile());
        // Note that we have to make sure the weighting used for LM preparation does not include turn costs, because
        // the LM preparation is running node-based and the landmark weights will be wrong if there are non-zero
        // turn costs, see discussion in #1960
        // Running the preparation without turn costs is also useful to allow e.g. changing the u_turn_costs per
        // request (we have to use the minimum weight settings (= no turn costs) for the preparation)
        Weighting weighting = createWeighting(profile, new PMap(), true);
        lmConfigs.add(new LMConfig(profile.getName(), weighting));
    }
    return lmConfigs;
}
Also used : Weighting(com.graphhopper.routing.weighting.Weighting) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) LMProfile(com.graphhopper.config.LMProfile) LMConfig(com.graphhopper.routing.lm.LMConfig) LMProfile(com.graphhopper.config.LMProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile)

Example 50 with Weighting

use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.

the class GraphHopper method buildSubnetworkRemovalJobs.

private List<PrepareJob> buildSubnetworkRemovalJobs() {
    List<PrepareJob> jobs = new ArrayList<>();
    for (Profile profile : profilesByName.values()) {
        // if turn costs are enabled use u-turn costs of zero as we only want to make sure the graph is fully connected assuming finite u-turn costs
        Weighting weighting = createWeighting(profile, new PMap().putObject(Parameters.Routing.U_TURN_COSTS, 0));
        jobs.add(new PrepareJob(encodingManager.getBooleanEncodedValue(Subnetwork.key(profile.getName())), weighting));
    }
    return jobs;
}
Also used : Weighting(com.graphhopper.routing.weighting.Weighting) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) LMProfile(com.graphhopper.config.LMProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) PrepareJob(com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks.PrepareJob)

Aggregations

Weighting (com.graphhopper.routing.weighting.Weighting)57 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)32 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)15 Test (org.junit.jupiter.api.Test)15 Test (org.junit.Test)12 Snap (com.graphhopper.storage.index.Snap)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)9 GHPoint (com.graphhopper.util.shapes.GHPoint)9 Path (com.graphhopper.routing.Path)8 Graph (com.graphhopper.storage.Graph)8 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)8 Profile (com.graphhopper.config.Profile)7 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)7 ArrayList (java.util.ArrayList)7 GraphHopper (com.graphhopper.GraphHopper)6 IOException (java.io.IOException)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 AbstractWeighting (com.graphhopper.routing.weighting.AbstractWeighting)5