Search in sources :

Example 16 with DecimalEncodedValue

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

the class TurnCostStorageTest method testMultipleTurnCosts.

/**
 * Test if multiple turn costs can be safely written to the storage and read from it.
 */
@Test
public void testMultipleTurnCosts() {
    GraphHopperStorage g = new GraphBuilder(manager).create();
    initGraph(g);
    TurnCostStorage turnCostStorage = g.getTurnCostStorage();
    DecimalEncodedValue carEnc = manager.getDecimalEncodedValue(TurnCost.key("car"));
    DecimalEncodedValue bikeEnc = manager.getDecimalEncodedValue(TurnCost.key("bike"));
    int edge42 = getEdge(g, 4, 2).getEdge();
    int edge23 = getEdge(g, 2, 3).getEdge();
    int edge31 = getEdge(g, 3, 1).getEdge();
    int edge10 = getEdge(g, 1, 0).getEdge();
    int edge02 = getEdge(g, 0, 2).getEdge();
    int edge24 = getEdge(g, 2, 4).getEdge();
    turnCostStorage.set(carEnc, edge42, 2, edge23, Double.POSITIVE_INFINITY);
    turnCostStorage.set(bikeEnc, edge42, 2, edge23, Double.POSITIVE_INFINITY);
    turnCostStorage.set(carEnc, edge23, 3, edge31, Double.POSITIVE_INFINITY);
    turnCostStorage.set(bikeEnc, edge23, 3, edge31, 2.0);
    turnCostStorage.set(carEnc, edge31, 1, edge10, 2.0);
    turnCostStorage.set(bikeEnc, edge31, 1, edge10, Double.POSITIVE_INFINITY);
    turnCostStorage.set(bikeEnc, edge02, 2, edge24, Double.POSITIVE_INFINITY);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(carEnc, edge42, 2, edge23), 0);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(bikeEnc, edge42, 2, edge23), 0);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(carEnc, edge23, 3, edge31), 0);
    assertEquals(2.0, turnCostStorage.get(bikeEnc, edge23, 3, edge31), 0);
    assertEquals(2.0, turnCostStorage.get(carEnc, edge31, 1, edge10), 0);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(bikeEnc, edge31, 1, edge10), 0);
    assertEquals(0.0, turnCostStorage.get(carEnc, edge02, 2, edge24), 0);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(bikeEnc, edge02, 2, edge24), 0);
    turnCostStorage.set(carEnc, edge02, 2, edge23, Double.POSITIVE_INFINITY);
    turnCostStorage.set(bikeEnc, edge02, 2, edge23, Double.POSITIVE_INFINITY);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(carEnc, edge02, 2, edge23), 0);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(bikeEnc, edge02, 2, edge23), 0);
    Set<List<Integer>> allTurnRelations = new HashSet<>();
    TurnCostStorage.TurnRelationIterator iterator = turnCostStorage.getAllTurnRelations();
    while (iterator.next()) {
        allTurnRelations.add(Arrays.asList(iterator.getFromEdge(), iterator.getViaNode(), iterator.getToEdge(), (int) iterator.getCost(carEnc), (int) iterator.getCost(bikeEnc)));
    }
    Set<List<Integer>> expectedTurnRelations = new HashSet<>();
    expectedTurnRelations.add(Arrays.asList(edge31, 1, edge10, 2, Integer.MAX_VALUE));
    expectedTurnRelations.add(Arrays.asList(edge42, 2, edge23, Integer.MAX_VALUE, Integer.MAX_VALUE));
    expectedTurnRelations.add(Arrays.asList(edge02, 2, edge24, 0, Integer.MAX_VALUE));
    expectedTurnRelations.add(Arrays.asList(edge02, 2, edge23, Integer.MAX_VALUE, Integer.MAX_VALUE));
    expectedTurnRelations.add(Arrays.asList(edge23, 3, edge31, Integer.MAX_VALUE, 2));
    assertEquals(expectedTurnRelations, allTurnRelations);
}
Also used : DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 17 with DecimalEncodedValue

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

