Search in sources :

Example 1 with Weighting

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

the class LMAlgoFactoryDecorator method createPreparations.

/**
     * This method creates the landmark storages ready for landmark creation.
     */
public void createPreparations(GraphHopperStorage ghStorage, TraversalMode traversalMode, LocationIndex locationIndex) {
    if (!isEnabled() || !preparations.isEmpty())
        return;
    if (weightings.isEmpty())
        throw new IllegalStateException("No landmark weightings found");
    List<LandmarkSuggestion> lmSuggestions = new ArrayList<>(lmSuggestionsLocations.size());
    if (!lmSuggestionsLocations.isEmpty()) {
        try {
            for (String loc : lmSuggestionsLocations) {
                lmSuggestions.add(LandmarkSuggestion.readLandmarks(loc, locationIndex));
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    for (Weighting weighting : getWeightings()) {
        Double maximumWeight = maximumWeights.get(weighting.getName());
        if (maximumWeight == null)
            throw new IllegalStateException("maximumWeight cannot be null. Default should be just negative");
        PrepareLandmarks tmpPrepareLM = new PrepareLandmarks(ghStorage.getDirectory(), ghStorage, weighting, traversalMode, landmarkCount, activeLandmarkCount).setLandmarkSuggestions(lmSuggestions).setMaximumWeight(maximumWeight);
        addPreparation(tmpPrepareLM);
    }
}
Also used : AbstractWeighting(com.graphhopper.routing.weighting.AbstractWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) IOException(java.io.IOException)

Example 2 with Weighting

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

the class AbstractRoutingAlgorithmTester method testQueryGraphAndFastest.

@Test
public void testQueryGraphAndFastest() {
    Weighting weighting = new FastestWeighting(carEncoder);
    GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false);
    initDirectedAndDiffSpeed(graph, carEncoder);
    Path p = calcPathViaQuery(weighting, graph, 0.002, 0.0005, 0.0017, 0.0031);
    assertEquals(Helper.createTList(8, 1, 5, 3, 9), p.calcNodes());
    assertEquals(602.98, p.getDistance(), 1e-1);
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 3 with Weighting

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

the class AbstractRoutingAlgorithmTester method testTwoWeightsPerEdge2.

@Test
public void testTwoWeightsPerEdge2() {
    // other direction should be different!
    Weighting fakeWeighting = new Weighting() {

        @Override
        public FlagEncoder getFlagEncoder() {
            return carEncoder;
        }

        @Override
        public double getMinWeight(double distance) {
            return 0.8 * distance;
        }

        @Override
        public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
            int adj = edgeState.getAdjNode();
            int base = edgeState.getBaseNode();
            if (reverse) {
                int tmp = base;
                base = adj;
                adj = tmp;
            }
            // a 'hill' at node 6
            if (adj == 6)
                return 3 * edgeState.getDistance();
            else if (base == 6)
                return edgeState.getDistance() * 0.9;
            else if (adj == 4)
                return 2 * edgeState.getDistance();
            return edgeState.getDistance() * 0.8;
        }

        private final Weighting tmpW = new FastestWeighting(carEncoder);

        @Override
        public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
            return tmpW.calcMillis(edgeState, reverse, prevOrNextEdgeId);
        }

        @Override
        public boolean matches(HintsMap map) {
            throw new UnsupportedOperationException("Not supported");
        }

        @Override
        public String getName() {
            return "custom";
        }
    };
    AlgorithmOptions opts = AlgorithmOptions.start().weighting(defaultOpts.getWeighting()).build();
    GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(opts.getWeighting()), true);
    initEleGraph(graph);
    Path p = createAlgo(graph, opts).calcPath(0, 10);
    // GHUtility.printEdgeInfo(graph, carEncoder);
    assertEquals(Helper.createTList(0, 4, 6, 10), p.calcNodes());
    AlgorithmOptions fakeOpts = AlgorithmOptions.start().weighting(fakeWeighting).build();
    graph = createGHStorage(encodingManager, Arrays.asList(fakeOpts.getWeighting()), true);
    initEleGraph(graph);
    QueryResult from = newQR(graph, 3, 0);
    QueryResult to = newQR(graph, 10, 9);
    RoutingAlgorithmFactory factory = createFactory(graph, fakeOpts);
    QueryGraph qGraph = new QueryGraph(getGraph(graph, fakeWeighting)).lookup(from, to);
    p = factory.createAlgo(qGraph, fakeOpts).calcPath(from.getClosestNode(), to.getClosestNode());
    assertEquals(Helper.createTList(12, 0, 1, 2, 11, 7, 10, 13), p.calcNodes());
    assertEquals(37009621, p.getTime());
    assertEquals(616827, p.getDistance(), 1);
    assertEquals(493462, p.getWeight(), 1);
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 4 with Weighting

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

the class AlternativeRouteTest method testDisconnectedAreas.

@Test
public void testDisconnectedAreas() {
    Graph g = createTestGraph(true, em);
    // one single disconnected node
    updateDistancesFor(g, 20, 0.00, -0.01);
    Weighting weighting = new FastestWeighting(carFE);
    AlternativeBidirSearch altDijkstra = new AlternativeBidirSearch(g, weighting, traversalMode, 1);
    Path path = altDijkstra.calcPath(1, 20);
    assertFalse(path.isFound());
    // make sure not the full graph is traversed!
    assertEquals(3, altDijkstra.getVisitedNodes());
}
Also used : AlternativeBidirSearch(com.graphhopper.routing.AlternativeRoute.AlternativeBidirSearch) Graph(com.graphhopper.storage.Graph) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 5 with Weighting

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

the class GraphHopperOSMTest method testGetWeightingForCH.

@Test
public void testGetWeightingForCH() {
    TestEncoder truck = new TestEncoder("truck");
    TestEncoder simpleTruck = new TestEncoder("simple_truck");
    // use simple truck first
    EncodingManager em = new EncodingManager(simpleTruck, truck);
    CHAlgoFactoryDecorator decorator = new CHAlgoFactoryDecorator();
    Weighting fwSimpleTruck = new FastestWeighting(simpleTruck);
    Weighting fwTruck = new FastestWeighting(truck);
    RAMDirectory ramDir = new RAMDirectory();
    GraphHopperStorage storage = new GraphHopperStorage(Arrays.asList(fwSimpleTruck, fwTruck), ramDir, em, false, new GraphExtension.NoOpExtension());
    decorator.addWeighting(fwSimpleTruck);
    decorator.addWeighting(fwTruck);
    decorator.addPreparation(new PrepareContractionHierarchies(ramDir, storage, storage.getGraph(CHGraph.class, fwSimpleTruck), fwSimpleTruck, TraversalMode.NODE_BASED));
    decorator.addPreparation(new PrepareContractionHierarchies(ramDir, storage, storage.getGraph(CHGraph.class, fwTruck), fwTruck, TraversalMode.NODE_BASED));
    HintsMap wMap = new HintsMap("fastest");
    wMap.put("vehicle", "truck");
    assertEquals("fastest|truck", ((PrepareContractionHierarchies) decorator.getDecoratedAlgorithmFactory(null, wMap)).getWeighting().toString());
    wMap.put("vehicle", "simple_truck");
    assertEquals("fastest|simple_truck", ((PrepareContractionHierarchies) decorator.getDecoratedAlgorithmFactory(null, wMap)).getWeighting().toString());
    // make sure weighting cannot be mixed
    decorator.addWeighting(fwTruck);
    decorator.addWeighting(fwSimpleTruck);
    try {
        decorator.addPreparation(new PrepareContractionHierarchies(ramDir, storage, storage.getGraph(CHGraph.class, fwSimpleTruck), fwSimpleTruck, TraversalMode.NODE_BASED));
        assertTrue(false);
    } catch (Exception ex) {
    }
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) AbstractWeighting(com.graphhopper.routing.weighting.AbstractWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) CHAlgoFactoryDecorator(com.graphhopper.routing.ch.CHAlgoFactoryDecorator) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Weighting (com.graphhopper.routing.weighting.Weighting)57 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)32 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)15 Test (org.junit.jupiter.api.Test)15 Test (org.junit.Test)12 Snap (com.graphhopper.storage.index.Snap)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)9 GHPoint (com.graphhopper.util.shapes.GHPoint)9 Path (com.graphhopper.routing.Path)8 Graph (com.graphhopper.storage.Graph)8 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)8 Profile (com.graphhopper.config.Profile)7 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)7 ArrayList (java.util.ArrayList)7 GraphHopper (com.graphhopper.GraphHopper)6 IOException (java.io.IOException)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 AbstractWeighting (com.graphhopper.routing.weighting.AbstractWeighting)5