Search in sources :

Example 21 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.

the class AlternativeRouteEdgeCHTest method createTestGraph.

public GraphHopperStorage createTestGraph(EncodingManager tmpEM) {
    final GraphHopperStorage graph = new GraphBuilder(tmpEM).withTurnCosts(true).create();
    /*

           9      11
          /\     /  \
         1  2-3-4-10-12
         \   /   \
         5--6-7---8

         */
    // Make all edges the length of T, the distance around v than an s->v->t path
    // has to be locally-shortest to be considered.
    // So we get all three alternatives.
    FlagEncoder encoder = carFE;
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(5, 6).setDistance(10000));
    EdgeIteratorState e6_3 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(6, 3).setDistance(10000));
    EdgeIteratorState e3_4 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(3, 4).setDistance(10000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(4, 10).setDistance(10000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(6, 7).setDistance(10000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(7, 8).setDistance(10000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(8, 4).setDistance(10000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(5, 1).setDistance(10000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 9).setDistance(10000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(9, 2).setDistance(10000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 3).setDistance(10000));
    EdgeIteratorState e4_11 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(4, 11).setDistance(9000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(11, 12).setDistance(9000));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(12, 10).setDistance(10000));
    TurnCostStorage turnCostStorage = graph.getTurnCostStorage();
    DecimalEncodedValue carTurnCost = em.getDecimalEncodedValue(TurnCost.key(carFE.toString()));
    turnCostStorage.set(carTurnCost, e3_4.getEdge(), 4, e4_11.getEdge(), Double.POSITIVE_INFINITY);
    turnCostStorage.set(carTurnCost, e6_3.getEdge(), 3, e3_4.getEdge(), Double.POSITIVE_INFINITY);
    graph.freeze();
    return graph;
}
Also used : CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue)

Example 22 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue 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 23 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.

the class QueryGraphTest method testVirtualEdgeIds_reverse.

@Test
public void testVirtualEdgeIds_reverse() {
    // virtual nodes:     2
    // 0 - x - 1
    // virtual edges:   1   2
    FlagEncoder encoder = new CarFlagEncoder(new PMap().putObject("speed_two_directions", true));
    EncodingManager encodingManager = EncodingManager.create(encoder);
    DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc();
    Graph g = new GraphBuilder(encodingManager).create();
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 50.00, 10.10);
    na.setNode(1, 50.00, 10.20);
    double dist = DistanceCalcEarth.DIST_EARTH.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1));
    // this time we store the edge the other way
    EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 0).setDistance(dist));
    edge.set(speedEnc, 100, 50);
    // query graph
    Snap snap = createLocationResult(50.00, 10.15, edge, 0, EDGE);
    QueryGraph queryGraph = QueryGraph.create(g, snap);
    assertEquals(3, queryGraph.getNodes());
    assertEquals(5, queryGraph.getEdges());
    assertEquals(4, queryGraph.getVirtualEdges().size());
    EdgeIteratorState edge_0x = queryGraph.getEdgeIteratorState(1, 2);
    EdgeIteratorState edge_x0 = queryGraph.getEdgeIteratorState(1, 0);
    EdgeIteratorState edge_x1 = queryGraph.getEdgeIteratorState(2, 1);
    EdgeIteratorState edge_1x = queryGraph.getEdgeIteratorState(2, 2);
    assertNodes(edge_0x, 0, 2);
    assertNodes(edge_x0, 2, 0);
    assertNodes(edge_x1, 2, 1);
    assertNodes(edge_1x, 1, 2);
    // virtual edge IDs are 1 and 2
    assertEquals(1, edge_0x.getEdge());
    assertEquals(1, edge_x0.getEdge());
    assertEquals(2, edge_x1.getEdge());
    assertEquals(2, edge_1x.getEdge());
    // edge keys
    assertEquals(2, edge_0x.getEdgeKey());
    assertEquals(3, edge_x0.getEdgeKey());
    assertEquals(4, edge_x1.getEdgeKey());
    assertEquals(5, edge_1x.getEdgeKey());
    assertNodes(queryGraph.getEdgeIteratorStateForKey(2), 0, 2);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(3), 2, 0);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(4), 2, 1);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(5), 1, 2);
    // internally each edge is represented by two edge states for the two directions
    assertSame(queryGraph.getVirtualEdges().get(0), edge_0x);
    assertSame(queryGraph.getVirtualEdges().get(1), edge_x0);
    assertSame(queryGraph.getVirtualEdges().get(2), edge_x1);
    assertSame(queryGraph.getVirtualEdges().get(3), edge_1x);
    for (EdgeIteratorState e : Arrays.asList(edge_0x, edge_x1)) {
        assertEquals(50, e.get(speedEnc), 1.e-6);
        assertEquals(100, e.getReverse(speedEnc), 1.e-6);
    }
    for (EdgeIteratorState e : Arrays.asList(edge_x0, edge_1x)) {
        assertEquals(100, e.get(speedEnc), 1.e-6);
        assertEquals(50, e.getReverse(speedEnc), 1.e-6);
    }
    try {
        queryGraph.getEdgeIteratorState(3, 2);
        fail("there should be an error");
    } catch (IndexOutOfBoundsException e) {
    // ok
    }
}
Also used : Snap(com.graphhopper.storage.index.Snap) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) Test(org.junit.jupiter.api.Test)

