use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class NodeContractorTest method testShortestPathSkipNode2.
@Test
public void testShortestPathSkipNode2() {
createExampleGraph();
final double normalDist = new Dijkstra(graph, weighting, traversalMode).calcPath(4, 2).getDistance();
assertEquals(3, normalDist, 1e-5);
DijkstraOneToMany algo = new DijkstraOneToMany(graph, weighting, traversalMode);
setMaxLevelOnAllNodes();
algo.setEdgeFilter(new NodeContractor.IgnoreNodeFilter(lg, graph.getNodes() + 1).setAvoidNode(3));
algo.setWeightLimit(10);
int nodeEntry = algo.findEndNode(4, 2);
assertEquals(4, algo.getWeight(nodeEntry), 1e-5);
nodeEntry = algo.findEndNode(4, 1);
assertEquals(4, algo.getWeight(nodeEntry), 1e-5);
}
use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class CHTurnCostTest method testFiniteUTurnCost_virtualViaNode.
@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void testFiniteUTurnCost_virtualViaNode(String algo) {
// if there is an extra virtual node it can be possible to do a u-turn that otherwise would not be possible
// and so there can be a difference between CH and non-CH... therefore u-turns at virtual nodes are forbidden
// 4->3->2->1-x-0
// |
// 5->6
GHUtility.setSpeed(60, true, false, encoder, graph.edge(4, 3).setDistance(0));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 2).setDistance(0));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 1).setDistance(0));
GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 0).setDistance(0));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 5).setDistance(0));
GHUtility.setSpeed(60, true, false, encoder, graph.edge(5, 6).setDistance(0));
updateDistancesFor(graph, 4, 0.1, 0.0);
updateDistancesFor(graph, 3, 0.1, 0.1);
updateDistancesFor(graph, 2, 0.1, 0.2);
updateDistancesFor(graph, 1, 0.1, 0.3);
updateDistancesFor(graph, 0, 0.1, 0.4);
updateDistancesFor(graph, 5, 0.0, 0.3);
updateDistancesFor(graph, 6, 0.0, 0.4);
// not allowed to turn right at node 1 -> we have to take a u-turn at node 0 (or at the virtual node...)
setRestriction(2, 1, 5);
graph.freeze();
chConfig = chConfigs.get(1);
prepareCH(0, 1, 2, 3, 4, 5, 6);
LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
index.prepareIndex();
GHPoint virtualPoint = new GHPoint(0.1, 0.35);
Snap snap = index.findClosest(virtualPoint.lat, virtualPoint.lon, EdgeFilter.ALL_EDGES);
QueryGraph chQueryGraph = QueryGraph.create(graph, snap);
assertEquals(3, snap.getClosestEdge().getEdge());
QueryRoutingCHGraph routingCHGraph = new QueryRoutingCHGraph(chGraph, chQueryGraph);
RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
Path path = chAlgo.calcPath(4, 6);
assertTrue(path.isFound());
assertEquals(IntArrayList.from(4, 3, 2, 1, 0, 1, 5, 6), path.calcNodes());
Snap snap2 = index.findClosest(virtualPoint.lat, virtualPoint.lon, EdgeFilter.ALL_EDGES);
QueryGraph queryGraph = QueryGraph.create(graph, snap2);
assertEquals(3, snap2.getClosestEdge().getEdge());
Weighting w = queryGraph.wrapWeighting(chConfig.getWeighting());
Dijkstra dijkstra = new Dijkstra(queryGraph, w, TraversalMode.EDGE_BASED);
Path dijkstraPath = dijkstra.calcPath(4, 6);
assertEquals(IntArrayList.from(4, 3, 2, 1, 7, 0, 7, 1, 5, 6), dijkstraPath.calcNodes());
assertEquals(dijkstraPath.getWeight(), path.getWeight(), 1.e-2);
assertEquals(dijkstraPath.getDistance(), path.getDistance(), 1.e-2);
assertEquals(dijkstraPath.getTime(), path.getTime());
}
use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class PrepareContractionHierarchiesTest method testReusingNodeOrdering.
@Test
public void testReusingNodeOrdering() {
CarFlagEncoder carFlagEncoder = new CarFlagEncoder();
MotorcycleFlagEncoder motorCycleEncoder = new MotorcycleFlagEncoder();
EncodingManager em = EncodingManager.create(carFlagEncoder, motorCycleEncoder);
CHConfig carConfig = CHConfig.nodeBased("c1", new FastestWeighting(carFlagEncoder));
CHConfig motorCycleConfig = CHConfig.nodeBased("c2", new FastestWeighting(motorCycleEncoder));
GraphHopperStorage ghStorage = new GraphBuilder(em).create();
int numNodes = 5_000;
int numQueries = 100;
long seed = System.nanoTime();
Random rnd = new Random(seed);
GHUtility.buildRandomGraph(ghStorage, rnd, numNodes, 1.3, true, true, carFlagEncoder.getAccessEnc(), carFlagEncoder.getAverageSpeedEnc(), null, 0.7, 0.9, 0.8);
ghStorage.freeze();
// create CH for cars
StopWatch sw = new StopWatch().start();
PrepareContractionHierarchies carPch = PrepareContractionHierarchies.fromGraphHopperStorage(ghStorage, carConfig);
PrepareContractionHierarchies.Result res = carPch.doWork();
long timeCar = sw.stop().getMillis();
// create CH for motorcycles, re-use car contraction order
// this speeds up contraction significantly, but can lead to slower queries
sw = new StopWatch().start();
CHStorage chStore = res.getCHStorage();
NodeOrderingProvider nodeOrderingProvider = chStore.getNodeOrderingProvider();
PrepareContractionHierarchies motorCyclePch = PrepareContractionHierarchies.fromGraphHopperStorage(ghStorage, motorCycleConfig).useFixedNodeOrdering(nodeOrderingProvider);
PrepareContractionHierarchies.Result resMotorCycle = motorCyclePch.doWork();
RoutingCHGraph motorCycleCH = ghStorage.createCHGraph(resMotorCycle.getCHStorage(), resMotorCycle.getCHConfig());
// run a few sample queries to check correctness
for (int i = 0; i < numQueries; ++i) {
Dijkstra dijkstra = new Dijkstra(ghStorage, motorCycleConfig.getWeighting(), TraversalMode.NODE_BASED);
RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(motorCycleCH).createAlgo(new PMap());
int from = rnd.nextInt(numNodes);
int to = rnd.nextInt(numNodes);
double dijkstraWeight = dijkstra.calcPath(from, to).getWeight();
double chWeight = chAlgo.calcPath(from, to).getWeight();
assertEquals(dijkstraWeight, chWeight, 1.e-1);
}
long timeMotorCycle = sw.getMillis();
assertTrue(timeMotorCycle < 0.5 * timeCar, "reusing node ordering should speed up ch contraction");
}
use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class InstructionListTest method testNoInstructionIfSlightTurnAndAlternativeIsSharp2.
@Test
public void testNoInstructionIfSlightTurnAndAlternativeIsSharp2() {
Graph g = new GraphBuilder(carManager).create();
// Real World Example: https://graphhopper.com/maps/?point=48.748493%2C9.322455&point=48.748776%2C9.321889
// https://github.com/graphhopper/graphhopper/issues/1441
// From 1 to 3
//
// 3
// \
// 2--- 1
// \
// 4
NodeAccess na = g.getNodeAccess();
na.setNode(1, 48.748493, 9.322455);
na.setNode(2, 48.748577, 9.322152);
na.setNode(3, 48.748776, 9.321889);
na.setNode(4, 48.74847, 9.322299);
GHUtility.setSpeed(60, true, true, carEncoder, g.edge(1, 2).setDistance(10));
GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 3).setDistance(10));
GHUtility.setSpeed(60, true, true, carEncoder, g.edge(2, 4).setDistance(10));
ShortestWeighting weighting = new ShortestWeighting(carEncoder);
Path p = new Dijkstra(g, weighting, tMode).calcPath(1, 3);
InstructionList wayList = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
List<String> tmpList = getTurnDescriptions(wayList);
assertEquals(Arrays.asList("continue", "arrive at destination"), tmpList);
}
use of com.graphhopper.routing.Dijkstra in project graphhopper by graphhopper.
the class InstructionListTest method testEmptyList.
@Test
public void testEmptyList() {
Graph g = new GraphBuilder(carManager).create();
g.getNodeAccess().setNode(1, 0, 0);
ShortestWeighting weighting = new ShortestWeighting(carEncoder);
Path p = new Dijkstra(g, weighting, tMode).calcPath(0, 1);
InstructionList il = InstructionsFromEdges.calcInstructions(p, g, weighting, carManager, usTR);
assertEquals(0, il.size());
}
Aggregations