Search in sources :

Example 21 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class LMProfileSelectorTest method assertProfileFound.

private void assertProfileFound(Profile expectedProfile, List<Profile> profiles, List<LMProfile> lmProfiles, String vehicle, String weighting, Boolean edgeBased, Integer uTurnCosts) {
    PMap hintsMap = createHintsMap(vehicle, weighting, edgeBased, uTurnCosts);
    try {
        Profile selectedProfile = new ProfileResolver(encodingManager, profiles, Collections.<CHProfile>emptyList(), lmProfiles).selectProfileLM(hintsMap);
        assertEquals(expectedProfile, selectedProfile);
    } catch (IllegalArgumentException e) {
        fail("no profile found\nexpected: " + expectedProfile + "\nerror: " + e.getMessage());
    }
}
Also used : CHProfile(com.graphhopper.config.CHProfile) ProfileResolver(com.graphhopper.routing.ProfileResolver) PMap(com.graphhopper.util.PMap) CHProfile(com.graphhopper.config.CHProfile) Profile(com.graphhopper.config.Profile) LMProfile(com.graphhopper.config.LMProfile)

Example 22 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class PrepareLandmarksTest method testLandmarkStorageAndRouting.

@Test
public void testLandmarkStorageAndRouting() {
    // create graph with lat,lon
    // 0  1  2  ...
    // 15 16 17 ...
    Random rand = new Random(0);
    int width = 15, height = 15;
    DecimalEncodedValue avSpeedEnc = encoder.getAverageSpeedEnc();
    BooleanEncodedValue accessEnc = encoder.getAccessEnc();
    for (int hIndex = 0; hIndex < height; hIndex++) {
        for (int wIndex = 0; wIndex < width; wIndex++) {
            int node = wIndex + hIndex * width;
            // do not connect first with last column!
            double speed = 20 + rand.nextDouble() * 30;
            if (wIndex + 1 < width)
                graph.edge(node, node + 1).set(accessEnc, true, true).set(avSpeedEnc, speed);
            // avoid dead ends
            if (hIndex + 1 < height)
                graph.edge(node, node + width).set(accessEnc, true, true).set(avSpeedEnc, speed);
            updateDistancesFor(graph, node, -hIndex / 50.0, wIndex / 50.0);
        }
    }
    Directory dir = new RAMDirectory();
    LocationIndexTree index = new LocationIndexTree(graph, dir);
    index.prepareIndex();
    int lm = 5, activeLM = 2;
    Weighting weighting = new FastestWeighting(encoder);
    LMConfig lmConfig = new LMConfig("car", weighting);
    LandmarkStorage store = new LandmarkStorage(graph, dir, lmConfig, lm);
    store.setMinimumNodes(2);
    store.createLandmarks();
    // landmarks should be the 4 corners of the grid:
    int[] intList = store.getLandmarks(1);
    Arrays.sort(intList);
    assertEquals("[0, 14, 70, 182, 224]", Arrays.toString(intList));
    // two landmarks: one for subnetwork 0 (all empty) and one for subnetwork 1
    assertEquals(2, store.getSubnetworksWithLandmarks());
    assertEquals(0, store.getFromWeight(0, 224));
    double factor = store.getFactor();
    assertEquals(4671, Math.round(store.getFromWeight(0, 47) * factor));
    assertEquals(3640, Math.round(store.getFromWeight(0, 52) * factor));
    long weight1_224 = store.getFromWeight(1, 224);
    assertEquals(5525, Math.round(weight1_224 * factor));
    long weight1_47 = store.getFromWeight(1, 47);
    assertEquals(921, Math.round(weight1_47 * factor));
    // grid is symmetric
    assertEquals(weight1_224, store.getToWeight(1, 224));
    assertEquals(weight1_47, store.getToWeight(1, 47));
    // prefer the landmarks before and behind the goal
    int[] activeLandmarkIndices = new int[activeLM];
    Arrays.fill(activeLandmarkIndices, -1);
    store.chooseActiveLandmarks(27, 47, activeLandmarkIndices, false);
    List<Integer> list = new ArrayList<>();
    for (int idx : activeLandmarkIndices) {
        list.add(store.getLandmarks(1)[idx]);
    }
    // TODO should better select 0 and 224?
    assertEquals(Arrays.asList(224, 70), list);
    PrepareLandmarks prepare = new PrepareLandmarks(new RAMDirectory(), graph, lmConfig, 4);
    prepare.setMinimumNodes(2);
    prepare.doWork();
    LandmarkStorage lms = prepare.getLandmarkStorage();
    AStar expectedAlgo = new AStar(graph, weighting, tm);
    Path expectedPath = expectedAlgo.calcPath(41, 183);
    PMap hints = new PMap().putObject(Parameters.Landmark.ACTIVE_COUNT, 2);
    // landmarks with A*
    RoutingAlgorithm oneDirAlgoWithLandmarks = new LMRoutingAlgorithmFactory(lms).createAlgo(graph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR).setTraversalMode(tm).setHints(hints));
    Path path = oneDirAlgoWithLandmarks.calcPath(41, 183);
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes() - 135, oneDirAlgoWithLandmarks.getVisitedNodes());
    // landmarks with bidir A*
    RoutingAlgorithm biDirAlgoWithLandmarks = new LMRoutingAlgorithmFactory(lms).createAlgo(graph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR_BI).setTraversalMode(tm).setHints(hints));
    path = biDirAlgoWithLandmarks.calcPath(41, 183);
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes() - 162, biDirAlgoWithLandmarks.getVisitedNodes());
    // landmarks with A* and a QueryGraph. We expect slightly less optimal as two more cycles needs to be traversed
    // due to the two more virtual nodes but this should not harm in practise
    Snap fromSnap = index.findClosest(-0.0401, 0.2201, EdgeFilter.ALL_EDGES);
    Snap toSnap = index.findClosest(-0.2401, 0.0601, EdgeFilter.ALL_EDGES);
    QueryGraph qGraph = QueryGraph.create(graph, fromSnap, toSnap);
    RoutingAlgorithm qGraphOneDirAlgo = new LMRoutingAlgorithmFactory(lms).createAlgo(qGraph, weighting, new AlgorithmOptions().setAlgorithm(ASTAR).setTraversalMode(tm).setHints(hints));
    path = qGraphOneDirAlgo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
    expectedAlgo = new AStar(qGraph, weighting, tm);
    expectedPath = expectedAlgo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
    assertEquals(expectedPath.getWeight(), path.getWeight(), .1);
    assertEquals(expectedPath.calcNodes(), path.calcNodes());
    assertEquals(expectedAlgo.getVisitedNodes() - 135, qGraphOneDirAlgo.getVisitedNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) ArrayList(java.util.ArrayList) AStar(com.graphhopper.routing.AStar) PMap(com.graphhopper.util.PMap) AlgorithmOptions(com.graphhopper.routing.AlgorithmOptions) Snap(com.graphhopper.storage.index.Snap) Random(java.util.Random) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Directory(com.graphhopper.storage.Directory) RAMDirectory(com.graphhopper.storage.RAMDirectory) Path(com.graphhopper.routing.Path) RAMDirectory(com.graphhopper.storage.RAMDirectory) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 23 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class CarFlagEncoderTest method testFordAccess.

