Search in sources :

Example 11 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworksTest method testPrepareSubnetwork_withTurnCosts.

@Test
public void testPrepareSubnetwork_withTurnCosts() {
    EncodingManager em = createEncodingManager("car|turn_costs=true");
    FlagEncoder encoder = em.fetchEdgeEncoders().iterator().next();
    // since the middle edge is blocked the upper component is a subnetwork (regardless of turn costs)
    GraphHopperStorage g = createSubnetworkTestStorage(em);
    PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, new DefaultTurnCostProvider(encoder, g.getTurnCostStorage(), 0))));
    instance.setMinNetworkSize(4);
    assertEquals(3, instance.doWork());
    assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, encoder));
    // if we open the edge it won't be a subnetwork anymore
    g = createSubnetworkTestStorage(em);
    EdgeIteratorState edge = GHUtility.getEdge(g, 3, 4);
    GHUtility.setSpeed(10, true, true, encoder, edge);
    instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, new DefaultTurnCostProvider(encoder, g.getTurnCostStorage(), 0))));
    instance.setMinNetworkSize(4);
    assertEquals(0, instance.doWork());
    assertEquals(IntArrayList.from(), getSubnetworkEdges(g, encoder));
    // ... and now for something interesting: if we open the edge *and* apply turn restrictions it will be a
    // subnetwork again
    g = createSubnetworkTestStorage(em);
    edge = GHUtility.getEdge(g, 3, 4);
    GHUtility.setSpeed(10, true, true, encoder, edge);
    DecimalEncodedValue turnCostEnc = em.getDecimalEncodedValue(TurnCost.key(encoder.toString()));
    g.getTurnCostStorage().set(turnCostEnc, 0, 4, 7, 1);
    g.getTurnCostStorage().set(turnCostEnc, 0, 4, 9, 1);
    instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, new DefaultTurnCostProvider(encoder, g.getTurnCostStorage(), 0))));
    instance.setMinNetworkSize(4);
    assertEquals(3, instance.doWork());
    assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, encoder));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) DefaultTurnCostProvider(com.graphhopper.routing.weighting.DefaultTurnCostProvider) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test)

Example 12 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.

the class AbstractGraphStorageTester method test8AndMoreBytesForEdgeFlags.

@Test
public void test8AndMoreBytesForEdgeFlags() {
    List<FlagEncoder> list = new ArrayList<>();
    list.add(new CarFlagEncoder(29, 0.001, 0) {

        @Override
        public String getName() {
            return "car0";
        }
    });
    list.add(new CarFlagEncoder(29, 0.001, 0));
    EncodingManager manager = EncodingManager.create(list);
    graph = new GraphBuilder(manager).create();
    EdgeIteratorState edge = graph.edge(0, 1);
    IntsRef intsRef = manager.createEdgeFlags();
    intsRef.ints[0] = Integer.MAX_VALUE / 3;
    edge.setFlags(intsRef);
    // System.out.println(BitUtil.LITTLE.toBitString(Long.MAX_VALUE / 3) + "\n" + BitUtil.LITTLE.toBitString(edge.getFlags()));
    assertEquals(Integer.MAX_VALUE / 3, edge.getFlags().ints[0]);
    graph.close();
    graph = new GraphBuilder(manager).create();
    DecimalEncodedValue avSpeed0Enc = manager.getDecimalEncodedValue(getKey("car0", "average_speed"));
    BooleanEncodedValue access0Enc = manager.getBooleanEncodedValue(getKey("car0", "access"));
    DecimalEncodedValue avSpeed1Enc = manager.getDecimalEncodedValue(getKey("car", "average_speed"));
    BooleanEncodedValue access1Enc = manager.getBooleanEncodedValue(getKey("car", "access"));
    edge = graph.edge(0, 1);
    GHUtility.setSpeed(99.123, true, true, list.get(0), edge);
    assertEquals(99.123, edge.get(avSpeed0Enc), 1e-3);
    EdgeIteratorState edgeIter = GHUtility.getEdge(graph, 1, 0);
    assertEquals(99.123, edgeIter.get(avSpeed0Enc), 1e-3);
    assertTrue(edgeIter.get(access0Enc));
    assertTrue(edgeIter.getReverse(access0Enc));
    edge = graph.edge(2, 3);
    GHUtility.setSpeed(44.123, true, false, list.get(1), edge);
    assertEquals(44.123, edge.get(avSpeed1Enc), 1e-3);
    edgeIter = GHUtility.getEdge(graph, 3, 2);
    assertEquals(44.123, edgeIter.get(avSpeed1Enc), 1e-3);
    assertEquals(44.123, edgeIter.getReverse(avSpeed1Enc), 1e-3);
    assertFalse(edgeIter.get(access1Enc));
    assertTrue(edgeIter.getReverse(access1Enc));
    list.clear();
    list.add(new CarFlagEncoder(29, 0.001, 0) {

        @Override
        public String getName() {
            return "car0";
        }
    });
    list.add(new CarFlagEncoder(29, 0.001, 0));
    list.add(new CarFlagEncoder(30, 0.001, 0) {

        @Override
        public String getName() {
            return "car2";
        }
    });
    manager = EncodingManager.create(list);
    graph = new GraphBuilder(manager).create();
    edgeIter = graph.edge(0, 1).set(access0Enc, true, false);
    assertTrue(edgeIter.get(access0Enc));
    assertFalse(edgeIter.getReverse(access0Enc));
}
Also used : BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 13 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.