Example 24 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.

the class QueryGraphTest method testOneWayLoop_Issue162.

@Test
public void testOneWayLoop_Issue162() {
    // do query at x, where edge is oneway
    // 
    // |\
    // | x
    // 0<-\
    // |
    // 1
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0, 0);
    na.setNode(1, 0, -0.001);
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(10));
    BooleanEncodedValue accessEnc = encoder.getAccessEnc();
    DecimalEncodedValue avSpeedEnc = encoder.getAverageSpeedEnc();
    // in the case of identical nodes the wayGeometry defines the direction!
    EdgeIteratorState edge = g.edge(0, 0).setDistance(100).set(accessEnc, true, false).set(avSpeedEnc, 20.0).setWayGeometry(Helper.createPointList(0.001, 0, 0, 0.001));
    Snap snap = new Snap(0.0011, 0.0009);
    snap.setClosestEdge(edge);
    snap.setWayIndex(1);
    snap.calcSnappedPoint(new DistanceCalcEuclidean());
    QueryGraph qg = lookup(snap);
    EdgeExplorer ee = qg.createEdgeExplorer();
    assertTrue(snap.getClosestNode() > 1);
    assertEquals(2, GHUtility.count(ee.setBaseNode(snap.getClosestNode())));
    EdgeIterator iter = ee.setBaseNode(snap.getClosestNode());
    iter.next();
    assertTrue(iter.get(accessEnc), iter.toString());
    assertFalse(iter.getReverse(accessEnc), iter.toString());
    iter.next();
    assertFalse(iter.get(accessEnc), iter.toString());
    assertTrue(iter.getReverse(accessEnc), iter.toString());
}
Also used : BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) Snap(com.graphhopper.storage.index.Snap) Test(org.junit.jupiter.api.Test)

Example 25 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue 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

DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)26 Test (org.junit.jupiter.api.Test)15 BooleanEncodedValue (com.graphhopper.routing.ev.BooleanEncodedValue)6 Snap (com.graphhopper.storage.index.Snap)6 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)6 ReaderWay (com.graphhopper.reader.ReaderWay)4 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)4 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)4 IntsRef (com.graphhopper.storage.IntsRef)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 EncodingManager (com.graphhopper.routing.util.EncodingManager)3 DefaultTurnCostProvider (com.graphhopper.routing.weighting.DefaultTurnCostProvider)3 Weighting (com.graphhopper.routing.weighting.Weighting)3 Graph (com.graphhopper.storage.Graph)3 TurnCostStorage (com.graphhopper.storage.TurnCostStorage)3 Path (com.graphhopper.routing.Path)2 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)2 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)2 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 GraphBuilder (com.graphhopper.storage.GraphBuilder)2