use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class LMAlgoFactoryDecorator method createPreparations.
/**
* This method creates the landmark storages ready for landmark creation.
*/
public void createPreparations(GraphHopperStorage ghStorage, TraversalMode traversalMode, LocationIndex locationIndex) {
if (!isEnabled() || !preparations.isEmpty())
return;
if (weightings.isEmpty())
throw new IllegalStateException("No landmark weightings found");
List<LandmarkSuggestion> lmSuggestions = new ArrayList<>(lmSuggestionsLocations.size());
if (!lmSuggestionsLocations.isEmpty()) {
try {
for (String loc : lmSuggestionsLocations) {
lmSuggestions.add(LandmarkSuggestion.readLandmarks(loc, locationIndex));
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
for (Weighting weighting : getWeightings()) {
Double maximumWeight = maximumWeights.get(weighting.getName());
if (maximumWeight == null)
throw new IllegalStateException("maximumWeight cannot be null. Default should be just negative");
PrepareLandmarks tmpPrepareLM = new PrepareLandmarks(ghStorage.getDirectory(), ghStorage, weighting, traversalMode, landmarkCount, activeLandmarkCount).setLandmarkSuggestions(lmSuggestions).setMaximumWeight(maximumWeight);
addPreparation(tmpPrepareLM);
}
}
use of com.graphhopper.routing.weighting.Weighting 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.Weighting 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.Weighting 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.Weighting 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) {
}
}
Aggregations