use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.
the class AbstractRoutingAlgorithmTester method testQueryGraphAndFastest.
@Test
public void testQueryGraphAndFastest() {
Weighting weighting = new FastestWeighting(carEncoder);
GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false);
initDirectedAndDiffSpeed(graph, carEncoder);
Path p = calcPathViaQuery(weighting, graph, 0.002, 0.0005, 0.0017, 0.0031);
assertEquals(Helper.createTList(8, 1, 5, 3, 9), p.calcNodes());
assertEquals(602.98, p.getDistance(), 1e-1);
}
use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.
the class AbstractRoutingAlgorithmTester method testTwoWeightsPerEdge2.
@Test
public void testTwoWeightsPerEdge2() {
// other direction should be different!
Weighting fakeWeighting = new Weighting() {
@Override
public FlagEncoder getFlagEncoder() {
return carEncoder;
}
@Override
public double getMinWeight(double distance) {
return 0.8 * distance;
}
@Override
public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
int adj = edgeState.getAdjNode();
int base = edgeState.getBaseNode();
if (reverse) {
int tmp = base;
base = adj;
adj = tmp;
}
// a 'hill' at node 6
if (adj == 6)
return 3 * edgeState.getDistance();
else if (base == 6)
return edgeState.getDistance() * 0.9;
else if (adj == 4)
return 2 * edgeState.getDistance();
return edgeState.getDistance() * 0.8;
}
private final Weighting tmpW = new FastestWeighting(carEncoder);
@Override
public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
return tmpW.calcMillis(edgeState, reverse, prevOrNextEdgeId);
}
@Override
public boolean matches(HintsMap map) {
throw new UnsupportedOperationException("Not supported");
}
@Override
public String getName() {
return "custom";
}
};
AlgorithmOptions opts = AlgorithmOptions.start().weighting(defaultOpts.getWeighting()).build();
GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(opts.getWeighting()), true);
initEleGraph(graph);
Path p = createAlgo(graph, opts).calcPath(0, 10);
// GHUtility.printEdgeInfo(graph, carEncoder);
assertEquals(Helper.createTList(0, 4, 6, 10), p.calcNodes());
AlgorithmOptions fakeOpts = AlgorithmOptions.start().weighting(fakeWeighting).build();
graph = createGHStorage(encodingManager, Arrays.asList(fakeOpts.getWeighting()), true);
initEleGraph(graph);
QueryResult from = newQR(graph, 3, 0);
QueryResult to = newQR(graph, 10, 9);
RoutingAlgorithmFactory factory = createFactory(graph, fakeOpts);
QueryGraph qGraph = new QueryGraph(getGraph(graph, fakeWeighting)).lookup(from, to);
p = factory.createAlgo(qGraph, fakeOpts).calcPath(from.getClosestNode(), to.getClosestNode());
assertEquals(Helper.createTList(12, 0, 1, 2, 11, 7, 10, 13), p.calcNodes());
assertEquals(37009621, p.getTime());
assertEquals(616827, p.getDistance(), 1);
assertEquals(493462, p.getWeight(), 1);
}
use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.
the class AlternativeRouteTest method testDisconnectedAreas.
@Test
public void testDisconnectedAreas() {
Graph g = createTestGraph(true, em);
// one single disconnected node
updateDistancesFor(g, 20, 0.00, -0.01);
Weighting weighting = new FastestWeighting(carFE);
AlternativeBidirSearch altDijkstra = new AlternativeBidirSearch(g, weighting, traversalMode, 1);
Path path = altDijkstra.calcPath(1, 20);
assertFalse(path.isFound());
// make sure not the full graph is traversed!
assertEquals(3, altDijkstra.getVisitedNodes());
}
use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.
the class GraphHopperOSMTest method testGetWeightingForCH.
@Test
public void testGetWeightingForCH() {
TestEncoder truck = new TestEncoder("truck");
TestEncoder simpleTruck = new TestEncoder("simple_truck");
// use simple truck first
EncodingManager em = new EncodingManager(simpleTruck, truck);
CHAlgoFactoryDecorator decorator = new CHAlgoFactoryDecorator();
Weighting fwSimpleTruck = new FastestWeighting(simpleTruck);
Weighting fwTruck = new FastestWeighting(truck);
RAMDirectory ramDir = new RAMDirectory();
GraphHopperStorage storage = new GraphHopperStorage(Arrays.asList(fwSimpleTruck, fwTruck), ramDir, em, false, new GraphExtension.NoOpExtension());
decorator.addWeighting(fwSimpleTruck);
decorator.addWeighting(fwTruck);
decorator.addPreparation(new PrepareContractionHierarchies(ramDir, storage, storage.getGraph(CHGraph.class, fwSimpleTruck), fwSimpleTruck, TraversalMode.NODE_BASED));
decorator.addPreparation(new PrepareContractionHierarchies(ramDir, storage, storage.getGraph(CHGraph.class, fwTruck), fwTruck, TraversalMode.NODE_BASED));
HintsMap wMap = new HintsMap("fastest");
wMap.put("vehicle", "truck");
assertEquals("fastest|truck", ((PrepareContractionHierarchies) decorator.getDecoratedAlgorithmFactory(null, wMap)).getWeighting().toString());
wMap.put("vehicle", "simple_truck");
assertEquals("fastest|simple_truck", ((PrepareContractionHierarchies) decorator.getDecoratedAlgorithmFactory(null, wMap)).getWeighting().toString());
// make sure weighting cannot be mixed
decorator.addWeighting(fwTruck);
decorator.addWeighting(fwSimpleTruck);
try {
decorator.addPreparation(new PrepareContractionHierarchies(ramDir, storage, storage.getGraph(CHGraph.class, fwSimpleTruck), fwSimpleTruck, TraversalMode.NODE_BASED));
assertTrue(false);
} catch (Exception ex) {
}
}
use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.
the class QueryGraphTest method testTurnCostsProperlyPropagated_Issue282.
@Test
public void testTurnCostsProperlyPropagated_Issue282() {
TurnCostExtension turnExt = new TurnCostExtension();
FlagEncoder encoder = new CarFlagEncoder(5, 5, 15);
GraphHopperStorage graphWithTurnCosts = new GraphHopperStorage(new RAMDirectory(), new EncodingManager(encoder), false, turnExt).create(100);
NodeAccess na = graphWithTurnCosts.getNodeAccess();
na.setNode(0, .00, .00);
na.setNode(1, .00, .01);
na.setNode(2, .01, .01);
EdgeIteratorState edge0 = graphWithTurnCosts.edge(0, 1, 10, true);
EdgeIteratorState edge1 = graphWithTurnCosts.edge(2, 1, 10, true);
QueryGraph qGraph = new QueryGraph(graphWithTurnCosts);
FastestWeighting weighting = new FastestWeighting(encoder);
TurnWeighting turnWeighting = new TurnWeighting(weighting, (TurnCostExtension) qGraph.getExtension());
assertEquals(0, turnWeighting.calcTurnWeight(edge0.getEdge(), 1, edge1.getEdge()), .1);
// now use turn costs and QueryGraph
turnExt.addTurnInfo(edge0.getEdge(), 1, edge1.getEdge(), encoder.getTurnFlags(false, 10));
assertEquals(10, turnWeighting.calcTurnWeight(edge0.getEdge(), 1, edge1.getEdge()), .1);
QueryResult res1 = createLocationResult(0.000, 0.005, edge0, 0, QueryResult.Position.EDGE);
QueryResult res2 = createLocationResult(0.005, 0.010, edge1, 0, QueryResult.Position.EDGE);
qGraph.lookup(Arrays.asList(res1, res2));
int fromQueryEdge = GHUtility.getEdge(qGraph, res1.getClosestNode(), 1).getEdge();
int toQueryEdge = GHUtility.getEdge(qGraph, res2.getClosestNode(), 1).getEdge();
assertEquals(10, turnWeighting.calcTurnWeight(fromQueryEdge, 1, toQueryEdge), .1);
graphWithTurnCosts.close();
}
Aggregations