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 RoutingAlgorithmWithOSMIT method testMonacoParallel.
@Test
public void testMonacoParallel() throws IOException {
System.out.println("testMonacoParallel takes a bit time...");
String graphFile = "target/monaco-gh";
Helper.removeDir(new File(graphFile));
final EncodingManager encodingManager = new EncodingManager("car");
final GraphHopper hopper = new GraphHopperOSM().setStoreOnFlush(true).setEncodingManager(encodingManager).setCHEnabled(false).setWayPointMaxDistance(0).setDataReaderFile(DIR + "/monaco.osm.gz").setGraphHopperLocation(graphFile).importOrLoad();
final Graph g = hopper.getGraphHopperStorage();
final LocationIndex idx = hopper.getLocationIndex();
final List<OneRun> instances = createMonacoCar();
List<Thread> threads = new ArrayList<Thread>();
final AtomicInteger integ = new AtomicInteger(0);
int MAX = 100;
final FlagEncoder carEncoder = encodingManager.getEncoder("car");
// testing if algorithms are independent. should be. so test only two algorithms.
// also the preparing is too costly to be called for every thread
int algosLength = 2;
final Weighting weighting = new ShortestWeighting(encodingManager.getEncoder("car"));
final EdgeFilter filter = new DefaultEdgeFilter(carEncoder);
for (int no = 0; no < MAX; no++) {
for (int instanceNo = 0; instanceNo < instances.size(); instanceNo++) {
String[] algos = new String[] { ASTAR, DIJKSTRA_BI };
for (final String algoStr : algos) {
// an algorithm is not thread safe! reuse via clear() is ONLY appropriated if used from same thread!
final int instanceIndex = instanceNo;
Thread t = new Thread() {
@Override
public void run() {
OneRun oneRun = instances.get(instanceIndex);
AlgorithmOptions opts = AlgorithmOptions.start().weighting(weighting).algorithm(algoStr).build();
testCollector.assertDistance(new AlgoHelperEntry(g, opts, idx, algoStr + "|" + weighting), oneRun.getList(idx, filter), oneRun);
integ.addAndGet(1);
}
};
t.start();
threads.add(t);
}
}
}
for (Thread t : threads) {
try {
t.join();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
assertEquals(MAX * algosLength * instances.size(), integ.get());
assertEquals(testCollector.toString(), 0, testCollector.errors.size());
hopper.close();
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class Measurement method start.
// creates properties file in the format key=value
// Every value is one y-value in a separate diagram with an identical x-value for every Measurement.start call
void start(CmdArgs args) {
String graphLocation = args.get("graph.location", "");
String propLocation = args.get("measurement.location", "");
if (Helper.isEmpty(propLocation))
propLocation = "measurement" + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".properties";
seed = args.getLong("measurement.seed", 123);
String gitCommit = args.get("measurement.gitinfo", "");
int count = args.getInt("measurement.count", 5000);
GraphHopper hopper = new GraphHopperOSM() {
@Override
protected void prepareCH() {
StopWatch sw = new StopWatch().start();
super.prepareCH();
put(Parameters.CH.PREPARE + "time", sw.stop().getTime());
int edges = getGraphHopperStorage().getAllEdges().getMaxId();
if (getCHFactoryDecorator().hasWeightings()) {
Weighting weighting = getCHFactoryDecorator().getWeightings().get(0);
int edgesAndShortcuts = getGraphHopperStorage().getGraph(CHGraph.class, weighting).getAllEdges().getMaxId();
put(Parameters.CH.PREPARE + "shortcuts", edgesAndShortcuts - edges);
}
}
@Override
protected DataReader importData() throws IOException {
StopWatch sw = new StopWatch().start();
DataReader dr = super.importData();
put("graph.import_time", sw.stop().getSeconds());
return dr;
}
};
hopper.init(args).forDesktop();
hopper.getCHFactoryDecorator().setDisablingAllowed(true);
hopper.getLMFactoryDecorator().setDisablingAllowed(true);
hopper.importOrLoad();
GraphHopperStorage g = hopper.getGraphHopperStorage();
String vehicleStr = args.get("graph.flag_encoders", "car");
FlagEncoder encoder = hopper.getEncodingManager().getEncoder(vehicleStr);
StopWatch sw = new StopWatch().start();
try {
maxNode = g.getNodes();
boolean isCH = false;
boolean isLM = false;
GHBitSet allowedEdges = printGraphDetails(g, vehicleStr);
printMiscUnitPerfTests(g, isCH, encoder, count * 100, allowedEdges);
printLocationIndexQuery(g, hopper.getLocationIndex(), count);
printTimeOfRouteQuery(hopper, isCH, isLM, count / 20, "routing", vehicleStr, true, -1);
if (hopper.getLMFactoryDecorator().isEnabled()) {
System.gc();
isLM = true;
int activeLMCount = 12;
for (; activeLMCount > 3; activeLMCount -= 4) {
printTimeOfRouteQuery(hopper, isCH, isLM, count / 4, "routingLM" + activeLMCount, vehicleStr, true, activeLMCount);
}
// compareRouting(hopper, vehicleStr, count / 5);
}
if (hopper.getCHFactoryDecorator().isEnabled()) {
isCH = true;
if (hopper.getLMFactoryDecorator().isEnabled()) {
isLM = true;
System.gc();
// try just one constellation, often ~4-6 is best
int lmCount = 5;
printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCHLM" + lmCount, vehicleStr, true, lmCount);
}
isLM = false;
System.gc();
Weighting weighting = hopper.getCHFactoryDecorator().getWeightings().get(0);
CHGraph lg = g.getGraph(CHGraph.class, weighting);
fillAllowedEdges(lg.getAllEdges(), allowedEdges);
printMiscUnitPerfTests(lg, isCH, encoder, count * 100, allowedEdges);
printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH", vehicleStr, true, -1);
printTimeOfRouteQuery(hopper, isCH, isLM, count, "routingCH_no_instr", vehicleStr, false, -1);
}
logger.info("store into " + propLocation);
} catch (Exception ex) {
logger.error("Problem while measuring " + graphLocation, ex);
put("error", ex.toString());
} finally {
put("measurement.gitinfo", gitCommit);
put("measurement.count", count);
put("measurement.seed", seed);
put("measurement.time", sw.stop().getTime());
System.gc();
put("measurement.totalMB", Helper.getTotalMB());
put("measurement.usedMB", Helper.getUsedMB());
try {
store(new FileWriter(propLocation), "measurement finish, " + new Date().toString() + ", " + Constants.BUILD_DATE);
} catch (IOException ex) {
logger.error("Problem while storing properties " + graphLocation + ", " + propLocation, ex);
}
}
}
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) {
}
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class RoutingAlgorithmIT method createAlgos.
public static List<AlgoHelperEntry> createAlgos(final GraphHopper hopper, final HintsMap hints, TraversalMode tMode) {
GraphHopperStorage ghStorage = hopper.getGraphHopperStorage();
LocationIndex idx = hopper.getLocationIndex();
String addStr = "";
if (tMode.isEdgeBased())
addStr = "turn|";
FlagEncoder encoder = hopper.getEncodingManager().getEncoder(hints.getVehicle());
Weighting weighting = hopper.createWeighting(hints, encoder, hopper.getGraphHopperStorage());
HintsMap defaultHints = new HintsMap().put(Parameters.CH.DISABLE, true).put(Parameters.Landmark.DISABLE, true).setVehicle(hints.getVehicle()).setWeighting(hints.getWeighting());
AlgorithmOptions defaultOpts = AlgorithmOptions.start(new AlgorithmOptions("", weighting, tMode)).hints(defaultHints).build();
List<AlgoHelperEntry> prepare = new ArrayList<>();
prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(ASTAR).build(), idx, "astar|beeline|" + addStr + weighting));
// later: include dijkstraOneToMany
prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA).build(), idx, "dijkstra|" + addStr + weighting));
AlgorithmOptions astarbiOpts = AlgorithmOptions.start(defaultOpts).algorithm(ASTAR_BI).build();
astarbiOpts.getHints().put(ASTAR_BI + ".approximation", "BeelineSimplification");
AlgorithmOptions dijkstrabiOpts = AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA_BI).build();
prepare.add(new AlgoHelperEntry(ghStorage, astarbiOpts, idx, "astarbi|beeline|" + addStr + weighting));
prepare.add(new AlgoHelperEntry(ghStorage, dijkstrabiOpts, idx, "dijkstrabi|" + addStr + weighting));
// add additional preparations if CH and LM preparation are enabled
if (hopper.getLMFactoryDecorator().isEnabled()) {
final HintsMap lmHints = new HintsMap(defaultHints).put(Parameters.Landmark.DISABLE, false);
prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(astarbiOpts).hints(lmHints).build(), idx, "astarbi|landmarks|" + weighting) {
@Override
public RoutingAlgorithmFactory createRoutingFactory() {
return hopper.getAlgorithmFactory(lmHints);
}
});
}
if (hopper.getCHFactoryDecorator().isEnabled()) {
final HintsMap chHints = new HintsMap(defaultHints).put(Parameters.CH.DISABLE, false);
Weighting pickedWeighting = null;
for (Weighting tmpWeighting : hopper.getCHFactoryDecorator().getWeightings()) {
if (tmpWeighting.equals(weighting)) {
pickedWeighting = tmpWeighting;
break;
}
}
if (pickedWeighting == null)
throw new IllegalStateException("Didn't find weighting " + hints.getWeighting() + " in " + hopper.getCHFactoryDecorator().getWeightings());
prepare.add(new AlgoHelperEntry(ghStorage.getGraph(CHGraph.class, pickedWeighting), AlgorithmOptions.start(dijkstrabiOpts).hints(chHints).build(), idx, "dijkstrabi|ch|prepare|" + hints.getWeighting()) {
@Override
public RoutingAlgorithmFactory createRoutingFactory() {
return hopper.getAlgorithmFactory(chHints);
}
});
prepare.add(new AlgoHelperEntry(ghStorage.getGraph(CHGraph.class, pickedWeighting), AlgorithmOptions.start(astarbiOpts).hints(chHints).build(), idx, "astarbi|ch|prepare|" + hints.getWeighting()) {
@Override
public RoutingAlgorithmFactory createRoutingFactory() {
return hopper.getAlgorithmFactory(chHints);
}
});
}
return prepare;
}
Aggregations