Search in sources :

Example 1 with RoutingAlgorithm

use of com.graphhopper.routing.RoutingAlgorithm in project graphhopper by graphhopper.

the class CHTurnCostTest method test_issue_1593_simple.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void test_issue_1593_simple(String algo) {
    // 1
    // |
    // 3-0-x-5-4
    // |
    // 2
    NodeAccess na = graph.getNodeAccess();
    na.setNode(1, 0.2, 0.0);
    na.setNode(3, 0.1, 0.0);
    na.setNode(2, 0.0, 0.0);
    na.setNode(0, 0.1, 0.1);
    na.setNode(5, 0.1, 0.2);
    na.setNode(4, 0.1, 0.3);
    EdgeIteratorState edge0 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(3, 1).setDistance(10));
    EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 3).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(3, 0).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 5).setDistance(10));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(5, 4).setDistance(10));
    // cannot go, 2-3-1
    setRestriction(edge1, edge0, 3);
    graph.freeze();
    prepareCH(0, 1, 2, 3, 4, 5);
    assertEquals(5, chGraph.getBaseGraph().getEdges());
    assertEquals(7, chGraph.getEdges(), "expected two shortcuts: 3->5 and 5->3");
    // there should be no path from 2 to 1, because of the turn restriction and because u-turns are not allowed
    assertFalse(findPathUsingDijkstra(2, 1).isFound());
    compareCHQueryWithDijkstra(2, 1);
    // we have to pay attention when there are virtual nodes: turning from the shortcut 3-5 onto the
    // virtual edge 5-x should be forbidden.
    LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
    index.prepareIndex();
    Snap snap = index.findClosest(0.1, 0.15, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, snap);
    assertEquals(1, queryGraph.getNodes() - chGraph.getNodes(), "expected one virtual node");
    QueryRoutingCHGraph routingCHGraph = new QueryRoutingCHGraph(chGraph, queryGraph);
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(2, 1);
    assertFalse(path.isFound(), "no path should be found, but found " + path.calcNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with RoutingAlgorithm

use of com.graphhopper.routing.RoutingAlgorithm in project graphhopper by graphhopper.

the class CHTurnCostTest method test_issue1593_full.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void test_issue1593_full(String algo) {
    // 6   5
    // 1<-x-4-x-3
    // ||    |
    // |x7   x8
    // ||   /
    // 2---
    NodeAccess na = graph.getNodeAccess();
    na.setNode(0, 49.407117, 9.701306);
    na.setNode(1, 49.406914, 9.703393);
    na.setNode(2, 49.404004, 9.709110);
    na.setNode(3, 49.400160, 9.708787);
    na.setNode(4, 49.400883, 9.706347);
    EdgeIteratorState edge0 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(4, 3).setDistance(194.063000));
    EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(525.106000));
    EdgeIteratorState edge2 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(525.106000));
    EdgeIteratorState edge3 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(4, 1).setDistance(703.778000));
    EdgeIteratorState edge4 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 4).setDistance(400.509000));
    // cannot go 4-2-1 and 1-2-4 (at least when using edge1, there is still edge2!)
    setRestriction(edge4, edge1, 2);
    setRestriction(edge1, edge4, 2);
    // cannot go 3-4-1
    setRestriction(edge0, edge3, 4);
    graph.freeze();
    LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
    index.prepareIndex();
    List<GHPoint> points = Arrays.asList(// 8 (on edge4)
    new GHPoint(49.401669187194116, 9.706821649608745), // 5 (on edge0)
    new GHPoint(49.40056349818417, 9.70767186472369), // 7 (on edge2)
    new GHPoint(49.406580835146556, 9.704665738628218), // 6 (on edge3)
    new GHPoint(49.40107534698834, 9.702248694088528));
    List<Snap> snaps = new ArrayList<>(points.size());
    for (GHPoint point : points) {
        snaps.add(index.findClosest(point.getLat(), point.getLon(), EdgeFilter.ALL_EDGES));
    }
    automaticPrepareCH();
    QueryGraph queryGraph = QueryGraph.create(chGraph.getBaseGraph(), snaps);
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(5, 6);
    // there should not be a path from 5 to 6, because first we cannot go directly 5-4-6, so we need to go left
    // to 8. then at 2 we cannot go on edge 1 because of another turn restriction, but we can go on edge 2 so we
    // travel via the virtual node 7 to node 1. From there we cannot go to 6 because of the one-way so we go back
    // to node 2 (no u-turn because of the duplicate edge) on edge1. And this is were the journey ends: we cannot
    // go to 8 because of the turn restriction from edge1 to edge4 -> there should not be a path!
    assertFalse(path.isFound(), "there should not be a path, but found: " + path.calcNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) IntArrayList(com.carrotsearch.hppc.IntArrayList) Snap(com.graphhopper.storage.index.Snap) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) GHPoint(com.graphhopper.util.shapes.GHPoint) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with RoutingAlgorithm

use of com.graphhopper.routing.RoutingAlgorithm in project graphhopper by graphhopper.

