Search in sources :

Example 6 with RoutingAlgorithm

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

the class PrepareContractionHierarchiesTest method testDirectedGraph2.

@Test
public void testDirectedGraph2() {
    initDirected2(g, carEncoder);
    int oldCount = g.getEdges();
    assertEquals(19, oldCount);
    PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g);
    useNodeOrdering(prepare, new int[] { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 11, 12, 13, 14, 15, 16 });
    PrepareContractionHierarchies.Result result = prepare.doWork();
    assertEquals(oldCount, g.getEdges());
    assertEquals(oldCount, GHUtility.count(g.getAllEdges()));
    long numShortcuts = 9;
    assertEquals(numShortcuts, result.getShortcuts());
    assertEquals(oldCount, g.getEdges());
    RoutingCHGraph routingCHGraph = g.createCHGraph(result.getCHStorage(), result.getCHConfig());
    assertEquals(oldCount + numShortcuts, routingCHGraph.getEdges());
    RoutingAlgorithm algo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap());
    Path p = algo.calcPath(0, 10);
    assertEquals(10, p.getDistance(), 1e-6);
    assertEquals(IntArrayList.from(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), p.calcNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) Test(org.junit.jupiter.api.Test)

Example 7 with RoutingAlgorithm

use of com.graphhopper.routing.RoutingAlgorithm 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 8 with RoutingAlgorithm

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

the class PrepareLandmarksTest method testLandmarkStorageAndRouting.

@Test
public void testLandmarkStorageAndRouting() {
    // create graph with lat,lon
    // 0  1  2  ...
    // 15 16 17 ...
    Random rand = new Random(0);
    int width = 15, height = 15;
    DecimalEncodedValue avSpeedEnc = encoder.getAverageSpeedEnc();
    BooleanEncodedValue accessEnc = encoder.getAccessEnc();
    for (int hIndex = 0; hIndex < height; hIndex++) {
        for (int wIndex = 0; wIndex < width; wIndex++) {
            int node = wIndex + hIndex * width;
            // do not connect first with last column!
            double speed = 20 + rand.nextDouble() * 30;
            if (wIndex + 1 < width)
                graph.edge(node, node + 1).set(accessEnc, true, true).set(avSpeedEnc, speed);
            // avoid dead ends
            if (hIndex + 1 < height)
                graph.edge(node, node + width).set(accessEnc, true, true).set(avSpeedEnc, speed);
            updateDistancesFor(graph, node, -hIndex / 50.0, wIndex / 50.0);
        }
    }
    Directory dir = new RAMDirectory();
    LocationIndexTree index = new LocationIndexTree(graph, dir);
    index.prepareIndex();
    int lm = 5, activeLM = 2;
    Weighting weighting = new FastestWeighting(encoder);
    LMConfig lmConfig = new LMConfig("car", weighting);
    LandmarkStorage store = new LandmarkStorage(graph, dir, lmConfig, lm);
    store.setMinimumNodes(2);
    store.createLandmarks();
    // landmarks should be the 4 corners of the grid:
    int[] intList = store.getLandmarks(1);
    Arrays.sort(intList);
    assertEquals("[0, 14, 70, 182, 224]", Arrays.toString(intList));
    // two landmarks: one for subnetwork 0 (all empty) and one for subnetwork 1
    assertEquals(2, store.getSubnetworksWithLandmarks());
    assertEquals(0, store.getFromWeight(0, 224));
    double factor = store.getFactor();
    assertEquals(4671, Math.round(store.getFromWeight(0, 47) * factor));
    assertEquals(3640, Math.round(store.getFromWeight(0, 52) * factor));
    long weight1_224 = store.getFromWeight(1, 224);
    assertEquals(5525, Math.round(weight1_224 * factor));
    long weight1_47 = store.getFromWeight(1, 47);
    assertEquals(921, Math.round(weight1_47 * factor));
    // grid is symmetric
    assertEquals(weight1_224, store.getToWeight(1, 224));
    assertEquals(weight1_47, store.getToWeight(1, 47));
    // prefer the landmarks before and behind the goal
    int[] activeLandmarkIndices = new int[activeLM];
    Arrays.fill(activeLandmarkIndices, -1);
    store.chooseActiveLandmarks(27, 47, activeLandmarkIndices, false);
    List<Integer> list = new ArrayList<>();
    for (int idx : activeLandmarkIndices) {
        list.add(store.getLandmarks(1)[idx]);
    }
    // TODO should better select 0 and 224?
    assertEquals(Arrays.asList(224, 70), list);
    PrepareLandmarks prepare = new PrepareLandmarks(new RAMDirectory(), graph, lmConfig, 4);
    prepare.setMinimumNodes(2);
    prepare.doWork();
    LandmarkStorage lms = prepare.getLandmarkStorage();
    AStar expectedAlgo = new AStar(graph, weighting, tm);
    Path expectedPath = expectedAlgo.calcPath(41, 183);
    PMap hints = new PMap().putObject(Parameters.Landmark.ACTIVE_COUNT, 2);
    // landmarks with A*
    RoutingAlgorithm oneDirAlgoWithLandmarks = new LMRoutingAlgorithmFactory(lms).createAlgo(graph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR).setTraversalMode(tm).setHints(hints));
    Path path = oneDirAlgoWithLandmarks.calcPath(41, 183);
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes() - 135, oneDirAlgoWithLandmarks.getVisitedNodes());
    // landmarks with bidir A*
    RoutingAlgorithm biDirAlgoWithLandmarks = new LMRoutingAlgorithmFactory(lms).createAlgo(graph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR_BI).setTraversalMode(tm).setHints(hints));
    path = biDirAlgoWithLandmarks.calcPath(41, 183);
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes() - 162, biDirAlgoWithLandmarks.getVisitedNodes());
    // landmarks with A* and a QueryGraph. We expect slightly less optimal as two more cycles needs to be traversed
    // due to the two more virtual nodes but this should not harm in practise
    Snap fromSnap = index.findClosest(-0.0401, 0.2201, EdgeFilter.ALL_EDGES);
    Snap toSnap = index.findClosest(-0.2401, 0.0601, EdgeFilter.ALL_EDGES);
    QueryGraph qGraph = QueryGraph.create(graph, fromSnap, toSnap);
    RoutingAlgorithm qGraphOneDirAlgo = new LMRoutingAlgorithmFactory(lms).createAlgo(qGraph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR).setTraversalMode(tm).setHints(hints));
    path = qGraphOneDirAlgo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
    expectedAlgo = new AStar(qGraph, weighting, tm);
    expectedPath = expectedAlgo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes() - 135, qGraphOneDirAlgo.getVisitedNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) ArrayList(java.util.ArrayList) AStar(com.graphhopper.routing.AStar) PMap(com.graphhopper.util.PMap) AlgorithmOptions(com.graphhopper.routing.AlgorithmOptions) Snap(com.graphhopper.storage.index.Snap) Random(java.util.Random) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Directory(com.graphhopper.storage.Directory) RAMDirectory(com.graphhopper.storage.RAMDirectory) Path(com.graphhopper.routing.Path) RAMDirectory(com.graphhopper.storage.RAMDirectory) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 9 with RoutingAlgorithm

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

