Search in sources :

Example 1 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class RoutingAlgorithmIT method createAlgos.

public static List<AlgoHelperEntry> createAlgos(final GraphHopper hopper, final HintsMap hints, TraversalMode tMode) {
    GraphHopperStorage ghStorage = hopper.getGraphHopperStorage();
    LocationIndex idx = hopper.getLocationIndex();
    String addStr = "";
    if (tMode.isEdgeBased())
        addStr = "turn|";
    FlagEncoder encoder = hopper.getEncodingManager().getEncoder(hints.getVehicle());
    Weighting weighting = hopper.createWeighting(hints, encoder, hopper.getGraphHopperStorage());
    HintsMap defaultHints = new HintsMap().put(Parameters.CH.DISABLE, true).put(Parameters.Landmark.DISABLE, true).setVehicle(hints.getVehicle()).setWeighting(hints.getWeighting());
    AlgorithmOptions defaultOpts = AlgorithmOptions.start(new AlgorithmOptions("", weighting, tMode)).hints(defaultHints).build();
    List<AlgoHelperEntry> prepare = new ArrayList<>();
    prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(ASTAR).build(), idx, "astar|beeline|" + addStr + weighting));
    // later: include dijkstraOneToMany
    prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA).build(), idx, "dijkstra|" + addStr + weighting));
    AlgorithmOptions astarbiOpts = AlgorithmOptions.start(defaultOpts).algorithm(ASTAR_BI).build();
    astarbiOpts.getHints().put(ASTAR_BI + ".approximation", "BeelineSimplification");
    AlgorithmOptions dijkstrabiOpts = AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA_BI).build();
    prepare.add(new AlgoHelperEntry(ghStorage, astarbiOpts, idx, "astarbi|beeline|" + addStr + weighting));
    prepare.add(new AlgoHelperEntry(ghStorage, dijkstrabiOpts, idx, "dijkstrabi|" + addStr + weighting));
    // add additional preparations if CH and LM preparation are enabled
    if (hopper.getLMFactoryDecorator().isEnabled()) {
        final HintsMap lmHints = new HintsMap(defaultHints).put(Parameters.Landmark.DISABLE, false);
        prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(astarbiOpts).hints(lmHints).build(), idx, "astarbi|landmarks|" + weighting) {

            @Override
            public RoutingAlgorithmFactory createRoutingFactory() {
                return hopper.getAlgorithmFactory(lmHints);
            }
        });
    }
    if (hopper.getCHFactoryDecorator().isEnabled()) {
        final HintsMap chHints = new HintsMap(defaultHints).put(Parameters.CH.DISABLE, false);
        Weighting pickedWeighting = null;
        for (Weighting tmpWeighting : hopper.getCHFactoryDecorator().getWeightings()) {
            if (tmpWeighting.equals(weighting)) {
                pickedWeighting = tmpWeighting;
                break;
            }
        }
        if (pickedWeighting == null)
            throw new IllegalStateException("Didn't find weighting " + hints.getWeighting() + " in " + hopper.getCHFactoryDecorator().getWeightings());
        prepare.add(new AlgoHelperEntry(ghStorage.getGraph(CHGraph.class, pickedWeighting), AlgorithmOptions.start(dijkstrabiOpts).hints(chHints).build(), idx, "dijkstrabi|ch|prepare|" + hints.getWeighting()) {

            @Override
            public RoutingAlgorithmFactory createRoutingFactory() {
                return hopper.getAlgorithmFactory(chHints);
            }
        });
        prepare.add(new AlgoHelperEntry(ghStorage.getGraph(CHGraph.class, pickedWeighting), AlgorithmOptions.start(astarbiOpts).hints(chHints).build(), idx, "astarbi|ch|prepare|" + hints.getWeighting()) {

            @Override
            public RoutingAlgorithmFactory createRoutingFactory() {
                return hopper.getAlgorithmFactory(chHints);
            }
        });
    }
    return prepare;
}
Also used : HintsMap(com.graphhopper.routing.util.HintsMap) AlgoHelperEntry(com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) ArrayList(java.util.ArrayList) LocationIndex(com.graphhopper.storage.index.LocationIndex) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Weighting(com.graphhopper.routing.weighting.Weighting)

Example 2 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class ChangeGraphHelperTest method testApplyChanges.

@Test
public void testApplyChanges() {
    // 0-1-2
    // | |
    // 3-4
    graph.edge(0, 1, 1, true);
    graph.edge(1, 2, 1, true);
    graph.edge(3, 4, 1, true);
    graph.edge(0, 3, 1, true);
    graph.edge(1, 4, 1, true);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.01, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, 0.01, 0.02);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.00, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, 0.00, 0.01);
    LocationIndex locationIndex = new LocationIndexTree(graph, new RAMDirectory()).prepareIndex();
    FlagEncoder encoder = encodingManager.getEncoder("car");
    double defaultSpeed = encoder.getSpeed(GHUtility.getEdge(graph, 0, 1).getFlags());
    AllEdgesIterator iter = graph.getAllEdges();
    while (iter.next()) {
        long flags = GHUtility.getEdge(graph, 0, 1).getFlags();
        assertEquals(defaultSpeed, encoder.getSpeed(flags), .1);
        assertTrue(encoder.isForward(flags));
    }
    Reader reader = new InputStreamReader(getClass().getResourceAsStream("overlaydata1.json"), Helper.UTF_CS);
    ChangeGraphHelper instance = new ChangeGraphHelper(graph, locationIndex);
    JsonFeatureConverter converter = new JsonFeatureConverter(ghson, instance, encodingManager);
    long updates = converter.applyChanges(reader);
    assertEquals(2, updates);
    // assert changed speed and access
    double newSpeed = encoder.getSpeed(GHUtility.getEdge(graph, 0, 1).getFlags());
    assertEquals(10, newSpeed, .1);
    assertTrue(newSpeed < defaultSpeed);
    assertFalse(encoder.isForward(GHUtility.getEdge(graph, 3, 4).getFlags()));
}
Also used : AllEdgesIterator(com.graphhopper.routing.util.AllEdgesIterator) InputStreamReader(java.io.InputStreamReader) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) JsonFeatureConverter(com.graphhopper.json.JsonFeatureConverter) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) LocationIndex(com.graphhopper.storage.index.LocationIndex) RAMDirectory(com.graphhopper.storage.RAMDirectory) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Test(org.junit.Test)

