use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class InstructionListTest method testNoInstructionIfSlightTurnAndAlternativeIsSharp.
@Test
public void testNoInstructionIfSlightTurnAndAlternativeIsSharp() {
Graph g = new GraphBuilder(carManager).create();
// Real World Example: https://graphhopper.com/maps/?point=51.734514%2C9.225571&point=51.734643%2C9.22541
// https://github.com/graphhopper/graphhopper/issues/1441
// From 1 to 3
//
// 3
// |
// 2
// /\
// 4 1
NodeAccess na = g.getNodeAccess();
na.setNode(1, 51.734514, 9.225571);
na.setNode(2, 51.73458, 9.225442);
na.setNode(3, 51.734643, 9.22541);
na.setNode(4, 51.734451, 9.225436);
GHUtility.setSpeed(60, true, true, carEncoder, g.edge(1, 2).setDistance(10));
GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 3).setDistance(10));
GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 4).setDistance(10));
ShortestWeighting weighting = new ShortestWeighting(carEncoder);
Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 3);
InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
List<String> tmpList = getTurnDescriptions(wayList);
assertEquals(Arrays.asList("continue", "arrive at destination"), tmpList);
}
use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class PathSimplificationTest method testScenario.
@Test
public void testScenario() {
Graph g = new GraphBuilder(carManager).create();
// 0-1-2
// | | |
// 3-4-5 9-10
// | | | |
// 6-7-8--*
NodeAccess na = g.getNodeAccess();
na.setNode(0, 1.2, 1.0);
na.setNode(1, 1.2, 1.1);
na.setNode(2, 1.2, 1.2);
na.setNode(3, 1.1, 1.0);
na.setNode(4, 1.1, 1.1);
na.setNode(5, 1.1, 1.2);
na.setNode(9, 1.1, 1.3);
na.setNode(10, 1.1, 1.4);
na.setNode(6, 1.0, 1.0);
na.setNode(7, 1.0, 1.1);
na.setNode(8, 1.0, 1.2);
ReaderWay w = new ReaderWay(1);
w.setTag("highway", "tertiary");
w.setTag("maxspeed", "10");
IntsRef relFlags = carManager.createRelationFlags();
EdgeIteratorState tmpEdge;
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(0, 1).setDistance(10000)).setName("0-1");
assertNotEquals(EncodingManager.Access.CAN_SKIP, carEncoder.getAccess(w));
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(1, 2).setDistance(11000)).setName("1-2");
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
w.setTag("maxspeed", "20");
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(0, 3).setDistance(11000));
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(1, 4).setDistance(10000)).setName("1-4");
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 5).setDistance(11000)).setName("5-2");
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
w.setTag("maxspeed", "30");
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(3, 6).setDistance(11000)).setName("3-6");
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(4, 7).setDistance(10000)).setName("4-7");
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(5, 8).setDistance(10000)).setName("5-8");
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
w.setTag("maxspeed", "40");
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(6, 7).setDistance(11000)).setName("6-7");
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(7, 8).setDistance(10000));
PointList list = new PointList();
list.add(1.0, 1.15);
list.add(1.0, 1.16);
tmpEdge.setWayGeometry(list);
tmpEdge.setName("7-8");
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
w.setTag("maxspeed", "50");
// missing edge name
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(9, 10).setDistance(10000));
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
tmpEdge = GHUtility.setSpeed(60, true, true, carEncoder, g.edge(8, 9).setDistance(20000));
list.clear();
list.add(1.0, 1.3);
list.add(1.0, 1.3001);
list.add(1.0, 1.3002);
list.add(1.0, 1.3003);
tmpEdge.setName("8-9");
tmpEdge.setWayGeometry(list);
tmpEdge.setFlags(carManager.handleWayTags(w, relFlags));
// Path is: [0 0-1, 3 1-4, 6 4-7, 9 7-8, 11 8-9, 10 9-10]
ShortestWeighting weighting = new ShortestWeighting(carEncoder);
Path p = new Dijkstra(g, weighting, tMode).calcPath(0, 10);
InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
Map<String, List<PathDetail>> details = PathDetailsFromEdges.calcDetails(p, carManager, weighting, Arrays.asList(AVERAGE_SPEED), new PathDetailsBuilderFactory(), 0);
ResponsePath responsePath = new ResponsePath();
responsePath.setInstructions(wayList);
responsePath.addPathDetails(details);
responsePath.setPoints(p.calcPoints());
int numberOfPoints = p.calcPoints().size();
DouglasPeucker douglasPeucker = new DouglasPeucker();
// Do not simplify anything
douglasPeucker.setMaxDistance(0);
PathSimplification.simplify(responsePath, douglasPeucker, true);
assertEquals(numberOfPoints, responsePath.getPoints().size());
responsePath = new ResponsePath();
responsePath.setInstructions(wayList);
responsePath.addPathDetails(details);
responsePath.setPoints(p.calcPoints());
douglasPeucker.setMaxDistance(100000000);
PathSimplification.simplify(responsePath, douglasPeucker, true);
assertTrue(numberOfPoints > responsePath.getPoints().size());
}
use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class CHTurnCostTest method findPathUsingDijkstra.
private Path findPathUsingDijkstra(int from, int to) {
Weighting w = graph.wrapWeighting(chConfig.getWeighting());
Dijkstra dijkstra = new Dijkstra(graph, w, TraversalMode.EDGE_BASED);
return dijkstra.calcPath(from, to);
}
use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class LMApproximatorTest method run.
private void run(long seed) {
Directory dir = new RAMDirectory();
CarFlagEncoder encoder = new CarFlagEncoder(5, 5, 1);
EncodingManager encodingManager = new EncodingManager.Builder().add(encoder).add(Subnetwork.create("car")).build();
GraphHopperStorage graph = new GraphBuilder(encodingManager).setDir(dir).withTurnCosts(true).create();
Random rnd = new Random(seed);
GHUtility.buildRandomGraph(graph, rnd, 100, 2.2, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, 0.7, 0.8, 0.8);
Weighting weighting = new FastestWeighting(encoder);
PrepareLandmarks lm = new PrepareLandmarks(dir, graph, new LMConfig("car", weighting), 16);
lm.setMaximumWeight(10000);
lm.doWork();
LandmarkStorage landmarkStorage = lm.getLandmarkStorage();
for (int t = 0; t < graph.getNodes(); t++) {
LMApproximator lmApproximator = new LMApproximator(graph, weighting, graph.getNodes(), landmarkStorage, 8, landmarkStorage.getFactor(), false);
WeightApproximator reverseLmApproximator = lmApproximator.reverse();
BeelineWeightApproximator beelineApproximator = new BeelineWeightApproximator(graph.getNodeAccess(), weighting);
WeightApproximator reverseBeelineApproximator = beelineApproximator.reverse();
PerfectApproximator perfectApproximator = new PerfectApproximator(graph, weighting, TraversalMode.NODE_BASED, false);
PerfectApproximator reversePerfectApproximator = new PerfectApproximator(graph, weighting, TraversalMode.NODE_BASED, true);
BalancedWeightApproximator balancedWeightApproximator = new BalancedWeightApproximator(new LMApproximator(graph, weighting, graph.getNodes(), landmarkStorage, 8, landmarkStorage.getFactor(), false));
lmApproximator.setTo(t);
beelineApproximator.setTo(t);
reverseLmApproximator.setTo(t);
reverseBeelineApproximator.setTo(t);
perfectApproximator.setTo(t);
reversePerfectApproximator.setTo(t);
balancedWeightApproximator.setFromTo(0, t);
int nOverApproximatedWeights = 0;
int nInconsistentWeights = 0;
for (int v = 0; v < graph.getNodes(); v++) {
Dijkstra dijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);
Path path = dijkstra.calcPath(v, t);
if (path.isFound()) {
// Give the beelineApproximator some slack, because the map distance of an edge
// can be _smaller_ than its Euklidean distance, due to rounding.
double slack = path.getEdgeCount() * (1 / 1000.0);
double realRemainingWeight = path.getWeight();
double approximatedRemainingWeight = lmApproximator.approximate(v);
if (approximatedRemainingWeight - slack > realRemainingWeight) {
System.out.printf("LM: %f\treal: %f\n", approximatedRemainingWeight, realRemainingWeight);
nOverApproximatedWeights++;
}
double beelineApproximatedRemainingWeight = beelineApproximator.approximate(v);
if (beelineApproximatedRemainingWeight - slack > realRemainingWeight) {
System.out.printf("beeline: %f\treal: %f\n", beelineApproximatedRemainingWeight, realRemainingWeight);
nOverApproximatedWeights++;
}
double approximate = perfectApproximator.approximate(v);
if (approximate > realRemainingWeight) {
System.out.printf("perfect: %f\treal: %f\n", approximate, realRemainingWeight);
nOverApproximatedWeights++;
}
// Triangle inequality for approximator. This is what makes it 'consistent'.
// That's a requirement for normal A*-implementations, because if it is violated,
// the heap-weight of settled nodes can decrease, and that would mean our
// stopping criterion is not sufficient.
EdgeIterator neighbors = graph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())).setBaseNode(v);
while (neighbors.next()) {
int w = neighbors.getAdjNode();
double vw = weighting.calcEdgeWeight(neighbors, false);
double vwApprox = lmApproximator.approximate(v) - lmApproximator.approximate(w);
if (vwApprox - lm.getLandmarkStorage().getFactor() > vw) {
System.out.printf("%f\t%f\n", vwApprox - lm.getLandmarkStorage().getFactor(), vw);
nInconsistentWeights++;
}
}
neighbors = graph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())).setBaseNode(v);
while (neighbors.next()) {
int w = neighbors.getAdjNode();
double vw = weighting.calcEdgeWeight(neighbors, false);
double vwApprox = balancedWeightApproximator.approximate(v, false) - balancedWeightApproximator.approximate(w, false);
if (vwApprox - lm.getLandmarkStorage().getFactor() > vw) {
System.out.printf("%f\t%f\n", vwApprox - lm.getLandmarkStorage().getFactor(), vw);
nInconsistentWeights++;
}
}
}
Dijkstra reverseDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);
Path reversePath = reverseDijkstra.calcPath(t, v);
if (reversePath.isFound()) {
// Give the beelineApproximator some slack, because the map distance of an edge
// can be _smaller_ than its Euklidean distance, due to rounding.
double slack = reversePath.getEdgeCount() * (1 / 1000.0);
double realRemainingWeight = reversePath.getWeight();
double approximatedRemainingWeight = reverseLmApproximator.approximate(v);
if (approximatedRemainingWeight - slack > realRemainingWeight) {
System.out.printf("LM: %f\treal: %f\n", approximatedRemainingWeight, realRemainingWeight);
nOverApproximatedWeights++;
}
double beelineApproximatedRemainingWeight = reverseBeelineApproximator.approximate(v);
if (beelineApproximatedRemainingWeight - slack > realRemainingWeight) {
System.out.printf("beeline: %f\treal: %f\n", beelineApproximatedRemainingWeight, realRemainingWeight);
nOverApproximatedWeights++;
}
double approximate = reversePerfectApproximator.approximate(v);
if (approximate > realRemainingWeight) {
System.out.printf("perfect: %f\treal: %f\n", approximate, realRemainingWeight);
nOverApproximatedWeights++;
}
}
}
assertEquals(0, nOverApproximatedWeights, "too many over approximated weights, seed: " + seed);
assertEquals(0, nInconsistentWeights, "too many inconsistent weights, seed: " + seed);
}
}
use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class NodeBasedNodeContractorTest method testNodeContraction_shortcutDistanceRounding.
@Test
public void testNodeContraction_shortcutDistanceRounding() {
assertTrue(weighting instanceof ShortestWeighting, "this test was constructed assuming we are using the ShortestWeighting");
// 0 ------------> 4
// \ /
// 1 --> 2 --> 3
double[] distances = { 4.019, 1.006, 1.004, 1.006, 1.004 };
GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 4).setDistance(distances[0]));
EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 1).setDistance(distances[1]));
EdgeIteratorState edge2 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(distances[2]));
EdgeIteratorState edge3 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 3).setDistance(distances[3]));
EdgeIteratorState edge4 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 4).setDistance(distances[4]));
freeze();
setMaxLevelOnAllNodes();
// make sure that distances do not get changed in storage (they might get truncated)
AllEdgesIterator iter = graph.getAllEdges();
double[] storedDistances = new double[iter.length()];
int count = 0;
while (iter.next()) {
storedDistances[count++] = iter.getDistance();
}
assertArrayEquals(distances, storedDistances, 1.e-6);
// perform CH contraction
contractInOrder(1, 3, 2, 0, 4);
// first we compare dijkstra with CH to make sure they produce the same results
int from = 0;
int to = 4;
Dijkstra dikstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);
Path dijkstraPath = dikstra.calcPath(from, to);
RoutingCHGraph lg = graph.createCHGraph(store, chConfig);
DijkstraBidirectionCH ch = new DijkstraBidirectionCH(lg);
Path chPath = ch.calcPath(from, to);
assertEquals(dijkstraPath.calcNodes(), chPath.calcNodes());
assertEquals(dijkstraPath.getDistance(), chPath.getDistance(), 1.e-6);
assertEquals(dijkstraPath.getWeight(), chPath.getWeight(), 1.e-6);
// on a more detailed level we check that the right shortcuts were added
// contracting nodes 1&3 will always introduce shortcuts, but contracting node 2 should not because going from
// 0 to 4 directly via edge 4 is cheaper. however, if shortcut distances get truncated it appears as if going
// via node 2 is better. here we check that this does not happen.
checkShortcuts(expectedShortcut(2, 0, edge2, edge1, false, true), expectedShortcut(2, 4, edge3, edge4, true, false));
}
Aggregations