the class CHTurnCostTest method testFiniteUTurnCost_virtualViaNode.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void testFiniteUTurnCost_virtualViaNode(String algo) {
    // if there is an extra virtual node it can be possible to do a u-turn that otherwise would not be possible
    // and so there can be a difference between CH and non-CH... therefore u-turns at virtual nodes are forbidden
    // 4->3->2->1-x-0
    // |
    // 5->6
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(4, 3).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 2).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 1).setDistance(0));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 0).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 5).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(5, 6).setDistance(0));
    updateDistancesFor(graph, 4, 0.1, 0.0);
    updateDistancesFor(graph, 3, 0.1, 0.1);
    updateDistancesFor(graph, 2, 0.1, 0.2);
    updateDistancesFor(graph, 1, 0.1, 0.3);
    updateDistancesFor(graph, 0, 0.1, 0.4);
    updateDistancesFor(graph, 5, 0.0, 0.3);
    updateDistancesFor(graph, 6, 0.0, 0.4);
    // not allowed to turn right at node 1 -> we have to take a u-turn at node 0 (or at the virtual node...)
    setRestriction(2, 1, 5);
    graph.freeze();
    chConfig = chConfigs.get(1);
    prepareCH(0, 1, 2, 3, 4, 5, 6);
    LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
    index.prepareIndex();
    GHPoint virtualPoint = new GHPoint(0.1, 0.35);
    Snap snap = index.findClosest(virtualPoint.lat, virtualPoint.lon, EdgeFilter.ALL_EDGES);
    QueryGraph chQueryGraph = QueryGraph.create(graph, snap);
    assertEquals(3, snap.getClosestEdge().getEdge());
    QueryRoutingCHGraph routingCHGraph = new QueryRoutingCHGraph(chGraph, chQueryGraph);
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(4, 6);
    assertTrue(path.isFound());
    assertEquals(IntArrayList.from(4, 3, 2, 1, 0, 1, 5, 6), path.calcNodes());
    Snap snap2 = index.findClosest(virtualPoint.lat, virtualPoint.lon, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, snap2);
    assertEquals(3, snap2.getClosestEdge().getEdge());
    Weighting w = queryGraph.wrapWeighting(chConfig.getWeighting());
    Dijkstra dijkstra = new Dijkstra(queryGraph, w, TraversalMode.EDGE_BASED);
    Path dijkstraPath = dijkstra.calcPath(4, 6);
    assertEquals(IntArrayList.from(4, 3, 2, 1, 7, 0, 7, 1, 5, 6), dijkstraPath.calcNodes());
    assertEquals(dijkstraPath.getWeight(), path.getWeight(), 1.e-2);
    assertEquals(dijkstraPath.getDistance(), path.getDistance(), 1.e-2);
    assertEquals(dijkstraPath.getTime(), path.getTime());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) GHPoint(com.graphhopper.util.shapes.GHPoint) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Dijkstra(com.graphhopper.routing.Dijkstra) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with RoutingAlgorithm

use of com.graphhopper.routing.RoutingAlgorithm in project graphhopper by graphhopper.

the class PrepareContractionHierarchiesTest method testDirectedGraph.

@Test
public void testDirectedGraph() {
    GHUtility.setSpeed(60, true, false, carEncoder, g.edge(5, 4).setDistance(3));
    GHUtility.setSpeed(60, true, false, carEncoder, g.edge(4, 5).setDistance(10));
    GHUtility.setSpeed(60, true, false, carEncoder, g.edge(2, 4).setDistance(1));
    GHUtility.setSpeed(60, true, false, carEncoder, g.edge(5, 2).setDistance(1));
    GHUtility.setSpeed(60, true, false, carEncoder, g.edge(3, 5).setDistance(1));
    GHUtility.setSpeed(60, true, false, carEncoder, g.edge(4, 3).setDistance(1));
    g.freeze();
    assertEquals(6, g.getEdges());
    PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
    PrepareContractionHierarchies.Result result = prepare.doWork();
    assertEquals(2, result.getShortcuts());
    RoutingCHGraph routingCHGraph = g.createCHGraph(result.getCHStorage(), result.getCHConfig());
    assertEquals(6 + 2, routingCHGraph.getEdges());
    RoutingAlgorithm algo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap());
    Path p = algo.calcPath(4, 2);
    assertEquals(3, p.getDistance(), 1e-6);
    assertEquals(IntArrayList.from(4, 3, 5, 2), p.calcNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) Test(org.junit.jupiter.api.Test)

Example 5 with RoutingAlgorithm

use of com.graphhopper.routing.RoutingAlgorithm in project graphhopper by graphhopper.

the class PrepareContractionHierarchiesTest method testRoundaboutUnpacking.

@Test
public void testRoundaboutUnpacking() {
    initRoundaboutGraph(g, carEncoder);
    int oldCount = g.getEdges();
    PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
    useNodeOrdering(prepare, new int[] { 26, 6, 12, 13, 2, 3, 8, 9, 10, 11, 14, 15, 16, 17, 18, 20, 21, 23, 24, 25, 19, 22, 27, 5, 29, 30, 31, 28, 7, 1, 0, 4 });
    PrepareContractionHierarchies.Result res = prepare.doWork();
    assertEquals(oldCount, g.getEdges());
    RoutingCHGraph routingCHGraph = g.createCHGraph(res.getCHStorage(), res.getCHConfig());
    assertEquals(oldCount, routingCHGraph.getBaseGraph().getEdges());
    assertEquals(oldCount + 23, routingCHGraph.getEdges());
    RoutingAlgorithm algo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap());
    Path p = algo.calcPath(4, 7);
    assertEquals(IntArrayList.from(4, 5, 6, 7), p.calcNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) Test(org.junit.jupiter.api.Test)

Aggregations

RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)14 Path (com.graphhopper.routing.Path)12 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)6 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)6 Snap (com.graphhopper.storage.index.Snap)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 ValueSource (org.junit.jupiter.params.provider.ValueSource)6 Test (org.junit.jupiter.api.Test)5 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)3 Dijkstra (com.graphhopper.routing.Dijkstra)2 BooleanEncodedValue (com.graphhopper.routing.ev.BooleanEncodedValue)2 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)2 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)2 Weighting (com.graphhopper.routing.weighting.Weighting)2 GHPoint (com.graphhopper.util.shapes.GHPoint)2 Random (java.util.Random)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 AStar (com.graphhopper.routing.AStar)1 AlgorithmOptions (com.graphhopper.routing.AlgorithmOptions)1 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)1