Search in sources :

Example 36 with EdgeIteratorState

use of com.graphhopper.util.EdgeIteratorState in project graphhopper by graphhopper.

the class RandomCHRoutingTest method buildRandomGraphLegacy.

/**
 * More or less does the same as {@link GHUtility#buildRandomGraph}, but since some special seeds
 * are used in a few tests above this code is kept here. Do not use it for new tests.
 */
private void buildRandomGraphLegacy(Graph graph, FlagEncoder encoder, Random random, int numNodes, double meanDegree, boolean allowLoops, boolean allowZeroDistance, double pBothDir) {
    for (int i = 0; i < numNodes; ++i) {
        double lat = 49.4 + (random.nextDouble() * 0.0001);
        double lon = 9.7 + (random.nextDouble() * 0.0001);
        graph.getNodeAccess().setNode(i, lat, lon);
    }
    double minDist = Double.MAX_VALUE;
    double maxDist = Double.MIN_VALUE;
    int numEdges = (int) (0.5 * meanDegree * numNodes);
    for (int i = 0; i < numEdges; ++i) {
        int from = random.nextInt(numNodes);
        int to = random.nextInt(numNodes);
        if (!allowLoops && from == to) {
            continue;
        }
        double distance = GHUtility.getDistance(from, to, graph.getNodeAccess());
        if (!allowZeroDistance) {
            distance = Math.max(0.001, distance);
        }
        // add some random offset for most cases, but also allow duplicate edges with same weight
        if (random.nextDouble() < 0.8)
            distance += random.nextDouble() * distance * 0.01;
        minDist = Math.min(minDist, distance);
        maxDist = Math.max(maxDist, distance);
        // using bidirectional edges will increase mean degree of graph above given value
        boolean bothDirections = random.nextDouble() < pBothDir;
        EdgeIteratorState edge = GHUtility.setSpeed(60, true, bothDirections, encoder, graph.edge(from, to).setDistance(distance));
        double fwdSpeed = 10 + random.nextDouble() * 120;
        double bwdSpeed = 10 + random.nextDouble() * 120;
        DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc();
        edge.set(speedEnc, fwdSpeed);
        if (speedEnc.isStoreTwoDirections())
            edge.setReverse(speedEnc, bwdSpeed);
    }
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue)

Example 37 with EdgeIteratorState

use of com.graphhopper.util.EdgeIteratorState in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method withVirtualEdges.

@Test
public void withVirtualEdges() {
    // 2 3
    // 0-x-1-2
    // 3
    na.setNode(0, 50.00, 10.00);
    na.setNode(1, 50.00, 10.10);
    na.setNode(2, 50.00, 10.20);
    EdgeIteratorState edge = addEdge(graph, 0, 1);
    addEdge(graph, 1, 2);
    graph.freeze();
    assertEquals(2, graph.getEdges());
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    Snap snap = new Snap(50.00, 10.05);
    snap.setClosestEdge(edge);
    snap.setWayIndex(0);
    snap.setSnappedPosition(Snap.Position.EDGE);
    snap.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.singletonList(snap));
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    assertEquals(4, queryCHGraph.getNodes());
    assertEquals(2 + 4, queryCHGraph.getEdges());
    assertNodesConnected(queryCHGraph, 1, 2, true);
    // virtual edges at virtual node 3
    assertNodesConnected(queryCHGraph, 0, 3, true);
    assertNodesConnected(queryCHGraph, 3, 1, true);
    // out-iter at real node
    RoutingCHEdgeIterator outIter = queryCHGraph.createOutEdgeExplorer().setBaseNode(2);
    assertNextEdge(outIter, 2, 1, 1);
    assertEnd(outIter);
    // in-iter at real node
    RoutingCHEdgeIterator inIter = queryCHGraph.createInEdgeExplorer().setBaseNode(2);
    assertNextEdge(inIter, 2, 1, 1);
    assertEnd(inIter);
    // out-iter at real node next to virtual node
    outIter = queryCHGraph.createOutEdgeExplorer().setBaseNode(0);
    assertNextEdge(outIter, 0, 3, 2);
    assertEnd(outIter);
    // in-iter at real node next to virtual node
    inIter = queryCHGraph.createInEdgeExplorer().setBaseNode(1);
    assertNextEdge(inIter, 1, 3, 3);
    assertNextEdge(inIter, 1, 2, 1);
    assertEnd(inIter);
    // out-iter at virtual node
    outIter = queryCHGraph.createOutEdgeExplorer().setBaseNode(3);
    assertNextEdge(outIter, 3, 0, 2);
    assertNextEdge(outIter, 3, 1, 3);
    assertEnd(outIter);
    // in-iter at virtual node
    inIter = queryCHGraph.createInEdgeExplorer().setBaseNode(3);
    assertNextEdge(inIter, 3, 0, 2);
    assertNextEdge(inIter, 3, 1, 3);
    assertEnd(inIter);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 38 with EdgeIteratorState

