I’m having some difficulty to use a chConfig when using an existing graph;
I have it working using a random graph (code below) but I cannot do the same with my graph that has already been created.
When ever I try to load my existing graph it says that: There is no CH graph ‘x’, existing: []
Is there something extra I need to do to the graph?
graphHopper = new GraphHopperOSM()
.setGraphHopperLocation(locationGraphHopperFiles)
.setEncodingManager(encodingManager)
.setDataReaderFile(locationRoutingFile)
.setElevation(true).setAllowWrites(true)
.setMemoryMapped()
.setElevationProvider(new CGIARProvider(System.getProperty("user.home")+File.separator+"srtm"+File.separator))
.setProfiles(Arrays.asList(
new Profile("foot").setVehicle("foot").setWeighting("fastest"),
new Profile("car").setVehicle("car").setWeighting("fastest"),
new Profile("rural").setVehicle("motorcycle").setWeighting("fastest").setTurnCosts(true).putHint("u_turn_costs", 3),
new CustomProfile("paxster").setCustomModel(model).setVehicle("foot").setWeighting("custom")
))
.forDesktop();
graphHopper.importOrLoad();
Here is the working example with a random graph.
public static void main(String[] args) {
dir = new RAMDirectory();
int maxTurnCosts = 10;
encoder = new CarFlagEncoder(5, 5, maxTurnCosts);
encodingManager = EncodingManager.create(encoder);
graph = new GraphBuilder(encodingManager).setDir(dir).withTurnCosts(true).build();
turnCostStorage = graph.getTurnCostStorage();
weighting = new FastestWeighting(encoder, new DefaultTurnCostProvider(encoder, turnCostStorage, uTurnCosts));
chConfig = CHConfig.edgeBased("x", weighting);
graph.addCHGraph(chConfig);
graph.create(1000);
final long seed = System.nanoTime();
final int numQueries = 10000;
Random rnd = new Random(seed);
GHUtility.buildRandomGraph(graph, rnd, 100, 2.2, true, true, encoder.getAverageSpeedEnc(), 0.7, 0.8, 0.8);
GHUtility.addRandomTurnCosts(graph, seed, encodingManager, encoder, maxTurnCosts, turnCostStorage);
preProcessGraph(graph);
long startNano = System.currentTimeMillis();
for (int i = 0; i < numQueries; i++) {
int source = getRandom(rnd, graph);
int target = getRandom(rnd, graph);
int sourceOutEdge = getSourceOutEdge(rnd, source, graph);
int targetInEdge = getTargetInEdge(rnd, target, graph);
Path path = createAlgo()
.calcPath(source, target, sourceOutEdge, targetInEdge);
System.out.println(path.getDistance());
}
long endNano = System.currentTimeMillis();
System.out.println("Done in "+(endNano - startNano)/ 1000+" seconds");
}
private static BidirRoutingAlgorithm createAlgo() {
CHRoutingAlgorithmFactory algoFactory = new CHRoutingAlgorithmFactory(routingCHGraph);
return algoFactory.createAlgo(new PMap().putObject(ALGORITHM, DIJKSTRA_BI));
}
private static int getTargetInEdge(Random rnd, int node, Graph graph) {
return getAdjEdge(rnd, node, graph);
}
private static int getSourceOutEdge(Random rnd, int node, Graph graph) {
return getAdjEdge(rnd, node, graph);
}
private static int getAdjEdge(Random rnd, int node, Graph graph) {
// sometimes do not restrict anything
if (rnd.nextDouble() < 0.05) {
return ANY_EDGE;
}
// sometimes use NO_EDGE
if (rnd.nextDouble() < 0.05) {
return NO_EDGE;
}
// use all edge explorer, sometimes we will find an edge we can restrict sometimes we do not
EdgeExplorer explorer = graph.createEdgeExplorer();
EdgeIterator iter = explorer.setBaseNode(node);
List<Integer> edgeIds = new ArrayList<>();
while (iter.next()) {
edgeIds.add(iter.getOrigEdgeFirst());
edgeIds.add(iter.getOrigEdgeLast());
}
return edgeIds.isEmpty() ? ANY_EDGE : edgeIds.get(rnd.nextInt(edgeIds.size()));
}
private static int getRandom(Random rnd, GraphHopperStorage graph) {
return rnd.nextInt(graph.getNodes());
}
private static void preProcessGraph(GraphHopperStorage graph) {
graph.freeze();
if (!prepareCH) {
return;
}
if (prepareCH) {
PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chConfig);
pch.doWork();
routingCHGraph = graph.getRoutingCHGraph(chConfig.getName());
}
}
}