the class CHTurnCostTest method test_astar_issue2061.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void test_astar_issue2061(String algo) {
    // here the direct path 0-2-3-4-5 is clearly the shortest, however there was a bug in the a-star(-like)
    // algo: first the non-optimal path 0-1-5 is found, but before we find the actual shortest path we explore
    // node 6 during the forward search. the path 0-6-x-5 cannot possibly be the shortest path because 0-6-5
    // is already worse than 0-1-5, even if there was a beeline link from 6 to 5. the problem was that then we
    // cancelled the entire fwd search instead of simply stalling node 6.
    // |-------1-|
    // 7-6---0---2-3-4-5
    BooleanEncodedValue accessEnc = encoder.getAccessEnc();
    DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc();
    graph.edge(0, 1).set(accessEnc, true).set(speedEnc, 60);
    graph.edge(1, 5).set(accessEnc, true).set(speedEnc, 60);
    graph.edge(0, 2).set(accessEnc, true).set(speedEnc, 60);
    graph.edge(2, 3).set(accessEnc, true).set(speedEnc, 60);
    graph.edge(3, 4).set(accessEnc, true).set(speedEnc, 60);
    graph.edge(4, 5).set(accessEnc, true).set(speedEnc, 60);
    graph.edge(0, 6).set(accessEnc, true).set(speedEnc, 60);
    graph.edge(6, 7).set(accessEnc, true).set(speedEnc, 60);
    updateDistancesFor(graph, 0, 46.5, 9.7);
    updateDistancesFor(graph, 1, 46.9, 9.8);
    updateDistancesFor(graph, 2, 46.7, 9.7);
    updateDistancesFor(graph, 4, 46.9, 9.7);
    updateDistancesFor(graph, 3, 46.8, 9.7);
    updateDistancesFor(graph, 5, 47.0, 9.7);
    updateDistancesFor(graph, 6, 46.3, 9.7);
    updateDistancesFor(graph, 7, 46.2, 9.7);
    graph.freeze();
    automaticPrepareCH();
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(chGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(0, 5);
    assertEquals(IntArrayList.from(0, 2, 3, 4, 5), path.calcNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with RoutingAlgorithm

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

the class CHTurnCostTest method testRouteViaVirtualNode.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void testRouteViaVirtualNode(String algo) {
    // 3
    // 0-x-1-2
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 1).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(0));
    updateDistancesFor(graph, 0, 0.00, 0.00);
    updateDistancesFor(graph, 1, 0.02, 0.02);
    updateDistancesFor(graph, 2, 0.03, 0.03);
    graph.freeze();
    automaticPrepareCH();
    LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
    index.prepareIndex();
    Snap snap = index.findClosest(0.01, 0.01, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, snap);
    assertEquals(3, snap.getClosestNode());
    assertEquals(0, snap.getClosestEdge().getEdge());
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(0, 2);
    assertTrue(path.isFound(), "it should be possible to route via a virtual node, but no path found");
    assertEquals(IntArrayList.from(0, 3, 1, 2), path.calcNodes());
    assertEquals(DistancePlaneProjection.DIST_PLANE.calcDist(0.00, 0.00, 0.03, 0.03), path.getDistance(), 1.e-1);
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) 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)

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