use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.
the class PathTest method testCalcInstructionsForSlightTurnOntoDifferentStreet.
@Test
public void testCalcInstructionsForSlightTurnOntoDifferentStreet() {
final Graph g = new GraphBuilder(carManager).create();
final NodeAccess na = g.getNodeAccess();
// Actual example: point=48.76445%2C8.679054&point=48.764152%2C8.678722
// 1
// /
// 2 - 3 - 4
//
na.setNode(1, 48.76423, 8.679103);
na.setNode(2, 48.76417, 8.678647);
na.setNode(3, 48.764149, 8.678926);
na.setNode(4, 48.764085, 8.679183);
GHUtility.setSpeed(60, true, true, encoder, g.edge(1, 3).setDistance(5)).setName("Talstraße, K 4313");
GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 3).setDistance(5)).setName("Calmbacher Straße, K 4312");
GHUtility.setSpeed(60, true, true, encoder, g.edge(3, 4).setDistance(5)).setName("Calmbacher Straße, K 4312");
ShortestWeighting weighting = new ShortestWeighting(encoder);
Path p = new Dijkstra(g, weighting, TraversalMode.NODE_BASED).calcPath(1, 2);
assertTrue(p.isFound());
InstructionList wayList = InstructionsFromEdges.calcInstructions(p, p.graph, weighting, carManager, tr);
assertEquals(3, wayList.size());
assertEquals(Instruction.TURN_SLIGHT_RIGHT, wayList.get(1).getSign());
}
use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.
the class LMApproximatorTest method run.
private void run(long seed) {
Directory dir = new RAMDirectory();
CarFlagEncoder encoder = new CarFlagEncoder(5, 5, 1);
EncodingManager encodingManager = new EncodingManager.Builder().add(encoder).add(Subnetwork.create("car")).build();
GraphHopperStorage graph = new GraphBuilder(encodingManager).setDir(dir).withTurnCosts(true).create();
Random rnd = new Random(seed);
GHUtility.buildRandomGraph(graph, rnd, 100, 2.2, true, true, encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), null, 0.7, 0.8, 0.8);
Weighting weighting = new FastestWeighting(encoder);
PrepareLandmarks lm = new PrepareLandmarks(dir, graph, new LMConfig("car", weighting), 16);
lm.setMaximumWeight(10000);
lm.doWork();
LandmarkStorage landmarkStorage = lm.getLandmarkStorage();
for (int t = 0; t < graph.getNodes(); t++) {
LMApproximator lmApproximator = new LMApproximator(graph, weighting, graph.getNodes(), landmarkStorage, 8, landmarkStorage.getFactor(), false);
WeightApproximator reverseLmApproximator = lmApproximator.reverse();
BeelineWeightApproximator beelineApproximator = new BeelineWeightApproximator(graph.getNodeAccess(), weighting);
WeightApproximator reverseBeelineApproximator = beelineApproximator.reverse();
PerfectApproximator perfectApproximator = new PerfectApproximator(graph, weighting, TraversalMode.NODE_BASED, false);
PerfectApproximator reversePerfectApproximator = new PerfectApproximator(graph, weighting, TraversalMode.NODE_BASED, true);
BalancedWeightApproximator balancedWeightApproximator = new BalancedWeightApproximator(new LMApproximator(graph, weighting, graph.getNodes(), landmarkStorage, 8, landmarkStorage.getFactor(), false));
lmApproximator.setTo(t);
beelineApproximator.setTo(t);
reverseLmApproximator.setTo(t);
reverseBeelineApproximator.setTo(t);
perfectApproximator.setTo(t);
reversePerfectApproximator.setTo(t);
balancedWeightApproximator.setFromTo(0, t);
int nOverApproximatedWeights = 0;
int nInconsistentWeights = 0;
for (int v = 0; v < graph.getNodes(); v++) {
Dijkstra dijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);
Path path = dijkstra.calcPath(v, t);
if (path.isFound()) {
// Give the beelineApproximator some slack, because the map distance of an edge
// can be _smaller_ than its Euklidean distance, due to rounding.
double slack = path.getEdgeCount() * (1 / 1000.0);
double realRemainingWeight = path.getWeight();
double approximatedRemainingWeight = lmApproximator.approximate(v);
if (approximatedRemainingWeight - slack > realRemainingWeight) {
System.out.printf("LM: %f\treal: %f\n", approximatedRemainingWeight, realRemainingWeight);
nOverApproximatedWeights++;
}
double beelineApproximatedRemainingWeight = beelineApproximator.approximate(v);
if (beelineApproximatedRemainingWeight - slack > realRemainingWeight) {
System.out.printf("beeline: %f\treal: %f\n", beelineApproximatedRemainingWeight, realRemainingWeight);
nOverApproximatedWeights++;
}
double approximate = perfectApproximator.approximate(v);
if (approximate > realRemainingWeight) {
System.out.printf("perfect: %f\treal: %f\n", approximate, realRemainingWeight);
nOverApproximatedWeights++;
}
// Triangle inequality for approximator. This is what makes it 'consistent'.
// That's a requirement for normal A*-implementations, because if it is violated,
// the heap-weight of settled nodes can decrease, and that would mean our
// stopping criterion is not sufficient.
EdgeIterator neighbors = graph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())).setBaseNode(v);
while (neighbors.next()) {
int w = neighbors.getAdjNode();
double vw = weighting.calcEdgeWeight(neighbors, false);
double vwApprox = lmApproximator.approximate(v) - lmApproximator.approximate(w);
if (vwApprox - lm.getLandmarkStorage().getFactor() > vw) {
System.out.printf("%f\t%f\n", vwApprox - lm.getLandmarkStorage().getFactor(), vw);
nInconsistentWeights++;
}
}
neighbors = graph.createEdgeExplorer(AccessFilter.outEdges(encoder.getAccessEnc())).setBaseNode(v);
while (neighbors.next()) {
int w = neighbors.getAdjNode();
double vw = weighting.calcEdgeWeight(neighbors, false);
double vwApprox = balancedWeightApproximator.approximate(v, false) - balancedWeightApproximator.approximate(w, false);
if (vwApprox - lm.getLandmarkStorage().getFactor() > vw) {
System.out.printf("%f\t%f\n", vwApprox - lm.getLandmarkStorage().getFactor(), vw);
nInconsistentWeights++;
}
}
}
Dijkstra reverseDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED);
Path reversePath = reverseDijkstra.calcPath(t, v);
if (reversePath.isFound()) {
// Give the beelineApproximator some slack, because the map distance of an edge
// can be _smaller_ than its Euklidean distance, due to rounding.
double slack = reversePath.getEdgeCount() * (1 / 1000.0);
double realRemainingWeight = reversePath.getWeight();
double approximatedRemainingWeight = reverseLmApproximator.approximate(v);
if (approximatedRemainingWeight - slack > realRemainingWeight) {
System.out.printf("LM: %f\treal: %f\n", approximatedRemainingWeight, realRemainingWeight);
nOverApproximatedWeights++;
}
double beelineApproximatedRemainingWeight = reverseBeelineApproximator.approximate(v);
if (beelineApproximatedRemainingWeight - slack > realRemainingWeight) {
System.out.printf("beeline: %f\treal: %f\n", beelineApproximatedRemainingWeight, realRemainingWeight);
nOverApproximatedWeights++;
}
double approximate = reversePerfectApproximator.approximate(v);
if (approximate > realRemainingWeight) {
System.out.printf("perfect: %f\treal: %f\n", approximate, realRemainingWeight);
nOverApproximatedWeights++;
}
}
}
assertEquals(0, nOverApproximatedWeights, "too many over approximated weights, seed: " + seed);
assertEquals(0, nInconsistentWeights, "too many inconsistent weights, seed: " + seed);
}
}
use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.
the class LMPreparationHandlerTest method maximumLMWeight.
@Test
public void maximumLMWeight() {
LMPreparationHandler handler = new LMPreparationHandler();
handler.setLMProfiles(new LMProfile("conf1").setMaximumLMWeight(65_000), new LMProfile("conf2").setMaximumLMWeight(20_000));
FlagEncoder car = new CarFlagEncoder();
EncodingManager em = EncodingManager.create(car);
List<LMConfig> lmConfigs = Arrays.asList(new LMConfig("conf1", new FastestWeighting(car)), new LMConfig("conf2", new ShortestWeighting(car)));
List<PrepareLandmarks> preparations = handler.createPreparations(lmConfigs, new GraphBuilder(em).build(), null);
assertEquals(1, preparations.get(0).getLandmarkStorage().getFactor(), .1);
assertEquals(0.3, preparations.get(1).getLandmarkStorage().getFactor(), .1);
}
use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.
the class LandmarkStorageTest method setUp.
@BeforeEach
public void setUp() {
encoder = new CarFlagEncoder();
subnetworkEnc = Subnetwork.create("car");
encodingManager = new EncodingManager.Builder().add(encoder).add(subnetworkEnc).build();
graph = new GraphBuilder(encodingManager).build();
graph.create(1000);
}
use of com.graphhopper.storage.GraphBuilder in project graphhopper by graphhopper.
the class EdgeBasedTarjanSCCTest method oneWayBridges.
@Test
public void oneWayBridges() {
GraphHopperStorage g = new GraphBuilder(em).create();
// 0 - 1 -> 2 - 3
// | |
// 4 - 5 -> 6 - 7
GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(1));
GHUtility.setSpeed(60, true, false, encoder, g.edge(1, 2).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 3).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, g.edge(2, 4).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, g.edge(3, 5).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, g.edge(4, 5).setDistance(1));
GHUtility.setSpeed(60, true, false, encoder, g.edge(5, 6).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, g.edge(6, 7).setDistance(1));
ConnectedComponents result = EdgeBasedTarjanSCC.findComponentsRecursive(g, fwdAccessFilter, false);
assertEquals(16, result.getEdgeKeys());
assertEquals(7, result.getTotalComponents());
// 0-1, 2-3-5-4-2 and 6-7
assertEquals(3, result.getComponents().size());
// 1->2, 2->1 and 5->6, 6<-5
assertEquals(4, result.getSingleEdgeComponents().cardinality());
assertEquals(result.getComponents().get(1), result.getBiggestComponent());
}
Aggregations