public static String getNodeInfo(CHGraph g, int nodeId, EdgeFilter filter) { CHEdgeExplorer ex = g.createEdgeExplorer(filter); CHEdgeIterator iter = ex.setBaseNode(nodeId); NodeAccess na = g.getNodeAccess(); String str = nodeId + ":" + na.getLatitude(nodeId) + "," + na.getLongitude(nodeId) + "\n"; while (iter.next()) { str += " ->" + iter.getAdjNode() + "(" + iter.getSkippedEdge1() + "," + iter.getSkippedEdge2() + ") " + iter.getEdge() + " \t" + BitUtil.BIG.toBitString(iter.getFlags(), 8) + "\n"; } return str; }
boolean isContracted(int node) { return prepareGraph.getLevel(node) != maxLevel; }
@Override public void initFromGraph() { inEdgeExplorer = prepareGraph.createEdgeExplorer(DefaultEdgeFilter.inEdges(encoder)); outEdgeExplorer = prepareGraph.createEdgeExplorer(DefaultEdgeFilter.outEdges(encoder)); maxLevel = prepareGraph.getNodes(); maxEdgesCount = prepareGraph.getOriginalEdges(); }
@Override public void prepareContraction() { // todo: initializing meanDegree here instead of in initFromGraph() means that in the first round of calculating // node priorities all shortcut searches are cancelled immediately and all possible shortcuts are counted because // no witness path can be found. this is not really what we want, but changing it requires re-optimizing the // graph contraction parameters, because it affects the node contraction order. // when this is done there should be no need for this method any longer. meanDegree = prepareGraph.getEdges() / prepareGraph.getNodes(); }
assertEquals(Helper.createPointList(-1, 3, 12, 10), edgeState.fetchWayGeometry(0)); assertEquals(0, lg.getLevel(0)); assertEquals(0, lg.getLevel(1)); NodeAccess na = lg.getNodeAccess(); assertEquals(0, na.getLatitude(0), 1e-6); assertEquals(1, na.getLongitude(0), 1e-6); assertEquals(2.5, na.getLatitude(1), 1e-6); assertEquals(4.5, na.getLongitude(1), 1e-6); assertEquals(9, lg.getNodes()); EdgeIterator iter = lg.createEdgeExplorer().setBaseNode(8); iter.next(); assertEquals(2.05, iter.getDistance(), 1e-6); assertTrue(iter.isForward(carEncoder)); iter = lg.createEdgeExplorer().setBaseNode(7); iter.next(); assertEquals(.7, iter.getDistance(), 1e-6);
assertEquals(1, GHUtility.count(chGraph.createEdgeExplorer().setBaseNode(1))); chGraph.shortcut(2, 3); assertEquals(1, GHUtility.count(chGraph.createEdgeExplorer().setBaseNode(1))); assertEquals(1, GHUtility.count(chGraph.createEdgeExplorer().setBaseNode(2))); assertEquals(10, chGraph.getNodes()); assertEquals(2, graph.getEdges()); assertEquals(3, chGraph.getEdges()); assertEquals(1, GHUtility.count(chGraph.createEdgeExplorer().setBaseNode(2))); AllCHEdgesIterator iter = chGraph.getAllEdges(); assertTrue(iter.next()); assertFalse(iter.isShortcut());
private void initFromGraph() { FlagEncoder prepareFlagEncoder = prepareWeighting.getFlagEncoder(); final EdgeFilter allFilter = DefaultEdgeFilter.allEdges(prepareFlagEncoder); maxLevel = prepareGraph.getNodes(); vehicleAllExplorer = prepareGraph.createEdgeExplorer(allFilter); vehicleAllTmpExplorer = prepareGraph.createEdgeExplorer(allFilter); // Use an alternative to PriorityQueue as it has some advantages: // 1. Gets automatically smaller if less entries are stored => less total RAM used. // Important because Graph is increasing until the end. // 2. is slightly faster // but we need the additional oldPriorities array to keep the old value which is necessary for the update method sortedNodes = new GHTreeMapComposed(); oldPriorities = new float[prepareGraph.getNodes()]; nodeContractor = new NodeBasedNodeContractor(prepareGraph, weighting, pMap); nodeContractor.initFromGraph(); }
periodicUpdateSW.start(); sortedNodes.clear(); for (int node = 0; node < prepareGraph.getNodes(); node++) { if (prepareGraph.getLevel(node) != maxLevel) continue; prepareGraph.setLevel(polledNode, level); level++; contractionSW.stop(); if (prepareGraph.getLevel(nn) != maxLevel) continue; prepareGraph.disconnect(vehicleAllTmpExplorer, iter);
@Test public void testQueryGraph() { graph = createGHStorage(); CHGraph chGraph = getGraph(graph); NodeAccess na = chGraph.getNodeAccess(); na.setNode(0, 1.00, 1.00); na.setNode(1, 1.02, 1.00); na.setNode(2, 1.04, 1.00); EdgeIteratorState edge1 = chGraph.edge(0, 1); chGraph.edge(1, 2); graph.freeze(); chGraph.shortcut(0, 1); QueryGraph qGraph = new QueryGraph(chGraph); QueryResult fromRes = createQR(1.004, 1.01, 0, edge1); QueryResult toRes = createQR(1.019, 1.00, 0, edge1); qGraph.lookup(fromRes, toRes); Graph baseGraph = qGraph.getBaseGraph(); EdgeExplorer explorer = baseGraph.createEdgeExplorer(); assertTrue(chGraph.getNodes() < qGraph.getNodes()); assertEquals(baseGraph.getNodes(), qGraph.getNodes()); // traverse virtual edges and normal edges but no shortcuts! assertEquals(GHUtility.asSet(fromRes.getClosestNode()), GHUtility.getNeighbors(explorer.setBaseNode(0))); assertEquals(GHUtility.asSet(toRes.getClosestNode(), 2), GHUtility.getNeighbors(explorer.setBaseNode(1))); // get neighbors from virtual nodes assertEquals(GHUtility.asSet(0, toRes.getClosestNode()), GHUtility.getNeighbors(explorer.setBaseNode(fromRes.getClosestNode()))); assertEquals(GHUtility.asSet(1, fromRes.getClosestNode()), GHUtility.getNeighbors(explorer.setBaseNode(toRes.getClosestNode()))); }
@Test public void testEdgeFilter() { graph = createGHStorage(); CHGraph g = getGraph(graph); g.edge(0, 1, 10, true); g.edge(0, 2, 20, true); g.edge(2, 3, 30, true); g.edge(10, 11, 1, true); graph.freeze(); CHEdgeIteratorState tmpIter = g.shortcut(3, 4); tmpIter.setDistance(40).setFlags(carEncoder.setAccess(0, true, true)); assertEquals(EdgeIterator.NO_EDGE, tmpIter.getSkippedEdge1()); assertEquals(EdgeIterator.NO_EDGE, tmpIter.getSkippedEdge2()); g.shortcut(0, 4).setDistance(40).setFlags(carEncoder.setAccess(0, true, true)); g.setLevel(0, 1); g.setLevel(4, 1); EdgeIterator iter = g.createEdgeExplorer(new LevelEdgeFilter(g)).setBaseNode(0); assertEquals(1, GHUtility.count(iter)); iter = g.createEdgeExplorer().setBaseNode(2); assertEquals(2, GHUtility.count(iter)); tmpIter = g.shortcut(5, 6); tmpIter.setSkippedEdges(1, 2); assertEquals(1, tmpIter.getSkippedEdge1()); assertEquals(2, tmpIter.getSkippedEdge2()); }
GraphHopperStorage ghStorage = new GraphBuilder(encodingManager).setCHGraph(weighting).create(); CHGraph g = ghStorage.getGraph(CHGraph.class, weighting); g.edge(0, 1).setDistance(12).setFlags(carFlagEncoder.setProperties(10, true, true)); g.edge(0, 2).setDistance(13).setFlags(carFlagEncoder.setProperties(20, true, true)); ghStorage.freeze(); assertEquals(2, GHUtility.count(g.getAllEdges())); assertEquals(1, GHUtility.count(g.createEdgeExplorer(carOutFilter).setBaseNode(1))); EdgeIteratorState iter = GHUtility.getEdge(g, 0, 1); assertEquals(1, iter.getAdjNode()); assertEquals(0, GHUtility.count(g.createEdgeExplorer(carOutFilter).setBaseNode(1))); iter = GHUtility.getEdge(g, 0, 1); assertEquals(carFlagEncoder.setProperties(20, true, false), iter.getFlags()); assertEquals(10, iter.getDistance(), 1e-4); assertEquals(1, GHUtility.getNeighbors(g.createEdgeExplorer().setBaseNode(1)).size()); assertEquals(0, GHUtility.getNeighbors(g.createEdgeExplorer(carOutFilter).setBaseNode(1)).size());
@Override public void initFromGraph() { super.initFromGraph(); ignoreNodeFilter = new IgnoreNodeFilter(prepareGraph, maxLevel); final EdgeFilter allFilter = DefaultEdgeFilter.allEdges(encoder); final EdgeFilter remainingNodesFilter = new LevelEdgeFilter(prepareGraph) { @Override public final boolean accept(EdgeIteratorState edgeState) { return super.accept(edgeState) && allFilter.accept(edgeState); } }; remainingEdgeExplorer = prepareGraph.createEdgeExplorer(remainingNodesFilter); prepareAlgo = new DijkstraOneToMany(prepareGraph, prepareWeighting, TraversalMode.NODE_BASED); }
@Override public void doSpecificWork() { allSW.start(); initFromGraph(); runGraphContraction(); logger.info("took:" + (int) allSW.stop().getSeconds() + "s " + ", new shortcuts: " + nf(nodeContractor.getAddedShortcutsCount()) + ", initSize:" + nf(initSize) + ", " + prepareWeighting + ", periodic:" + params.getPeriodicUpdatesPercentage() + ", lazy:" + params.getLastNodesLazyUpdatePercentage() + ", neighbor:" + params.getNeighborUpdatePercentage() + ", " + getTimesAsString() + ", lazy-overhead: " + (int) (100 * ((checkCounter / (double) initSize) - 1)) + "%" + ", " + Helper.getMemInfo()); int edgeCount = prepareGraph.getOriginalEdges(); logger.info("graph now - num edges: {}, num nodes: {}, num shortcuts: {}", nf(edgeCount), nf(prepareGraph.getNodes()), nf(prepareGraph.getEdges() - edgeCount)); }
public LevelEdgeFilter(CHGraph g) { graph = g; maxNodes = g.getNodes(); }
void setOrigEdgeCount(int edgeId, int value) { edgeId -= maxEdgesCount; if (edgeId < 0) { // ignore setting as every normal edge has original edge count of 1 if (value != 1) throw new IllegalStateException("Trying to set original edge count for normal edge to a value = " + value + ", edge:" + (edgeId + maxEdgesCount) + ", max:" + maxEdgesCount + ", graph.max:" + prepareGraph.getEdges()); return; } long tmp = (long) edgeId * 4; originalEdges.ensureCapacity(tmp + 4); originalEdges.setInt(tmp, value); }
@Test public void testSimpleShortcutCreationAndTraversal() { graph = createGHStorage(); graph.edge(1, 3, 10, true); graph.edge(3, 4, 10, true); graph.freeze(); CHGraph lg = graph.getGraph(CHGraph.class); lg.shortcut(1, 4).setWeight(3).setFlags(carEncoder.setProperties(10, true, true)); EdgeExplorer vehicleOutExplorer = lg.createEdgeExplorer(DefaultEdgeFilter.outEdges(carEncoder)); // iteration should result in same nodes even if reusing the iterator assertEquals(GHUtility.asSet(3, 4), GHUtility.getNeighbors(vehicleOutExplorer.setBaseNode(1))); assertEquals(GHUtility.asSet(3, 4), GHUtility.getNeighbors(vehicleOutExplorer.setBaseNode(1))); }
@Test public void testAddShortcutSkippedEdgesWriteRead_writeWithCHEdgeIterator() { graph = createGHStorage(); final EdgeIteratorState edge1 = graph.edge(1, 3, 10, true); final EdgeIteratorState edge2 = graph.edge(3, 4, 10, true); graph.freeze(); CHGraph lg = graph.getGraph(CHGraph.class); CHEdgeIteratorState shortcut = lg.shortcut(1, 4); shortcut.setSkippedEdges(edge1.getEdge(), edge2.getEdge()); AllCHEdgesIterator iter = lg.getAllEdges(); iter.next(); iter.next(); iter.next(); assertTrue(iter.isShortcut()); assertEquals(edge1.getEdge(), iter.getSkippedEdge1()); assertEquals(edge2.getEdge(), iter.getSkippedEdge2()); }
@Override public void prepareContraction() { // todo: initializing meanDegree here instead of in initFromGraph() means that in the first round of calculating // node priorities all shortcut searches are cancelled immediately and all possible shortcuts are counted because // no witness path can be found. this is not really what we want, but changing it requires re-optimizing the // graph contraction parameters, because it affects the node contraction order. // when this is done there should be no need for this method any longer. meanDegree = prepareGraph.getAllEdges().length() / prepareGraph.getNodes(); }