the class RacingBikeFlagEncoderTest method assertPriority.

private void assertPriority(EncodingManager encodingManager, int expectedPrio, ReaderWay way) {
    IntsRef edgeFlags = encodingManager.handleWayTags(way, encodingManager.createRelationFlags());
    FlagEncoder encoder = encodingManager.fetchEdgeEncoders().iterator().next();
    DecimalEncodedValue enc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(encoder.toString(), "priority"));
    assertEquals(PriorityCode.getValue(expectedPrio), enc.getDecimal(false, edgeFlags), 0.01);
}
Also used : DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) IntsRef(com.graphhopper.storage.IntsRef)

Example 14 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.

the class CHPreparationGraph method buildTurnCostFunctionFromTurnCostStorage.

/**
 * Builds a turn cost function for a given graph('s turn cost storage) and a weighting.
 * The trivial implementation would be simply returning {@link Weighting#calcTurnWeight}. However, it turned out
 * that reading all turn costs for the current encoder and then storing them in separate arrays upfront speeds up
 * edge-based CH preparation by about 25%. See #2084
 */
public static TurnCostFunction buildTurnCostFunctionFromTurnCostStorage(Graph graph, Weighting weighting) {
    FlagEncoder encoder = weighting.getFlagEncoder();
    String key = TurnCost.key(encoder.toString());
    if (!encoder.hasEncodedValue(key))
        return (inEdge, viaNode, outEdge) -> 0;
    DecimalEncodedValue turnCostEnc = encoder.getDecimalEncodedValue(key);
    TurnCostStorage turnCostStorage = graph.getTurnCostStorage();
    // we maintain a list of inEdge/outEdge/turn-cost triples (we use two arrays for this) that is sorted by nodes
    LongArrayList turnCostEdgePairs = new LongArrayList();
    DoubleArrayList turnCosts = new DoubleArrayList();
    // for each node we store the index of the first turn cost entry/triple in the list
    final int[] turnCostNodes = new int[graph.getNodes() + 1];
    TurnCostStorage.TurnRelationIterator tcIter = turnCostStorage.getAllTurnRelations();
    int lastNode = -1;
    while (tcIter.next()) {
        int viaNode = tcIter.getViaNode();
        if (viaNode < lastNode)
            throw new IllegalStateException();
        long edgePair = BitUtil.LITTLE.combineIntsToLong(tcIter.getFromEdge(), tcIter.getToEdge());
        // note that as long as we only use OSM turn restrictions all the turn costs are infinite anyway
        double turnCost = tcIter.getCost(turnCostEnc);
        int index = turnCostEdgePairs.size();
        turnCostEdgePairs.add(edgePair);
        turnCosts.add(turnCost);
        if (viaNode != lastNode) {
            for (int i = lastNode + 1; i <= viaNode; i++) {
                turnCostNodes[i] = index;
            }
        }
        lastNode = viaNode;
    }
    for (int i = lastNode + 1; i <= turnCostNodes.length - 1; i++) {
        turnCostNodes[i] = turnCostEdgePairs.size();
    }
    turnCostNodes[turnCostNodes.length - 1] = turnCostEdgePairs.size();
    // currently the u-turn costs are the same for all junctions, so for now we just get them for one of them
    double uTurnCosts = weighting.calcTurnWeight(1, 0, 1);
    return (inEdge, viaNode, outEdge) -> {
        if (!EdgeIterator.Edge.isValid(inEdge) || !EdgeIterator.Edge.isValid(outEdge))
            return 0;
        else if (inEdge == outEdge)
            return uTurnCosts;
        // traverse all turn cost entries we have for this viaNode and return the turn costs if we find a match
        for (int i = turnCostNodes[viaNode]; i < turnCostNodes[viaNode + 1]; i++) {
            long l = turnCostEdgePairs.get(i);
            if (inEdge == BitUtil.LITTLE.getIntLow(l) && outEdge == BitUtil.LITTLE.getIntHigh(l))
                return turnCosts.get(i);
        }
        return 0;
    };
}
Also used : IndirectSort(com.carrotsearch.hppc.sorting.IndirectSort) com.carrotsearch.hppc(com.carrotsearch.hppc) TurnCost(com.graphhopper.routing.ev.TurnCost) EdgeIterator(com.graphhopper.util.EdgeIterator) GHUtility(com.graphhopper.util.GHUtility) BitUtil(com.graphhopper.util.BitUtil) ArrayUtil.zero(com.graphhopper.util.ArrayUtil.zero) IndirectComparator(com.carrotsearch.hppc.sorting.IndirectComparator) TurnCostStorage(com.graphhopper.storage.TurnCostStorage) Weighting(com.graphhopper.routing.weighting.Weighting) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) Graph(com.graphhopper.storage.Graph) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) AllEdgesIterator(com.graphhopper.routing.util.AllEdgesIterator) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) TurnCostStorage(com.graphhopper.storage.TurnCostStorage)

