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);
}
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));
}
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));
}
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));
}
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();
}
Aggregations