use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class PrepareContractionHierarchiesTest method testMultiplePreparationsIdenticalView.
@Test
public void testMultiplePreparationsIdenticalView() {
CarFlagEncoder tmpCarEncoder = new CarFlagEncoder();
BikeFlagEncoder tmpBikeEncoder = new BikeFlagEncoder();
EncodingManager tmpEncodingManager = new EncodingManager(tmpCarEncoder, tmpBikeEncoder);
// FastestWeighting would lead to different shortcuts due to different default speeds for bike and car
Weighting carWeighting = new ShortestWeighting(tmpCarEncoder);
Weighting bikeWeighting = new ShortestWeighting(tmpBikeEncoder);
List<Weighting> chWeightings = Arrays.asList(carWeighting, bikeWeighting);
GraphHopperStorage ghStorage = new GraphHopperStorage(chWeightings, dir, tmpEncodingManager, false, new GraphExtension.NoOpExtension()).create(1000);
initShortcutsGraph(ghStorage);
ghStorage.freeze();
for (Weighting w : chWeightings) {
checkPath(ghStorage, w, 7, 5, Helper.createTList(3, 9, 14, 16, 13, 12));
}
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class PrepareLandmarksTest method testStoreAndLoad.
@Test
public void testStoreAndLoad() {
graph.edge(0, 1, 80_000, true);
graph.edge(1, 2, 80_000, true);
String fileStr = "./target/tmp-lm";
Helper.removeDir(new File(fileStr));
Directory dir = new RAMDirectory(fileStr, true).create();
Weighting weighting = new FastestWeighting(encoder);
PrepareLandmarks plm = new PrepareLandmarks(dir, graph, weighting, tm, 2, 2);
plm.setMinimumNodes(2);
plm.doWork();
double expectedFactor = plm.getLandmarkStorage().getFactor();
assertTrue(plm.getLandmarkStorage().isInitialized());
assertEquals(Arrays.toString(new int[] { 2, 0 }), Arrays.toString(plm.getLandmarkStorage().getLandmarks(1)));
assertEquals(4791, Math.round(plm.getLandmarkStorage().getFromWeight(0, 1) * expectedFactor));
dir = new RAMDirectory(fileStr, true);
plm = new PrepareLandmarks(dir, graph, weighting, tm, 2, 2);
assertTrue(plm.loadExisting());
assertEquals(expectedFactor, plm.getLandmarkStorage().getFactor(), 1e-6);
assertEquals(Arrays.toString(new int[] { 2, 0 }), Arrays.toString(plm.getLandmarkStorage().getLandmarks(1)));
assertEquals(4791, Math.round(plm.getLandmarkStorage().getFromWeight(0, 1) * expectedFactor));
Helper.removeDir(new File(fileStr));
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class PrepareLandmarksTest method testLandmarkStorageAndRouting.
@Test
public void testLandmarkStorageAndRouting() {
// create graph with lat,lon
// 0 1 2 ...
// 15 16 17 ...
Random rand = new Random(0);
int width = 15, height = 15;
for (int hIndex = 0; hIndex < height; hIndex++) {
for (int wIndex = 0; wIndex < width; wIndex++) {
int node = wIndex + hIndex * width;
long flags = encoder.setProperties(20 + rand.nextDouble() * 30, true, true);
// do not connect first with last column!
if (wIndex + 1 < width)
graph.edge(node, node + 1).setFlags(flags);
// avoid dead ends
if (hIndex + 1 < height)
graph.edge(node, node + width).setFlags(flags);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, node, -hIndex / 50.0, wIndex / 50.0);
}
}
Directory dir = new RAMDirectory();
LocationIndex index = new LocationIndexTree(graph, dir);
index.prepareIndex();
int lm = 5, activeLM = 2;
Weighting weighting = new FastestWeighting(encoder);
LandmarkStorage store = new LandmarkStorage(graph, dir, lm, weighting, tm);
store.setMinimumNodes(2);
store.createLandmarks();
// landmarks should be the 4 corners of the grid:
int[] intList = store.getLandmarks(1);
Arrays.sort(intList);
assertEquals("[0, 14, 70, 182, 224]", Arrays.toString(intList));
// two landmarks: one for subnetwork 0 (all empty) and one for subnetwork 1
assertEquals(2, store.getSubnetworksWithLandmarks());
assertEquals(0, store.getFromWeight(0, 224));
double factor = store.getFactor();
assertEquals(4671, Math.round(store.getFromWeight(0, 47) * factor));
assertEquals(3640, Math.round(store.getFromWeight(0, 52) * factor));
long weight1_224 = store.getFromWeight(1, 224);
assertEquals(5525, Math.round(weight1_224 * factor));
long weight1_47 = store.getFromWeight(1, 47);
assertEquals(921, Math.round(weight1_47 * factor));
// grid is symmetric
assertEquals(weight1_224, store.getToWeight(1, 224));
assertEquals(weight1_47, store.getToWeight(1, 47));
// prefer the landmarks before and behind the goal
int[] activeLandmarkIndices = new int[activeLM];
int[] activeFroms = new int[activeLM];
int[] activeTos = new int[activeLM];
Arrays.fill(activeLandmarkIndices, -1);
store.initActiveLandmarks(27, 47, activeLandmarkIndices, activeFroms, activeTos, false);
List<Integer> list = new ArrayList<>();
for (int idx : activeLandmarkIndices) {
list.add(store.getLandmarks(1)[idx]);
}
// TODO should better select 0 and 224?
assertEquals(Arrays.asList(224, 70), list);
AlgorithmOptions opts = AlgorithmOptions.start().weighting(weighting).traversalMode(tm).build();
PrepareLandmarks prepare = new PrepareLandmarks(new RAMDirectory(), graph, weighting, tm, 4, 2);
prepare.setMinimumNodes(2);
prepare.doWork();
AStar expectedAlgo = new AStar(graph, weighting, tm);
Path expectedPath = expectedAlgo.calcPath(41, 183);
// landmarks with A*
RoutingAlgorithm oneDirAlgoWithLandmarks = prepare.getDecoratedAlgorithm(graph, new AStar(graph, weighting, tm), opts);
Path path = oneDirAlgoWithLandmarks.calcPath(41, 183);
assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
assertEquals(expectedPath.calcNodes(), path.calcNodes());
assertEquals(expectedAlgo.getVisitedNodes(), oneDirAlgoWithLandmarks.getVisitedNodes() + 142);
// landmarks with bidir A*
opts.getHints().put("lm.recalc_count", 50);
RoutingAlgorithm biDirAlgoWithLandmarks = prepare.getDecoratedAlgorithm(graph, new AStarBidirection(graph, weighting, tm), opts);
path = biDirAlgoWithLandmarks.calcPath(41, 183);
assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
assertEquals(expectedPath.calcNodes(), path.calcNodes());
assertEquals(expectedAlgo.getVisitedNodes(), biDirAlgoWithLandmarks.getVisitedNodes() + 164);
// landmarks with A* and a QueryGraph. We expect slightly less optimal as two more cycles needs to be traversed
// due to the two more virtual nodes but this should not harm in practise
QueryGraph qGraph = new QueryGraph(graph);
QueryResult fromQR = index.findClosest(-0.0401, 0.2201, EdgeFilter.ALL_EDGES);
QueryResult toQR = index.findClosest(-0.2401, 0.0601, EdgeFilter.ALL_EDGES);
qGraph.lookup(fromQR, toQR);
RoutingAlgorithm qGraphOneDirAlgo = prepare.getDecoratedAlgorithm(qGraph, new AStar(qGraph, weighting, tm), opts);
path = qGraphOneDirAlgo.calcPath(fromQR.getClosestNode(), toQR.getClosestNode());
expectedAlgo = new AStar(qGraph, weighting, tm);
expectedPath = expectedAlgo.calcPath(fromQR.getClosestNode(), toQR.getClosestNode());
assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
assertEquals(expectedPath.calcNodes(), path.calcNodes());
assertEquals(expectedAlgo.getVisitedNodes(), qGraphOneDirAlgo.getVisitedNodes() + 133);
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class RoundTripRoutingTemplateTest method testCalcRoundTrip.
@Test
public void testCalcRoundTrip() throws Exception {
Weighting weighting = new FastestWeighting(carFE);
Graph g = createTestGraph(true);
RoundTripRoutingTemplate rTripRouting = new RoundTripRoutingTemplate(new GHRequest(), new GHResponse(), null, 1);
LocationIndex locationIndex = new LocationIndexTree(g, new RAMDirectory()).prepareIndex();
QueryResult qr4 = locationIndex.findClosest(0.05, 0.25, EdgeFilter.ALL_EDGES);
assertEquals(4, qr4.getClosestNode());
QueryResult qr5 = locationIndex.findClosest(0.00, 0.05, EdgeFilter.ALL_EDGES);
assertEquals(5, qr5.getClosestNode());
QueryResult qr6 = locationIndex.findClosest(0.00, 0.10, EdgeFilter.ALL_EDGES);
assertEquals(6, qr6.getClosestNode());
QueryGraph qGraph = new QueryGraph(g);
qGraph.lookup(qr4, qr5);
rTripRouting.setQueryResults(Arrays.asList(qr5, qr4, qr5));
List<Path> paths = rTripRouting.calcPaths(qGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
assertEquals(2, paths.size());
assertEquals(Helper.createTList(5, 6, 3, 4), paths.get(0).calcNodes());
assertEquals(Helper.createTList(4, 8, 7, 6, 5), paths.get(1).calcNodes());
qGraph = new QueryGraph(g);
qGraph.lookup(qr4, qr6);
rTripRouting.setQueryResults(Arrays.asList(qr6, qr4, qr6));
paths = rTripRouting.calcPaths(qGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
assertEquals(2, paths.size());
assertEquals(Helper.createTList(6, 3, 4), paths.get(0).calcNodes());
assertEquals(Helper.createTList(4, 8, 7, 6), paths.get(1).calcNodes());
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class AbstractRoutingAlgorithmTester method testWithCoordinates.
// a-b-0-c-1
// | | _/\
// | / / |
// d-2--3-e-4
@Test
public void testWithCoordinates() {
Weighting weighting = new ShortestWeighting(carEncoder);
GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false);
graph.edge(0, 1, 2, true).setWayGeometry(Helper.createPointList(1.5, 1));
graph.edge(2, 3, 2, true).setWayGeometry(Helper.createPointList(0, 1.5));
graph.edge(3, 4, 2, true).setWayGeometry(Helper.createPointList(0, 2));
// duplicate but the second edge is longer
graph.edge(0, 2, 1.2, true);
graph.edge(0, 2, 1.5, true).setWayGeometry(Helper.createPointList(0.5, 0));
graph.edge(1, 3, 1.3, true).setWayGeometry(Helper.createPointList(0.5, 1.5));
graph.edge(1, 4, 1, true);
updateDistancesFor(graph, 0, 1, 0.6);
updateDistancesFor(graph, 1, 1, 1.5);
updateDistancesFor(graph, 2, 0, 0);
updateDistancesFor(graph, 3, 0, 1);
updateDistancesFor(graph, 4, 0, 2);
AlgorithmOptions opts = new AlgorithmOptions(DIJKSTRA_BI, weighting);
RoutingAlgorithmFactory prepare = createFactory(graph, opts);
Path p = prepare.createAlgo(getGraph(graph, opts.getWeighting()), opts).calcPath(4, 0);
assertEquals(Helper.createTList(4, 1, 0), p.calcNodes());
assertEquals(Helper.createPointList(0, 2, 1, 1.5, 1.5, 1, 1, 0.6), p.calcPoints());
assertEquals(274128, p.calcPoints().calcDistance(new DistanceCalcEarth()), 1);
p = prepare.createAlgo(getGraph(graph, opts.getWeighting()), opts).calcPath(2, 1);
assertEquals(Helper.createTList(2, 0, 1), p.calcNodes());
assertEquals(Helper.createPointList(0, 0, 1, 0.6, 1.5, 1, 1, 1.5), p.calcPoints());
assertEquals(279482, p.calcPoints().calcDistance(new DistanceCalcEarth()), 1);
}
Aggregations