da.create(2000); LandmarkStorage lms = new LandmarkStorage(ghStorage, dir, new FastestWeighting(encoder), 4). setMaximumWeight(LandmarkStorage.PRECISION); lms.setWeight(0, 0, 16, Math.pow(2, 18), true); assertEquals((int) Math.pow(2, 18) - 2, lms.getFromWeight(0, 0)); lms.setWeight(0, 0, 16, 999999, true); assertEquals((int) Math.pow(2, 18) - 2, lms.getFromWeight(0, 0)); lms.setWeight(0, 0, 16, 999999, false); assertEquals((int) (Math.pow(2, 18) - 2 + Math.pow(2, 13) - 2), lms.getToWeight(0, 0)); lms.setWeight(0, 0, 16, 1, false); assertEquals((int) (Math.pow(2, 18) - 2 + -Math.pow(2, 13)), lms.getToWeight(0, 0)); assertTrue(lms.isInfinity(0)); lms.setWeight(0, 0, 16, 999999, true); assertFalse(lms.isInfinity(0));
@Test public void testInfinitWeight() { Directory dir = new RAMDirectory(); EdgeIteratorState edge = ghStorage.edge(0, 1); int res = new LandmarkStorage(ghStorage, dir, new FastestWeighting(encoder) { @Override public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { return Integer.MAX_VALUE * 2L; } }, 8).setMaximumWeight(LandmarkStorage.PRECISION).calcWeight(edge, false); assertEquals(Integer.MAX_VALUE, res); dir = new RAMDirectory(); res = new LandmarkStorage(ghStorage, dir, new FastestWeighting(encoder) { @Override public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { return Double.POSITIVE_INFINITY; } }, 8).setMaximumWeight(LandmarkStorage.PRECISION).calcWeight(edge, false); assertEquals(Integer.MAX_VALUE, res); }
@Override public void doSpecificWork() { StopWatch sw = new StopWatch().start(); LOGGER.info("Start calculating " + lms.getLandmarkCount() + " landmarks, default active lms:" + defaultActiveLandmarks + ", weighting:" + lms.getLmSelectionWeighting() + ", " + Helper.getMemInfo()); lms.createLandmarks(); lms.flush(); LOGGER.info("Calculated landmarks for " + (lms.getSubnetworksWithLandmarks() - 1) + " subnetworks, took:" + sw.stop().getSeconds() + " => " + lms.getLandmarksAsGeoJSON() + ", stored weights:" + lms.getLandmarkCount() + ", nodes:" + graph.getNodes() + ", " + Helper.getMemInfo()); }
int fromWeightInt = activeFromIntWeights[activeLMIdx] - (lms.getFromWeight(landmarkIndex, node) + virtEdgeWeightInt); int toWeightInt = lms.getToWeight(landmarkIndex, node) - activeToIntWeights[activeLMIdx]; if (reverse) { fromWeightInt = -fromWeightInt;
@Override public double approximate(final int queryNode) { if (!doALMRecalc && fallback || lms.isEmpty()) return fallBackApproximation.approximate(queryNode); boolean res = lms.initActiveLandmarks(node, to, activeLandmarks, activeFromIntWeights, activeToIntWeights, reverse); if (!res) {
LandmarkStorage store = new LandmarkStorage(graph, dir, weighting, lm); store.setMinimumNodes(2); store.createLandmarks(); int[] intList = store.getLandmarks(1); Arrays.sort(intList); assertEquals("[0, 14, 70, 182, 224]", Arrays.toString(intList)); assertEquals(2, store.getSubnetworksWithLandmarks()); assertEquals(0, store.getFromWeight(0, 224)); double factor = store.getFactor(); assertEquals(4671, Math.round(store.getFromWeight(0, 47) * factor)); assertEquals(3640, Math.round(store.getFromWeight(0, 52) * factor)); long weight1_224 = store.getFromWeight(1, 224); assertEquals(5525, Math.round(weight1_224 * factor)); long weight1_47 = store.getFromWeight(1, 47); assertEquals(921, Math.round(weight1_47 * factor)); assertEquals(weight1_224, store.getToWeight(1, 224)); assertEquals(weight1_47, store.getToWeight(1, 47)); int activeTos[] = new int[activeLM]; Arrays.fill(activeLandmarkIndices, -1); store.initActiveLandmarks(27, 47, activeLandmarkIndices, activeFroms, activeTos, false); List<Integer> list = new ArrayList<>(); for (int idx : activeLandmarkIndices) { list.add(store.getLandmarks(1)[idx]);
@Test public void testWeightingConsistence() { // create an indifferent problem: shortest weighting can pass the speed==0 edge but fastest cannot (?) ghStorage.edge(0, 1, 10, true); ghStorage.edge(1, 2).setDistance(10).setFlags(encoder.setProperties(0.9, true, true)); ghStorage.edge(2, 3, 10, true); LandmarkStorage storage = new LandmarkStorage(ghStorage, new RAMDirectory(), new FastestWeighting(encoder), 2); storage.setMinimumNodes(2); storage.createLandmarks(); assertEquals(2, storage.getSubnetworksWithLandmarks()); assertEquals("[1, 0]", Arrays.toString(storage.getLandmarks(1))); }
AbstractRoutingAlgorithmTester.initBiGraph(ghStorage); LandmarkStorage storage = new LandmarkStorage(ghStorage, new RAMDirectory(), new FastestWeighting(encoder), 2); final SpatialRule ruleRight = new DefaultSpatialRule() { @Override storage.setSpatialRuleLookup(lookup); storage.setMinimumNodes(2); storage.createLandmarks(); assertEquals(3, storage.getSubnetworksWithLandmarks());
@Test public void testDeltaWarning() { int distance = 1000000; ghStorage.edge(1, 2, distance, false); ghStorage.edge(2, 3, distance, false); ghStorage.edge(3, 4, distance, false); ghStorage.edge(4, 5, distance, false); ghStorage.edge(5, 6, distance, false); ghStorage.edge(6, 1, distance, false); ghStorage.edge(1, 7, distance, true); ghStorage.edge(7, 8, distance, true); ghStorage.edge(8, 9, distance, true); ghStorage.edge(6, 10, distance, true); ghStorage.edge(10, 11, distance, true); ghStorage.edge(11, 12, distance, true); LandmarkStorage storage = new LandmarkStorage(ghStorage, new RAMDirectory(), new FastestWeighting(encoder), 2); storage.createLandmarks(); assertEquals(12, storage.getLandmarks(1)[0]); assertEquals(9, storage.getLandmarks(1)[1]); assertEquals((int) Math.pow(2, 13) - 2, storage.getToWeight(0, 9) - storage.getFromWeight(0, 9)); assertEquals((int) -Math.pow(2, 13), storage.getToWeight(1, 12) - storage.getFromWeight(1, 12)); } }
this.lms = lms; this.factor = factor; if (activeCount > lms.getLandmarkCount()) throw new IllegalArgumentException("Active landmarks " + activeCount + " should be lower or equals to landmark count " + lms.getLandmarkCount()); this.fallBackApproximation = new BeelineWeightApproximator(graph.getNodeAccess(), lms.getWeighting()); this.maxBaseNodes = maxBaseNodes; int idxVirtNode = maxBaseNodes; int weight = lms.calcWeight(edge, reverse); int reverseWeight = lms.calcWeight(edge, !reverse); VirtEntry virtEntry = new VirtEntry(); if (weight < Integer.MAX_VALUE && (reverseWeight >= Integer.MAX_VALUE || weight < reverseWeight)) {
public RoutingAlgorithm getDecoratedAlgorithm(Graph qGraph, RoutingAlgorithm algo, AlgorithmOptions opts) { int activeLM = Math.max(1, opts.getHints().getInt(Landmark.ACTIVE_COUNT, defaultActiveLandmarks)); if (algo instanceof AStar) { if (!lms.isInitialized()) throw new IllegalStateException("Initialize landmark storage before creating algorithms"); astar.setApproximation(new LMApproximator(qGraph, this.graph.getNodes(), lms, activeLM, lms.getFactor(), false). setEpsilon(epsilon)); return algo; } else if (algo instanceof AStarBidirection) { if (!lms.isInitialized()) throw new IllegalStateException("Initialize landmark storage before creating algorithms"); astarbi.setApproximation(new LMApproximator(qGraph, this.graph.getNodes(), lms, activeLM, lms.getFactor(), false). setEpsilon(epsilon)); return algo; } else if (algo instanceof AlternativeRoute) { if (!lms.isInitialized()) throw new IllegalStateException("Initialize landmark storage before creating algorithms"); altRoute.setApproximation(new LMApproximator(qGraph, this.graph.getNodes(), lms, activeLM, lms.getFactor(), false). setEpsilon(epsilon));
/** * @see LandmarkStorage#setMinimumNodes(int) */ public void setMinimumNodes(int nodes) { if (nodes < 2) throw new IllegalArgumentException("minimum node count must be at least 2"); lms.setMinimumNodes(nodes); }
/** * @see LandmarkStorage#setMaximumWeight(double) */ public PrepareLandmarks setMaximumWeight(double maximumWeight) { lms.setMaximumWeight(maximumWeight); return this; }
public PrepareLandmarks(Directory dir, GraphHopperStorage graph, Weighting weighting, int landmarks, int activeLandmarks) { if (activeLandmarks > landmarks) throw new IllegalArgumentException("Default value for active landmarks " + activeLandmarks + " should be less or equal to landmark count of " + landmarks); this.graph = graph; this.defaultActiveLandmarks = activeLandmarks; this.weighting = weighting; lms = new LandmarkStorage(graph, dir, weighting, landmarks); }
public int getSubnetworksWithLandmarks() { return lms.getSubnetworksWithLandmarks(); }
int fromWeight = getFromWeight(lmIdx, nodeId); int delta; if (fromWeight == Integer.MAX_VALUE) {
@Test public void addWeighting() { LMAlgoFactoryDecorator dec = new LMAlgoFactoryDecorator().setEnabled(true); dec.addWeighting("fastest"); assertEquals(Arrays.asList("fastest"), dec.getWeightingsAsStrings()); // special parameters like the maximum weight dec = new LMAlgoFactoryDecorator().setEnabled(true); dec.addWeighting("fastest|maximum=65000"); dec.addWeighting("shortest|maximum=20000"); assertEquals(Arrays.asList("fastest", "shortest"), dec.getWeightingsAsStrings()); FlagEncoder car = new CarFlagEncoder(); EncodingManager em = new EncodingManager(car); dec.addWeighting(new FastestWeighting(car)).addWeighting(new ShortestWeighting(car)); dec.createPreparations(new GraphHopperStorage(new RAMDirectory(), em, false, new GraphExtension.NoOpExtension()), null); assertEquals(1, dec.getPreparations().get(0).getLandmarkStorage().getFactor(), .1); assertEquals(0.3, dec.getPreparations().get(1).getLandmarkStorage().getFactor(), .1); }
@Test public void testWithSubnetworks() { ghStorage.edge(0, 1, 10, true); ghStorage.edge(1, 2, 10, true); ghStorage.edge(2, 4).setFlags(encoder.setAccess(0, false, false)); ghStorage.edge(4, 5, 10, true); ghStorage.edge(5, 6, 10, false); LandmarkStorage storage = new LandmarkStorage(ghStorage, new RAMDirectory(), new FastestWeighting(encoder), 2); storage.setMinimumNodes(2); storage.createLandmarks(); assertEquals(3, storage.getSubnetworksWithLandmarks()); assertEquals("[2, 0]", Arrays.toString(storage.getLandmarks(1))); assertEquals("[6, 4]", Arrays.toString(storage.getLandmarks(2))); }
ghStorage.edge(14, 15, distance, true); LandmarkStorage storage = new LandmarkStorage(ghStorage, new RAMDirectory(), new FastestWeighting(encoder), 2); storage.createLandmarks(); assertEquals(15, storage.getLandmarks(1)[0]); assertEquals(9, storage.getLandmarks(1)[1]); assertEquals(35680, storage.getFromWeight(0, 1)); assertEquals(40777, storage.getToWeight(0, 1)); assertEquals(71361, storage.getFromWeight(0, 9)); assertEquals(66264, storage.getToWeight(0, 9)); assertEquals(15291, storage.getFromWeight(0, 12)); assertEquals(15291, storage.getToWeight(0, 12)); assertEquals(40777, storage.getFromWeight(1, 1)); assertEquals(35680, storage.getToWeight(1, 1)); assertEquals(50972, storage.getFromWeight(1, 12)); assertEquals(56069, storage.getToWeight(1, 12)); assertEquals(66264, storage.getFromWeight(1, 15)); assertEquals(71361, storage.getToWeight(1, 15));
int fromWeight = getFromWeight(lmIndex, toNode) - getFromWeight(lmIndex, fromNode); int toWeight = getToWeight(lmIndex, fromNode) - getToWeight(lmIndex, toNode); activeFroms[i] = getFromWeight(lmIndex, toNode); activeTos[i] = getToWeight(lmIndex, toNode);