use of com.graphhopper.storage.TurnCostExtension in project graphhopper by graphhopper.
the class EdgeBasedRoutingAlgorithmTest method testBasicTurnCosts.
@Test
public void testBasicTurnCosts() {
GraphHopperStorage g = createStorage(createEncodingManager(false));
initGraph(g);
TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
Path p = createAlgo(g, AlgorithmOptions.start().weighting(createWeighting(carEncoder, tcs, 40)).traversalMode(TraversalMode.EDGE_BASED_1DIR).build()).calcPath(5, 1);
// no restriction and costs
EdgeIteratorState e3_6 = getEdge(g, 5, 6);
e3_6.setDistance(2);
assertEquals(Helper.createTList(5, 2, 3, 1), p.calcNodes());
// now introduce some turn costs
long tflags = carEncoder.getTurnFlags(false, 2);
tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags);
p = createAlgo(g, AlgorithmOptions.start().weighting(createWeighting(carEncoder, tcs, 40)).traversalMode(TraversalMode.EDGE_BASED_1DIR).build()).calcPath(5, 1);
assertEquals(Helper.createTList(5, 6, 3, 1), p.calcNodes());
}
use of com.graphhopper.storage.TurnCostExtension in project graphhopper by graphhopper.
the class EdgeBasedRoutingAlgorithmTest method testUTurns.
@Test
public void testUTurns() {
GraphHopperStorage g = createStorage(createEncodingManager(true));
initGraph(g);
TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
long tflags = carEncoder.getTurnFlags(true, 0);
// force u-turn via lowering the cost for it
EdgeIteratorState e3_6 = getEdge(g, 3, 6);
e3_6.setDistance(0.1);
getEdge(g, 3, 2).setDistance(864);
getEdge(g, 1, 0).setDistance(864);
tcs.addTurnInfo(getEdge(g, 7, 6).getEdge(), 6, getEdge(g, 6, 5).getEdge(), tflags);
tcs.addTurnInfo(getEdge(g, 4, 3).getEdge(), 3, e3_6.getEdge(), tflags);
AlgorithmOptions opts = AlgorithmOptions.start().weighting(createWeighting(carEncoder, tcs, 50)).traversalMode(TraversalMode.EDGE_BASED_2DIR_UTURN).build();
Path p = createAlgo(g, opts).calcPath(7, 5);
assertEquals(Helper.createTList(7, 6, 3, 6, 5), p.calcNodes());
// no u-turn for 6-3
opts = AlgorithmOptions.start().weighting(createWeighting(carEncoder, tcs, 100)).traversalMode(TraversalMode.EDGE_BASED_2DIR_UTURN).build();
tcs.addTurnInfo(getEdge(g, 6, 3).getEdge(), 3, getEdge(g, 3, 6).getEdge(), tflags);
p = createAlgo(g, opts).calcPath(7, 5);
assertEquals(Helper.createTList(7, 6, 3, 2, 5), p.calcNodes());
}
use of com.graphhopper.storage.TurnCostExtension in project graphhopper by graphhopper.
the class EdgeBasedRoutingAlgorithmTest method testBlockANode.
@Test
public void testBlockANode() {
GraphHopperStorage g = createStorage(createEncodingManager(true));
initGraph(g);
TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
blockNode3(g, tcs, carEncoder);
for (int i = 0; i <= 7; i++) {
if (i == 3)
continue;
for (int j = 0; j <= 7; j++) {
if (j == 3)
continue;
Path p = createAlgo(g, AlgorithmOptions.start().weighting(createWeighting(carEncoder, tcs, 40)).traversalMode(TraversalMode.EDGE_BASED_2DIR).build()).calcPath(i, j);
// We can go from everywhere to everywhere else without using node 3
assertTrue(p.isFound());
for (IntCursor node : p.calcNodes()) {
assertNotEquals(p.calcNodes().toString(), 3, node.value);
}
}
}
}
use of com.graphhopper.storage.TurnCostExtension in project graphhopper by graphhopper.
the class EdgeBasedRoutingAlgorithmTest method testBasicTurnRestriction.
@Test
public void testBasicTurnRestriction() {
GraphHopperStorage g = createStorage(createEncodingManager(true));
initGraph(g);
TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
initTurnRestrictions(g, tcs, carEncoder);
Path p = createAlgo(g, AlgorithmOptions.start().weighting(createWeighting(carEncoder, tcs, 40)).traversalMode(TraversalMode.EDGE_BASED_2DIR).build()).calcPath(5, 1);
assertEquals(Helper.createTList(5, 2, 3, 4, 7, 6, 3, 1), p.calcNodes());
// test 7-6-5 and reverse
p = createAlgo(g, AlgorithmOptions.start().weighting(createWeighting(carEncoder, tcs, 40)).traversalMode(TraversalMode.EDGE_BASED_1DIR).build()).calcPath(5, 7);
assertEquals(Helper.createTList(5, 6, 7), p.calcNodes());
p = createAlgo(g, AlgorithmOptions.start().weighting(createWeighting(carEncoder, tcs, 40)).traversalMode(TraversalMode.EDGE_BASED_1DIR).build()).calcPath(7, 5);
assertEquals(Helper.createTList(7, 6, 3, 2, 5), p.calcNodes());
}
use of com.graphhopper.storage.TurnCostExtension in project graphhopper by graphhopper.
the class TestAlgoCollector method assertDistance.
public TestAlgoCollector assertDistance(AlgoHelperEntry algoEntry, List<QueryResult> queryList, OneRun oneRun) {
List<Path> altPaths = new ArrayList<>();
QueryGraph queryGraph = new QueryGraph(algoEntry.getForQueryGraph());
queryGraph.lookup(queryList);
AlgorithmOptions opts = algoEntry.getAlgorithmOptions();
FlagEncoder encoder = opts.getWeighting().getFlagEncoder();
if (encoder.supports(TurnWeighting.class)) {
if (!opts.getTraversalMode().isEdgeBased()) {
errors.add("Cannot use TurnWeighting with a node based traversal");
return this;
}
algoEntry.setAlgorithmOptions(AlgorithmOptions.start(opts).weighting(new TurnWeighting(opts.getWeighting(), (TurnCostExtension) queryGraph.getExtension())).build());
}
RoutingAlgorithmFactory factory = algoEntry.createRoutingFactory();
for (int i = 0; i < queryList.size() - 1; i++) {
RoutingAlgorithm algo = factory.createAlgo(queryGraph, algoEntry.getAlgorithmOptions());
// if (!algoEntry.getExpectedAlgo().equals(algo.toString())) {
// errors.add("Algorithm expected " + algoEntry.getExpectedAlgo() + " but was " + algo.toString());
// return this;
// }
Path path = algo.calcPath(queryList.get(i).getClosestNode(), queryList.get(i + 1).getClosestNode());
altPaths.add(path);
}
PathMerger pathMerger = new PathMerger().setCalcPoints(true).setSimplifyResponse(false).setEnableInstructions(true);
PathWrapper rsp = new PathWrapper();
pathMerger.doWork(rsp, altPaths, trMap.getWithFallBack(Locale.US));
if (rsp.hasErrors()) {
errors.add("response for " + algoEntry + " contains errors. Expected distance: " + oneRun.getDistance() + ", expected points: " + oneRun + ". " + queryList + ", errors:" + rsp.getErrors());
return this;
}
PointList pointList = rsp.getPoints();
double tmpDist = pointList.calcDistance(distCalc);
if (Math.abs(rsp.getDistance() - tmpDist) > 2) {
errors.add(algoEntry + " path.getDistance was " + rsp.getDistance() + "\t pointList.calcDistance was " + tmpDist + "\t (expected points " + oneRun.getLocs() + ", expected distance " + oneRun.getDistance() + ") " + queryList);
}
if (Math.abs(rsp.getDistance() - oneRun.getDistance()) > 2) {
errors.add(algoEntry + " returns path not matching the expected distance of " + oneRun.getDistance() + "\t Returned was " + rsp.getDistance() + "\t (expected points " + oneRun.getLocs() + ", was " + pointList.getSize() + ") " + queryList);
}
// There are real world instances where A-B-C is identical to A-C (in meter precision).
if (Math.abs(pointList.getSize() - oneRun.getLocs()) > 1) {
errors.add(algoEntry + " returns path not matching the expected points of " + oneRun.getLocs() + "\t Returned was " + pointList.getSize() + "\t (expected distance " + oneRun.getDistance() + ", was " + rsp.getDistance() + ") " + queryList);
}
return this;
}
Aggregations