示例#1
0
  private void assertCandidate(Tuple<MatcherCandidate, Double> candidate, Point sample) {
    Polyline polyline = map.get(candidate.one().point().edge().id()).geometry();
    double f = spatial.intercept(polyline, sample);
    Point i = spatial.interpolate(polyline, f);
    double l = spatial.distance(i, sample);
    double sig2 = Math.pow(5d, 2);
    double sqrt_2pi_sig2 = Math.sqrt(2d * Math.PI * sig2);
    double p = 1 / sqrt_2pi_sig2 * Math.exp((-1) * l / (2 * sig2));

    assertEquals(f, candidate.one().point().fraction(), 10E-6);
    assertEquals(p, candidate.two(), 10E-6);
  }
示例#2
0
  @SuppressWarnings("unused")
  private Set<Long> refset(Point sample, double radius) {
    Set<Long> refset = new HashSet<Long>();
    Iterator<Road> roads = map.edges();
    while (roads.hasNext()) {
      Road road = roads.next();
      double f = spatial.intercept(road.geometry(), sample);
      Point i = spatial.interpolate(road.geometry(), f);
      double l = spatial.distance(i, sample);

      if (l <= radius) {
        refset.add(road.id());
      }
    }
    return refset;
  }
示例#3
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;
  }
示例#4
0
public class MatcherTest {
  private final SpatialOperator spatial = new Geography();
  private final Router<Road, RoadPoint> router = new Dijkstra<Road, RoadPoint>();
  private final Cost<Road> cost = new Time();
  private final RoadMap map =
      RoadMap.Load(
          new RoadReader() {
            class Entry extends Quintuple<Long, Long, Long, Boolean, String> {
              private static final long serialVersionUID = 1L;

              public Entry(Long one, Long two, Long three, Boolean four, String five) {
                super(one, two, three, four, five);
              }
            };

            private Set<Entry> entries =
                new HashSet<Entry>(
                    Arrays.asList(
                        new Entry(0L, 0L, 1L, false, "LINESTRING(11.000 48.000, 11.010 48.000)"),
                        new Entry(1L, 1L, 2L, false, "LINESTRING(11.010 48.000, 11.020 48.000)"),
                        new Entry(2L, 2L, 3L, false, "LINESTRING(11.020 48.000, 11.030 48.000)"),
                        new Entry(3L, 1L, 4L, true, "LINESTRING(11.010 48.000, 11.011 47.999)"),
                        new Entry(4L, 4L, 5L, true, "LINESTRING(11.011 47.999, 11.021 47.999)"),
                        new Entry(5L, 5L, 6L, true, "LINESTRING(11.021 47.999, 11.021 48.010)")));

            private Iterator<BaseRoad> iterator = null;

            private ArrayList<BaseRoad> roads = new ArrayList<BaseRoad>();

            @Override
            public boolean isOpen() {
              return (iterator != null);
            }

            @Override
            public void open() throws SourceException {
              if (roads.isEmpty()) {
                for (Entry entry : entries) {
                  Polyline geometry =
                      (Polyline)
                          GeometryEngine.geometryFromWkt(
                              entry.five(), WktImportFlags.wktImportDefaults, Type.Polyline);
                  roads.add(
                      new BaseRoad(
                          entry.one(),
                          entry.two(),
                          entry.three(),
                          entry.one(),
                          entry.four(),
                          (short) 0,
                          1.0f,
                          100.0f,
                          100.0f,
                          (float) spatial.length(geometry),
                          geometry));
                }
              }

              iterator = roads.iterator();
            }

            @Override
            public void open(Polygon polygon, HashSet<Short> exclusions) throws SourceException {
              open();
            }

            @Override
            public void close() throws SourceException {
              iterator = null;
            }

            @Override
            public BaseRoad next() throws SourceException {
              return iterator.hasNext() ? iterator.next() : null;
            }
          });

  public MatcherTest() {
    map.construct();
  }

  private void assertCandidate(Tuple<MatcherCandidate, Double> candidate, Point sample) {
    Polyline polyline = map.get(candidate.one().point().edge().id()).geometry();
    double f = spatial.intercept(polyline, sample);
    Point i = spatial.interpolate(polyline, f);
    double l = spatial.distance(i, sample);
    double sig2 = Math.pow(5d, 2);
    double sqrt_2pi_sig2 = Math.sqrt(2d * Math.PI * sig2);
    double p = 1 / sqrt_2pi_sig2 * Math.exp((-1) * l / (2 * sig2));

    assertEquals(f, candidate.one().point().fraction(), 10E-6);
    assertEquals(p, candidate.two(), 10E-6);
  }

