use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.
the class TrafficChangeWithNodeOrderingReusingTest method runPerformanceTest.
private static void runPerformanceTest(final GraphHopperStorage ghStorage, CHConfig chConfig, long seed, final int iterations) {
final int numNodes = ghStorage.getNodes();
RoutingCHGraph chGraph = ghStorage.createCHGraph(ghStorage.createCHStorage(chConfig), chConfig);
final Random random = new Random(seed);
LOGGER.info("Running performance test, seed = {}", seed);
final double[] distAndWeight = { 0.0, 0.0 };
MiniPerfTest performanceTest = new MiniPerfTest();
performanceTest.setIterations(iterations).start(new MiniPerfTest.Task() {
private long queryTime;
@Override
public int doCalc(boolean warmup, int run) {
if (!warmup && run % 1000 == 0) {
LOGGER.debug("Finished {} of {} runs. {}", run, iterations, run > 0 ? String.format(Locale.ROOT, " Time: %6.2fms", queryTime * 1.e-6 / run) : "");
}
if (run == iterations - 1) {
String avg = fmt(queryTime * 1.e-6 / run);
LOGGER.debug("Finished all ({}) runs, avg time: {}ms", iterations, avg);
}
int from = random.nextInt(numNodes);
int to = random.nextInt(numNodes);
long start = nanoTime();
RoutingAlgorithm algo = new CHRoutingAlgorithmFactory(chGraph).createAlgo(new PMap());
Path path = algo.calcPath(from, to);
if (!warmup && !path.isFound())
return 1;
if (!warmup) {
queryTime += nanoTime() - start;
double distance = path.getDistance();
double weight = path.getWeight();
distAndWeight[0] += distance;
distAndWeight[1] += weight;
}
return 0;
}
});
if (performanceTest.getDummySum() > 0.5 * iterations) {
throw new IllegalStateException("too many errors, probably something is wrong");
}
LOGGER.info("Total distance: {}, total weight: {}", distAndWeight[0], distAndWeight[1]);
LOGGER.info("Average query time: {}ms", performanceTest.getMean());
}
use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.
the class TrafficChangeWithNodeOrderingReusingTest method checkCorrectness.
private static void checkCorrectness(GraphHopperStorage ghStorage, CHConfig chConfig, long seed, long numQueries) {
LOGGER.info("checking correctness");
RoutingCHGraph chGraph = ghStorage.createCHGraph(ghStorage.createCHStorage(chConfig), chConfig);
Random rnd = new Random(seed);
int numFails = 0;
for (int i = 0; i < numQueries; ++i) {
Dijkstra dijkstra = new Dijkstra(ghStorage, chConfig.getWeighting(), TraversalMode.NODE_BASED);
RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(chGraph).createAlgo(new PMap());
int from = rnd.nextInt(ghStorage.getNodes());
int to = rnd.nextInt(ghStorage.getNodes());
double dijkstraWeight = dijkstra.calcPath(from, to).getWeight();
double chWeight = chAlgo.calcPath(from, to).getWeight();
double error = Math.abs(dijkstraWeight - chWeight);
if (error > 1) {
System.out.println("failure from " + from + " to " + to + " dijkstra: " + dijkstraWeight + " ch: " + chWeight);
numFails++;
}
}
LOGGER.info("number of failed queries: " + numFails);
assertEquals(0, numFails);
}
use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.
the class DijkstraBidirectionCHTest method createCHAlgo.
private RoutingAlgorithm createCHAlgo(RoutingCHGraph chGraph, boolean withSOD) {
PMap opts = new PMap();
if (!withSOD) {
opts.putObject("stall_on_demand", false);
}
opts.putObject(ALGORITHM, DIJKSTRA_BI);
return new CHRoutingAlgorithmFactory(chGraph).createAlgo(opts);
}
use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.
the class RandomCHRoutingTest method runRandomTest.
private void runRandomTest(Fixture f, Random rnd, int numVirtualNodes) {
LocationIndexTree locationIndex = new LocationIndexTree(f.graph, f.dir);
locationIndex.prepareIndex();
f.freeze();
PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(f.graph, f.chConfig);
PrepareContractionHierarchies.Result res = pch.doWork();
RoutingCHGraph chGraph = f.graph.createCHGraph(res.getCHStorage(), res.getCHConfig());
int numQueryGraph = 25;
for (int j = 0; j < numQueryGraph; j++) {
// add virtual nodes and edges, because they can change the routing behavior and/or produce bugs, e.g.
// when via-points are used
List<Snap> snaps = createRandomSnaps(f.graph.getBounds(), locationIndex, rnd, numVirtualNodes, false, EdgeFilter.ALL_EDGES);
QueryGraph queryGraph = QueryGraph.create(f.graph, snaps);
int numQueries = 100;
int numPathsNotFound = 0;
List<String> strictViolations = new ArrayList<>();
for (int i = 0; i < numQueries; i++) {
int from = rnd.nextInt(queryGraph.getNodes());
int to = rnd.nextInt(queryGraph.getNodes());
Weighting w = queryGraph.wrapWeighting(f.weighting);
// using plain dijkstra instead of bidirectional, because of #1592
RoutingAlgorithm refAlgo = new Dijkstra(queryGraph, w, f.traversalMode);
Path refPath = refAlgo.calcPath(from, to);
double refWeight = refPath.getWeight();
QueryRoutingCHGraph routingCHGraph = new QueryRoutingCHGraph(chGraph, queryGraph);
RoutingAlgorithm algo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap().putObject("stall_on_demand", true));
Path path = algo.calcPath(from, to);
if (refPath.isFound() && !path.isFound())
fail("path not found for " + from + "->" + to + ", expected weight: " + refWeight);
assertEquals(refPath.isFound(), path.isFound());
if (!path.isFound()) {
numPathsNotFound++;
continue;
}
double weight = path.getWeight();
if (Math.abs(refWeight - weight) > 1.e-2) {
LOGGER.warn("expected: " + refPath.calcNodes());
LOGGER.warn("given: " + path.calcNodes());
fail("wrong weight: " + from + "->" + to + ", dijkstra: " + refWeight + " vs. ch: " + path.getWeight());
}
if (Math.abs(path.getDistance() - refPath.getDistance()) > 1.e-1) {
strictViolations.add("wrong distance " + from + "->" + to + ", expected: " + refPath.getDistance() + ", given: " + path.getDistance());
}
if (Math.abs(path.getTime() - refPath.getTime()) > 50) {
strictViolations.add("wrong time " + from + "->" + to + ", expected: " + refPath.getTime() + ", given: " + path.getTime());
}
}
if (numPathsNotFound > 0.9 * numQueries) {
fail("Too many paths not found: " + numPathsNotFound + "/" + numQueries);
}
if (strictViolations.size() > 0.05 * numQueries) {
fail("Too many strict violations: " + strictViolations.size() + "/" + numQueries + "\n" + Helper.join("\n", strictViolations));
}
}
}
use of com.graphhopper.routing.ch.CHRoutingAlgorithmFactory in project graphhopper by graphhopper.
the class LowLevelAPIExample method useContractionHierarchiesToMakeQueriesFaster.
public static void useContractionHierarchiesToMakeQueriesFaster() {
// Creating and saving the graph
FlagEncoder encoder = new CarFlagEncoder();
EncodingManager em = EncodingManager.create(encoder);
Weighting weighting = new FastestWeighting(encoder);
CHConfig chConfig = CHConfig.nodeBased("my_profile", weighting);
GraphHopperStorage graph = new GraphBuilder(em).setRAM(graphLocation, true).create();
graph.flush();
// Set node coordinates and build location index
NodeAccess na = graph.getNodeAccess();
graph.edge(0, 1).set(encoder.getAccessEnc(), true).set(encoder.getAverageSpeedEnc(), 10).setDistance(1020);
na.setNode(0, 15.15, 20.20);
na.setNode(1, 15.25, 20.21);
// Prepare the graph for fast querying ...
graph.freeze();
PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chConfig);
PrepareContractionHierarchies.Result pchRes = pch.doWork();
RoutingCHGraph chGraph = graph.createCHGraph(pchRes.getCHStorage(), pchRes.getCHConfig());
// create location index
LocationIndexTree index = new LocationIndexTree(graph, graph.getDirectory());
index.prepareIndex();
// calculate a path with location index
Snap fromSnap = index.findClosest(15.15, 20.20, EdgeFilter.ALL_EDGES);
Snap toSnap = index.findClosest(15.25, 20.21, EdgeFilter.ALL_EDGES);
QueryGraph queryGraph = QueryGraph.create(graph, fromSnap, toSnap);
BidirRoutingAlgorithm algo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap());
Path path = algo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
assert Helper.round(path.getDistance(), -2) == 1000;
}
Aggregations