@Test
public void testFordAccess() {
    ReaderNode node = new ReaderNode(0, 0.0, 0.0);
    node.setTag("ford", "yes");
    ReaderWay way = new ReaderWay(1);
    way.setTag("highway", "unclassified");
    way.setTag("ford", "yes");
    // Node and way are initially blocking
    assertTrue(encoder.isBlockFords());
    assertTrue(encoder.getAccess(way).canSkip());
    assertTrue(encoder.isBarrier(node));
    CarFlagEncoder tmpEncoder = new CarFlagEncoder(new PMap("block_fords=false"));
    EncodingManager.create(tmpEncoder);
    assertTrue(tmpEncoder.getAccess(way).isWay());
    assertFalse(tmpEncoder.isBarrier(node));
}
Also used : ReaderNode(com.graphhopper.reader.ReaderNode) PMap(com.graphhopper.util.PMap) ReaderWay(com.graphhopper.reader.ReaderWay) Test(org.junit.jupiter.api.Test)

Example 24 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class CarFlagEncoderTest method testPrivateTag.

@Test
public void testPrivateTag() {
    // allow private access
    CarFlagEncoder carEncoder = new CarFlagEncoder(new PMap("block_private=false"));
    FlagEncoder bikeEncoder = new BikeFlagEncoder(new PMap("block_private=false"));
    EncodingManager em = new EncodingManager.Builder().add(carEncoder).add(bikeEncoder).build();
    FastestWeighting weighting = new FastestWeighting(carEncoder);
    FastestWeighting bikeWeighting = new FastestWeighting(bikeEncoder);
    ReaderWay way = new ReaderWay(1);
    way.setTag("highway", "secondary");
    assertNotEquals(EncodingManager.Access.CAN_SKIP, carEncoder.getAccess(way));
    IntsRef edgeFlags = em.handleWayTags(way, em.createRelationFlags());
    assertEquals(60, weighting.calcEdgeWeight(GHUtility.createMockedEdgeIteratorState(1000, edgeFlags), false), 0.1);
    assertEquals(200, bikeWeighting.calcEdgeWeight(GHUtility.createMockedEdgeIteratorState(1000, edgeFlags), false), 0.1);
    way.setTag("highway", "secondary");
    way.setTag("access", "private");
    assertNotEquals(EncodingManager.Access.CAN_SKIP, carEncoder.getAccess(way));
    edgeFlags = em.handleWayTags(way, em.createRelationFlags());
    assertEquals(600, weighting.calcEdgeWeight(GHUtility.createMockedEdgeIteratorState(1000, edgeFlags), false), 0.1);
    // private should influence bike only slightly
    assertEquals(240, bikeWeighting.calcEdgeWeight(GHUtility.createMockedEdgeIteratorState(1000, edgeFlags), false), 0.1);
}
Also used : PMap(com.graphhopper.util.PMap) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) ReaderWay(com.graphhopper.reader.ReaderWay) IntsRef(com.graphhopper.storage.IntsRef) Test(org.junit.jupiter.api.Test)

