Search in sources :

Example 26 with FlagEncoder

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

the class TurnCostExtensionTest method testMultipleTurnCosts.

/**
 * Test if multiple turn costs can be safely written to the storage and read from it.
 */
@Test
public void testMultipleTurnCosts() {
    FlagEncoder carEncoder = new CarFlagEncoder(5, 5, 3);
    FlagEncoder bikeEncoder = new BikeFlagEncoder(5, 5, 3);
    EncodingManager manager = new EncodingManager(carEncoder, bikeEncoder);
    GraphHopperStorage g = new GraphBuilder(manager).create();
    initGraph(g);
    TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
    // introduce some turn costs
    long carRestricted = carEncoder.getTurnFlags(true, 0);
    long carCosts = carEncoder.getTurnFlags(false, 2);
    long bikeRestricted = bikeEncoder.getTurnFlags(true, 0);
    long bikeCosts = bikeEncoder.getTurnFlags(false, 2);
    int edge42 = getEdge(g, 4, 2).getEdge();
    int edge23 = getEdge(g, 2, 3).getEdge();
    int edge31 = getEdge(g, 3, 1).getEdge();
    int edge10 = getEdge(g, 1, 0).getEdge();
    int edge02 = getEdge(g, 0, 2).getEdge();
    int edge24 = getEdge(g, 2, 4).getEdge();
    tcs.mergeOrOverwriteTurnInfo(edge42, 2, edge23, carRestricted, true);
    tcs.mergeOrOverwriteTurnInfo(edge42, 2, edge23, bikeRestricted, true);
    tcs.mergeOrOverwriteTurnInfo(edge23, 3, edge31, carRestricted, true);
    tcs.mergeOrOverwriteTurnInfo(edge23, 3, edge31, bikeCosts, true);
    tcs.mergeOrOverwriteTurnInfo(edge31, 1, edge10, carCosts, true);
    tcs.mergeOrOverwriteTurnInfo(edge31, 1, edge10, bikeRestricted, true);
    tcs.mergeOrOverwriteTurnInfo(edge02, 2, edge24, carRestricted, false);
    tcs.mergeOrOverwriteTurnInfo(edge02, 2, edge24, bikeRestricted, false);
    long flags423 = tcs.getTurnCostFlags(edge42, 2, edge23);
    assertEquals(Double.POSITIVE_INFINITY, carEncoder.getTurnCost(flags423), 0);
    assertEquals(Double.POSITIVE_INFINITY, bikeEncoder.getTurnCost(flags423), 0);
    long flags231 = tcs.getTurnCostFlags(edge23, 3, edge31);
    assertEquals(Double.POSITIVE_INFINITY, carEncoder.getTurnCost(flags231), 0);
    assertEquals(2.0, bikeEncoder.getTurnCost(flags231), 0);
    long flags310 = tcs.getTurnCostFlags(edge31, 1, edge10);
    assertEquals(2.0, carEncoder.getTurnCost(flags310), 0);
    assertEquals(Double.POSITIVE_INFINITY, bikeEncoder.getTurnCost(flags310), 0);
    long flags024 = tcs.getTurnCostFlags(edge02, 2, edge24);
    assertEquals(0.0, carEncoder.getTurnCost(flags024), 0);
    assertEquals(Double.POSITIVE_INFINITY, bikeEncoder.getTurnCost(flags024), 0);
    // merge per default
    tcs.addTurnInfo(edge02, 2, edge23, carRestricted);
    tcs.addTurnInfo(edge02, 2, edge23, bikeRestricted);
    long flags023 = tcs.getTurnCostFlags(edge02, 2, edge23);
    assertEquals(Double.POSITIVE_INFINITY, carEncoder.getTurnCost(flags023), 0);
    assertEquals(Double.POSITIVE_INFINITY, bikeEncoder.getTurnCost(flags023), 0);
    // overwrite
    tcs.mergeOrOverwriteTurnInfo(edge02, 2, edge23, bikeRestricted, false);
    flags023 = tcs.getTurnCostFlags(edge02, 2, edge23);
    assertEquals(0, carEncoder.getTurnCost(flags023), 0);
    assertEquals(Double.POSITIVE_INFINITY, bikeEncoder.getTurnCost(flags023), 0);
    // clear
    tcs.mergeOrOverwriteTurnInfo(edge02, 2, edge23, 0, false);
    flags023 = tcs.getTurnCostFlags(edge02, 2, edge23);
    assertEquals(0, carEncoder.getTurnCost(flags023), 0);
    assertEquals(0, bikeEncoder.getTurnCost(flags023), 0);
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) BikeFlagEncoder(com.graphhopper.routing.util.BikeFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) BikeFlagEncoder(com.graphhopper.routing.util.BikeFlagEncoder) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) Test(org.junit.Test)