the class TurnCostStorageTest method testMergeFlagsBeforeAdding.

@Test
public void testMergeFlagsBeforeAdding() {
    GraphHopperStorage g = new GraphBuilder(manager).create();
    initGraph(g);
    TurnCostStorage turnCostStorage = g.getTurnCostStorage();
    DecimalEncodedValue carEnc = manager.getDecimalEncodedValue(TurnCost.key("car"));
    DecimalEncodedValue bikeEnc = manager.getDecimalEncodedValue(TurnCost.key("bike"));
    int edge23 = getEdge(g, 2, 3).getEdge();
    int edge02 = getEdge(g, 0, 2).getEdge();
    turnCostStorage.set(carEnc, edge02, 2, edge23, Double.POSITIVE_INFINITY);
    turnCostStorage.set(bikeEnc, edge02, 2, edge23, Double.POSITIVE_INFINITY);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(carEnc, edge02, 2, edge23), 0);
    assertEquals(Double.POSITIVE_INFINITY, turnCostStorage.get(bikeEnc, edge02, 2, edge23), 0);
    Set<List<Integer>> allTurnRelations = new HashSet<>();
    TurnCostStorage.TurnRelationIterator iterator = turnCostStorage.getAllTurnRelations();
    while (iterator.next()) {
        allTurnRelations.add(Arrays.asList(iterator.getFromEdge(), iterator.getViaNode(), iterator.getToEdge(), (int) iterator.getCost(carEnc), (int) iterator.getCost(bikeEnc)));
    }
    Set<List<Integer>> expectedTurnRelations = new HashSet<>();
    expectedTurnRelations.add(Arrays.asList(edge02, 2, edge23, Integer.MAX_VALUE, Integer.MAX_VALUE));
    assertEquals(expectedTurnRelations, allTurnRelations);
}
Also used : DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 18 with DecimalEncodedValue

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

the class DefaultBidirPathExtractorTest method testExtract2.

@Test
public void testExtract2() {
    // 1->2->3
    Graph graph = createGraph();
    GHUtility.setSpeed(60, true, false, carEncoder, graph.edge(1, 2).setDistance(10));
    GHUtility.setSpeed(60, true, false, carEncoder, graph.edge(2, 3).setDistance(20));
    // add some turn costs at node 2 where fwd&bwd searches meet. these costs have to be included in the
    // weight and the time of the path
    TurnCostStorage turnCostStorage = graph.getTurnCostStorage();
    DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(carEncoder.toString()));
    turnCostStorage.set(turnCostEnc, 0, 2, 1, 5);
    SPTEntry fwdEntry = new SPTEntry(0, 2, 0.6);
    fwdEntry.parent = new SPTEntry(EdgeIterator.NO_EDGE, 1, 0);
    SPTEntry bwdEntry = new SPTEntry(1, 2, 1.2);
    bwdEntry.parent = new SPTEntry(EdgeIterator.NO_EDGE, 3, 0);
    Path p = DefaultBidirPathExtractor.extractPath(graph, new FastestWeighting(carEncoder, new DefaultTurnCostProvider(carEncoder, turnCostStorage)), fwdEntry, bwdEntry, 0);
    p.setWeight(5 + 1.8);
    assertEquals(IntArrayList.from(1, 2, 3), p.calcNodes());
    assertEquals(30, p.getDistance(), 1e-4);
    assertEquals(5 + 1.8, p.getWeight(), 1e-4);
    assertEquals(5000 + 1800, p.getTime(), 1.e-6);
}
Also used : Graph(com.graphhopper.storage.Graph) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) TurnCostStorage(com.graphhopper.storage.TurnCostStorage) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) DefaultTurnCostProvider(com.graphhopper.routing.weighting.DefaultTurnCostProvider) Test(org.junit.jupiter.api.Test)

