Search in sources :

Example 1 with BitSetIterator

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

the class PrepareRoutingSubnetworks method setSubnetworks.

private int setSubnetworks(Weighting weighting, BooleanEncodedValue subnetworkEnc) {
    // partition graph into strongly connected components using Tarjan's algorithm
    StopWatch sw = new StopWatch().start();
    EdgeBasedTarjanSCC.ConnectedComponents ccs = EdgeBasedTarjanSCC.findComponents(ghStorage, (prev, edge) -> Double.isFinite(GHUtility.calcWeightWithTurnWeightWithAccess(weighting, edge, false, prev)), false);
    List<IntArrayList> components = ccs.getComponents();
    BitSet singleEdgeComponents = ccs.getSingleEdgeComponents();
    long numSingleEdgeComponents = singleEdgeComponents.cardinality();
    logger.info(subnetworkEnc.getName().replaceAll("_subnetwork", "") + " - Found " + ccs.getTotalComponents() + " subnetworks (" + numSingleEdgeComponents + " single edges and " + components.size() + " components with more than one edge, total nodes: " + ccs.getEdgeKeys() + "), took: " + sw.stop().getSeconds() + "s");
    final int minNetworkSizeEdgeKeys = 2 * minNetworkSize;
    // make all small components subnetworks, but keep the biggest (even when its smaller than the given min_network_size)
    sw = new StopWatch().start();
    int subnetworks = 0;
    int markedEdges = 0;
    int smallestNonSubnetwork = ccs.getBiggestComponent().size();
    int biggestSubnetwork = 0;
    for (IntArrayList component : components) {
        if (component == ccs.getBiggestComponent())
            continue;
        if (component.size() < minNetworkSizeEdgeKeys) {
            for (IntCursor cursor : component) markedEdges += setSubnetworkEdge(cursor.value, weighting, subnetworkEnc);
            subnetworks++;
            biggestSubnetwork = Math.max(biggestSubnetwork, component.size());
        } else {
            smallestNonSubnetwork = Math.min(smallestNonSubnetwork, component.size());
        }
    }
    if (minNetworkSizeEdgeKeys > 0) {
        BitSetIterator iter = singleEdgeComponents.iterator();
        for (int edgeKey = iter.nextSetBit(); edgeKey >= 0; edgeKey = iter.nextSetBit()) {
            markedEdges += setSubnetworkEdge(edgeKey, weighting, subnetworkEnc);
            subnetworks++;
            biggestSubnetwork = Math.max(biggestSubnetwork, 1);
        }
    } else if (numSingleEdgeComponents > 0) {
        smallestNonSubnetwork = Math.min(smallestNonSubnetwork, 1);
    }
    int allowedMarked = ghStorage.getEdges() / 2;
    if (markedEdges / 2 > allowedMarked)
        throw new IllegalStateException("Too many total (directed) edges were marked as subnetwork edges: " + markedEdges + " out of " + (2 * ghStorage.getEdges()) + "\n" + "The maximum number of subnetwork edges is: " + (2 * allowedMarked));
    logger.info(subnetworkEnc.getName().replaceAll("_subnetwork", "") + " - Marked " + subnetworks + " subnetworks (biggest: " + biggestSubnetwork + " edges) -> " + (ccs.getTotalComponents() - subnetworks) + " components(s) remain (smallest: " + smallestNonSubnetwork + ", biggest: " + ccs.getBiggestComponent().size() + " edges)" + ", total marked edges: " + markedEdges + ", took: " + sw.stop().getSeconds() + "s");
    return markedEdges;
}
Also used : BitSetIterator(com.carrotsearch.hppc.BitSetIterator) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) BitSet(com.carrotsearch.hppc.BitSet) IntArrayList(com.carrotsearch.hppc.IntArrayList) StopWatch(com.graphhopper.util.StopWatch)

Aggregations

BitSet (com.carrotsearch.hppc.BitSet)1 BitSetIterator (com.carrotsearch.hppc.BitSetIterator)1 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)1 StopWatch (com.graphhopper.util.StopWatch)1