Search in sources :

Example 21 with Weighting

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) {
    }
}
Also used : ArrayList(java.util.ArrayList) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 22 with Weighting

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);
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Test(org.junit.Test)

Example 23 with Weighting

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);
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) Graph(com.graphhopper.storage.Graph) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 24 with Weighting

use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.

the class AlternativeRouteTest method testCalcAlternatives.

@Test
public void testCalcAlternatives() throws Exception {
    Weighting weighting = new FastestWeighting(carFE);
    GraphHopperStorage g = createTestGraph(true, em);
    AlternativeRoute altDijkstra = new AlternativeRoute(g, weighting, traversalMode);
    altDijkstra.setMaxShareFactor(0.5);
    altDijkstra.setMaxWeightFactor(2);
    List<AlternativeRoute.AlternativeInfo> pathInfos = altDijkstra.calcAlternatives(5, 4);
    checkAlternatives(pathInfos);
    assertEquals(2, pathInfos.size());
    DijkstraBidirectionRef dijkstra = new DijkstraBidirectionRef(g, weighting, traversalMode);
    Path bestPath = dijkstra.calcPath(5, 4);
    Path bestAlt = pathInfos.get(0).getPath();
    Path secondAlt = pathInfos.get(1).getPath();
    assertEquals(bestPath.calcNodes(), bestAlt.calcNodes());
    assertEquals(bestPath.getWeight(), bestAlt.getWeight(), 1e-3);
    assertEquals(Helper.createTList(5, 6, 3, 4), bestAlt.calcNodes());
    // Note: here plateau is longer, even longer than optimum, but path is longer
    // so which alternative is better? longer plateau.weight with bigger path.weight or smaller path.weight with smaller plateau.weight
    // assertEquals(Helper.createTList(5, 1, 9, 2, 3, 4), secondAlt.calcNodes());
    assertEquals(Helper.createTList(5, 6, 7, 8, 4), secondAlt.calcNodes());
    assertEquals(1667.9, secondAlt.getWeight(), .1);
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.Test)

Aggregations

Weighting (com.graphhopper.routing.weighting.Weighting)24 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)16 Test (org.junit.Test)16 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)8 AbstractWeighting (com.graphhopper.routing.weighting.AbstractWeighting)6 LocationIndex (com.graphhopper.storage.index.LocationIndex)5 QueryResult (com.graphhopper.storage.index.QueryResult)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 GraphHopper (com.graphhopper.GraphHopper)3 Graph (com.graphhopper.storage.Graph)3 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)3 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)3 GHRequest (com.graphhopper.GHRequest)2 GHResponse (com.graphhopper.GHResponse)2 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)2 BikeFlagEncoder (com.graphhopper.routing.util.BikeFlagEncoder)2 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 AlgoHelperEntry (com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry)2