コード例 #1
0
 @Test
 public void testUnpackingOrder_Fastest() {
   LevelGraphStorage g = (LevelGraphStorage) createGraph();
   PrepareContractionHierarchies prepare = new PrepareContractionHierarchies().graph(g);
   WeightCalculation calc = new FastestCalc(carEncoder);
   initUnpackingGraph(g, calc);
   RoutingAlgorithm algo = prepare.type(calc).vehicle(carEncoder).createAlgo();
   Path p = algo.calcPath(10, 6);
   assertEquals(7, p.distance(), 1e-1);
   assertEquals(Helper.createTList(10, 0, 1, 2, 3, 4, 5, 6), p.calcNodes());
 }
コード例 #2
0
 @Test
 public void testRoundaboutUnpacking() {
   LevelGraph g = createGraph();
   initRoundaboutGraph(g);
   int old = g.getAllEdges().maxId();
   PrepareContractionHierarchies prepare = new PrepareContractionHierarchies().graph(g);
   prepare.doWork();
   assertEquals(old + 25, g.getAllEdges().maxId());
   RoutingAlgorithm algo = prepare.createAlgo();
   Path p = algo.calcPath(4, 7);
   assertEquals(Helper.createTList(4, 5, 6, 7), p.calcNodes());
 }
コード例 #3
0
  @Test
  public void testDirectedGraph2() {
    LevelGraph g = createGraph();
    initDirected2(g);
    int old = GHUtility.count(g.getAllEdges());
    PrepareContractionHierarchies prepare = new PrepareContractionHierarchies().graph(g);
    prepare.doWork();
    // PrepareTowerNodesShortcutsTest.printEdges(g);
    assertEquals(old + 15, GHUtility.count(g.getAllEdges()));
    RoutingAlgorithm algo = prepare.createAlgo();

    Path p = algo.calcPath(0, 10);
    assertEquals(10, p.distance(), 1e-6);
    assertEquals(Helper.createTList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), p.calcNodes());
  }
コード例 #4
0
 @Test
 public void testDirectedGraph() {
   LevelGraph g = createGraph();
   g.edge(5, 4, 3, false);
   g.edge(4, 5, 10, false);
   g.edge(2, 4, 1, false);
   g.edge(5, 2, 1, false);
   g.edge(3, 5, 1, false);
   g.edge(4, 3, 1, false);
   int old = GHUtility.count(g.getAllEdges());
   PrepareContractionHierarchies prepare = new PrepareContractionHierarchies().graph(g);
   prepare.doWork();
   // PrepareTowerNodesShortcutsTest.printEdges(g);
   assertEquals(old + 3, GHUtility.count(g.getAllEdges()));
   RoutingAlgorithm algo = prepare.createAlgo();
   Path p = algo.calcPath(4, 2);
   assertEquals(3, p.distance(), 1e-6);
   assertEquals(Helper.createTList(4, 3, 5, 2), p.calcNodes());
 }
