Search in sources :

Example 1 with MapEntry

use of com.graphhopper.coll.MapEntry in project graphhopper by graphhopper.

the class LandmarkStorage method initActiveLandmarks.

// From all available landmarks pick just a few active ones
boolean initActiveLandmarks(int fromNode, int toNode, int[] activeLandmarkIndices, int[] activeFroms, int[] activeTos, boolean reverse) {
    if (fromNode < 0 || toNode < 0)
        throw new IllegalStateException("from " + fromNode + " and to " + toNode + " nodes have to be 0 or positive to init landmarks");
    int subnetworkFrom = subnetworkStorage.getSubnetwork(fromNode);
    int subnetworkTo = subnetworkStorage.getSubnetwork(toNode);
    if (subnetworkFrom <= UNCLEAR_SUBNETWORK || subnetworkTo <= UNCLEAR_SUBNETWORK)
        return false;
    if (subnetworkFrom != subnetworkTo) {
        throw new ConnectionNotFoundException("Connection between locations not found. Different subnetworks " + subnetworkFrom + " vs. " + subnetworkTo, new HashMap<String, Object>());
    }
    int[] tmpIDs = landmarkIDs.get(subnetworkFrom);
    // kind of code duplication to approximate
    List<Map.Entry<Integer, Integer>> list = new ArrayList<>(tmpIDs.length);
    for (int lmIndex = 0; lmIndex < tmpIDs.length; lmIndex++) {
        int fromWeight = getFromWeight(lmIndex, toNode) - getFromWeight(lmIndex, fromNode);
        int toWeight = getToWeight(lmIndex, fromNode) - getToWeight(lmIndex, toNode);
        list.add(new MapEntry<>(reverse ? Math.max(-fromWeight, -toWeight) : Math.max(fromWeight, toWeight), lmIndex));
    }
    Collections.sort(list, SORT_BY_WEIGHT);
    if (activeLandmarkIndices[0] >= 0) {
        IntHashSet set = new IntHashSet(activeLandmarkIndices.length);
        set.addAll(activeLandmarkIndices);
        int existingLandmarkCounter = 0;
        final int COUNT = Math.min(activeLandmarkIndices.length - 2, 2);
        for (int i = 0; i < activeLandmarkIndices.length; i++) {
            if (i >= activeLandmarkIndices.length - COUNT + existingLandmarkCounter) {
                // keep at least two of the previous landmarks (pick the best)
                break;
            } else {
                activeLandmarkIndices[i] = list.get(i).getValue();
                if (set.contains(activeLandmarkIndices[i]))
                    existingLandmarkCounter++;
            }
        }
    } else {
        for (int i = 0; i < activeLandmarkIndices.length; i++) {
            activeLandmarkIndices[i] = list.get(i).getValue();
        }
    }
    // store weight values of active landmarks in 'cache' arrays
    for (int i = 0; i < activeLandmarkIndices.length; i++) {
        int lmIndex = activeLandmarkIndices[i];
        activeFroms[i] = getFromWeight(lmIndex, toNode);
        activeTos[i] = getToWeight(lmIndex, toNode);
    }
    return true;
}
Also used : ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException) MapEntry(com.graphhopper.coll.MapEntry) IntArrayList(com.carrotsearch.hppc.IntArrayList) IntHashSet(com.carrotsearch.hppc.IntHashSet) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 2 with MapEntry

use of com.graphhopper.coll.MapEntry in project graphhopper by graphhopper.

the class LandmarkStorage method chooseActiveLandmarks.

// From all available landmarks pick just a few active ones
boolean chooseActiveLandmarks(int fromNode, int toNode, int[] activeLandmarkIndices, boolean reverse) {
    if (fromNode < 0 || toNode < 0)
        throw new IllegalStateException("from " + fromNode + " and to " + toNode + " nodes have to be 0 or positive to init landmarks");
    int subnetworkFrom = subnetworkStorage.getSubnetwork(fromNode);
    int subnetworkTo = subnetworkStorage.getSubnetwork(toNode);
    if (subnetworkFrom <= UNCLEAR_SUBNETWORK || subnetworkTo <= UNCLEAR_SUBNETWORK)
        return false;
    if (subnetworkFrom != subnetworkTo) {
        throw new ConnectionNotFoundException("Connection between locations not found. Different subnetworks " + subnetworkFrom + " vs. " + subnetworkTo, new HashMap<>());
    }
    // See the similar formula in LMApproximator.approximateForLandmark
    List<Map.Entry<Integer, Integer>> list = new ArrayList<>(landmarks);
    for (int lmIndex = 0; lmIndex < landmarks; lmIndex++) {
        int fromWeight = getFromWeight(lmIndex, toNode) - getFromWeight(lmIndex, fromNode);
        int toWeight = getToWeight(lmIndex, fromNode) - getToWeight(lmIndex, toNode);
        list.add(new MapEntry<>(reverse ? Math.max(-fromWeight, -toWeight) : Math.max(fromWeight, toWeight), lmIndex));
    }
    Collections.sort(list, SORT_BY_WEIGHT);
    if (activeLandmarkIndices[0] >= 0) {
        IntHashSet set = new IntHashSet(activeLandmarkIndices.length);
        set.addAll(activeLandmarkIndices);
        int existingLandmarkCounter = 0;
        final int COUNT = Math.min(activeLandmarkIndices.length - 2, 2);
        for (int i = 0; i < activeLandmarkIndices.length; i++) {
            if (i >= activeLandmarkIndices.length - COUNT + existingLandmarkCounter) {
                // keep at least two of the previous landmarks (pick the best)
                break;
            } else {
                activeLandmarkIndices[i] = list.get(i).getValue();
                if (set.contains(activeLandmarkIndices[i]))
                    existingLandmarkCounter++;
            }
        }
    } else {
        for (int i = 0; i < activeLandmarkIndices.length; i++) {
            activeLandmarkIndices[i] = list.get(i).getValue();
        }
    }
    return true;
}
Also used : ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException) SPTEntry(com.graphhopper.routing.SPTEntry) MapEntry(com.graphhopper.coll.MapEntry) IntArrayList(com.carrotsearch.hppc.IntArrayList) IntHashSet(com.carrotsearch.hppc.IntHashSet) GHPoint(com.graphhopper.util.shapes.GHPoint)

Aggregations

IntArrayList (com.carrotsearch.hppc.IntArrayList)2 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 MapEntry (com.graphhopper.coll.MapEntry)2 ConnectionNotFoundException (com.graphhopper.util.exceptions.ConnectionNotFoundException)2 GHPoint (com.graphhopper.util.shapes.GHPoint)2 SPTEntry (com.graphhopper.routing.SPTEntry)1