use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.
the class DirectionResolverOnQueryGraphTest method assertUnrestricted.
private void assertUnrestricted(double lat, double lon) {
Snap snap = snapCoordinate(lat, lon);
queryGraph = QueryGraph.create(graph, snap);
DirectionResolver resolver = new DirectionResolver(queryGraph, this::isAccessible);
assertEquals(unrestricted(), resolver.resolveDirections(snap.getClosestNode(), snap.getQueryPoint()));
}
use of com.graphhopper.storage.index.Snap 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.storage.index.Snap in project graphhopper by graphhopper.
the class DirectionResolverOnQueryGraphTest method closeToTowerNode_issue2443.
@Test
public void closeToTowerNode_issue2443() {
// 0x-1
addNode(0, 51.986000, 19.255000);
addNode(1, 51.985500, 19.254000);
DistancePlaneProjection distCalc = new DistancePlaneProjection();
addEdge(0, 1, true).setDistance(distCalc.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1)));
init();
double lat = 51.9855003;
double lon = 19.2540003;
Snap snap = snapCoordinate(lat, lon);
queryGraph = QueryGraph.create(graph, snap);
DirectionResolver resolver = new DirectionResolver(queryGraph, this::isAccessible);
DirectionResolverResult result = resolver.resolveDirections(snap.getClosestNode(), snap.getQueryPoint());
assertEquals(0, result.getInEdgeRight());
assertEquals(0, result.getOutEdgeRight());
assertEquals(0, result.getInEdgeLeft());
assertEquals(0, result.getOutEdgeRight());
}
use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.
the class CHTurnCostTest method testRouteViaVirtualNode.
@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void testRouteViaVirtualNode(String algo) {
// 3
// 0-x-1-2
GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 1).setDistance(0));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(0));
updateDistancesFor(graph, 0, 0.00, 0.00);
updateDistancesFor(graph, 1, 0.02, 0.02);
updateDistancesFor(graph, 2, 0.03, 0.03);
graph.freeze();
automaticPrepareCH();
LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
index.prepareIndex();
Snap snap = index.findClosest(0.01, 0.01, EdgeFilter.ALL_EDGES);
QueryGraph queryGraph = QueryGraph.create(graph, snap);
assertEquals(3, snap.getClosestNode());
assertEquals(0, snap.getClosestEdge().getEdge());
RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
Path path = chAlgo.calcPath(0, 2);
assertTrue(path.isFound(), "it should be possible to route via a virtual node, but no path found");
assertEquals(IntArrayList.from(0, 3, 1, 2), path.calcNodes());
assertEquals(DistancePlaneProjection.DIST_PLANE.calcDist(0.00, 0.00, 0.03, 0.03), path.getDistance(), 1.e-1);
}
use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.
the class CHTurnCostTest method testRouteViaVirtualNode_withAlternative.
@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void testRouteViaVirtualNode_withAlternative(String algo) {
// 3
// 0-x-1
// \ |
// \-2
GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 0).setDistance(1));
updateDistancesFor(graph, 0, 0.01, 0.00);
updateDistancesFor(graph, 1, 0.01, 0.02);
updateDistancesFor(graph, 2, 0.00, 0.02);
graph.freeze();
automaticPrepareCH();
LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
index.prepareIndex();
Snap snap = index.findClosest(0.01, 0.01, EdgeFilter.ALL_EDGES);
QueryGraph queryGraph = QueryGraph.create(graph, snap);
assertEquals(3, snap.getClosestNode());
assertEquals(0, snap.getClosestEdge().getEdge());
QueryRoutingCHGraph routingCHGraph = new QueryRoutingCHGraph(chGraph, queryGraph);
RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
Path path = chAlgo.calcPath(1, 0);
assertEquals(IntArrayList.from(1, 3, 0), path.calcNodes());
}
Aggregations