Example 25 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class CHProfileSelectorTest method assertProfileFound.

private void assertProfileFound(Profile expectedProfile, List<Profile> profiles, List<CHProfile> chProfiles, String vehicle, String weighting, Boolean edgeBased, Integer uTurnCosts) {
    PMap hintsMap = createHintsMap(vehicle, weighting, edgeBased, uTurnCosts);
    try {
        Profile selectedProfile = new ProfileResolver(encodingManager, profiles, chProfiles, Collections.<LMProfile>emptyList()).selectProfileCH(hintsMap);
        assertEquals(expectedProfile, selectedProfile);
    } catch (IllegalArgumentException e) {
        fail("no profile found\nexpected: " + expectedProfile + "\nerror: " + e.getMessage());
    }
}
Also used : ProfileResolver(com.graphhopper.routing.ProfileResolver) PMap(com.graphhopper.util.PMap) LMProfile(com.graphhopper.config.LMProfile) CHProfile(com.graphhopper.config.CHProfile) Profile(com.graphhopper.config.Profile) LMProfile(com.graphhopper.config.LMProfile)

Aggregations

PMap (com.graphhopper.util.PMap)50 Test (org.junit.jupiter.api.Test)27 LMProfile (com.graphhopper.config.LMProfile)12 Profile (com.graphhopper.config.Profile)12 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)12 CHProfile (com.graphhopper.config.CHProfile)10 ArrayList (java.util.ArrayList)10 GraphHopper (com.graphhopper.GraphHopper)6 MapMatching (com.graphhopper.matching.MapMatching)5 MatchResult (com.graphhopper.matching.MatchResult)5 CHProfileSelectorTest (com.graphhopper.routing.ch.CHProfileSelectorTest)5 CHRoutingAlgorithmFactory (com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)5 LMProfileSelectorTest (com.graphhopper.routing.lm.LMProfileSelectorTest)5 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)5 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)5 Gpx (com.graphhopper.jackson.Gpx)4 ReaderWay (com.graphhopper.reader.ReaderWay)4 ProfileResolver (com.graphhopper.routing.ProfileResolver)4 Weighting (com.graphhopper.routing.weighting.Weighting)4 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)4