Search in sources :

Example 6 with DefaultTurnCostProvider

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

the class DefaultBidirPathExtractorTest method testExtract2.

@Test
public void testExtract2() {
    // 1->2->3
    Graph graph = createGraph();
    GHUtility.setSpeed(60, true, false, carEncoder, graph.edge(1, 2).setDistance(10));
    GHUtility.setSpeed(60, true, false, carEncoder, graph.edge(2, 3).setDistance(20));
    // add some turn costs at node 2 where fwd&bwd searches meet. these costs have to be included in the
    // weight and the time of the path
    TurnCostStorage turnCostStorage = graph.getTurnCostStorage();
    DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(carEncoder.toString()));
    turnCostStorage.set(turnCostEnc, 0, 2, 1, 5);
    SPTEntry fwdEntry = new SPTEntry(0, 2, 0.6);
    fwdEntry.parent = new SPTEntry(EdgeIterator.NO_EDGE, 1, 0);
    SPTEntry bwdEntry = new SPTEntry(1, 2, 1.2);
    bwdEntry.parent = new SPTEntry(EdgeIterator.NO_EDGE, 3, 0);
    Path p = DefaultBidirPathExtractor.extractPath(graph, new FastestWeighting(carEncoder, new DefaultTurnCostProvider(carEncoder, turnCostStorage)), fwdEntry, bwdEntry, 0);
    p.setWeight(5 + 1.8);
    assertEquals(IntArrayList.from(1, 2, 3), p.calcNodes());
    assertEquals(30, p.getDistance(), 1e-4);
    assertEquals(5 + 1.8, p.getWeight(), 1e-4);
    assertEquals(5000 + 1800, p.getTime(), 1.e-6);
}
Also used : Graph(com.graphhopper.storage.Graph) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) TurnCostStorage(com.graphhopper.storage.TurnCostStorage) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) DefaultTurnCostProvider(com.graphhopper.routing.weighting.DefaultTurnCostProvider) Test(org.junit.jupiter.api.Test)

Example 7 with DefaultTurnCostProvider

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

the class AlternativeRouteEdgeCHTest method prepareCH.

private RoutingCHGraph prepareCH(GraphHopperStorage graph) {
    TurnCostProvider turnCostProvider = new DefaultTurnCostProvider(carFE, graph.getTurnCostStorage());
    CHConfig chConfig = CHConfig.edgeBased("profile", new FastestWeighting(carFE, turnCostProvider));
    PrepareContractionHierarchies contractionHierarchies = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chConfig);
    PrepareContractionHierarchies.Result res = contractionHierarchies.doWork();
    return graph.createCHGraph(res.getCHStorage(), res.getCHConfig());
}
Also used : TurnCostProvider(com.graphhopper.routing.weighting.TurnCostProvider) DefaultTurnCostProvider(com.graphhopper.routing.weighting.DefaultTurnCostProvider) PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) DefaultTurnCostProvider(com.graphhopper.routing.weighting.DefaultTurnCostProvider)

Example 8 with DefaultTurnCostProvider

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

the class EdgeBasedRoutingAlgorithmTest method testTurnCostsBug_991.

@ParameterizedTest
@ArgumentsSource(FixtureProvider.class)
public void testTurnCostsBug_991(String algoStr) {
    final GraphHopperStorage g = createStorage(createEncodingManager(false));
    initGraph(g, carEncoder);
    setTurnCost(g, 2, 5, 2, 3);
    setTurnCost(g, 2, 2, 0, 1);
    setTurnCost(g, 2, 5, 6, 3);
    setTurnCost(g, 1, 6, 7, 4);
    FastestWeighting weighting = new FastestWeighting(carEncoder, new DefaultTurnCostProvider(carEncoder, tcs) {

        @Override
        public double calcTurnWeight(int edgeFrom, int nodeVia, int edgeTo) {
            if (edgeFrom >= 0)
                assertNotNull(g.getEdgeIteratorState(edgeFrom, nodeVia), "edge " + edgeFrom + " to " + nodeVia + " does not exist");
            if (edgeTo >= 0)
                assertNotNull(g.getEdgeIteratorState(edgeTo, nodeVia), "edge " + edgeTo + " to " + nodeVia + " does not exist");
            return super.calcTurnWeight(edgeFrom, nodeVia, edgeTo);
        }
    });
    Path p = createAlgo(g, weighting, algoStr, EDGE_BASED).calcPath(5, 1);
    assertEquals(IntArrayList.from(5, 6, 7, 4, 3, 1), p.calcNodes());
    assertEquals(5 * 0.06 + 1, p.getWeight(), 1.e-6);
    assertEquals(1300, p.getTime(), .1);
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) DefaultTurnCostProvider(com.graphhopper.routing.weighting.DefaultTurnCostProvider) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 9 with DefaultTurnCostProvider

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

