Search in sources :

Example 1 with AlgoHelperEntry

use of com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry in project graphhopper by graphhopper.

the class RoutingAlgorithmWithOSMIT method testMonacoParallel.

@Test
public void testMonacoParallel() throws IOException {
    System.out.println("testMonacoParallel takes a bit time...");
    String graphFile = "target/monaco-gh";
    Helper.removeDir(new File(graphFile));
    final EncodingManager encodingManager = new EncodingManager("car");
    final GraphHopper hopper = new GraphHopperOSM().setStoreOnFlush(true).setEncodingManager(encodingManager).setCHEnabled(false).setWayPointMaxDistance(0).setDataReaderFile(DIR + "/monaco.osm.gz").setGraphHopperLocation(graphFile).importOrLoad();
    final Graph g = hopper.getGraphHopperStorage();
    final LocationIndex idx = hopper.getLocationIndex();
    final List<OneRun> instances = createMonacoCar();
    List<Thread> threads = new ArrayList<Thread>();
    final AtomicInteger integ = new AtomicInteger(0);
    int MAX = 100;
    final FlagEncoder carEncoder = encodingManager.getEncoder("car");
    // testing if algorithms are independent. should be. so test only two algorithms. 
    // also the preparing is too costly to be called for every thread
    int algosLength = 2;
    final Weighting weighting = new ShortestWeighting(encodingManager.getEncoder("car"));
    final EdgeFilter filter = new DefaultEdgeFilter(carEncoder);
    for (int no = 0; no < MAX; no++) {
        for (int instanceNo = 0; instanceNo < instances.size(); instanceNo++) {
            String[] algos = new String[] { ASTAR, DIJKSTRA_BI };
            for (final String algoStr : algos) {
                // an algorithm is not thread safe! reuse via clear() is ONLY appropriated if used from same thread!
                final int instanceIndex = instanceNo;
                Thread t = new Thread() {

                    @Override
                    public void run() {
                        OneRun oneRun = instances.get(instanceIndex);
                        AlgorithmOptions opts = AlgorithmOptions.start().weighting(weighting).algorithm(algoStr).build();
                        testCollector.assertDistance(new AlgoHelperEntry(g, opts, idx, algoStr + "|" + weighting), oneRun.getList(idx, filter), oneRun);
                        integ.addAndGet(1);
                    }
                };
                t.start();
                threads.add(t);
            }
        }
    }
    for (Thread t : threads) {
        try {
            t.join();
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    }
    assertEquals(MAX * algosLength * instances.size(), integ.get());
    assertEquals(testCollector.toString(), 0, testCollector.errors.size());
    hopper.close();
}
Also used : AlgoHelperEntry(com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry) ArrayList(java.util.ArrayList) GraphHopperOSM(com.graphhopper.reader.osm.GraphHopperOSM) OneRun(com.graphhopper.routing.util.TestAlgoCollector.OneRun) GraphHopper(com.graphhopper.GraphHopper) LocationIndex(com.graphhopper.storage.index.LocationIndex) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Graph(com.graphhopper.storage.Graph) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) Test(org.junit.Test)

Example 2 with AlgoHelperEntry

use of com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry in project graphhopper by graphhopper.

the class RoutingAlgorithmIT method createAlgos.