  private void assertTransition(
      Tuple<MatcherTransition, Double> transition,
      Tuple<MatcherCandidate, MatcherSample> source,
      Tuple<MatcherCandidate, MatcherSample> target,
      double lambda) {
    List<Road> edges = router.route(source.one().point(), target.one().point(), cost);

    if (edges == null) {
      // fail();
    }

    Route route = new Route(source.one().point(), target.one().point(), edges);

    assertEquals(route.length(), transition.one().route().length(), 10E-6);
    assertEquals(route.source().edge().id(), transition.one().route().source().edge().id());
    assertEquals(route.target().edge().id(), transition.one().route().target().edge().id());

    double beta =
        lambda == 0 ? (2.0 * (target.two().time() - source.two().time()) / 1000) : 1 / lambda;
    double base = 1.0 * spatial.distance(source.two().point(), target.two().point()) / 60;
    double p =
        (1 / beta) * Math.exp((-1.0) * Math.max(0, route.cost(new TimePriority()) - base) / beta);

    assertEquals(transition.two(), p, 10E-6);
  }

  @SuppressWarnings("unused")
  private Set<Long> refset(Point sample, double radius) {
    Set<Long> refset = new HashSet<Long>();
    Iterator<Road> roads = map.edges();
    while (roads.hasNext()) {
      Road road = roads.next();
      double f = spatial.intercept(road.geometry(), sample);
      Point i = spatial.interpolate(road.geometry(), f);
      double l = spatial.distance(i, sample);

      if (l <= radius) {
        refset.add(road.id());
      }
    }
    return refset;
  }

  @Test
  public void TestCandidates() throws JSONException {
    Matcher filter = new Matcher(map, router, cost, spatial);
    {
      filter.setMaxRadius(100);
      Point sample = new Point(11.001, 48.001);

      Set<Tuple<MatcherCandidate, Double>> candidates =
          filter.candidates(new MatcherSample(0, sample));

      assertEquals(0, candidates.size());
    }
    {
      double radius = 200;
      filter.setMaxRadius(radius);
      Point sample = new Point(11.001, 48.001);

      Set<Tuple<MatcherCandidate, Double>> candidates =
          filter.candidates(new MatcherSample(0, sample));

      Set<Long> refset = new HashSet<Long>(Arrays.asList(0L, 1L));
      Set<Long> set = new HashSet<Long>();

      for (Tuple<MatcherCandidate, Double> candidate : candidates) {
        assertTrue(refset.contains(candidate.one().point().edge().id()));
        assertCandidate(candidate, sample);
        set.add(candidate.one().point().edge().id());
      }

      assertEquals(refset.size(), candidates.size());
      assertTrue(set.containsAll(refset));
    }
    {
      double radius = 200;
      filter.setMaxRadius(radius);
      Point sample = new Point(11.010, 48.000);

      Set<Tuple<MatcherCandidate, Double>> candidates =
          filter.candidates(new MatcherSample(0, sample));

      Set<Long> refset = new HashSet<Long>(Arrays.asList(0L, 3L));
      Set<Long> set = new HashSet<Long>();

      for (Tuple<MatcherCandidate, Double> candidate : candidates) {
        assertTrue(
            Long.toString(candidate.one().point().edge().id()),
            refset.contains(candidate.one().point().edge().id()));
        assertCandidate(candidate, sample);
        set.add(candidate.one().point().edge().id());
      }

      assertEquals(refset.size(), candidates.size());
      assertTrue(set.containsAll(refset));
    }
    {
      double radius = 200;
      filter.setMaxRadius(radius);
      Point sample = new Point(11.011, 48.001);

      Set<Tuple<MatcherCandidate, Double>> candidates =
          filter.candidates(new MatcherSample(0, sample));

      Set<Long> refset = new HashSet<Long>(Arrays.asList(0L, 2L, 3L));
      Set<Long> set = new HashSet<Long>();

      for (Tuple<MatcherCandidate, Double> candidate : candidates) {
        assertTrue(
            Long.toString(candidate.one().point().edge().id()),
            refset.contains(candidate.one().point().edge().id()));
        assertCandidate(candidate, sample);
        set.add(candidate.one().point().edge().id());
      }

      assertEquals(refset.size(), candidates.size());
      assertTrue(set.containsAll(refset));
    }
    {
      double radius = 300;
      filter.setMaxRadius(radius);
      Point sample = new Point(11.011, 48.001);

      Set<Tuple<MatcherCandidate, Double>> candidates =
          filter.candidates(new MatcherSample(0, sample));

      Set<Long> refset = new HashSet<Long>(Arrays.asList(0L, 2L, 3L, 8L));
      Set<Long> set = new HashSet<Long>();

      for (Tuple<MatcherCandidate, Double> candidate : candidates) {
        assertTrue(
            Long.toString(candidate.one().point().edge().id()),
            refset.contains(candidate.one().point().edge().id()));
        assertCandidate(candidate, sample);
        set.add(candidate.one().point().edge().id());
      }

      assertEquals(refset.size(), candidates.size());
      assertTrue(set.containsAll(refset));
    }
    {
      double radius = 200;
      filter.setMaxRadius(radius);
      Point sample = new Point(11.019, 48.001);

      Set<Tuple<MatcherCandidate, Double>> candidates =
          filter.candidates(new MatcherSample(0, sample));

      Set<Long> refset = new HashSet<Long>(Arrays.asList(2L, 3L, 5L, 10L));
      Set<Long> set = new HashSet<Long>();

      for (Tuple<MatcherCandidate, Double> candidate : candidates) {
        assertTrue(
            Long.toString(candidate.one().point().edge().id()),
            refset.contains(candidate.one().point().edge().id()));
        assertCandidate(candidate, sample);
        set.add(candidate.one().point().edge().id());
      }

      assertEquals(refset.size(), candidates.size());
      assertTrue(set.containsAll(refset));
    }
  }

