use of com.graphhopper.routing.weighting.FastestWeighting 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.FastestWeighting in project graphhopper by graphhopper.
the class PathTest method testFound.
@Test
public void testFound() {
GraphHopperStorage g = new GraphBuilder(carManager).create();
Path p = new Path(g, new FastestWeighting(encoder));
assertFalse(p.isFound());
assertEquals(0, p.getDistance(), 1e-7);
assertEquals(0, p.calcNodes().size());
g.close();
}
use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.
the class PathTest method testWayList.
public void testWayList() {
GraphHopperStorage g = new GraphBuilder(carManager).create();
NodeAccess na = g.getNodeAccess();
na.setNode(0, 0.0, 0.1);
na.setNode(1, 1.0, 0.1);
na.setNode(2, 2.0, 0.1);
EdgeIteratorState edge1 = g.edge(0, 1).setDistance(1000).setFlags(encoder.setProperties(10, true, true));
edge1.setWayGeometry(Helper.createPointList(8, 1, 9, 1));
EdgeIteratorState edge2 = g.edge(2, 1).setDistance(2000).setFlags(encoder.setProperties(50, true, true));
edge2.setWayGeometry(Helper.createPointList(11, 1, 10, 1));
Path path = new Path(g, new FastestWeighting(encoder));
SPTEntry e1 = new SPTEntry(edge2.getEdge(), 2, 1);
e1.parent = new SPTEntry(edge1.getEdge(), 1, 1);
e1.parent.parent = new SPTEntry(-1, 0, 1);
path.setSPTEntry(e1);
path.extract();
// 0-1-2
assertPList(Helper.createPointList(0, 0.1, 8, 1, 9, 1, 1, 0.1, 10, 1, 11, 1, 2, 0.1), path.calcPoints());
InstructionList instr = path.calcInstructions(tr);
List<Map<String, Object>> res = instr.createJson();
Map<String, Object> tmp = res.get(0);
assertEquals(3000.0, tmp.get("distance"));
assertEquals(504000L, tmp.get("time"));
assertEquals("Continue", tmp.get("text"));
assertEquals("[0, 6]", tmp.get("interval").toString());
tmp = res.get(1);
assertEquals(0.0, tmp.get("distance"));
assertEquals(0L, tmp.get("time"));
assertEquals("Finish!", tmp.get("text"));
assertEquals("[6, 6]", tmp.get("interval").toString());
int lastIndex = (Integer) ((List) res.get(res.size() - 1).get("interval")).get(0);
assertEquals(path.calcPoints().size() - 1, lastIndex);
// force minor change for instructions
edge2.setName("2");
path = new Path(g, new FastestWeighting(encoder));
e1 = new SPTEntry(edge2.getEdge(), 2, 1);
e1.parent = new SPTEntry(edge1.getEdge(), 1, 1);
e1.parent.parent = new SPTEntry(-1, 0, 1);
path.setSPTEntry(e1);
path.extract();
instr = path.calcInstructions(tr);
res = instr.createJson();
tmp = res.get(0);
assertEquals(1000.0, tmp.get("distance"));
assertEquals(360000L, tmp.get("time"));
assertEquals("Continue", tmp.get("text"));
assertEquals("[0, 3]", tmp.get("interval").toString());
tmp = res.get(1);
assertEquals(2000.0, tmp.get("distance"));
assertEquals(144000L, tmp.get("time"));
assertEquals("Turn sharp right onto 2", tmp.get("text"));
assertEquals("[3, 6]", tmp.get("interval").toString());
lastIndex = (Integer) ((List) res.get(res.size() - 1).get("interval")).get(0);
assertEquals(path.calcPoints().size() - 1, lastIndex);
// now reverse order
path = new Path(g, new FastestWeighting(encoder));
e1 = new SPTEntry(edge1.getEdge(), 0, 1);
e1.parent = new SPTEntry(edge2.getEdge(), 1, 1);
e1.parent.parent = new SPTEntry(-1, 2, 1);
path.setSPTEntry(e1);
path.extract();
// 2-1-0
assertPList(Helper.createPointList(2, 0.1, 11, 1, 10, 1, 1, 0.1, 9, 1, 8, 1, 0, 0.1), path.calcPoints());
instr = path.calcInstructions(tr);
res = instr.createJson();
tmp = res.get(0);
assertEquals(2000.0, tmp.get("distance"));
assertEquals(144000L, tmp.get("time"));
assertEquals("Continue onto 2", tmp.get("text"));
assertEquals("[0, 3]", tmp.get("interval").toString());
tmp = res.get(1);
assertEquals(1000.0, tmp.get("distance"));
assertEquals(360000L, tmp.get("time"));
assertEquals("Turn sharp left", tmp.get("text"));
assertEquals("[3, 6]", tmp.get("interval").toString());
lastIndex = (Integer) ((List) res.get(res.size() - 1).get("interval")).get(0);
assertEquals(path.calcPoints().size() - 1, lastIndex);
}
use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.
the class PathTest method testFindInstruction.
@Test
public void testFindInstruction() {
Graph g = new GraphBuilder(carManager).create();
NodeAccess na = g.getNodeAccess();
na.setNode(0, 0.0, 0.0);
na.setNode(1, 5.0, 0.0);
na.setNode(2, 5.0, 0.5);
na.setNode(3, 10.0, 0.5);
na.setNode(4, 7.5, 0.25);
EdgeIteratorState edge1 = g.edge(0, 1).setDistance(1000).setFlags(encoder.setProperties(50, true, true));
edge1.setWayGeometry(Helper.createPointList());
edge1.setName("Street 1");
EdgeIteratorState edge2 = g.edge(1, 2).setDistance(1000).setFlags(encoder.setProperties(50, true, true));
edge2.setWayGeometry(Helper.createPointList());
edge2.setName("Street 2");
EdgeIteratorState edge3 = g.edge(2, 3).setDistance(1000).setFlags(encoder.setProperties(50, true, true));
edge3.setWayGeometry(Helper.createPointList());
edge3.setName("Street 3");
EdgeIteratorState edge4 = g.edge(3, 4).setDistance(500).setFlags(encoder.setProperties(50, true, true));
edge4.setWayGeometry(Helper.createPointList());
edge4.setName("Street 4");
Path path = new Path(g, new FastestWeighting(encoder));
SPTEntry e1 = new SPTEntry(edge4.getEdge(), 4, 1);
e1.parent = new SPTEntry(edge3.getEdge(), 3, 1);
e1.parent.parent = new SPTEntry(edge2.getEdge(), 2, 1);
e1.parent.parent.parent = new SPTEntry(edge1.getEdge(), 1, 1);
e1.parent.parent.parent.parent = new SPTEntry(-1, 0, 1);
path.setSPTEntry(e1);
path.extract();
InstructionList il = path.calcInstructions(tr);
Instruction nextInstr0 = il.find(-0.001, 0.0, 1000);
assertEquals(Instruction.CONTINUE_ON_STREET, nextInstr0.getSign());
Instruction nextInstr1 = il.find(0.001, 0.001, 1000);
assertEquals(Instruction.TURN_RIGHT, nextInstr1.getSign());
Instruction nextInstr2 = il.find(5.0, 0.004, 1000);
assertEquals(Instruction.TURN_LEFT, nextInstr2.getSign());
Instruction nextInstr3 = il.find(9.99, 0.503, 1000);
assertEquals(Instruction.TURN_SHARP_LEFT, nextInstr3.getSign());
// a bit far away ...
Instruction nextInstr4 = il.find(7.40, 0.25, 20000);
assertEquals(Instruction.FINISH, nextInstr4.getSign());
// too far away
assertNull(il.find(50.8, 50.25, 1000));
}
use of com.graphhopper.routing.weighting.FastestWeighting 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());
}
Aggregations