Search in sources :

Example 61 with IntArrayList

use of com.carrotsearch.hppc.IntArrayList in project graphhopper by graphhopper.

the class ArrayUtil method permutation.

/**
 * Creates an IntArrayList filled with a permutation of the numbers 0,1,2,...,size-1
 */
public static IntArrayList permutation(int size, Random rnd) {
    IntArrayList result = iota(size);
    shuffle(result, rnd);
    return result;
}
Also used : IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 62 with IntArrayList

use of com.carrotsearch.hppc.IntArrayList in project graphhopper by graphhopper.

the class ArrayUtil method zero.

/**
 * Creates an IntArrayList filled with zeros
 */
public static IntArrayList zero(int size) {
    IntArrayList result = new IntArrayList(size);
    result.elementsCount = size;
    return result;
}
Also used : IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 63 with IntArrayList

use of com.carrotsearch.hppc.IntArrayList in project graphhopper by graphhopper.

the class GHUtility method shuffle.

public static Graph shuffle(Graph g, Graph sortedGraph) {
    if (g.getTurnCostStorage() != null)
        throw new IllegalArgumentException("Shuffling the graph is currently not supported in the presence of turn costs");
    IntArrayList nodes = ArrayUtil.permutation(g.getNodes(), new Random());
    IntArrayList edges = ArrayUtil.permutation(g.getEdges(), new Random());
    return createSortedGraph(g, sortedGraph, nodes, edges);
}
Also used : IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 64 with IntArrayList

use of com.carrotsearch.hppc.IntArrayList in project graphhopper by graphhopper.

the class TarjanSCCTest method test481.

@Test
public void test481() {
    // 0->1->3->4->5->6->7
    // \ |      \<-----/
    // 2
    GraphHopperStorage graph = new GraphBuilder(em).create();
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(0, 1).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 2).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 0).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 3).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 4).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(4, 5).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(5, 6).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(6, 7).setDistance(1));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(7, 4).setDistance(1));
    TarjanSCC.ConnectedComponents scc = TarjanSCC.findComponentsRecursive(graph, edgeFilter, false);
    List<IntArrayList> components = scc.getComponents();
    assertEquals(3, scc.getTotalComponents());
    assertEquals(2, components.size());
    assertEquals(IntArrayList.from(2, 1, 0), components.get(1));
    assertEquals(IntArrayList.from(7, 6, 5, 4), components.get(0));
    assertEquals(1, scc.getSingleNodeComponents().cardinality());
    assertTrue(scc.getSingleNodeComponents().get(3));
    assertEquals(8, scc.getNodes());
    assertEquals(components.get(0), scc.getBiggestComponent());
    // exclude single
    scc = TarjanSCC.findComponentsRecursive(graph, edgeFilter, true);
    assertTrue(scc.getSingleNodeComponents().isEmpty());
    assertEquals(3, scc.getTotalComponents());
    assertEquals(2, scc.getComponents().size());
    assertEquals(8, scc.getNodes());
}
Also used : GraphBuilder(com.graphhopper.storage.GraphBuilder) IntArrayList(com.carrotsearch.hppc.IntArrayList) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 65 with IntArrayList

use of com.carrotsearch.hppc.IntArrayList in project graphhopper by graphhopper.

the class Measurement method measureRouting.

