Search in sources :

Example 16 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project graphhopper by graphhopper.

the class AlternativeRouteCH method calcAlternatives.

List<AlternativeInfo> calcAlternatives(final int s, final int t) {
    // First, do a regular bidirectional route search
    checkAlreadyRun();
    init(s, 0, t, 0);
    runAlgo();
    final Path bestPath = extractPath();
    if (!bestPath.isFound()) {
        return Collections.emptyList();
    }
    alternatives.add(new AlternativeInfo(bestPath, 0));
    final ArrayList<PotentialAlternativeInfo> potentialAlternativeInfos = new ArrayList<>();
    bestWeightMapFrom.forEach((IntObjectPredicate<SPTEntry>) (v, fromSPTEntry) -> {
        SPTEntry toSPTEntry = bestWeightMapTo.get(v);
        if (toSPTEntry == null)
            return true;
        if (fromSPTEntry.getWeightOfVisitedPath() + toSPTEntry.getWeightOfVisitedPath() > bestPath.getWeight() * maxWeightFactor)
            return true;
        // This gives us a path s -> v -> t, but since we are using contraction hierarchies,
        // s -> v and v -> t need not be shortest paths. In fact, they can sometimes be pretty strange.
        // We still use this preliminary path to filter for shared path length with other alternatives,
        // so we don't have to work so much.
        Path preliminaryRoute = createPathExtractor().extract(fromSPTEntry, toSPTEntry, fromSPTEntry.getWeightOfVisitedPath() + toSPTEntry.getWeightOfVisitedPath());
        double preliminaryShare = calculateShare(preliminaryRoute);
        if (preliminaryShare > maxShareFactor) {
            return true;
        }
        PotentialAlternativeInfo potentialAlternativeInfo = new PotentialAlternativeInfo();
        potentialAlternativeInfo.v = v;
        potentialAlternativeInfo.weight = 2 * (fromSPTEntry.getWeightOfVisitedPath() + toSPTEntry.getWeightOfVisitedPath()) + preliminaryShare;
        potentialAlternativeInfos.add(potentialAlternativeInfo);
        return true;
    });
    potentialAlternativeInfos.sort(Comparator.comparingDouble(o -> o.weight));
    for (PotentialAlternativeInfo potentialAlternativeInfo : potentialAlternativeInfos) {
        int v = potentialAlternativeInfo.v;
        // Okay, now we want the s -> v -> t shortest via-path, so we route s -> v and v -> t
        // and glue them together.
        DijkstraBidirectionCH svRouter = new DijkstraBidirectionCH(graph);
        final Path svPath = svRouter.calcPath(s, v);
        extraVisitedNodes += svRouter.getVisitedNodes();
        DijkstraBidirectionCH vtRouter = new DijkstraBidirectionCH(graph);
        final Path vtPath = vtRouter.calcPath(v, t);
        Path path = concat(graph.getBaseGraph(), svPath, vtPath);
        extraVisitedNodes += vtRouter.getVisitedNodes();
        double sharedDistanceWithShortest = sharedDistanceWithShortest(path);
        double detourLength = path.getDistance() - sharedDistanceWithShortest;
        double directLength = bestPath.getDistance() - sharedDistanceWithShortest;
        if (detourLength > directLength * maxWeightFactor) {
            continue;
        }
        double share = calculateShare(path);
        if (share > maxShareFactor) {
            continue;
        }
        // This is the final test we need: Discard paths that are not "locally shortest" around v.
        // So move a couple of nodes to the left and right from v on our path,
        // route, and check if v is on the shortest path.
        final IntIndexedContainer svNodes = svPath.calcNodes();
        int vIndex = svNodes.size() - 1;
        if (!tTest(path, vIndex))
            continue;
        alternatives.add(new AlternativeInfo(path, share));
        if (alternatives.size() >= maxPaths)
            break;
    }
    return alternatives;
}
Also used : RoutingCHGraph(com.graphhopper.storage.RoutingCHGraph) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) List(java.util.List) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) PMap(com.graphhopper.util.PMap) IntObjectPredicate(com.carrotsearch.hppc.predicates.IntObjectPredicate) Graph(com.graphhopper.storage.Graph) Comparator(java.util.Comparator) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer)

Example 17 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project graphhopper by graphhopper.

the class AlternativeRouteEdgeCH method tTest.