the class CHTurnCostTest method createCHConfigs.

/**
 * Creates a list of distinct CHProfiles with different u-turn costs that can be used by the tests.
 * There is always a profile with infinite u-turn costs and one with u-turn-costs = 50.
 */
private List<CHConfig> createCHConfigs() {
    Set<CHConfig> configs = new LinkedHashSet<>(5);
    // the first one is always the one with infinite u-turn costs
    configs.add(CHConfig.edgeBased("p0", new ShortestWeighting(encoder, new DefaultTurnCostProvider(encoder, turnCostStorage, INFINITE_U_TURN_COSTS))));
    // this one we also always add
    configs.add(CHConfig.edgeBased("p1", new ShortestWeighting(encoder, new DefaultTurnCostProvider(encoder, turnCostStorage, 50))));
    // add more (distinct) profiles
    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    while (configs.size() < 5) {
        int uTurnCosts = 10 + rnd.nextInt(90);
        configs.add(CHConfig.edgeBased("p" + configs.size(), new ShortestWeighting(encoder, new DefaultTurnCostProvider(encoder, turnCostStorage, uTurnCosts))));
    }
    return new ArrayList<>(configs);
}
Also used : IntArrayList(com.carrotsearch.hppc.IntArrayList) DefaultTurnCostProvider(com.graphhopper.routing.weighting.DefaultTurnCostProvider) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 10 with DefaultTurnCostProvider

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

the class QueryGraphTest method testTurnCostsProperlyPropagated_Issue282.

@Test
public void testTurnCostsProperlyPropagated_Issue282() {
    FlagEncoder encoder = new CarFlagEncoder(5, 5, 15);
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graphWithTurnCosts = new GraphBuilder(em).withTurnCosts(true).create();
    TurnCostStorage turnExt = graphWithTurnCosts.getTurnCostStorage();
    DecimalEncodedValue turnCostEnc = em.getDecimalEncodedValue(TurnCost.key(encoder.toString()));
    NodeAccess na = graphWithTurnCosts.getNodeAccess();
    na.setNode(0, .00, .00);
    na.setNode(1, .00, .01);
    na.setNode(2, .01, .01);
    EdgeIteratorState edge0 = GHUtility.setSpeed(60, true, true, encoder, graphWithTurnCosts.edge(0, 1).setDistance(10));
    EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, encoder, graphWithTurnCosts.edge(2, 1).setDistance(10));
    Weighting weighting = new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder, graphWithTurnCosts.getTurnCostStorage()));
    // no turn costs initially
    assertEquals(0, weighting.calcTurnWeight(edge0.getEdge(), 1, edge1.getEdge()), .1);
    // now use turn costs
    turnExt.set(turnCostEnc, edge0.getEdge(), 1, edge1.getEdge(), 10);
    assertEquals(10, weighting.calcTurnWeight(edge0.getEdge(), 1, edge1.getEdge()), .1);
    // now use turn costs with query graph
    Snap res1 = createLocationResult(0.000, 0.005, edge0, 0, Snap.Position.EDGE);
    Snap res2 = createLocationResult(0.005, 0.010, edge1, 0, Snap.Position.EDGE);
    QueryGraph qGraph = QueryGraph.create(graphWithTurnCosts, res1, res2);
    weighting = qGraph.wrapWeighting(weighting);
    int fromQueryEdge = GHUtility.getEdge(qGraph, res1.getClosestNode(), 1).getEdge();
    int toQueryEdge = GHUtility.getEdge(qGraph, res2.getClosestNode(), 1).getEdge();
    assertEquals(10, weighting.calcTurnWeight(fromQueryEdge, 1, toQueryEdge), .1);
    graphWithTurnCosts.close();
}
Also used : DefaultTurnCostProvider(com.graphhopper.routing.weighting.DefaultTurnCostProvider) Snap(com.graphhopper.storage.index.Snap) GHPoint(com.graphhopper.util.shapes.GHPoint) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultTurnCostProvider (com.graphhopper.routing.weighting.DefaultTurnCostProvider)10 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)6 Test (org.junit.jupiter.api.Test)5 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)3 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)3 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 TurnCostProvider (com.graphhopper.routing.weighting.TurnCostProvider)2 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)2 GHPoint (com.graphhopper.util.shapes.GHPoint)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 IntHashSet (com.carrotsearch.hppc.IntHashSet)1 IntSet (com.carrotsearch.hppc.IntSet)1 PrepareContractionHierarchies (com.graphhopper.routing.ch.PrepareContractionHierarchies)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 Weighting (com.graphhopper.routing.weighting.Weighting)1 Graph (com.graphhopper.storage.Graph)1 TurnCostStorage (com.graphhopper.storage.TurnCostStorage)1 Snap (com.graphhopper.storage.index.Snap)1 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)1