public static List<AlgoHelperEntry> createAlgos(final GraphHopper hopper, final HintsMap hints, TraversalMode tMode) {
    GraphHopperStorage ghStorage = hopper.getGraphHopperStorage();
    LocationIndex idx = hopper.getLocationIndex();
    String addStr = "";
    if (tMode.isEdgeBased())
        addStr = "turn|";
    FlagEncoder encoder = hopper.getEncodingManager().getEncoder(hints.getVehicle());
    Weighting weighting = hopper.createWeighting(hints, encoder, hopper.getGraphHopperStorage());
    HintsMap defaultHints = new HintsMap().put(Parameters.CH.DISABLE, true).put(Parameters.Landmark.DISABLE, true).setVehicle(hints.getVehicle()).setWeighting(hints.getWeighting());
    AlgorithmOptions defaultOpts = AlgorithmOptions.start(new AlgorithmOptions("", weighting, tMode)).hints(defaultHints).build();
    List<AlgoHelperEntry> prepare = new ArrayList<>();
    prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(ASTAR).build(), idx, "astar|beeline|" + addStr + weighting));
    // later: include dijkstraOneToMany
    prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA).build(), idx, "dijkstra|" + addStr + weighting));
    AlgorithmOptions astarbiOpts = AlgorithmOptions.start(defaultOpts).algorithm(ASTAR_BI).build();
    astarbiOpts.getHints().put(ASTAR_BI + ".approximation", "BeelineSimplification");
    AlgorithmOptions dijkstrabiOpts = AlgorithmOptions.start(defaultOpts).algorithm(DIJKSTRA_BI).build();
    prepare.add(new AlgoHelperEntry(ghStorage, astarbiOpts, idx, "astarbi|beeline|" + addStr + weighting));
    prepare.add(new AlgoHelperEntry(ghStorage, dijkstrabiOpts, idx, "dijkstrabi|" + addStr + weighting));
    // add additional preparations if CH and LM preparation are enabled
    if (hopper.getLMFactoryDecorator().isEnabled()) {
        final HintsMap lmHints = new HintsMap(defaultHints).put(Parameters.Landmark.DISABLE, false);
        prepare.add(new AlgoHelperEntry(ghStorage, AlgorithmOptions.start(astarbiOpts).hints(lmHints).build(), idx, "astarbi|landmarks|" + weighting) {

            @Override
            public RoutingAlgorithmFactory createRoutingFactory() {
                return hopper.getAlgorithmFactory(lmHints);
            }
        });
    }
    if (hopper.getCHFactoryDecorator().isEnabled()) {
        final HintsMap chHints = new HintsMap(defaultHints).put(Parameters.CH.DISABLE, false);
        Weighting pickedWeighting = null;
        for (Weighting tmpWeighting : hopper.getCHFactoryDecorator().getWeightings()) {
            if (tmpWeighting.equals(weighting)) {
                pickedWeighting = tmpWeighting;
                break;
            }
        }
        if (pickedWeighting == null)
            throw new IllegalStateException("Didn't find weighting " + hints.getWeighting() + " in " + hopper.getCHFactoryDecorator().getWeightings());
        prepare.add(new AlgoHelperEntry(ghStorage.getGraph(CHGraph.class, pickedWeighting), AlgorithmOptions.start(dijkstrabiOpts).hints(chHints).build(), idx, "dijkstrabi|ch|prepare|" + hints.getWeighting()) {

            @Override
            public RoutingAlgorithmFactory createRoutingFactory() {
                return hopper.getAlgorithmFactory(chHints);
            }
        });
        prepare.add(new AlgoHelperEntry(ghStorage.getGraph(CHGraph.class, pickedWeighting), AlgorithmOptions.start(astarbiOpts).hints(chHints).build(), idx, "astarbi|ch|prepare|" + hints.getWeighting()) {

            @Override
            public RoutingAlgorithmFactory createRoutingFactory() {
                return hopper.getAlgorithmFactory(chHints);
            }
        });
    }
    return prepare;
}
Also used : HintsMap(com.graphhopper.routing.util.HintsMap) AlgoHelperEntry(com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) ArrayList(java.util.ArrayList) LocationIndex(com.graphhopper.storage.index.LocationIndex) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Weighting(com.graphhopper.routing.weighting.Weighting)

Example 3 with AlgoHelperEntry

use of com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry in project graphhopper by graphhopper.

the class RoutingAlgorithmWithOSMIT method runAlgo.

/**
     * @param withCH if true also the CH and LM algorithms will be tested which need
     *               preparation and takes a bit longer
     */
Graph runAlgo(TestAlgoCollector testCollector, String osmFile, String graphFile, List<OneRun> forEveryAlgo, String importVehicles, boolean withCH, String vehicle, String weightStr, boolean is3D) {
    // for different weightings we need a different storage, otherwise we would need to remove the graph folder
    // everytime we come with a different weighting
    // graphFile += weightStr;
    AlgoHelperEntry algoEntry = null;
    OneRun tmpOneRun = null;
    try {
        Helper.removeDir(new File(graphFile));
        GraphHopper hopper = new GraphHopperOSM().setStoreOnFlush(true).setCHEnabled(false).setDataReaderFile(osmFile).setGraphHopperLocation(graphFile).setEncodingManager(new EncodingManager(importVehicles));
        // avoid that path.getDistance is too different to path.getPoint.calcDistance
        hopper.setWayPointMaxDistance(0);
        // always enable landmarks
        hopper.getLMFactoryDecorator().addWeighting(weightStr).setEnabled(true).setDisablingAllowed(true);
        if (withCH)
            hopper.getCHFactoryDecorator().addWeighting(weightStr).setEnabled(true).setDisablingAllowed(true);
        if (is3D)
            hopper.setElevationProvider(new SRTMProvider().setCacheDir(new File(DIR)));
        hopper.importOrLoad();
        TraversalMode tMode = importVehicles.contains("turn_costs=true") ? TraversalMode.EDGE_BASED_2DIR : TraversalMode.NODE_BASED;
        FlagEncoder encoder = hopper.getEncodingManager().getEncoder(vehicle);
        HintsMap hints = new HintsMap().setWeighting(weightStr).setVehicle(vehicle);
        Collection<AlgoHelperEntry> prepares = RoutingAlgorithmIT.createAlgos(hopper, hints, tMode);
        EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
        for (AlgoHelperEntry entry : prepares) {
            algoEntry = entry;
            LocationIndex idx = entry.getIdx();
            for (OneRun oneRun : forEveryAlgo) {
                tmpOneRun = oneRun;
                List<QueryResult> list = oneRun.getList(idx, edgeFilter);
                testCollector.assertDistance(algoEntry, list, oneRun);
            }
        }
        return hopper.getGraphHopperStorage();
    } catch (Exception ex) {
        if (algoEntry == null)
            throw new RuntimeException("cannot handle file " + osmFile + ", " + ex.getMessage(), ex);
        throw new RuntimeException("cannot handle " + algoEntry.toString() + ", for " + tmpOneRun + ", file " + osmFile + ", " + ex.getMessage(), ex);
    } finally {
    // Helper.removeDir(new File(graphFile));
    }
}
Also used : AlgoHelperEntry(com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry) OneRun(com.graphhopper.routing.util.TestAlgoCollector.OneRun) GraphHopperOSM(com.graphhopper.reader.osm.GraphHopperOSM) GraphHopper(com.graphhopper.GraphHopper) LocationIndex(com.graphhopper.storage.index.LocationIndex) IOException(java.io.IOException) SRTMProvider(com.graphhopper.reader.dem.SRTMProvider) QueryResult(com.graphhopper.storage.index.QueryResult) File(java.io.File)

