use of com.graphhopper.routing.DijkstraBidirectionCH in project graphhopper by graphhopper.
the class NodeBasedNodeContractorTest method testNodeContraction_shortcutWeightRounding.
/**
* similar to the previous test, but using the fastest weighting
*/
@Test
public void testNodeContraction_shortcutWeightRounding() {
CarFlagEncoder encoder = new CarFlagEncoder();
EncodingManager encodingManager = EncodingManager.create(encoder);
GraphHopperStorage graph = new GraphBuilder(encodingManager).create();
// 0 ------------> 4
// \ /
// 1 --> 2 --> 3
double fac = 60 / 3.6;
double[] distances = { fac * 4.019, fac * 1.006, fac * 1.004, fac * 1.006, fac * 1.004 };
GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 4).setDistance(distances[0]));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 1).setDistance(distances[1]));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(distances[2]));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 3).setDistance(distances[3]));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 4).setDistance(distances[4]));
graph.freeze();
Weighting weighting = new FastestWeighting(encoder);
CHConfig chConfig = CHConfig.nodeBased("p1", weighting);
CHStorage chStore = graph.createCHStorage(chConfig);
setMaxLevelOnAllNodes(chStore);
// perform CH contraction
contractInOrder(graph, chStore, chConfig, 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);
DijkstraBidirectionCH ch = new DijkstraBidirectionCH(graph.createCHGraph(chStore, chConfig));
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);
}
use of com.graphhopper.routing.DijkstraBidirectionCH 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