use of com.graphhopper.util.EdgeIteratorState in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method getLevel.

@Test
public void getLevel() {
    // 0-x-1
    na.setNode(0, 50.00, 10.00);
    na.setNode(1, 50.00, 10.10);
    EdgeIteratorState edge = addEdge(graph, 0, 1);
    graph.freeze();
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    CHStorageBuilder chBuilder = new CHStorageBuilder(chStore);
    chBuilder.setLevel(0, 5);
    chBuilder.setLevel(1, 7);
    Snap snap = new Snap(50.00, 10.05);
    snap.setClosestEdge(edge);
    snap.setWayIndex(0);
    snap.setSnappedPosition(Snap.Position.EDGE);
    snap.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.singletonList(snap));
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    assertEquals(5, queryCHGraph.getLevel(0));
    assertEquals(7, queryCHGraph.getLevel(1));
    assertEquals(Integer.MAX_VALUE, queryCHGraph.getLevel(2));
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 39 with EdgeIteratorState

use of com.graphhopper.util.EdgeIteratorState in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method getWeight.

@Test
public void getWeight() {
    // /---\
    // 0-x-1-2
    // 3
    na.setNode(0, 50.00, 10.00);
    na.setNode(1, 50.00, 10.10);
    na.setNode(2, 50.00, 10.20);
    EdgeIteratorState edge = addEdge(graph, 0, 1).set(encoder.getAverageSpeedEnc(), 90, 30);
    addEdge(graph, 1, 2);
    graph.freeze();
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    CHStorageBuilder chBuilder = new CHStorageBuilder(chStore);
    chBuilder.setIdentityLevels();
    chBuilder.addShortcutEdgeBased(0, 2, PrepareEncoder.getScDirMask(), 20, 0, 1, 0, 1);
    // without query graph
    RoutingCHEdgeIterator iter = routingCHGraph.createOutEdgeExplorer().setBaseNode(0);
    assertNextShortcut(iter, 0, 2, 0, 1);
    assertEquals(20, iter.getWeight(false), 1.e-6);
    assertEquals(20, iter.getWeight(true), 1.e-6);
    assertNextEdge(iter, 0, 1, 0);
    assertEquals(285.89888, iter.getWeight(false), 1.e-6);
    assertEquals(857.69664, iter.getWeight(true), 1.e-6);
    assertEnd(iter);
    // for incoming edges its the same
    iter = routingCHGraph.createInEdgeExplorer().setBaseNode(0);
    assertNextShortcut(iter, 0, 2, 0, 1);
    assertEquals(20, iter.getWeight(false), 1.e-6);
    assertEquals(20, iter.getWeight(true), 1.e-6);
    assertNextEdge(iter, 0, 1, 0);
    assertEquals(285.89888, iter.getWeight(false), 1.e-6);
    assertEquals(857.69664, iter.getWeight(true), 1.e-6);
    assertEnd(iter);
    // now including virtual edges
    Snap snap = new Snap(50.00, 10.05);
    snap.setClosestEdge(edge);
    snap.setWayIndex(0);
    snap.setSnappedPosition(Snap.Position.EDGE);
    snap.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.singletonList(snap));
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    iter = queryCHGraph.createOutEdgeExplorer().setBaseNode(0);
    assertNextEdge(iter, 0, 3, 2);
    // should be about half the weight as for the original edge as the query point is in the middle of the edge
    assertEquals(142.9494, iter.getWeight(false), 1.e-4);
    assertEquals(428.8483, iter.getWeight(true), 1.e-4);
    assertNextShortcut(iter, 0, 2, 0, 1);
    assertEquals(20, iter.getWeight(false), 1.e-6);
    assertEquals(20, iter.getWeight(true), 1.e-6);
    assertEnd(iter);
    iter = queryCHGraph.createInEdgeExplorer().setBaseNode(0);
    assertNextEdge(iter, 0, 3, 2);
    assertEquals(142.9494, iter.getWeight(false), 1.e-4);
    assertEquals(428.8483, iter.getWeight(true), 1.e-4);
    assertNextShortcut(iter, 0, 2, 0, 1);
    assertEquals(20, iter.getWeight(false), 1.e-6);
    assertEquals(20, iter.getWeight(true), 1.e-6);
    assertEnd(iter);
    // at the virtual node
    iter = queryCHGraph.createOutEdgeExplorer().setBaseNode(3);
    assertNextEdge(iter, 3, 0, 2);
    assertEquals(428.8483, iter.getWeight(false), 1.e-4);
    assertEquals(142.9494, iter.getWeight(true), 1.e-4);
    assertNextEdge(iter, 3, 1, 3);
    assertEquals(142.9494, iter.getWeight(false), 1.e-4);
    assertEquals(428.8483, iter.getWeight(true), 1.e-4);
    assertEnd(iter);
    iter = queryCHGraph.createInEdgeExplorer().setBaseNode(3);
    assertNextEdge(iter, 3, 0, 2);
    assertEquals(428.8483, iter.getWeight(false), 1.e-4);
    assertEquals(142.9494, iter.getWeight(true), 1.e-4);
    assertNextEdge(iter, 3, 1, 3);
    assertEquals(142.9494, iter.getWeight(false), 1.e-4);
    assertEquals(428.8483, iter.getWeight(true), 1.e-4);
    assertEnd(iter);
    // getting a single edge
    RoutingCHEdgeIteratorState edgeState = queryCHGraph.getEdgeIteratorState(3, 3);
    assertEdgeState(edgeState, 0, 3, 2);
    assertEquals(142.9494, edgeState.getWeight(false), 1.e-4);
    assertEquals(428.8483, edgeState.getWeight(true), 1.e-4);
    edgeState = queryCHGraph.getEdgeIteratorState(3, 0);
    assertEdgeState(edgeState, 3, 0, 2);
    assertEquals(428.8483, edgeState.getWeight(false), 1.e-4);
    assertEquals(142.9494, edgeState.getWeight(true), 1.e-4);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 40 with EdgeIteratorState