コード例 #5
0
  public TestAlgoCollector assertDistance(
      AlgoHelperEntry algoEntry, List<QueryResult> queryList, OneRun oneRun) {
    List<Path> altPaths = new ArrayList<Path>();
    QueryGraph queryGraph = new QueryGraph(algoEntry.getQueryGraph());
    queryGraph.lookup(queryList);
    AlgorithmOptions opts = algoEntry.opts;
    FlagEncoder encoder = opts.getFlagEncoder();
    if (encoder.supports(TurnWeighting.class))
      algoEntry.setAlgorithmOptions(
          AlgorithmOptions.start(opts)
              .weighting(
                  new TurnWeighting(
                      opts.getWeighting(),
                      opts.getFlagEncoder(),
                      (TurnCostExtension) queryGraph.getExtension()))
              .build());

    for (int i = 0; i < queryList.size() - 1; i++) {
      RoutingAlgorithm algo = algoEntry.createAlgo(queryGraph);
      Path path =
          algo.calcPath(queryList.get(i).getClosestNode(), queryList.get(i + 1).getClosestNode());
      // System.out.println(path.calcInstructions().createGPX("temp", 0, "GMT"));
      altPaths.add(path);
    }

    PathMerger pathMerger =
        new PathMerger().setCalcPoints(true).setSimplifyResponse(false).setEnableInstructions(true);
    AltResponse rsp = new AltResponse();
    pathMerger.doWork(rsp, altPaths, trMap.getWithFallBack(Locale.US));

    if (rsp.hasErrors()) {
      errors.add(
          algoEntry
              + " response contains errors. Expected distance: "
              + rsp.getDistance()
              + ", expected points: "
              + oneRun
              + ". "
              + queryList
              + ", errors:"
              + rsp.getErrors());
      return this;
    }

    PointList pointList = rsp.getPoints();
    double tmpDist = pointList.calcDistance(distCalc);
    if (Math.abs(rsp.getDistance() - tmpDist) > 2) {
      errors.add(
          algoEntry
              + " path.getDistance was  "
              + rsp.getDistance()
              + "\t pointList.calcDistance was "
              + tmpDist
              + "\t (expected points "
              + oneRun.getLocs()
              + ", expected distance "
              + oneRun.getDistance()
              + ") "
              + queryList);
    }

    if (Math.abs(rsp.getDistance() - oneRun.getDistance()) > 2) {
      errors.add(
          algoEntry
              + " returns path not matching the expected distance of "
              + oneRun.getDistance()
              + "\t Returned was "
              + rsp.getDistance()
              + "\t (expected points "
              + oneRun.getLocs()
              + ", was "
              + pointList.getSize()
              + ") "
              + queryList);
    }

    // There are real world instances where A-B-C is identical to A-C (in meter precision).
    if (Math.abs(pointList.getSize() - oneRun.getLocs()) > 1) {
      errors.add(
          algoEntry
              + " returns path not matching the expected points of "
              + oneRun.getLocs()
              + "\t Returned was "
              + pointList.getSize()
              + "\t (expected distance "
              + oneRun.getDistance()
              + ", was "
              + rsp.getDistance()
              + ") "
              + queryList);
    }
    return this;
  }
コード例 #6
0
ファイル: GraphHopper.java プロジェクト: rchampa/graphhopper
  @Override
  public GHResponse route(GHRequest request) {
    request.check();
    StopWatch sw = new StopWatch().start();
    GHResponse rsp = new GHResponse();

    if (!setSupportsVehicle(request.getVehicle())) {
      rsp.addError(
          new IllegalArgumentException(
              "Vehicle "
                  + request.getVehicle()
                  + " unsupported. Supported are: "
                  + getEncodingManager()));
      return rsp;
    }

    EdgeFilter edgeFilter = new DefaultEdgeFilter(encodingManager.getEncoder(request.getVehicle()));
    int from =
        index
            .findClosest(request.getFrom().lat, request.getFrom().lon, edgeFilter)
            .getClosestNode();
    int to =
        index.findClosest(request.getTo().lat, request.getTo().lon, edgeFilter).getClosestNode();
    String debug = "idLookup:" + sw.stop().getSeconds() + "s";

    if (from < 0)
      rsp.addError(new IllegalArgumentException("Cannot find point 1: " + request.getFrom()));

    if (to < 0)
      rsp.addError(new IllegalArgumentException("Cannot find point 2: " + request.getTo()));

    if (from == to) rsp.addError(new IllegalArgumentException("Point 1 is equal to point 2"));

    sw = new StopWatch().start();
    RoutingAlgorithm algo = null;

    if (chUsage) {
      if (request.getAlgorithm().equals("dijkstrabi")) algo = prepare.createAlgo();
      else if (request.getAlgorithm().equals("astarbi"))
        algo = ((PrepareContractionHierarchies) prepare).createAStar();
      else
        rsp.addError(
            new IllegalStateException(
                "Only dijkstrabi and astarbi is supported for LevelGraph (using contraction hierarchies)!"));

    } else {
      prepare =
          NoOpAlgorithmPreparation.createAlgoPrepare(
              graph,
              request.getAlgorithm(),
              encodingManager.getEncoder(request.getVehicle()),
              request.getType());
      algo = prepare.createAlgo();
    }

    if (rsp.hasErrors()) {
      return rsp;
    }
    debug += ", algoInit:" + sw.stop().getSeconds() + "s";

    sw = new StopWatch().start();
    Path path = algo.calcPath(from, to);
    debug +=
        ", "
            + algo.getName()
            + "-routing:"
            + sw.stop().getSeconds()
            + "s"
            + ", "
            + path.getDebugInfo();
    PointList points = path.calcPoints();
    simplifyRequest = request.getHint("simplifyRequest", simplifyRequest);
    if (simplifyRequest) {
      sw = new StopWatch().start();
      int orig = points.getSize();
      double minPathPrecision = request.getHint("douglas.minprecision", 1d);
      if (minPathPrecision > 0) {
        new DouglasPeucker().setMaxDistance(minPathPrecision).simplify(points);
      }
      debug +=
          ", simplify (" + orig + "->" + points.getSize() + "):" + sw.stop().getSeconds() + "s";
    }

    enableInstructions = request.getHint("instructions", enableInstructions);
    if (enableInstructions) {
      sw = new StopWatch().start();
      rsp.setInstructions(path.calcInstructions());
      debug += ", instructions:" + sw.stop().getSeconds() + "s";
    }
    return rsp.setPoints(points)
        .setDistance(path.getDistance())
        .setTime(path.getTime())
        .setDebugInfo(debug);
  }
