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);
}
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);
}
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);
}
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));
}
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());
}
Aggregations