Search in sources :

Example 1 with Dijkstra

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

the class NodeContractorTest method testShortestPathSkipNode2.

@Test
public void testShortestPathSkipNode2() {
    createExampleGraph();
    final double normalDist = new Dijkstra(graph, weighting, traversalMode).calcPath(4, 2).getDistance();
    assertEquals(3, normalDist, 1e-5);
    DijkstraOneToMany algo = new DijkstraOneToMany(graph, weighting, traversalMode);
    setMaxLevelOnAllNodes();
    algo.setEdgeFilter(new NodeContractor.IgnoreNodeFilter(lg, graph.getNodes() + 1).setAvoidNode(3));
    algo.setWeightLimit(10);
    int nodeEntry = algo.findEndNode(4, 2);
    assertEquals(4, algo.getWeight(nodeEntry), 1e-5);
    nodeEntry = algo.findEndNode(4, 1);
    assertEquals(4, algo.getWeight(nodeEntry), 1e-5);
}
Also used : DijkstraOneToMany(com.graphhopper.routing.DijkstraOneToMany) Dijkstra(com.graphhopper.routing.Dijkstra) Test(org.junit.Test)

Example 2 with Dijkstra

use of com.graphhopper.routing.Dijkstra 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 3 with Dijkstra

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

the class PrepareContractionHierarchiesTest method testReusingNodeOrdering.

@Test
public void testReusingNodeOrdering() {
    CarFlagEncoder carFlagEncoder = new CarFlagEncoder();
    MotorcycleFlagEncoder motorCycleEncoder = new MotorcycleFlagEncoder();
    EncodingManager em = EncodingManager.create(carFlagEncoder, motorCycleEncoder);
    CHConfig carConfig = CHConfig.nodeBased("c1", new FastestWeighting(carFlagEncoder));
    CHConfig motorCycleConfig = CHConfig.nodeBased("c2", new FastestWeighting(motorCycleEncoder));
    GraphHopperStorage ghStorage = new GraphBuilder(em).create();
    int numNodes = 5_000;
    int numQueries = 100;
    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    GHUtility.buildRandomGraph(ghStorage, rnd, numNodes, 1.3, true, true, carFlagEncoder.getAccessEnc(), carFlagEncoder.getAverageSpeedEnc(), null, 0.7, 0.9, 0.8);
    ghStorage.freeze();
    // create CH for cars
    StopWatch sw = new StopWatch().start();
    PrepareContractionHierarchies carPch = PrepareContractionHierarchies.fromGraphHopperStorage(ghStorage, carConfig);
    PrepareContractionHierarchies.Result res = carPch.doWork();
    long timeCar = sw.stop().getMillis();
    // create CH for motorcycles, re-use car contraction order
    // this speeds up contraction significantly, but can lead to slower queries
    sw = new StopWatch().start();
    CHStorage chStore = res.getCHStorage();
    NodeOrderingProvider nodeOrderingProvider = chStore.getNodeOrderingProvider();
    PrepareContractionHierarchies motorCyclePch = PrepareContractionHierarchies.fromGraphHopperStorage(ghStorage, motorCycleConfig).useFixedNodeOrdering(nodeOrderingProvider);
    PrepareContractionHierarchies.Result resMotorCycle = motorCyclePch.doWork();
    RoutingCHGraph motorCycleCH = ghStorage.createCHGraph(resMotorCycle.getCHStorage(), resMotorCycle.getCHConfig());
    // run a few sample queries to check correctness
    for (int i = 0; i < numQueries; ++i) {
        Dijkstra dijkstra = new Dijkstra(ghStorage, motorCycleConfig.getWeighting(), TraversalMode.NODE_BASED);
        RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(motorCycleCH).createAlgo(new PMap());
        int from = rnd.nextInt(numNodes);
        int to = rnd.nextInt(numNodes);
        double dijkstraWeight = dijkstra.calcPath(from, to).getWeight();
        double chWeight = chAlgo.calcPath(from, to).getWeight();
        assertEquals(dijkstraWeight, chWeight, 1.e-1);
    }
    long timeMotorCycle = sw.getMillis();
    assertTrue(timeMotorCycle < 0.5 * timeCar, "reusing node ordering should speed up ch contraction");
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Dijkstra(com.graphhopper.routing.Dijkstra) Random(java.util.Random) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.jupiter.api.Test)

Example 4 with Dijkstra

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

the class InstructionListTest method testNoInstructionIfSlightTurnAndAlternativeIsSharp2.

@Test
public void testNoInstructionIfSlightTurnAndAlternativeIsSharp2() {
    Graph g = new GraphBuilder(carManager).create();
    // Real World Example: https://graphhopper.com/maps/?point=48.748493%2C9.322455&point=48.748776%2C9.321889
    // https://github.com/graphhopper/graphhopper/issues/1441
    // From 1 to 3
    // 
    // 3
    // \
    // 2--- 1
    // \
    // 4
    NodeAccess na = g.getNodeAccess();
    na.setNode(1, 48.748493, 9.322455);
    na.setNode(2, 48.748577, 9.322152);
    na.setNode(3, 48.748776, 9.321889);
    na.setNode(4, 48.74847, 9.322299);
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(1, 2).setDistance(10));
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 3).setDistance(10));
    GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 4).setDistance(10));
    ShortestWeighting weighting = new ShortestWeighting(carEncoder);
    Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 3);
    InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    List<String> tmpList = getTurnDescriptions(wayList);
    assertEquals(Arrays.asList("continue", "arrive at destination"), tmpList);
}
Also used : Path(com.graphhopper.routing.Path) NodeAccess(com.graphhopper.storage.NodeAccess) Graph(com.graphhopper.storage.Graph) GraphBuilder(com.graphhopper.storage.GraphBuilder) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Dijkstra(com.graphhopper.routing.Dijkstra) Test(org.junit.jupiter.api.Test)

Example 5 with Dijkstra

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

the class InstructionListTest method testEmptyList.

@Test
public void testEmptyList() {
    Graph g = new GraphBuilder(carManager).create();
    g.getNodeAccess().setNode(1, 0, 0);
    ShortestWeighting weighting = new ShortestWeighting(carEncoder);
    Path p = new Dijkstra(g, weighting, tMode).calcPath(0, 1);
    InstructionList il = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
    assertEquals(0, il.size());
}
Also used : Path(com.graphhopper.routing.Path) Graph(com.graphhopper.storage.Graph) GraphBuilder(com.graphhopper.storage.GraphBuilder) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Dijkstra(com.graphhopper.routing.Dijkstra) Test(org.junit.jupiter.api.Test)

Aggregations

Dijkstra (com.graphhopper.routing.Dijkstra)21 Path (com.graphhopper.routing.Path)16 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)14 Test (org.junit.jupiter.api.Test)12 Graph (com.graphhopper.storage.Graph)10 GraphBuilder (com.graphhopper.storage.GraphBuilder)10 NodeAccess (com.graphhopper.storage.NodeAccess)8 Weighting (com.graphhopper.routing.weighting.Weighting)4 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)3 Test (org.junit.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 DijkstraBidirectionCH (com.graphhopper.routing.DijkstraBidirectionCH)2 DijkstraOneToMany (com.graphhopper.routing.DijkstraOneToMany)2 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)2 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)2 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)2 Snap (com.graphhopper.storage.index.Snap)2 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)2