示例#1
0
  @Override
  protected Set<Tuple<MatcherCandidate, Double>> candidates(
      Set<MatcherCandidate> predecessors, MatcherSample sample) {
    if (logger.isTraceEnabled()) {
      logger.trace(
          "finding candidates for sample {} {}",
          new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ").format(sample.time()),
          GeometryEngine.geometryToWkt(sample.point(), WktExportFlags.wktExportPoint));
    }

    Set<RoadPoint> points_ = map.spatial().radius(sample.point(), radius);
    Set<RoadPoint> points = new HashSet<RoadPoint>(Minset.minimize(points_));

    Map<Long, RoadPoint> map = new HashMap<Long, RoadPoint>();
    for (RoadPoint point : points) {
      map.put(point.edge().id(), point);
    }

    for (MatcherCandidate predecessor : predecessors) {
      RoadPoint point = map.get(predecessor.point().edge().id());
      if (point != null && point.fraction() < predecessor.point().fraction()) {
        points.remove(point);
        points.add(predecessor.point());
      }
    }

    Set<Tuple<MatcherCandidate, Double>> candidates =
        new HashSet<Tuple<MatcherCandidate, Double>>();

    logger.debug("{} ({}) candidates", points.size(), points_.size());

    for (RoadPoint point : points) {
      double dz = spatial.distance(sample.point(), point.geometry());
      double emission = 1 / sqrt_2pi_sig2 * Math.exp((-1) * dz / (2 * sig2));

      MatcherCandidate candidate = new MatcherCandidate(point);
      candidates.add(new Tuple<MatcherCandidate, Double>(candidate, emission));

      logger.trace("{} {} {}", candidate.id(), dz, emission);
    }

    return candidates;
  }
示例#2
0
  @Override
  protected Map<MatcherCandidate, Map<MatcherCandidate, Tuple<MatcherTransition, Double>>>
      transitions(
          final Tuple<MatcherSample, Set<MatcherCandidate>> predecessors,
          final Tuple<MatcherSample, Set<MatcherCandidate>> candidates) {

    if (logger.isTraceEnabled()) {
      logger.trace(
          "finding transitions for sample {} {} with {} x {} candidates",
          new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ").format(candidates.one().time()),
          GeometryEngine.geometryToWkt(candidates.one().point(), WktExportFlags.wktExportPoint),
          predecessors.two().size(),
          candidates.two().size());
    }

    Stopwatch sw = new Stopwatch();
    sw.start();

    final Set<RoadPoint> targets = new HashSet<RoadPoint>();
    for (MatcherCandidate candidate : candidates.two()) {
      targets.add(candidate.point());
    }

    final AtomicInteger count = new AtomicInteger();
    final Map<MatcherCandidate, Map<MatcherCandidate, Tuple<MatcherTransition, Double>>>
        transitions =
            new ConcurrentHashMap<
                MatcherCandidate, Map<MatcherCandidate, Tuple<MatcherTransition, Double>>>();
    final double base =
        1.0 * spatial.distance(predecessors.one().point(), candidates.one().point()) / 60;
    final double bound =
        Math.max(
            1000d,
            Math.min(
                distance, ((candidates.one().time() - predecessors.one().time()) / 1000) * 100));

    InlineScheduler scheduler = StaticScheduler.scheduler();
    for (final MatcherCandidate predecessor : predecessors.two()) {
      scheduler.spawn(
          new Task() {
            @Override
            public void run() {
              Map<MatcherCandidate, Tuple<MatcherTransition, Double>> map =
                  new HashMap<MatcherCandidate, Tuple<MatcherTransition, Double>>();
              Stopwatch sw = new Stopwatch();
              sw.start();
              Map<RoadPoint, List<Road>> routes =
                  router.route(predecessor.point(), targets, cost, new Distance(), bound);
              sw.stop();

              logger.trace("{} routes ({} ms)", routes.size(), sw.ms());

              for (MatcherCandidate candidate : candidates.two()) {
                List<Road> edges = routes.get(candidate.point());

                if (edges == null) {
                  continue;
                }

                Route route = new Route(predecessor.point(), candidate.point(), edges);

                // According to Newson and Krumm 2009, transition probability is lambda *
                // Math.exp((-1.0) * lambda * Math.abs(dt - route.length())), however, we
                // experimentally choose lambda * Math.exp((-1.0) * lambda * Math.max(0,
                // route.length() - dt)) to avoid unnecessary routes in case of u-turns.

                double beta =
                    lambda == 0
                        ? (2.0
                            * Math.max(1d, candidates.one().time() - predecessors.one().time())
                            / 1000)
                        : 1 / lambda;

                double transition =
                    (1 / beta)
                        * Math.exp(
                            (-1.0) * Math.max(0, route.cost(new TimePriority()) - base) / beta);

                map.put(
                    candidate,
                    new Tuple<MatcherTransition, Double>(new MatcherTransition(route), transition));

                logger.trace(
                    "{} -> {} {} {} {}",
                    predecessor.id(),
                    candidate.id(),
                    base,
                    route.length(),
                    transition);
                count.incrementAndGet();
              }

              transitions.put(predecessor, map);
            }
          });
    }
    if (!scheduler.sync()) {
      throw new RuntimeException();
    }

    sw.stop();

    logger.trace("{} transitions ({} ms)", count.get(), sw.ms());

    return transitions;
  }