  @Test
  public void TestTransitions() throws JSONException {
    Matcher filter = new Matcher(map, router, cost, spatial);
    filter.setMaxRadius(200);
    {
      MatcherSample sample1 = new MatcherSample(0, new Point(11.001, 48.001));
      MatcherSample sample2 = new MatcherSample(60000, new Point(11.019, 48.001));

      Set<MatcherCandidate> predecessors = new HashSet<MatcherCandidate>();
      Set<MatcherCandidate> candidates = new HashSet<MatcherCandidate>();

      for (Tuple<MatcherCandidate, Double> candidate : filter.candidates(sample1)) {
        predecessors.add(candidate.one());
      }

      for (Tuple<MatcherCandidate, Double> candidate : filter.candidates(sample2)) {
        candidates.add(candidate.one());
      }

      assertEquals(2, predecessors.size());
      assertEquals(4, candidates.size());

      Map<MatcherCandidate, Map<MatcherCandidate, Tuple<MatcherTransition, Double>>> transitions =
          filter.transitions(
              new Tuple<MatcherSample, Set<MatcherCandidate>>(sample1, predecessors),
              new Tuple<MatcherSample, Set<MatcherCandidate>>(sample2, candidates));

      assertEquals(2, transitions.size());

      for (Entry<MatcherCandidate, Map<MatcherCandidate, Tuple<MatcherTransition, Double>>> source :
          transitions.entrySet()) {
        assertEquals(4, source.getValue().size());

        for (Entry<MatcherCandidate, Tuple<MatcherTransition, Double>> target :
            source.getValue().entrySet()) {
          assertTransition(
              target.getValue(),
              new Tuple<MatcherCandidate, MatcherSample>(source.getKey(), sample1),
              new Tuple<MatcherCandidate, MatcherSample>(target.getKey(), sample2),
              filter.getLambda());
        }
      }
    }
    {
      MatcherSample sample1 = new MatcherSample(0, new Point(11.019, 48.001));
      MatcherSample sample2 = new MatcherSample(60000, new Point(11.001, 48.001));

      Set<MatcherCandidate> predecessors = new HashSet<MatcherCandidate>();
      Set<MatcherCandidate> candidates = new HashSet<MatcherCandidate>();

      for (Tuple<MatcherCandidate, Double> candidate : filter.candidates(sample1)) {
        predecessors.add(candidate.one());
      }

      for (Tuple<MatcherCandidate, Double> candidate : filter.candidates(sample2)) {
        candidates.add(candidate.one());
      }

      assertEquals(4, predecessors.size());
      assertEquals(2, candidates.size());

      Map<MatcherCandidate, Map<MatcherCandidate, Tuple<MatcherTransition, Double>>> transitions =
          filter.transitions(
              new Tuple<MatcherSample, Set<MatcherCandidate>>(sample1, predecessors),
              new Tuple<MatcherSample, Set<MatcherCandidate>>(sample2, candidates));

      assertEquals(4, transitions.size());

      for (Entry<MatcherCandidate, Map<MatcherCandidate, Tuple<MatcherTransition, Double>>> source :
          transitions.entrySet()) {
        if (source.getKey().point().edge().id() == 10) {
          assertEquals(0, source.getValue().size());
        } else {
          assertEquals(2, source.getValue().size());
        }

        for (Entry<MatcherCandidate, Tuple<MatcherTransition, Double>> target :
            source.getValue().entrySet()) {
          assertTransition(
              target.getValue(),
              new Tuple<MatcherCandidate, MatcherSample>(source.getKey(), sample1),
              new Tuple<MatcherCandidate, MatcherSample>(target.getKey(), sample2),
              filter.getLambda());
        }
      }
    }
  }
}
示例#5
0
 public MatcherTest() {
   map.construct();
 }