Skip to content

Commit

Permalink
Make more usage of PrepareContractionHierarchies#fromGraphHopperStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Nov 13, 2019
1 parent 5f18863 commit d3ebf2e
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.routing.weighting.ShortestWeighting;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.*;
import com.graphhopper.storage.CHGraph;
import com.graphhopper.storage.CHProfile;
import com.graphhopper.storage.GraphHopperStorage;
import com.graphhopper.storage.RAMDirectory;
import com.graphhopper.util.Parameters;

import java.util.ArrayList;
Expand Down Expand Up @@ -55,8 +58,9 @@ protected GraphHopperStorage createGHStorage(
@Override
public RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, AlgorithmOptions opts) {
ghStorage.freeze();
CHGraph chGraph = ghStorage.getCHGraph(CHProfile.edgeBased(opts.getWeighting(), INFINITE_U_TURN_COSTS));
PrepareContractionHierarchies ch = new PrepareContractionHierarchies(chGraph);
CHProfile chProfile = CHProfile.edgeBased(opts.getWeighting(), INFINITE_U_TURN_COSTS);
CHGraph chGraph = ghStorage.getCHGraph(chProfile);
PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage(ghStorage, chProfile);
// make sure the contraction runs only once
if (chGraph.getEdges() == chGraph.getBaseGraph().getEdges()) {
ch.doWork();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
import com.graphhopper.routing.weighting.FastestWeighting;
import com.graphhopper.routing.weighting.ShortestWeighting;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.*;
import com.graphhopper.storage.CHGraph;
import com.graphhopper.storage.CHProfile;
import com.graphhopper.storage.GraphHopperStorage;
import com.graphhopper.storage.RAMDirectory;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.GHUtility;
import com.graphhopper.util.Parameters;
Expand Down Expand Up @@ -58,10 +61,11 @@ protected GraphHopperStorage createGHStorage(EncodingManager em,
@Override
public RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, AlgorithmOptions opts) {
ghStorage.freeze();
CHGraph chGraph = ghStorage.getCHGraph(CHProfile.nodeBased(opts.getWeighting()));
PrepareContractionHierarchies ch = new PrepareContractionHierarchies(chGraph);
CHProfile chProfile = CHProfile.nodeBased(opts.getWeighting());
PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage(ghStorage, chProfile);

// make sure the contraction runs only once
if (chGraph.getEdges() == chGraph.getBaseGraph().getEdges()) {
if (ghStorage.getCHGraph(chProfile).getEdges() == ghStorage.getEdges()) {
ch.doWork();
}
return ch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import com.graphhopper.routing.ch.PrepareContractionHierarchies;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.*;
import com.graphhopper.storage.CHGraph;
import com.graphhopper.storage.CHProfile;
import com.graphhopper.storage.GraphHopperStorage;
import com.graphhopper.storage.RAMDirectory;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -48,7 +51,7 @@ public RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, Algor
ghStorage.freeze();
CHProfile chProfile = CHProfile.edgeBased(opts.getWeighting(), INFINITE_U_TURN_COSTS);
CHGraph chGraph = ghStorage.getCHGraph(chProfile);
PrepareContractionHierarchies ch = new PrepareContractionHierarchies(chGraph);
PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage(ghStorage, chProfile);
// make sure the contraction runs only once
if (chGraph.getEdges() == chGraph.getBaseGraph().getEdges()) {
ch.doWork();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void runRandomTest(Random rnd, int numVirtualNodes) {
graph.freeze();
CHProfile chProfile = new CHProfile(weighting, traversalMode, uTurnCosts);
CHGraph chGraph = graph.getCHGraph(chProfile);
PrepareContractionHierarchies pch = new PrepareContractionHierarchies(chGraph);
PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chProfile);
pch.doWork();

int numQueryGraph = 25;
Expand Down
31 changes: 17 additions & 14 deletions core/src/test/java/com/graphhopper/routing/ch/CHTurnCostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class CHTurnCostTest {
private GraphHopperStorage graph;
private TurnCostExtension turnCostExtension;
private List<CHProfile> chProfiles;
private CHProfile chProfile;
private CHGraph chGraph;
private boolean checkStrict;

Expand All @@ -77,9 +78,9 @@ public void init() {
weighting = new ShortestWeighting(encoder);
chProfiles = createCHProfiles();
graph = new GraphBuilder(encodingManager).setCHProfiles(chProfiles).create();
// the default CH graph with infinite u-turn costs, can be reset in tests that should run with finite u-turn
// the default CH profile with infinite u-turn costs, can be reset in tests that should run with finite u-turn
// costs
chGraph = graph.getCHGraph(CHProfile.edgeBased(weighting, INFINITE_U_TURN_COSTS));
chProfile = CHProfile.edgeBased(weighting, INFINITE_U_TURN_COSTS);
turnCostExtension = graph.getTurnCostExtension();
checkStrict = true;
}
Expand Down Expand Up @@ -591,7 +592,7 @@ public void testFindPath_highlyConnectedGraph_compareWithDijkstra() {
}
}

List<Integer> contractionOrder = getRandomIntegerSequence(chGraph.getNodes(), rnd);
List<Integer> contractionOrder = getRandomIntegerSequence(graph.getNodes(), rnd);
checkStrict = false;
compareCHWithDijkstra(numQueries, contractionOrder);
}
Expand Down Expand Up @@ -662,7 +663,7 @@ public void testFindPath_finiteUTurnCost() {
graph.edge(3, 1, 100, false);
addRestriction(0, 3, 1);
graph.freeze();
chGraph = graph.getCHGraph(CHProfile.edgeBased(weighting, 50));
chProfile = CHProfile.edgeBased(weighting, 50);
RoutingAlgorithmFactory pch = prepareCH(Arrays.asList(4, 0, 2, 3, 1));
Path path = pch.createAlgo(chGraph, AlgorithmOptions.start().build()).calcPath(0, 1);
assertEquals(IntArrayList.from(0, 3, 4, 3, 1), path.calcNodes());
Expand Down Expand Up @@ -1005,7 +1006,7 @@ public void testFiniteUTurnCost_virtualViaNode() {
// not allowed to turn right at node 1 -> we have to take a u-turn at node 0 (or at the virtual node...)
addRestriction(2, 1, 5);
graph.freeze();
chGraph = graph.getCHGraph(CHProfile.edgeBased(weighting, 50));
chProfile = CHProfile.edgeBased(weighting, 50);
RoutingAlgorithmFactory pch = prepareCH(Arrays.asList(0, 1, 2, 3, 4, 5, 6));
LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
index.prepareIndex();
Expand Down Expand Up @@ -1049,8 +1050,8 @@ public void testFindPath_random_compareWithDijkstra() {
public void testFindPath_random_compareWithDijkstra_finiteUTurnCost() {
long seed = System.nanoTime();
LOGGER.info("Seed for testFindPath_random_compareWithDijkstra_finiteUTurnCost: {}", seed);
chGraph = graph.getCHGraph(chProfiles.get(1 + new Random(seed).nextInt(chProfiles.size() - 1)));
LOGGER.info("U-turn-costs: " + chGraph.getCHProfile().getUTurnCostsInt());
chProfile = chProfiles.get(1 + new Random(seed).nextInt(chProfiles.size() - 1));
LOGGER.info("U-turn-costs: " + chProfile.getUTurnCostsInt());
compareWithDijkstraOnRandomGraph(seed);
}

Expand All @@ -1061,7 +1062,7 @@ private void compareWithDijkstraOnRandomGraph(long seed) {
GHUtility.addRandomTurnCosts(graph, seed, encoder, maxCost, turnCostExtension);
graph.freeze();
checkStrict = false;
List<Integer> contractionOrder = getRandomIntegerSequence(chGraph.getNodes(), rnd);
List<Integer> contractionOrder = getRandomIntegerSequence(graph.getNodes(), rnd);
compareCHWithDijkstra(100, contractionOrder);
}

Expand All @@ -1081,8 +1082,8 @@ public void testFindPath_heuristic_compareWithDijkstra() {
public void testFindPath_heuristic_compareWithDijkstra_finiteUTurnCost() {
long seed = System.nanoTime();
LOGGER.info("Seed for testFindPath_heuristic_compareWithDijkstra_finiteUTurnCost: {}", seed);
chGraph = graph.getCHGraph(chProfiles.get(1 + new Random(seed).nextInt(chProfiles.size() - 1)));
LOGGER.info("U-turn-costs: " + chGraph.getCHProfile().getUTurnCostsInt());
chProfile = chProfiles.get(1 + new Random(seed).nextInt(chProfiles.size() - 1));
LOGGER.info("U-turn-costs: " + chProfile.getUTurnCostsInt());
compareWithDijkstraOnRandomGraph_heuristic(seed);
}

Expand All @@ -1104,7 +1105,7 @@ private double nextDist(int maxDist, Random rnd) {
}

private void checkPathUsingRandomContractionOrder(IntArrayList expectedPath, int expectedWeight, int expectedTurnCosts, int from, int to) {
List<Integer> contractionOrder = getRandomIntegerSequence(chGraph.getNodes());
List<Integer> contractionOrder = getRandomIntegerSequence(graph.getNodes());
checkPath(expectedPath, expectedWeight, expectedTurnCosts, from, to, contractionOrder);
}

Expand Down Expand Up @@ -1136,7 +1137,7 @@ private void checkPathUsingCH(IntArrayList expectedPath, int expectedEdgeWeight,
}

private Path findPathUsingDijkstra(int from, int to) {
Dijkstra dijkstra = new Dijkstra(graph, new TurnWeighting(weighting, turnCostExtension, chGraph.getCHProfile().getUTurnCosts()), TraversalMode.EDGE_BASED);
Dijkstra dijkstra = new Dijkstra(graph, new TurnWeighting(weighting, turnCostExtension, chProfile.getUTurnCosts()), TraversalMode.EDGE_BASED);
return dijkstra.calcPath(from, to);
}

Expand All @@ -1160,9 +1161,10 @@ public int getNumNodes() {
return contractionOrder.size();
}
};
PrepareContractionHierarchies ch = new PrepareContractionHierarchies(chGraph)
PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chProfile)
.useFixedNodeOrdering(nodeOrderingProvider);
ch.doWork();
chGraph = graph.getCHGraph(chProfile);
return ch;
}

Expand All @@ -1172,9 +1174,10 @@ private RoutingAlgorithmFactory automaticPrepareCH() {
pMap.put(LAST_LAZY_NODES_UPDATES, 100);
pMap.put(NEIGHBOR_UPDATES, 4);
pMap.put(LOG_MESSAGES, 10);
PrepareContractionHierarchies ch = new PrepareContractionHierarchies(chGraph);
PrepareContractionHierarchies ch = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chProfile);
ch.setParams(pMap);
ch.doWork();
chGraph = graph.getCHGraph(chProfile);
return ch;
}

Expand Down
Loading

0 comments on commit d3ebf2e

Please sign in to comment.