use of com.graphhopper.routing.util.EncodingManager in project massim by agentcontest.
the class GraphHopperManager method init.
/**
* Creates a new GraphHopper for the given map name.
* @param newMapName the name of the map to load
*/
public static void init(String newMapName) {
if (newMapName.equals(mapName))
return;
mapName = newMapName;
hopper = new GraphHopper().forDesktop();
hopper.setOSMFile("osm" + File.separator + mapName + ".osm.pbf");
// CH does not work with shortest weighting (at the moment)
hopper.setCHEnabled(false);
// where to store GH files?
hopper.setGraphHopperLocation("graphs" + File.separator + mapName);
hopper.setEncodingManager(new EncodingManager("car"));
// this may take a few minutes
hopper.importOrLoad();
}
use of com.graphhopper.routing.util.EncodingManager 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.routing.util.EncodingManager 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.routing.util.EncodingManager in project graphhopper by graphhopper.
the class NodeBasedNodeContractorTest method testNodeContraction_preventUnnecessaryShortcutWithLoop.
@Test
public void testNodeContraction_preventUnnecessaryShortcutWithLoop() {
// there should not be shortcuts where one of the skipped edges is a loop at the node to be contracted,
// see also #1583
CarFlagEncoder encoder = new CarFlagEncoder();
EncodingManager encodingManager = EncodingManager.create(encoder);
GraphHopperStorage graph = new GraphBuilder(encodingManager).create();
// 0 - 1 - 2 - 3
// o o
GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 3).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 0).setDistance(1));
GHUtility.setSpeed(60, true, true, encoder, graph.edge(3, 3).setDistance(1));
graph.freeze();
Weighting weighting = new FastestWeighting(encoder);
CHConfig chConfig = CHConfig.nodeBased("p1", weighting);
CHStorage chStore = graph.createCHStorage(chConfig);
setMaxLevelOnAllNodes(chStore);
NodeContractor nodeContractor = createNodeContractor(graph, chStore, chConfig);
nodeContractor.contractNode(0);
nodeContractor.contractNode(3);
checkNoShortcuts(chStore);
}
use of com.graphhopper.routing.util.EncodingManager in project graphhopper by graphhopper.
the class PrepareRoutingSubnetworksTest method testPrepareSubnetwork_withTurnCosts.
@Test
public void testPrepareSubnetwork_withTurnCosts() {
EncodingManager em = createEncodingManager("car|turn_costs=true");
FlagEncoder encoder = em.fetchEdgeEncoders().iterator().next();
// since the middle edge is blocked the upper component is a subnetwork (regardless of turn costs)
GraphHopperStorage g = createSubnetworkTestStorage(em);
PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, new DefaultTurnCostProvider(encoder, g.getTurnCostStorage(), 0))));
instance.setMinNetworkSize(4);
assertEquals(3, instance.doWork());
assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, encoder));
// if we open the edge it won't be a subnetwork anymore
g = createSubnetworkTestStorage(em);
EdgeIteratorState edge = GHUtility.getEdge(g, 3, 4);
GHUtility.setSpeed(10, true, true, encoder, edge);
instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, new DefaultTurnCostProvider(encoder, g.getTurnCostStorage(), 0))));
instance.setMinNetworkSize(4);
assertEquals(0, instance.doWork());
assertEquals(IntArrayList.from(), getSubnetworkEdges(g, encoder));
// ... and now for something interesting: if we open the edge *and* apply turn restrictions it will be a
// subnetwork again
g = createSubnetworkTestStorage(em);
edge = GHUtility.getEdge(g, 3, 4);
GHUtility.setSpeed(10, true, true, encoder, edge);
DecimalEncodedValue turnCostEnc = em.getDecimalEncodedValue(TurnCost.key(encoder.toString()));
g.getTurnCostStorage().set(turnCostEnc, 0, 4, 7, 1);
g.getTurnCostStorage().set(turnCostEnc, 0, 4, 9, 1);
instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, new DefaultTurnCostProvider(encoder, g.getTurnCostStorage(), 0))));
instance.setMinNetworkSize(4);
assertEquals(3, instance.doWork());
assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, encoder));
}
Aggregations