Example 27 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder 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 28 with FlagEncoder

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

the class PrepareRoutingSubnetworksTest method testNodeOrderingRegression.

// Previous two-pass implementation failed on 1 -> 2 -> 0
@Test
public void testNodeOrderingRegression() {
    // 1 -> 2 -> 0 - 3 - 4 - 5
    GraphHopperStorage g = new GraphBuilder(createEncodingManager("car")).create();
    FlagEncoder encoder = g.getEncodingManager().fetchEdgeEncoders().iterator().next();
    GHUtility.setSpeed(60, true, false, encoder, g.edge(1, 2).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, g.edge(2, 0).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 3).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(3, 4).setDistance(1));
    GHUtility.setSpeed(60, true, true, encoder, g.edge(4, 5).setDistance(1));
    PrepareRoutingSubnetworks.PrepareJob job = createJob(g.getEncodingManager(), encoder, NO_TURN_COST_PROVIDER);
    PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(job)).setMinNetworkSize(2);
    int subnetworkEdges = instance.doWork();
    assertEquals(2, subnetworkEdges);
    assertEquals(IntArrayList.from(0, 1), getSubnetworkEdges(g, encoder));
}
Also used : FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GraphBuilder(com.graphhopper.storage.GraphBuilder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test)

Example 29 with FlagEncoder

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

the class PrepareRoutingSubnetworksTest method testPrepareSubnetworks_oneVehicle.

@Test
public void testPrepareSubnetworks_oneVehicle() {
    EncodingManager em = createEncodingManager("car");
    FlagEncoder encoder = em.getEncoder("car");
    GraphHopperStorage g = createSubnetworkTestStorage(em);
    PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, NO_TURN_COST_PROVIDER)));
    // this will make the upper small network a subnetwork
    instance.setMinNetworkSize(4);
    assertEquals(3, instance.doWork());
    assertEquals(IntArrayList.from(7, 8, 9), getSubnetworkEdges(g, encoder));
    // this time we lower the threshold and the upper network won't be set to be a subnetwork
    g = createSubnetworkTestStorage(em);
    instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(createJob(em, encoder, NO_TURN_COST_PROVIDER)));
    instance.setMinNetworkSize(3);
    assertEquals(0, instance.doWork());
    assertEquals(IntArrayList.from(), getSubnetworkEdges(g, encoder));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test)

Example 30 with FlagEncoder

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

the class PrepareRoutingSubnetworksTest method createEncodingManager.

private static EncodingManager createEncodingManager(String flagEncodersStr) {
    EncodingManager.Builder builder = new EncodingManager.Builder();
    for (String encoderStr : flagEncodersStr.split(",")) {
        encoderStr = encoderStr.trim();
        FlagEncoder encoder = new DefaultFlagEncoderFactory().createFlagEncoder(encoderStr.split("\\|")[0], new PMap(encoderStr));
        builder.add(encoder);
        builder.add(Subnetwork.create(encoder.toString()));
    }
    return builder.build();
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) DefaultFlagEncoderFactory(com.graphhopper.routing.util.DefaultFlagEncoderFactory) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GraphBuilder(com.graphhopper.storage.GraphBuilder) PMap(com.graphhopper.util.PMap)

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