private boolean tTest(Path path, int vIndex) {
    if (path.getEdgeCount() == 0)
        return true;
    double detourDistance = detourDistance(path);
    double T = 0.5 * localOptimalityFactor * detourDistance;
    EdgeIteratorState fromNode = getPreviousNodeTMetersAway(path, vIndex, T);
    EdgeIteratorState toNode = getNextNodeTMetersAway(path, vIndex, T);
    DijkstraBidirectionEdgeCHNoSOD tRouter = new DijkstraBidirectionEdgeCHNoSOD(graph);
    Path tPath = tRouter.calcPath(fromNode.getBaseNode(), toNode.getAdjNode(), fromNode.getEdge(), toNode.getEdge());
    extraVisitedNodes += tRouter.getVisitedNodes();
    IntIndexedContainer tNodes = tPath.calcNodes();
    int v = path.calcNodes().get(vIndex);
    return tNodes.contains(v);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer)

Example 18 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project graphhopper by graphhopper.

the class AbstractRoutingAlgorithmTester method testRekeyBugOfIntBinHeap.

@Test
public void testRekeyBugOfIntBinHeap() {
    // using Dijkstra + IntBinHeap then rekey loops endlessly
    GraphHopperStorage matrixGraph = createMatrixGraph();
    Path p = createAlgo(matrixGraph).calcPath(36, 91);
    assertEquals(12, p.calcNodes().size());
    IntIndexedContainer list = p.calcNodes();
    if (!Helper.createTList(36, 46, 56, 66, 76, 86, 85, 84, 94, 93, 92, 91).equals(list) && !Helper.createTList(36, 46, 56, 66, 76, 86, 85, 84, 83, 82, 92, 91).equals(list)) {
        assertTrue("wrong locations: " + list.toString(), false);
    }
    assertEquals(66f, p.getDistance(), 1e-3);
    testBug1(matrixGraph);
    testCorrectWeight(matrixGraph);
}
Also used : IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) Test(org.junit.Test)

Example 19 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project graphhopper by graphhopper.

the class MiniGraphUI method plotPath.

private Path plotPath(Path tmpPath, Graphics2D g2, int w) {
    if (!tmpPath.isFound()) {
        logger.info("nothing found " + w);
        return tmpPath;
    }
    double prevLat = Double.NaN;
    double prevLon = Double.NaN;
    boolean plotNodes = false;
    IntIndexedContainer nodes = tmpPath.calcNodes();
    if (plotNodes) {
        for (int i = 0; i < nodes.size(); i++) {
            plotNodeName(g2, nodes.get(i));
        }
    }
    PointList list = tmpPath.calcPoints();
    for (int i = 0; i < list.size(); i++) {
        double lat = list.getLat(i);
        double lon = list.getLon(i);
        if (!Double.isNaN(prevLat)) {
            mg.plotEdge(g2, prevLat, prevLon, lat, lon, w);
        } else {
            mg.plot(g2, lat, lon, w);
        }
        prevLat = lat;
        prevLon = lon;
    }
    logger.info("dist:" + tmpPath.getDistance() + ", path points(" + list.size() + ")");
    return tmpPath;
}
Also used : PointList(com.graphhopper.util.PointList) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer)

Example 20 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project graphhopper by graphhopper.

the class AlternativeRouteEdgeCH method calcAlternatives.

