@Test
  public void testDifferentEdgeFilter() {
    Graph g = new GraphBuilder(encodingManager).levelGraphCreate();
    g.edge(4, 3, 10, true);
    g.edge(3, 6, 10, true);

    g.edge(4, 5, 10, true);
    g.edge(5, 6, 10, true);

    AlgorithmPreparation prep = prepareGraph(g);
    DijkstraOneToMany algo = (DijkstraOneToMany) prep.createAlgo();
    algo.setEdgeFilter(
        new EdgeFilter() {
          @Override
          public boolean accept(EdgeIteratorState iter) {
            return iter.getAdjNode() != 5;
          }
        });
    Path p = algo.calcPath(4, 6);
    assertEquals(Helper.createTList(4, 3, 6), p.calcNodes());

    // important call!
    algo.clear();
    algo.setEdgeFilter(
        new EdgeFilter() {
          @Override
          public boolean accept(EdgeIteratorState iter) {
            return iter.getAdjNode() != 3;
          }
        });
    p = algo.calcPath(4, 6);
    assertEquals(Helper.createTList(4, 5, 6), p.calcNodes());
  }
  @Test
  public void testUseCache() {
    AlgorithmPreparation prep = prepareGraph(createTestGraph());
    RoutingAlgorithm algo = prep.createAlgo();
    Path p = algo.calcPath(0, 4);
    assertEquals(Helper.createTList(0, 4), p.calcNodes());

    // expand SPT
    p = algo.calcPath(0, 7);
    assertEquals(Helper.createTList(0, 4, 6, 5, 7), p.calcNodes());

    // use SPT
    p = algo.calcPath(0, 2);
    assertEquals(Helper.createTList(0, 1, 2), p.calcNodes());
  }
Exemplo n.º 3
0
 public void prepare() {
   boolean tmpPrepare = doPrepare && prepare != null;
   graph.getProperties().put("prepare.done", tmpPrepare);
   if (tmpPrepare) {
     if (prepare instanceof PrepareContractionHierarchies
         && encodingManager.getVehicleCount() > 1) {
       throw new IllegalArgumentException(
           "Contraction hierarchies preparation "
               + "requires (at the moment) only one vehicle. But was:"
               + encodingManager);
     }
     logger.info("calling prepare.doWork ... (" + Helper.getMemInfo() + ")");
     prepare.doWork();
   }
 }
Exemplo n.º 4
0
  private void postProcessing() {
    encodingManager = graph.getEncodingManager();
    if (chUsage) {
      PrepareContractionHierarchies tmpPrepareCH = new PrepareContractionHierarchies();
      FlagEncoder encoder = encodingManager.getSingle();
      if (chFast) tmpPrepareCH.setType(new FastestCalc(encoder));
      else tmpPrepareCH.setType(new ShortestCalc());

      tmpPrepareCH.setVehicle(encoder);
      tmpPrepareCH
          .setPeriodicUpdates(periodicUpdates)
          .setLazyUpdates(lazyUpdates)
          .setNeighborUpdates(neighborUpdates);

      prepare = tmpPrepareCH;
      prepare.setGraph(graph);
    }

    if ("false".equals(graph.getProperties().get("prepare.done"))) prepare();
  }
Exemplo n.º 5
0
  @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);
  }