use of com.graphhopper.util.EdgeIteratorState in project graphhopper by graphhopper.

the class LandmarkStorageTest method testInfiniteWeight.

@Test
public void testInfiniteWeight() {
    Directory dir = new RAMDirectory();
    EdgeIteratorState edge = graph.edge(0, 1);
    int res = new LandmarkStorage(graph, dir, new LMConfig("c1", new FastestWeighting(encoder) {

        @Override
        public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
            return Integer.MAX_VALUE * 2L;
        }
    }), 8).setMaximumWeight(LandmarkStorage.PRECISION).calcWeight(edge, false);
    assertEquals(Integer.MAX_VALUE, res);
    dir = new RAMDirectory();
    res = new LandmarkStorage(graph, dir, new LMConfig("c2", new FastestWeighting(encoder) {

        @Override
        public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
            return Double.POSITIVE_INFINITY;
        }
    }), 8).setMaximumWeight(LandmarkStorage.PRECISION).calcWeight(edge, false);
    assertEquals(Integer.MAX_VALUE, res);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) RAMDirectory(com.graphhopper.storage.RAMDirectory) RAMDirectory(com.graphhopper.storage.RAMDirectory) Directory(com.graphhopper.storage.Directory) RoutingAlgorithmTest(com.graphhopper.routing.RoutingAlgorithmTest) Test(org.junit.jupiter.api.Test)

Aggregations

EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)137 Test (org.junit.jupiter.api.Test)55 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 Test (org.junit.Test)22 RepeatedTest (org.junit.jupiter.api.RepeatedTest)17 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)13 Snap (com.graphhopper.storage.index.Snap)12 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)11 CHEdgeIteratorState (com.graphhopper.util.CHEdgeIteratorState)11 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)10 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)10 NodeAccess (com.graphhopper.storage.NodeAccess)10 GHIntHashSet (com.graphhopper.coll.GHIntHashSet)9 GraphBuilder (com.graphhopper.storage.GraphBuilder)8 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)7 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)6 EncodingManager (com.graphhopper.routing.util.EncodingManager)6 GHPoint (com.graphhopper.util.shapes.GHPoint)6 Fun (org.mapdb.Fun)6 IntArrayList (com.carrotsearch.hppc.IntArrayList)5