use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.
the class RandomCHRoutingTest method buildRandomGraphLegacy.
/**
* More or less does the same as {@link GHUtility#buildRandomGraph}, but since some special seeds
* are used in a few tests above this code is kept here. Do not use it for new tests.
*/
private void buildRandomGraphLegacy(Graph graph, FlagEncoder encoder, Random random, int numNodes, double meanDegree, boolean allowLoops, boolean allowZeroDistance, double pBothDir) {
for (int i = 0; i < numNodes; ++i) {
double lat = 49.4 + (random.nextDouble() * 0.0001);
double lon = 9.7 + (random.nextDouble() * 0.0001);
graph.getNodeAccess().setNode(i, lat, lon);
}
double minDist = Double.MAX_VALUE;
double maxDist = Double.MIN_VALUE;
int numEdges = (int) (0.5 * meanDegree * numNodes);
for (int i = 0; i < numEdges; ++i) {
int from = random.nextInt(numNodes);
int to = random.nextInt(numNodes);
if (!allowLoops && from == to) {
continue;
}
double distance = GHUtility.getDistance(from, to, graph.getNodeAccess());
if (!allowZeroDistance) {
distance = Math.max(0.001, distance);
}
// add some random offset for most cases, but also allow duplicate edges with same weight
if (random.nextDouble() < 0.8)
distance += random.nextDouble() * distance * 0.01;
minDist = Math.min(minDist, distance);
maxDist = Math.max(maxDist, distance);
// using bidirectional edges will increase mean degree of graph above given value
boolean bothDirections = random.nextDouble() < pBothDir;
EdgeIteratorState edge = GHUtility.setSpeed(60, true, bothDirections, encoder, graph.edge(from, to).setDistance(distance));
double fwdSpeed = 10 + random.nextDouble() * 120;
double bwdSpeed = 10 + random.nextDouble() * 120;
DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc();
edge.set(speedEnc, fwdSpeed);
if (speedEnc.isStoreTwoDirections())
edge.setReverse(speedEnc, bwdSpeed);
}
}
use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.
the class PriorityRoutingTest method maxSpeedEdge.
private EdgeIteratorState maxSpeedEdge(EncodingManager em, GraphHopperStorage graph, int p, int q, FlagEncoder encoder, double prio) {
BooleanEncodedValue accessEnc = encoder.getAccessEnc();
DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc();
DecimalEncodedValue priorityEnc = em.getDecimalEncodedValue(EncodingManager.getKey(encoder, "priority"));
EnumEncodedValue<RoadClass> roadClassEnc = em.getEnumEncodedValue(RoadClass.KEY, RoadClass.class);
return graph.edge(p, q).set(accessEnc, true).set(speedEnc, encoder.getMaxSpeed()).set(priorityEnc, prio).set(roadClassEnc, RoadClass.MOTORWAY).setDistance(calcDist(graph, p, q));
}
use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.
the class RoutingAlgorithmTest method test0SpeedButUnblocked_Issue242.
@ParameterizedTest
@ArgumentsSource(FixtureProvider.class)
public void test0SpeedButUnblocked_Issue242(Fixture f) {
GraphHopperStorage graph = f.createGHStorage();
EdgeIteratorState edge01 = graph.edge(0, 1).setDistance(10);
EdgeIteratorState edge12 = graph.edge(1, 2).setDistance(10);
BooleanEncodedValue carAccessEnc = f.carEncoder.getAccessEnc();
DecimalEncodedValue carAvSpeedEnc = f.carEncoder.getAverageSpeedEnc();
edge01.set(carAvSpeedEnc, 0.0).set(carAccessEnc, true, true);
edge01.setFlags(edge01.getFlags());
edge12.set(carAvSpeedEnc, 0.0).set(carAccessEnc, true, true);
edge12.setFlags(edge12.getFlags());
try {
f.calcPath(graph, 0, 2);
fail("there should have been an exception");
} catch (Exception ex) {
assertTrue(ex.getMessage().startsWith("Speed cannot be 0"), ex.getMessage());
}
}
use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.
the class QueryGraphTest method testVirtualEdgeIds.
@Test
public void testVirtualEdgeIds() {
// virtual nodes: 2
// 0 - x - 1
// virtual edges: 1 2
FlagEncoder encoder = new CarFlagEncoder(new PMap().putObject("speed_two_directions", true));
EncodingManager encodingManager = EncodingManager.create(encoder);
DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc();
Graph g = new GraphBuilder(encodingManager).create();
NodeAccess na = g.getNodeAccess();
na.setNode(0, 50.00, 10.10);
na.setNode(1, 50.00, 10.20);
double dist = DistanceCalcEarth.DIST_EARTH.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1));
EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(dist));
edge.set(speedEnc, 50);
edge.setReverse(speedEnc, 100);
// query graph
Snap snap = createLocationResult(50.00, 10.15, edge, 0, EDGE);
QueryGraph queryGraph = QueryGraph.create(g, snap);
assertEquals(3, queryGraph.getNodes());
assertEquals(5, queryGraph.getEdges());
assertEquals(4, queryGraph.getVirtualEdges().size());
EdgeIteratorState edge_0x = queryGraph.getEdgeIteratorState(1, 2);
EdgeIteratorState edge_x0 = queryGraph.getEdgeIteratorState(1, 0);
EdgeIteratorState edge_x1 = queryGraph.getEdgeIteratorState(2, 1);
EdgeIteratorState edge_1x = queryGraph.getEdgeIteratorState(2, 2);
assertNodes(edge_0x, 0, 2);
assertNodes(edge_x0, 2, 0);
assertNodes(edge_x1, 2, 1);
assertNodes(edge_1x, 1, 2);
// virtual edge IDs are 1 and 2
assertEquals(1, edge_0x.getEdge());
assertEquals(1, edge_x0.getEdge());
assertEquals(2, edge_x1.getEdge());
assertEquals(2, edge_1x.getEdge());
// edge keys
assertEquals(2, edge_0x.getEdgeKey());
assertEquals(3, edge_x0.getEdgeKey());
assertEquals(4, edge_x1.getEdgeKey());
assertEquals(5, edge_1x.getEdgeKey());
assertNodes(queryGraph.getEdgeIteratorStateForKey(2), 0, 2);
assertNodes(queryGraph.getEdgeIteratorStateForKey(3), 2, 0);
assertNodes(queryGraph.getEdgeIteratorStateForKey(4), 2, 1);
assertNodes(queryGraph.getEdgeIteratorStateForKey(5), 1, 2);
// internally each edge is represented by two edge states for the two directions
assertSame(queryGraph.getVirtualEdges().get(0), edge_0x);
assertSame(queryGraph.getVirtualEdges().get(1), edge_x0);
assertSame(queryGraph.getVirtualEdges().get(2), edge_x1);
assertSame(queryGraph.getVirtualEdges().get(3), edge_1x);
for (EdgeIteratorState e : Arrays.asList(edge_0x, edge_x1)) {
assertEquals(50, e.get(speedEnc), 1.e-6);
assertEquals(100, e.getReverse(speedEnc), 1.e-6);
}
for (EdgeIteratorState e : Arrays.asList(edge_x0, edge_1x)) {
assertEquals(100, e.get(speedEnc), 1.e-6);
assertEquals(50, e.getReverse(speedEnc), 1.e-6);
}
try {
queryGraph.getEdgeIteratorState(3, 2);
fail("there should be an error");
} catch (IndexOutOfBoundsException e) {
// ok
}
}
use of com.graphhopper.routing.ev.DecimalEncodedValue 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;
DecimalEncodedValue avSpeedEnc = encoder.getAverageSpeedEnc();
BooleanEncodedValue accessEnc = encoder.getAccessEnc();
for (int hIndex = 0; hIndex < height; hIndex++) {
for (int wIndex = 0; wIndex < width; wIndex++) {
int node = wIndex + hIndex * width;
// do not connect first with last column!
double speed = 20 + rand.nextDouble() * 30;
if (wIndex + 1 < width)
graph.edge(node, node + 1).set(accessEnc, true, true).set(avSpeedEnc, speed);
// avoid dead ends
if (hIndex + 1 < height)
graph.edge(node, node + width).set(accessEnc, true, true).set(avSpeedEnc, speed);
updateDistancesFor(graph, node, -hIndex / 50.0, wIndex / 50.0);
}
}
Directory dir = new RAMDirectory();
LocationIndexTree index = new LocationIndexTree(graph, dir);
index.prepareIndex();
int lm = 5, activeLM = 2;
Weighting weighting = new FastestWeighting(encoder);
LMConfig lmConfig = new LMConfig("car", weighting);
LandmarkStorage store = new LandmarkStorage(graph, dir, lmConfig, lm);
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];
Arrays.fill(activeLandmarkIndices, -1);
store.chooseActiveLandmarks(27, 47, activeLandmarkIndices, 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);
PrepareLandmarks prepare = new PrepareLandmarks(new RAMDirectory(), graph, lmConfig, 4);
prepare.setMinimumNodes(2);
prepare.doWork();
LandmarkStorage lms = prepare.getLandmarkStorage();
AStar expectedAlgo = new AStar(graph, weighting, tm);
Path expectedPath = expectedAlgo.calcPath(41, 183);
PMap hints = new PMap().putObject(Parameters.Landmark.ACTIVE_COUNT, 2);
// landmarks with A*
RoutingAlgorithm oneDirAlgoWithLandmarks = new LMRoutingAlgorithmFactory(lms).createAlgo(graph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR).setTraversalMode(tm).setHints(hints));
Path path = oneDirAlgoWithLandmarks.calcPath(41, 183);
assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
assertEquals(expectedPath.calcNodes(), path.calcNodes());
assertEquals(expectedAlgo.getVisitedNodes() - 135, oneDirAlgoWithLandmarks.getVisitedNodes());
// landmarks with bidir A*
RoutingAlgorithm biDirAlgoWithLandmarks = new LMRoutingAlgorithmFactory(lms).createAlgo(graph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR_BI).setTraversalMode(tm).setHints(hints));
path = biDirAlgoWithLandmarks.calcPath(41, 183);
assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
assertEquals(expectedPath.calcNodes(), path.calcNodes());
assertEquals(expectedAlgo.getVisitedNodes() - 162, biDirAlgoWithLandmarks.getVisitedNodes());
// 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
Snap fromSnap = index.findClosest(-0.0401, 0.2201, EdgeFilter.ALL_EDGES);
Snap toSnap = index.findClosest(-0.2401, 0.0601, EdgeFilter.ALL_EDGES);
QueryGraph qGraph = QueryGraph.create(graph, fromSnap, toSnap);
RoutingAlgorithm qGraphOneDirAlgo = new LMRoutingAlgorithmFactory(lms).createAlgo(qGraph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR).setTraversalMode(tm).setHints(hints));
path = qGraphOneDirAlgo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
expectedAlgo = new AStar(qGraph, weighting, tm);
expectedPath = expectedAlgo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
assertEquals(expectedPath.calcNodes(), path.calcNodes());
assertEquals(expectedAlgo.getVisitedNodes() - 135, qGraphOneDirAlgo.getVisitedNodes());
}
Aggregations