Skip to content

Commit

Permalink
Prevent running CH contraction twice on the same CHGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Oct 22, 2019
1 parent 643ddbc commit 5668e4d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
Expand Up @@ -116,6 +116,9 @@ public void doSpecificWork() {
if (!prepareGraph.isReadyForContraction()) {
throw new IllegalStateException("Given CHGraph has not been frozen yet");
}
if (prepareGraph.getEdges() > prepareGraph.getBaseGraph().getEdges()) {
throw new IllegalStateException("Given CHGraph has been contracted already");
}
allSW.start();
initFromGraph();
runGraphContraction();
Expand Down
Expand Up @@ -55,9 +55,12 @@ protected GraphHopperStorage createGHStorage(
@Override
public RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, AlgorithmOptions opts) {
ghStorage.freeze();
PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage(
ghStorage, CHProfile.edgeBased(opts.getWeighting(), INFINITE_U_TURN_COSTS));
ch.doWork();
CHGraph chGraph = ghStorage.getCHGraph(CHProfile.edgeBased(opts.getWeighting(), INFINITE_U_TURN_COSTS));
PrepareContractionHierarchies ch = new PrepareContractionHierarchies(chGraph);
// make sure the contraction runs only once
if (chGraph.getEdges() == chGraph.getBaseGraph().getEdges()) {
ch.doWork();
}
return ch;
}

Expand Down
Expand Up @@ -26,7 +26,6 @@
import com.graphhopper.routing.weighting.ShortestWeighting;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.*;
import com.graphhopper.util.CHEdgeIteratorState;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.GHUtility;
import com.graphhopper.util.Parameters;
Expand Down Expand Up @@ -59,9 +58,12 @@ protected GraphHopperStorage createGHStorage(EncodingManager em,
@Override
public RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, AlgorithmOptions opts) {
ghStorage.freeze();
PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage(
ghStorage, CHProfile.nodeBased(opts.getWeighting()));
ch.doWork();
CHGraph chGraph = ghStorage.getCHGraph(CHProfile.nodeBased(opts.getWeighting()));
PrepareContractionHierarchies ch = new PrepareContractionHierarchies(chGraph);
// make sure the contraction runs only once
if (chGraph.getEdges() == chGraph.getBaseGraph().getEdges()) {
ch.doWork();
}
return ch;
}

Expand Down
Expand Up @@ -46,9 +46,13 @@ protected GraphHopperStorage createGHStorage(
@Override
public RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, AlgorithmOptions opts) {
ghStorage.freeze();
PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage(
ghStorage, CHProfile.edgeBased(opts.getWeighting(), INFINITE_U_TURN_COSTS));
ch.doWork();
CHProfile chProfile = CHProfile.edgeBased(opts.getWeighting(), INFINITE_U_TURN_COSTS);
CHGraph chGraph = ghStorage.getCHGraph(chProfile);
PrepareContractionHierarchies ch = new PrepareContractionHierarchies(chGraph);
// make sure the contraction runs only once
if (chGraph.getEdges() == chGraph.getBaseGraph().getEdges()) {
ch.doWork();
}
return ch;
}

Expand Down
Expand Up @@ -353,7 +353,7 @@ public void testUnpackingOrder() {
CHGraph lg = g.getCHGraph();
initUnpackingGraph(g, lg, weighting);
PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g, lg);
prepare.doWork();
// do not call prepare.doWork() here
RoutingAlgorithm algo = prepare.createAlgo(lg, new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
Path p = algo.calcPath(10, 6);
assertEquals(7, p.getDistance(), 1e-5);
Expand All @@ -368,7 +368,7 @@ public void testUnpackingOrder_Fastest() {
initUnpackingGraph(g, lg, w);

PrepareContractionHierarchies prepare = createPrepareContractionHierarchies(g, lg);
prepare.doWork();
// do not call prepare.doWork() here
RoutingAlgorithm algo = prepare.createAlgo(lg, new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
Path p = algo.calcPath(10, 6);
assertEquals(7, p.getDistance(), 1e-1);
Expand Down

0 comments on commit 5668e4d

Please sign in to comment.