Example 4 with AlgoHelperEntry

use of com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry in project graphhopper by graphhopper.

the class RoutingAlgorithmIT method testPerformance.

@Test
public void testPerformance() throws IOException {
    int N = 10;
    int noJvmWarming = N / 4;
    Random rand = new Random(0);
    final EncodingManager eManager = new EncodingManager("car");
    final GraphHopperStorage graph = new GraphBuilder(eManager).create();
    String bigFile = "10000EWD.txt.gz";
    new PrinctonReader(graph).setStream(new GZIPInputStream(PrinctonReader.class.getResourceAsStream(bigFile))).read();
    GraphHopper hopper = new GraphHopper() {

        {
            setCHEnabled(false);
            setEncodingManager(eManager);
            loadGraph(graph);
        }

        @Override
        protected LocationIndex createLocationIndex(Directory dir) {
            return new LocationIndexTree(graph, dir);
        }
    };
    Collection<AlgoHelperEntry> prepares = createAlgos(hopper, new HintsMap().setWeighting("shortest").setVehicle("car"), TraversalMode.NODE_BASED);
    for (AlgoHelperEntry entry : prepares) {
        StopWatch sw = new StopWatch();
        for (int i = 0; i < N; i++) {
            int node1 = Math.abs(rand.nextInt(graph.getNodes()));
            int node2 = Math.abs(rand.nextInt(graph.getNodes()));
            RoutingAlgorithm d = entry.createRoutingFactory().createAlgo(graph, entry.getAlgorithmOptions());
            if (i >= noJvmWarming)
                sw.start();
            Path p = d.calcPath(node1, node2);
            // avoid jvm optimization => call p.distance
            if (i >= noJvmWarming && p.getDistance() > -1)
                sw.stop();
        // System.out.println("#" + i + " " + name + ":" + sw.getSeconds() + " " + p.nodes());
        }
        float perRun = sw.stop().getSeconds() / ((float) (N - noJvmWarming));
        System.out.println("# " + getClass().getSimpleName() + " " + entry + ":" + sw.stop().getSeconds() + ", per run:" + perRun);
        assertTrue("speed to low!? " + perRun + " per run", perRun < 0.08);
    }
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) HintsMap(com.graphhopper.routing.util.HintsMap) AlgoHelperEntry(com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry) GraphHopper(com.graphhopper.GraphHopper) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) StopWatch(com.graphhopper.util.StopWatch) GZIPInputStream(java.util.zip.GZIPInputStream) Random(java.util.Random) PrinctonReader(com.graphhopper.reader.PrinctonReader) GraphBuilder(com.graphhopper.storage.GraphBuilder) Directory(com.graphhopper.storage.Directory) Test(org.junit.Test)

Aggregations

AlgoHelperEntry (com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry)4 GraphHopper (com.graphhopper.GraphHopper)3 LocationIndex (com.graphhopper.storage.index.LocationIndex)3 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)2 HintsMap (com.graphhopper.routing.util.HintsMap)2 OneRun (com.graphhopper.routing.util.TestAlgoCollector.OneRun)2 Weighting (com.graphhopper.routing.weighting.Weighting)2 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 PrinctonReader (com.graphhopper.reader.PrinctonReader)1 SRTMProvider (com.graphhopper.reader.dem.SRTMProvider)1 EncodingManager (com.graphhopper.routing.util.EncodingManager)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)1 Directory (com.graphhopper.storage.Directory)1 Graph (com.graphhopper.storage.Graph)1 GraphBuilder (com.graphhopper.storage.GraphBuilder)1 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)1