Example 19 with DecimalEncodedValue

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

the class QueryRoutingCHGraphTest method getTurnCost.

@Test
public void getTurnCost() {
    // /-----\
    // 0-x-1-x-2
    // 3   4
    na.setNode(0, 50.00, 10.00);
    na.setNode(1, 50.00, 10.10);
    na.setNode(2, 50.00, 10.20);
    EdgeIteratorState edge1 = addEdge(graph, 0, 1);
    EdgeIteratorState edge2 = addEdge(graph, 1, 2);
    DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(encoder.toString()));
    graph.getTurnCostStorage().set(turnCostEnc, 0, 1, 1, 5);
    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.getScFwdDir(), 20, 0, 1, 0, 1);
    // without virtual nodes
    assertEquals(5, routingCHGraph.getTurnWeight(0, 1, 1));
    // with virtual nodes
    Snap snap1 = new Snap(50.00, 10.05);
    snap1.setClosestEdge(edge1);
    snap1.setWayIndex(0);
    snap1.setSnappedPosition(Snap.Position.EDGE);
    snap1.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    Snap snap2 = new Snap(50.00, 10.15);
    snap2.setClosestEdge(edge2);
    snap2.setWayIndex(0);
    snap2.setSnappedPosition(Snap.Position.EDGE);
    snap2.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    QueryGraph queryGraph = QueryGraph.create(graph, Arrays.asList(snap1, snap2));
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    assertEquals(5, queryCHGraph.getTurnWeight(0, 1, 1));
    // take a look at edges 3->1 and 1->4, their original edge ids are 3 and 4 (not 4 and 5)
    assertNodesConnected(queryCHGraph, 3, 1, true);
    assertNodesConnected(queryCHGraph, 1, 4, true);
    int expectedEdge31 = 3;
    int expectedEdge14 = 4;
    RoutingCHEdgeIterator iter = queryCHGraph.createOutEdgeExplorer().setBaseNode(3);
    assertNextEdge(iter, 3, 0, 2);
    assertNextEdge(iter, 3, 1, expectedEdge31);
    assertEnd(iter);
    iter = queryCHGraph.createOutEdgeExplorer().setBaseNode(1);
    assertNextEdge(iter, 1, 3, 3);
    assertNextEdge(iter, 1, 4, expectedEdge14);
    assertEnd(iter);
    // check the turn weight between these edges
    assertEquals(5, queryCHGraph.getTurnWeight(expectedEdge31, 1, expectedEdge14));
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) 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 20 with DecimalEncodedValue

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

the class DijkstraBidirectionCHTest method runTestWithDirectionDependentEdgeSpeed.

private void runTestWithDirectionDependentEdgeSpeed(double speed, double revSpeed, int from, int to, IntArrayList expectedPath, FlagEncoder encoder) {
    GraphHopperStorage graph = createGHStorage();
    EdgeIteratorState edge = GHUtility.setSpeed(encoder.getMaxSpeed() / 2, true, true, encoder, graph.edge(0, 1).setDistance(2));
    DecimalEncodedValue avSpeedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(encoder, "average_speed"));
    edge.set(avSpeedEnc, speed, revSpeed);
    GHUtility.setSpeed(encoder.getMaxSpeed() / 2, true, true, encoder, graph.edge(1, 2).setDistance(1));
    graph.freeze();
    FastestWeighting weighting = new FastestWeighting(encoder);
    CHConfig chConfig = CHConfig.nodeBased(weighting.getName(), weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    new CHStorageBuilder(chStore).setIdentityLevels();
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    RoutingAlgorithm algo = createCHAlgo(routingCHGraph, true);
    Path p = algo.calcPath(from, to);
    assertEquals(3, p.getDistance(), 1.e-3);
    assertEquals(expectedPath, p.calcNodes(), p.toString());
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting)

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