use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class CustomWeightingTest method maxSpeedViolated_bug_2307.
@Test
public void maxSpeedViolated_bug_2307() {
EdgeIteratorState motorway = graph.edge(0, 1).setDistance(10).set(roadClassEnc, MOTORWAY).set(avSpeedEnc, 80).set(accessEnc, true, true);
CustomModel customModel = new CustomModel().addToSpeed(Statement.If("road_class == MOTORWAY", Statement.Op.MULTIPLY, 0.7)).addToSpeed(Statement.Else(LIMIT, 30));
Weighting weighting = createWeighting(customModel);
assertEquals(1.3429, weighting.calcEdgeWeight(motorway, false), 1e-4);
assertEquals(10 / (80 * 0.7 / 3.6) * 1000, weighting.calcEdgeMillis(motorway, false), 1);
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class PrepareContractionHierarchiesTest method testUnpackingOrder_Fastest.
@Test
public void testUnpackingOrder_Fastest() {
GraphHopperStorage ghStorage = createGHStorage();
CHGraph lg = ghStorage.getGraph(CHGraph.class);
Weighting w = new FastestWeighting(carEncoder);
initUnpackingGraph(ghStorage, lg, w);
PrepareContractionHierarchies prepare = new PrepareContractionHierarchies(dir, ghStorage, lg, weighting, tMode);
RoutingAlgorithm algo = prepare.createAlgo(lg, new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
Path p = algo.calcPath(10, 6);
assertEquals(7, p.getDistance(), 1e-1);
assertEquals(Helper.createTList(10, 0, 1, 2, 3, 4, 5, 6), p.calcNodes());
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class GraphHopperStorageCHTest method testShortcutCreationAndAccessForManyVehicles.
@Test
public void testShortcutCreationAndAccessForManyVehicles() {
FlagEncoder tmpCar = new CarFlagEncoder();
FlagEncoder tmpBike = new Bike2WeightFlagEncoder();
EncodingManager em = new EncodingManager(tmpCar, tmpBike);
List<Weighting> chWeightings = new ArrayList<Weighting>();
chWeightings.add(new FastestWeighting(tmpCar));
chWeightings.add(new FastestWeighting(tmpBike));
graph = new GraphHopperStorage(chWeightings, new RAMDirectory(), em, false, new GraphExtension.NoOpExtension()).create(1000);
graph.edge(0, 1).setDistance(10).setFlags(tmpCar.setProperties(100, true, true) | tmpBike.setProperties(10, true, true));
graph.edge(1, 2).setDistance(10).setFlags(tmpCar.setProperties(100, true, true) | tmpBike.setProperties(10, true, true));
graph.freeze();
CHGraph carCHGraph = graph.getGraph(CHGraph.class, chWeightings.get(0));
// enable forward directions for car
EdgeIteratorState carSC02 = carCHGraph.shortcut(0, 2).setWeight(10).setFlags(PrepareEncoder.getScFwdDir()).setDistance(20);
CHGraph bikeCHGraph = graph.getGraph(CHGraph.class, chWeightings.get(1));
// enable both directions for bike
EdgeIteratorState bikeSC02 = bikeCHGraph.shortcut(0, 2).setWeight(10).setFlags(PrepareEncoder.getScDirMask()).setDistance(20);
// assert car CH graph
assertTrue(carCHGraph.getEdgeIteratorState(carSC02.getEdge(), 2).isForward(tmpCar));
assertFalse(carCHGraph.getEdgeIteratorState(carSC02.getEdge(), 2).isBackward(tmpCar));
// throw exception for wrong encoder
try {
assertFalse(carCHGraph.getEdgeIteratorState(carSC02.getEdge(), 2).isForward(tmpBike));
assertTrue(false);
} catch (AssertionError ex) {
}
// assert bike CH graph
assertTrue(bikeCHGraph.getEdgeIteratorState(bikeSC02.getEdge(), 2).isForward(tmpBike));
assertTrue(bikeCHGraph.getEdgeIteratorState(bikeSC02.getEdge(), 2).isBackward(tmpBike));
// throw exception for wrong encoder
try {
assertFalse(bikeCHGraph.getEdgeIteratorState(bikeSC02.getEdge(), 2).isBackward(tmpCar));
assertTrue(false);
} catch (AssertionError ex) {
}
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class AbstractRoutingAlgorithmTester method testWithCoordinates.
// a-b-0-c-1
// | | _/\
// | / / |
// d-2--3-e-4
@Test
public void testWithCoordinates() {
Weighting weighting = new ShortestWeighting(carEncoder);
GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false);
graph.edge(0, 1, 2, true).setWayGeometry(Helper.createPointList(1.5, 1));
graph.edge(2, 3, 2, true).setWayGeometry(Helper.createPointList(0, 1.5));
graph.edge(3, 4, 2, true).setWayGeometry(Helper.createPointList(0, 2));
// duplicate but the second edge is longer
graph.edge(0, 2, 1.2, true);
graph.edge(0, 2, 1.5, true).setWayGeometry(Helper.createPointList(0.5, 0));
graph.edge(1, 3, 1.3, true).setWayGeometry(Helper.createPointList(0.5, 1.5));
graph.edge(1, 4, 1, true);
updateDistancesFor(graph, 0, 1, 0.6);
updateDistancesFor(graph, 1, 1, 1.5);
updateDistancesFor(graph, 2, 0, 0);
updateDistancesFor(graph, 3, 0, 1);
updateDistancesFor(graph, 4, 0, 2);
AlgorithmOptions opts = new AlgorithmOptions(DIJKSTRA_BI, weighting);
RoutingAlgorithmFactory prepare = createFactory(graph, opts);
Path p = prepare.createAlgo(getGraph(graph, opts.getWeighting()), opts).calcPath(4, 0);
assertEquals(Helper.createTList(4, 1, 0), p.calcNodes());
assertEquals(Helper.createPointList(0, 2, 1, 1.5, 1.5, 1, 1, 0.6), p.calcPoints());
assertEquals(274128, p.calcPoints().calcDistance(new DistanceCalcEarth()), 1);
p = prepare.createAlgo(getGraph(graph, opts.getWeighting()), opts).calcPath(2, 1);
assertEquals(Helper.createTList(2, 0, 1), p.calcNodes());
assertEquals(Helper.createPointList(0, 0, 1, 0.6, 1.5, 1, 1, 1.5), p.calcPoints());
assertEquals(279482, p.calcPoints().calcDistance(new DistanceCalcEarth()), 1);
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class AlternativeRouteTest method testCalcAlternatives2.
@Test
public void testCalcAlternatives2() throws Exception {
Weighting weighting = new FastestWeighting(carFE);
Graph g = createTestGraph(true, em);
AlternativeRoute altDijkstra = new AlternativeRoute(g, weighting, traversalMode);
altDijkstra.setMaxPaths(3);
altDijkstra.setMaxShareFactor(0.7);
altDijkstra.setMinPlateauFactor(0.15);
altDijkstra.setMaxWeightFactor(2);
// edge based traversal requires a bit more exploration than the default of 1
altDijkstra.setMaxExplorationFactor(1.2);
List<AlternativeRoute.AlternativeInfo> pathInfos = altDijkstra.calcAlternatives(5, 4);
checkAlternatives(pathInfos);
assertEquals(3, pathInfos.size());
// result is sorted based on the plateau to full weight ratio
assertEquals(Helper.createTList(5, 6, 3, 4), pathInfos.get(0).getPath().calcNodes());
assertEquals(Helper.createTList(5, 6, 7, 8, 4), pathInfos.get(1).getPath().calcNodes());
assertEquals(Helper.createTList(5, 1, 9, 2, 3, 4), pathInfos.get(2).getPath().calcNodes());
assertEquals(2416.0, pathInfos.get(2).getPath().getWeight(), .1);
}
Aggregations