コード例 #7
0
  public void runShortestPathPerf(int runs, RoutingAlgorithm algo) throws Exception {
    BBox bbox = unterfrankenGraph.getBounds();
    double minLat = bbox.minLat, minLon = bbox.minLon;
    double maxLat = bbox.maxLat, maxLon = bbox.maxLon;
    if (unterfrankenGraph instanceof LevelGraph) {
      if (algo instanceof DijkstraBidirectionRef)
        algo = new PrepareContractionHierarchies().setGraph(unterfrankenGraph).createAlgo();
      //                algo = new
      // PrepareSimpleShortcuts().setGraph(unterfrankenGraph).createAlgo();
      else if (algo instanceof AStarBidirection)
        algo = new PrepareSimpleShortcuts().setGraph(unterfrankenGraph).createAStar();
      else
        // level graph accepts all algorithms but normally we want to use an optimized one
        throw new IllegalStateException(
            "algorithm which boosts query time for levelgraph not found " + algo);
      logger.info("[experimental] using shortcuts with " + algo);
    } else logger.info("running " + algo);

    Random rand = new Random(123);
    StopWatch sw = new StopWatch();

    // System.out.println("cap:" + ((GraphStorage) unterfrankenGraph).capacity());
    for (int i = 0; i < runs; i++) {
      double fromLat = rand.nextDouble() * (maxLat - minLat) + minLat;
      double fromLon = rand.nextDouble() * (maxLon - minLon) + minLon;
      //            sw.start();
      int from = idx.findID(fromLat, fromLon);
      double toLat = rand.nextDouble() * (maxLat - minLat) + minLat;
      double toLon = rand.nextDouble() * (maxLon - minLon) + minLon;
      int to = idx.findID(toLat, toLon);
      //            sw.stop();
      //                logger.info(i + " " + sw + " from (" + from + ")" + fromLat + ", " + fromLon
      // + " to (" + to + ")" + toLat + ", " + toLon);
      if (from == to) {
        logger.warn("skipping i " + i + " from==to " + from);
        continue;
      }

      algo.clear();
      sw.start();
      Path p = algo.calcPath(from, to);
      sw.stop();
      if (!p.found()) {
        // there are still paths not found cause of oneway motorways => only routable in one
        // direction
        // e.g. unterfrankenGraph.getLatitude(798809) + "," + unterfrankenGraph.getLongitude(798809)
        logger.warn(
            "no route found for i="
                + i
                + " !? "
                + "graph-from "
                + from
                + "("
                + fromLat
                + ","
                + fromLon
                + "), "
                + "graph-to "
                + to
                + "("
                + toLat
                + ","
                + toLon
                + ")");
        continue;
      }
      if (i % 20 == 0)
        logger.info(i + " " + sw.getSeconds() / (i + 1) + " secs/run"); // (" + p + ")");
    }
  }