Example 3 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworks method removeEdges.

/**
 * This method removes the access to edges available from the nodes contained in the components.
 * But only if a components' size is smaller then the specified min value.
 * <p>
 *
 * @return number of removed edges
 */
int removeEdges(final PrepEdgeFilter bothFilter, List<IntArrayList> components, int min) {
    // remove edges determined from nodes but only if less than minimum size
    FlagEncoder encoder = bothFilter.getEncoder();
    EdgeExplorer explorer = ghStorage.createEdgeExplorer(bothFilter);
    int removedEdges = 0;
    for (IntArrayList component : components) {
        removedEdges += removeEdges(explorer, encoder, component, min);
    }
    return removedEdges;
}
Also used : FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GHIntArrayList(com.graphhopper.coll.GHIntArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 4 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworks method findSubnetworks.

/**
 * This method finds the double linked components according to the specified filter.
 */
List<IntArrayList> findSubnetworks(PrepEdgeFilter filter) {
    final FlagEncoder encoder = filter.getEncoder();
    final EdgeExplorer explorer = ghStorage.createEdgeExplorer(filter);
    int locs = ghStorage.getNodes();
    List<IntArrayList> list = new ArrayList<IntArrayList>(100);
    final GHBitSet bs = new GHBitSetImpl(locs);
    for (int start = 0; start < locs; start++) {
        if (bs.contains(start))
            continue;
        final IntArrayList intList = new IntArrayList(20);
        list.add(intList);
        new BreadthFirstSearch() {

            int tmpCounter = 0;

            @Override
            protected GHBitSet createBitSet() {
                return bs;
            }

            @Override
            protected final boolean goFurther(int nodeId) {
                if (tmpCounter > maxEdgesPerNode.get())
                    maxEdgesPerNode.set(tmpCounter);
                tmpCounter = 0;
                intList.add(nodeId);
                return true;
            }

            @Override
            protected final boolean checkAdjacent(EdgeIteratorState edge) {
                if (encoder.isForward(edge.getFlags()) || encoder.isBackward(edge.getFlags())) {
                    tmpCounter++;
                    return true;
                }
                return false;
            }
        }.start(explorer, start);
        intList.trimToSize();
    }
    return list;
}
Also used : GHBitSetImpl(com.graphhopper.coll.GHBitSetImpl) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GHBitSet(com.graphhopper.coll.GHBitSet) GHIntArrayList(com.graphhopper.coll.GHIntArrayList) ArrayList(java.util.ArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList) GHIntArrayList(com.graphhopper.coll.GHIntArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 5 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworks method keepLargeNetworks.

/**
 * Deletes all but the largest subnetworks.
 */
int keepLargeNetworks(PrepEdgeFilter filter, List<IntArrayList> components) {
    if (components.size() <= 1)
        return 0;
    int maxCount = -1;
    IntIndexedContainer oldComponent = null;
    int allRemoved = 0;
    FlagEncoder encoder = filter.getEncoder();
    EdgeExplorer explorer = ghStorage.createEdgeExplorer(filter);
    for (IntArrayList component : components) {
        if (maxCount < 0) {
            maxCount = component.size();
            oldComponent = component;
            continue;
        }
        int removedEdges;
        if (maxCount < component.size()) {
            // new biggest area found. remove old
            removedEdges = removeEdges(explorer, encoder, oldComponent, minNetworkSize);
            maxCount = component.size();
            oldComponent = component;
        } else {
            removedEdges = removeEdges(explorer, encoder, component, minNetworkSize);
        }
        allRemoved += removedEdges;
    }
    if (allRemoved > ghStorage.getAllEdges().getMaxId() / 2)
        throw new IllegalStateException("Too many total edges were removed: " + allRemoved + ", all edges:" + ghStorage.getAllEdges().getMaxId());
    return allRemoved;
}
Also used : FlagEncoder(com.graphhopper.routing.util.FlagEncoder) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) GHIntArrayList(com.graphhopper.coll.GHIntArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Aggregations

FlagEncoder (com.graphhopper.routing.util.FlagEncoder)46 Test (org.junit.jupiter.api.Test)23 EncodingManager (com.graphhopper.routing.util.EncodingManager)19 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)14 GraphBuilder (com.graphhopper.storage.GraphBuilder)14 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)13 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)12 NodeAccess (com.graphhopper.storage.NodeAccess)10 Graph (com.graphhopper.storage.Graph)7 GHIntHashSet (com.graphhopper.coll.GHIntHashSet)6 Test (org.junit.Test)6 BikeFlagEncoder (com.graphhopper.routing.util.BikeFlagEncoder)5 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)5 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)4 IntArrayList (com.carrotsearch.hppc.IntArrayList)3 GHBitSetImpl (com.graphhopper.coll.GHBitSetImpl)3 GHIntArrayList (com.graphhopper.coll.GHIntArrayList)3 LMProfile (com.graphhopper.config.LMProfile)3 ReaderWay (com.graphhopper.reader.ReaderWay)3 AllEdgesIterator (com.graphhopper.routing.util.AllEdgesIterator)3