Example 15 with DecimalEncodedValue

use of com.graphhopper.routing.ev.DecimalEncodedValue in project graphhopper by graphhopper.

the class OSMTurnRelationParserTest method testGetRestrictionAsEntries.

@Test
public void testGetRestrictionAsEntries() {
    CarFlagEncoder encoder = new CarFlagEncoder(5, 5, 1);
    final Map<Long, Integer> osmNodeToInternal = new HashMap<>();
    final Map<Integer, Long> internalToOSMEdge = new HashMap<>();
    osmNodeToInternal.put(3L, 3);
    // edge ids are only stored if they occurred before in an OSMRelation
    internalToOSMEdge.put(3, 3L);
    internalToOSMEdge.put(4, 4L);
    OSMTurnRelationParser parser = new OSMTurnRelationParser(encoder.toString(), 1, OSMRoadAccessParser.toOSMRestrictions(TransportationMode.CAR));
    GraphHopperStorage ghStorage = new GraphBuilder(new EncodingManager.Builder().add(encoder).addTurnCostParser(parser).build()).create();
    EdgeBasedRoutingAlgorithmTest.initGraph(ghStorage, encoder);
    TurnCostParser.ExternalInternalMap map = new TurnCostParser.ExternalInternalMap() {

        @Override
        public int getInternalNodeIdOfOsmNode(long nodeOsmId) {
            return osmNodeToInternal.getOrDefault(nodeOsmId, -1);
        }

        @Override
        public long getOsmIdOfInternalEdge(int edgeId) {
            Long l = internalToOSMEdge.get(edgeId);
            if (l == null)
                return -1;
            return l;
        }
    };
    // TYPE == ONLY
    OSMTurnRelation instance = new OSMTurnRelation(4, 3, 3, OSMTurnRelation.Type.ONLY);
    parser.addRelationToTCStorage(instance, map, ghStorage);
    TurnCostStorage tcs = ghStorage.getTurnCostStorage();
    DecimalEncodedValue tce = parser.getTurnCostEnc();
    assertTrue(Double.isInfinite(tcs.get(tce, 4, 3, 6)));
    assertEquals(0, tcs.get(tce, 4, 3, 3), .1);
    assertTrue(Double.isInfinite(tcs.get(tce, 4, 3, 2)));
    // TYPE == NOT
    instance = new OSMTurnRelation(4, 3, 3, OSMTurnRelation.Type.NOT);
    parser.addRelationToTCStorage(instance, map, ghStorage);
    assertTrue(Double.isInfinite(tcs.get(tce, 4, 3, 3)));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) OSMTurnRelation(com.graphhopper.reader.OSMTurnRelation) HashMap(java.util.HashMap) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) GraphBuilder(com.graphhopper.storage.GraphBuilder) TurnCostStorage(com.graphhopper.storage.TurnCostStorage) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) EdgeBasedRoutingAlgorithmTest(com.graphhopper.routing.EdgeBasedRoutingAlgorithmTest) Test(org.junit.jupiter.api.Test)

Aggregations

DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)26 Test (org.junit.jupiter.api.Test)15 BooleanEncodedValue (com.graphhopper.routing.ev.BooleanEncodedValue)6 Snap (com.graphhopper.storage.index.Snap)6 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)6 ReaderWay (com.graphhopper.reader.ReaderWay)4 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)4 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)4 IntsRef (com.graphhopper.storage.IntsRef)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 EncodingManager (com.graphhopper.routing.util.EncodingManager)3 DefaultTurnCostProvider (com.graphhopper.routing.weighting.DefaultTurnCostProvider)3 Weighting (com.graphhopper.routing.weighting.Weighting)3 Graph (com.graphhopper.storage.Graph)3 TurnCostStorage (com.graphhopper.storage.TurnCostStorage)3 Path (com.graphhopper.routing.Path)2 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)2 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)2 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 GraphBuilder (com.graphhopper.storage.GraphBuilder)2