List<AlternativeInfo> calcAlternatives(final int s, final int t) {
    // First, do a regular bidirectional route search
    checkAlreadyRun();
    init(s, 0, t, 0);
    runAlgo();
    final Path bestPath = extractPath();
    if (!bestPath.isFound()) {
        return Collections.emptyList();
    }
    alternatives.add(new AlternativeInfo(bestPath, 0));
    final ArrayList<PotentialAlternativeInfo> potentialAlternativeInfos = new ArrayList<>();
    final Map<Integer, SPTEntry> bestWeightMapByNode = new HashMap<>();
    bestWeightMapTo.forEach((IntObjectPredicate<SPTEntry>) (key, value) -> {
        bestWeightMapByNode.put(value.adjNode, value);
        return true;
    });
    bestWeightMapFrom.forEach((IntObjectPredicate<SPTEntry>) (wurst, fromSPTEntry) -> {
        SPTEntry toSPTEntry = bestWeightMapByNode.get(fromSPTEntry.adjNode);
        if (toSPTEntry == null)
            return true;
        if (fromSPTEntry.getWeightOfVisitedPath() + toSPTEntry.getWeightOfVisitedPath() > bestPath.getWeight() * maxWeightFactor)
            return true;
        // This gives us a path s -> v -> t, but since we are using contraction hierarchies,
        // s -> v and v -> t need not be shortest paths. In fact, they can sometimes be pretty strange.
        // We still use this preliminary path to filter for shared path length with other alternatives,
        // so we don't have to work so much.
        Path preliminaryRoute = createPathExtractor().extract(fromSPTEntry, toSPTEntry, fromSPTEntry.getWeightOfVisitedPath() + toSPTEntry.getWeightOfVisitedPath());
        double preliminaryShare = calculateShare(preliminaryRoute);
        if (preliminaryShare > maxShareFactor) {
            return true;
        }
        assert fromSPTEntry.adjNode == toSPTEntry.adjNode;
        PotentialAlternativeInfo potentialAlternativeInfo = new PotentialAlternativeInfo();
        potentialAlternativeInfo.v = fromSPTEntry.adjNode;
        potentialAlternativeInfo.edgeIn = getIncomingEdge(fromSPTEntry);
        potentialAlternativeInfo.weight = 2 * (fromSPTEntry.getWeightOfVisitedPath() + toSPTEntry.getWeightOfVisitedPath()) + preliminaryShare;
        potentialAlternativeInfos.add(potentialAlternativeInfo);
        return true;
    });
    potentialAlternativeInfos.sort(Comparator.comparingDouble(o -> o.weight));
    for (PotentialAlternativeInfo potentialAlternativeInfo : potentialAlternativeInfos) {
        int v = potentialAlternativeInfo.v;
        int tailSv = potentialAlternativeInfo.edgeIn;
        // Okay, now we want the s -> v -> t shortest via-path, so we route s -> v and v -> t
        // and glue them together.
        DijkstraBidirectionEdgeCHNoSOD svRouter = new DijkstraBidirectionEdgeCHNoSOD(graph);
        final Path suvPath = svRouter.calcPath(s, v, ANY_EDGE, tailSv);
        extraVisitedNodes += svRouter.getVisitedNodes();
        int u = graph.getBaseGraph().getEdgeIteratorState(tailSv, v).getBaseNode();
        DijkstraBidirectionEdgeCHNoSOD vtRouter = new DijkstraBidirectionEdgeCHNoSOD(graph);
        final Path uvtPath = vtRouter.calcPath(u, t, tailSv, ANY_EDGE);
        Path path = concat(graph.getBaseGraph(), suvPath, uvtPath);
        extraVisitedNodes += vtRouter.getVisitedNodes();
        double sharedDistanceWithShortest = sharedDistanceWithShortest(path);
        double detourLength = path.getDistance() - sharedDistanceWithShortest;
        double directLength = bestPath.getDistance() - sharedDistanceWithShortest;
        if (detourLength > directLength * maxWeightFactor) {
            continue;
        }
        double share = calculateShare(path);
        if (share > maxShareFactor) {
            continue;
        }
        // This is the final test we need: Discard paths that are not "locally shortest" around v.
        // So move a couple of nodes to the left and right from v on our path,
        // route, and check if v is on the shortest path.
        final IntIndexedContainer svNodes = suvPath.calcNodes();
        int vIndex = svNodes.size() - 1;
        if (!tTest(path, vIndex))
            continue;
        alternatives.add(new AlternativeInfo(path, share));
        if (alternatives.size() >= maxPaths)
            break;
    }
    return alternatives;
}
Also used : RoutingCHGraph(com.graphhopper.storage.RoutingCHGraph) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) java.util(java.util) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) PMap(com.graphhopper.util.PMap) IntObjectPredicate(com.carrotsearch.hppc.predicates.IntObjectPredicate) Graph(com.graphhopper.storage.Graph) ANY_EDGE(com.graphhopper.util.EdgeIterator.ANY_EDGE) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer)

Aggregations

IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)30 Map (java.util.Map)17 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)10 ArrayList (java.util.ArrayList)10 TreeMap (java.util.TreeMap)7 Index (org.elasticsearch.index.Index)7 IntArrayList (com.carrotsearch.hppc.IntArrayList)6 RelationName (io.crate.metadata.RelationName)6 List (java.util.List)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)6 Metadata (org.elasticsearch.cluster.metadata.Metadata)6 ShardId (org.elasticsearch.index.shard.ShardId)6 Test (org.junit.Test)6 Routing (io.crate.metadata.Routing)5 ClusterService (org.elasticsearch.cluster.service.ClusterService)5 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)5 ShardCollectorProvider (io.crate.execution.engine.collect.ShardCollectorProvider)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 IllegalIndexShardStateException (org.elasticsearch.index.shard.IllegalIndexShardStateException)4