Search in sources :

Example 1 with CustomWeighting

use of com.graphhopper.routing.weighting.custom.CustomWeighting in project graphhopper by graphhopper.

the class PriorityRoutingTest method testMaxPriority.

@Test
void testMaxPriority() {
    BikeFlagEncoder encoder = new BikeFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).create();
    NodeAccess na = graph.getNodeAccess();
    na.setNode(0, 48.0, 11.0);
    na.setNode(1, 48.1, 11.1);
    na.setNode(2, 48.2, 11.2);
    na.setNode(3, 48.3, 11.3);
    na.setNode(4, 48.1, 11.0);
    na.setNode(5, 48.2, 11.1);
    // 0 - 1 - 2 - 3
    // \- 4 - 5 -/
    double dist1 = 0;
    dist1 += maxSpeedEdge(em, graph, 0, 1, encoder, 1.0).getDistance();
    dist1 += maxSpeedEdge(em, graph, 1, 2, encoder, 1.0).getDistance();
    dist1 += maxSpeedEdge(em, graph, 2, 3, encoder, 1.0).getDistance();
    final double maxPrio = PriorityCode.getFactor(PriorityCode.BEST.getValue());
    double dist2 = 0;
    dist2 += maxSpeedEdge(em, graph, 0, 4, encoder, maxPrio).getDistance();
    dist2 += maxSpeedEdge(em, graph, 4, 5, encoder, maxPrio).getDistance();
    dist2 += maxSpeedEdge(em, graph, 5, 3, encoder, maxPrio).getDistance();
    // the routes 0-1-2-3 and 0-4-5-3 have similar distances (and use max speed everywhere)
    // ... but the shorter route 0-1-2-3 has smaller priority
    assertEquals(40101, dist1, 1);
    assertEquals(43005, dist2, 1);
    // A* and Dijkstra should yield the same path (the max priority must be taken into account by weighting.getMinWeight)
    {
        PriorityWeighting weighting = new PriorityWeighting(encoder, new PMap(), TurnCostProvider.NO_TURN_COST_PROVIDER);
        Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes());
        assertEquals(IntArrayList.from(0, 4, 5, 3), pathAStar.calcNodes());
    }
    {
        CustomModel customModel = new CustomModel();
        CustomWeighting weighting = CustomModelParser.createWeighting(encoder, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel);
        Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes());
        assertEquals(IntArrayList.from(0, 4, 5, 3), pathAStar.calcNodes());
    }
    {
        CustomModel customModel = new CustomModel();
        // now we even increase the priority in the custom model, which also needs to be accounted for in weighting.getMinWeight
        customModel.addToPriority(Statement.If("road_class == MOTORWAY", Statement.Op.MULTIPLY, 3));
        CustomWeighting weighting = CustomModelParser.createWeighting(encoder, em, TurnCostProvider.NO_TURN_COST_PROVIDER, customModel);
        Path pathDijkstra = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        Path pathAStar = new AStar(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 3);
        assertEquals(pathDijkstra.calcNodes(), pathAStar.calcNodes());
        assertEquals(IntArrayList.from(0, 4, 5, 3), pathAStar.calcNodes());
    }
}
Also used : NodeAccess(com.graphhopper.storage.NodeAccess) PMap(com.graphhopper.util.PMap) CustomModel(com.graphhopper.util.CustomModel) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) GraphBuilder(com.graphhopper.storage.GraphBuilder) PriorityWeighting(com.graphhopper.routing.weighting.PriorityWeighting) Test(org.junit.jupiter.api.Test)

Aggregations

PriorityWeighting (com.graphhopper.routing.weighting.PriorityWeighting)1 CustomWeighting (com.graphhopper.routing.weighting.custom.CustomWeighting)1 GraphBuilder (com.graphhopper.storage.GraphBuilder)1 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)1 NodeAccess (com.graphhopper.storage.NodeAccess)1 CustomModel (com.graphhopper.util.CustomModel)1 PMap (com.graphhopper.util.PMap)1 Test (org.junit.jupiter.api.Test)1