private void measureRouting(final GraphHopper hopper, final QuerySettings querySettings) {
    final Graph g = hopper.getGraphHopperStorage();
    final AtomicLong maxDistance = new AtomicLong(0);
    final AtomicLong minDistance = new AtomicLong(Long.MAX_VALUE);
    final AtomicLong distSum = new AtomicLong(0);
    final AtomicLong airDistSum = new AtomicLong(0);
    final AtomicLong altCount = new AtomicLong(0);
    final AtomicInteger failedCount = new AtomicInteger(0);
    final DistanceCalc distCalc = new DistanceCalcEarth();
    String profileName = querySettings.edgeBased ? "profile_tc" : "profile_no_tc";
    Weighting weighting = hopper.createWeighting(hopper.getProfile(profileName), new PMap());
    final EdgeFilter edgeFilter = new DefaultSnapFilter(weighting, hopper.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profileName)));
    final EdgeExplorer edgeExplorer = g.createEdgeExplorer(edgeFilter);
    final AtomicLong visitedNodesSum = new AtomicLong(0);
    final AtomicLong maxVisitedNodes = new AtomicLong(0);
    final Random rand = new Random(seed);
    final NodeAccess na = g.getNodeAccess();
    MiniPerfTest miniPerf = new MiniPerfTest().setIterations(querySettings.count).start((warmup, run) -> {
        GHRequest req = new GHRequest(querySettings.points);
        IntArrayList nodes = new IntArrayList(querySettings.points);
        // we try a few times to find points that do not lie within our blocked area
        for (int i = 0; i < 5; i++) {
            nodes.clear();
            List<GHPoint> points = new ArrayList<>();
            List<String> pointHints = new ArrayList<>();
            int tries = 0;
            while (nodes.size() < querySettings.points) {
                int node = rand.nextInt(maxNode);
                if (++tries > g.getNodes())
                    throw new RuntimeException("Could not find accessible points");
                // probe location. it could be a pedestrian area or an edge removed in the subnetwork removal process
                if (GHUtility.count(edgeExplorer.setBaseNode(node)) == 0)
                    continue;
                nodes.add(node);
                points.add(new GHPoint(na.getLat(node), na.getLon(node)));
                if (querySettings.withPointHints) {
                    // we add some point hint to make sure the name similarity filter has to do some actual work
                    pointHints.add("probably_not_found");
                }
            }
            req.setPoints(points);
            req.setPointHints(pointHints);
            if (querySettings.blockArea == null)
                break;
            try {
                req.getHints().putObject(BLOCK_AREA, querySettings.blockArea);
                // run this method to check if creating the blocked area is possible
                GraphEdgeIdFinder.createBlockArea(hopper.getGraphHopperStorage(), hopper.getLocationIndex(), req.getPoints(), req.getHints(), edgeFilter);
                break;
            } catch (IllegalArgumentException ex) {
                if (i >= 4)
                    throw new RuntimeException("Give up after 5 tries. Cannot find points outside of the block_area " + querySettings.blockArea + " - too big block_area or map too small? Request:" + req);
            }
        }
        req.setProfile(profileName);
        req.getHints().putObject(CH.DISABLE, !querySettings.ch).putObject("stall_on_demand", querySettings.sod).putObject(Landmark.DISABLE, !querySettings.lm).putObject(Landmark.ACTIVE_COUNT, querySettings.activeLandmarks).putObject("instructions", querySettings.withInstructions);
        if (querySettings.alternative)
            req.setAlgorithm(ALT_ROUTE);
        if (querySettings.pathDetails)
            req.setPathDetails(Arrays.asList(Parameters.Details.AVERAGE_SPEED, Parameters.Details.EDGE_ID, Parameters.Details.STREET_NAME));
        if (!querySettings.simplify)
            req.getHints().putObject(Parameters.Routing.WAY_POINT_MAX_DISTANCE, 0);
        GHResponse rsp;
        try {
            rsp = hopper.route(req);
        } catch (Exception ex) {
            // 'not found' can happen if import creates more than one subnetwork
            throw new RuntimeException("Error while calculating route! nodes: " + nodes + ", request:" + req, ex);
        }
        if (rsp.hasErrors()) {
            if (!warmup)
                failedCount.incrementAndGet();
            if (rsp.getErrors().get(0).getMessage() == null)
                rsp.getErrors().get(0).printStackTrace();
            else if (!toLowerCase(rsp.getErrors().get(0).getMessage()).contains("not found")) {
                if (stopOnError)
                    throw new RuntimeException("errors should NOT happen in Measurement! " + req + " => " + rsp.getErrors());
                else
                    logger.error("errors should NOT happen in Measurement! " + req + " => " + rsp.getErrors());
            }
            return 0;
        }
        ResponsePath responsePath = rsp.getBest();
        if (!warmup) {
            long visitedNodes = rsp.getHints().getLong("visited_nodes.sum", 0);
            visitedNodesSum.addAndGet(visitedNodes);
            if (visitedNodes > maxVisitedNodes.get()) {
                maxVisitedNodes.set(visitedNodes);
            }
            long dist = (long) responsePath.getDistance();
            distSum.addAndGet(dist);
            GHPoint prev = req.getPoints().get(0);
            for (GHPoint point : req.getPoints()) {
                airDistSum.addAndGet((long) distCalc.calcDist(prev.getLat(), prev.getLon(), point.getLat(), point.getLon()));
                prev = point;
            }
            if (dist > maxDistance.get())
                maxDistance.set(dist);
            if (dist < minDistance.get())
                minDistance.set(dist);
            if (querySettings.alternative)
                altCount.addAndGet(rsp.getAll().size());
        }
        return responsePath.getPoints().size();
    });
    int count = querySettings.count - failedCount.get();
    if (count == 0)
        throw new RuntimeException("All requests failed, something must be wrong: " + failedCount.get());
    // if using non-bidirectional algorithm make sure you exclude CH routing
    String algoStr = (querySettings.ch && !querySettings.edgeBased) ? Algorithms.DIJKSTRA_BI : Algorithms.ASTAR_BI;
    if (querySettings.ch && !querySettings.sod) {
        algoStr += "_no_sod";
    }
    String prefix = querySettings.prefix;
    put(prefix + ".guessed_algorithm", algoStr);
    put(prefix + ".failed_count", failedCount.get());
    put(prefix + ".distance_min", minDistance.get());
    put(prefix + ".distance_mean", (float) distSum.get() / count);
    put(prefix + ".air_distance_mean", (float) airDistSum.get() / count);
    put(prefix + ".distance_max", maxDistance.get());
    put(prefix + ".visited_nodes_mean", (float) visitedNodesSum.get() / count);
    put(prefix + ".visited_nodes_max", (float) maxVisitedNodes.get());
    put(prefix + ".alternative_rate", (float) altCount.get() / count);
    print(prefix, miniPerf);
}
Also used : IntArrayList(com.carrotsearch.hppc.IntArrayList) GHPoint(com.graphhopper.util.shapes.GHPoint) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Weighting(com.graphhopper.routing.weighting.Weighting) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntArrayList(com.carrotsearch.hppc.IntArrayList) GHPoint(com.graphhopper.util.shapes.GHPoint)

Aggregations

IntArrayList (com.carrotsearch.hppc.IntArrayList)94 Test (org.junit.jupiter.api.Test)16 RepeatedTest (org.junit.jupiter.api.RepeatedTest)13 GHPoint (com.graphhopper.util.shapes.GHPoint)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)7 IntObjectHashMap (com.carrotsearch.hppc.IntObjectHashMap)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)5 HashMap (java.util.HashMap)5 UUID (java.util.UUID)5 GHIntArrayList (com.graphhopper.coll.GHIntArrayList)4 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)4 RelationName (io.crate.metadata.RelationName)4 IOException (java.io.IOException)4 IntObjectMap (com.carrotsearch.hppc.IntObjectMap)3 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)3 PrepEdgeFilter (com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks.PrepEdgeFilter)3 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)3