Search in sources :

Example 6 with QualifiedModeSet

use of org.opentripplanner.api.parameter.QualifiedModeSet in project OpenTripPlanner by opentripplanner.

the class RepeatedRaptorTestResource method oneOrigin.

private void oneOrigin(double lat, double lon, String banAgency) {
    ProfileRequest req = new ProfileRequest();
    req.fromLat = lat;
    req.fromLon = lon;
    req.fromTime = 60 * 60 * 8;
    req.toTime = 60 * 60 * 9;
    req.walkSpeed = 2;
    req.bikeSpeed = 4;
    req.carSpeed = 8;
    req.date = new LocalDate(2015, 04, 20);
    // minutes
    req.maxWalkTime = 20;
    req.accessModes = new QualifiedModeSet("WALK");
    req.egressModes = new QualifiedModeSet("WALK");
    req.transitModes = new TraverseModeSet("TRANSIT");
    req.analyst = true;
    if (surfaceCache == null) {
        LOG.error("You must run OTP with the --analyst option to enable spatial analysis features.");
    }
    final RepeatedRaptorProfileRouter router_a = new RepeatedRaptorProfileRouter(graph, req);
    final RepeatedRaptorProfileRouter router_b = new RepeatedRaptorProfileRouter(graph, req);
    router_b.banAgency = banAgency;
    try {
        router_a.route();
        router_b.route();
    } catch (VertexNotFoundException ex) {
        LOG.error("vertex not found");
        return;
    }
    System.out.printf("stop, min_a, min_b, min_diff, max_a, max_b, max_diff\n");
    boolean decreased = false;
    // Compare the propagated results
    decreased = false;
    TimeSurface.RangeSet timeSurfaces_a = router_a.timeSurfaceRangeSet;
    TimeSurface.RangeSet timeSurfaces_b = router_b.timeSurfaceRangeSet;
    for (Vertex destVertex : timeSurfaces_a.min.times.keySet()) {
        int min_a = timeSurfaces_a.min.getTime(destVertex);
        int max_a = timeSurfaces_a.max.getTime(destVertex);
        int avg_a = timeSurfaces_a.avg.getTime(destVertex);
        int min_b = timeSurfaces_b.min.getTime(destVertex);
        int max_b = timeSurfaces_b.max.getTime(destVertex);
        int avg_b = timeSurfaces_b.avg.getTime(destVertex);
        long min_diff = (long) min_b - min_a;
        long max_diff = (long) max_b - max_a;
        long avg_diff = (long) avg_b - avg_a;
        if (min_b == TimeSurface.UNREACHABLE) {
            min_diff = Integer.MAX_VALUE;
            max_diff = Integer.MAX_VALUE;
            avg_diff = Integer.MAX_VALUE;
        }
        n_total += 1;
        if (min_diff < 0 || max_diff < 0 || avg_diff < 0) {
            n_decrease += 1;
            sum_decrease += max_diff;
            // Time decreased due to banning a route. This is bad, print it out.
            System.out.printf("\"%s\",%d,%d,%d,%d,%d,%d\n", destVertex.getName(), min_a, min_b, min_diff, max_a, max_b, max_diff);
            decreased = true;
        } else if (avg_diff > 0) {
            n_increase += 1;
        }
    }
    if (decreased) {
        LOG.error("Decreases happened at propagated street vertices for this origin!");
    }
    LOG.info("Street Vertices: {} increased, {} decreased out of {} destinations total", n_increase, n_decrease, n_total);
}
Also used : RepeatedRaptorProfileRouter(org.opentripplanner.profile.RepeatedRaptorProfileRouter) Vertex(org.opentripplanner.routing.graph.Vertex) TimeSurface(org.opentripplanner.analyst.TimeSurface) QualifiedModeSet(org.opentripplanner.api.parameter.QualifiedModeSet) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) ProfileRequest(org.opentripplanner.profile.ProfileRequest) LocalDate(org.joda.time.LocalDate) VertexNotFoundException(org.opentripplanner.routing.error.VertexNotFoundException)

Example 7 with QualifiedModeSet

use of org.opentripplanner.api.parameter.QualifiedModeSet in project OpenTripPlanner by opentripplanner.

the class ConvertToFrequencyTest method testSimpleConversion.

/**
 * The simplest case of frequency conversion: no weird loop routes or anything like that, travel times always same, etc.
 */
@Test
public void testSimpleConversion() throws Exception {
    Graph gg = buildGraphNoTransit();
    addTransit(gg);
    link(gg);
    gg.index(new DefaultStreetVertexIndexFactory());
    ProfileRequest pr1 = new ProfileRequest();
    pr1.date = new LocalDate(2015, 6, 10);
    pr1.fromTime = 7 * 3600;
    pr1.toTime = 9 * 3600;
    pr1.fromLat = pr1.toLat = 39.9621;
    pr1.fromLon = pr1.toLon = -83.0007;
    pr1.accessModes = pr1.egressModes = pr1.directModes = new QualifiedModeSet("WALK");
    pr1.transitModes = new TraverseModeSet("TRANSIT");
    RepeatedRaptorProfileRouter rrpr1 = new RepeatedRaptorProfileRouter(gg, pr1);
    rrpr1.route();
    ProfileRequest pr2 = new ProfileRequest();
    pr2.date = new LocalDate(2015, 6, 10);
    pr2.fromTime = 7 * 3600;
    pr2.toTime = 9 * 3600;
    pr2.fromLat = pr2.toLat = 39.9621;
    pr2.fromLon = pr2.toLon = -83.0007;
    pr2.accessModes = pr2.egressModes = pr2.directModes = new QualifiedModeSet("WALK");
    pr2.transitModes = new TraverseModeSet("TRANSIT");
    ConvertToFrequency ctf = new ConvertToFrequency();
    ctf.groupBy = ConvertToFrequency.ConversionGroup.ROUTE_DIRECTION;
    ctf.routeId = new String[] { "route" };
    ctf.windowStart = 5 * 3600;
    ctf.windowEnd = 10 * 3600;
    pr2.scenario = new Scenario(0);
    pr2.scenario.modifications = Arrays.asList(ctf);
    RepeatedRaptorProfileRouter rrpr2 = new RepeatedRaptorProfileRouter(gg, pr2);
    rrpr2.route();
    assertFalse(rrpr1.raptorWorkerData.hasFrequencies);
    assertTrue(rrpr2.raptorWorkerData.hasFrequencies);
    RaptorWorkerTimetable tt = rrpr2.raptorWorkerData.timetablesForPattern.get(0);
    assertEquals(FakeGraph.FREQUENCY, tt.headwaySecs[0]);
    assertEquals(FakeGraph.TRAVEL_TIME, tt.frequencyTrips[0][2]);
}
Also used : RepeatedRaptorProfileRouter(org.opentripplanner.profile.RepeatedRaptorProfileRouter) FakeGraph(org.opentripplanner.graph_builder.module.FakeGraph) Graph(org.opentripplanner.routing.graph.Graph) ConvertToFrequency(org.opentripplanner.analyst.scenario.ConvertToFrequency) QualifiedModeSet(org.opentripplanner.api.parameter.QualifiedModeSet) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) RaptorWorkerTimetable(org.opentripplanner.profile.RaptorWorkerTimetable) ProfileRequest(org.opentripplanner.profile.ProfileRequest) LocalDate(org.joda.time.LocalDate) Scenario(org.opentripplanner.analyst.scenario.Scenario) Test(org.junit.Test)

Example 8 with QualifiedModeSet

use of org.opentripplanner.api.parameter.QualifiedModeSet in project OpenTripPlanner by opentripplanner.

the class ConvertToFrequencyTest method testMultiplePatterns.

/**
 * Test the case where there are multiple patterns that need to be chosen from.
 */
@Test
public void testMultiplePatterns() throws Exception {
    Graph gg = buildGraphNoTransit();
    addMultiplePatterns(gg);
    link(gg);
    gg.index(new DefaultStreetVertexIndexFactory());
    ProfileRequest pr1 = new ProfileRequest();
    pr1.date = new LocalDate(2015, 6, 10);
    pr1.fromTime = 7 * 3600;
    pr1.toTime = 9 * 3600;
    pr1.fromLat = pr1.toLat = 39.9621;
    pr1.fromLon = pr1.toLon = -83.0007;
    pr1.accessModes = pr1.egressModes = pr1.directModes = new QualifiedModeSet("WALK");
    pr1.transitModes = new TraverseModeSet("TRANSIT");
    RepeatedRaptorProfileRouter rrpr1 = new RepeatedRaptorProfileRouter(gg, pr1);
    rrpr1.route();
    ProfileRequest pr2 = new ProfileRequest();
    pr2.date = new LocalDate(2015, 6, 10);
    pr2.fromTime = 7 * 3600;
    pr2.toTime = 9 * 3600;
    pr2.fromLat = pr2.toLat = 39.9621;
    pr2.fromLon = pr2.toLon = -83.0007;
    pr2.accessModes = pr2.egressModes = pr2.directModes = new QualifiedModeSet("WALK");
    pr2.transitModes = new TraverseModeSet("TRANSIT");
    ConvertToFrequency ctf = new ConvertToFrequency();
    ctf.groupBy = ConvertToFrequency.ConversionGroup.ROUTE_DIRECTION;
    ctf.routeId = new String[] { "route" };
    ctf.windowStart = 5 * 3600;
    ctf.windowEnd = 10 * 3600;
    pr2.scenario = new Scenario(0);
    pr2.scenario.modifications = Arrays.asList(ctf);
    RepeatedRaptorProfileRouter rrpr2 = new RepeatedRaptorProfileRouter(gg, pr2);
    rrpr2.route();
    assertFalse(rrpr1.raptorWorkerData.hasFrequencies);
    assertTrue(rrpr2.raptorWorkerData.hasFrequencies);
    // everything should have gotten merged into one pattern
    assertEquals(1, rrpr2.raptorWorkerData.timetablesForPattern.size());
    RaptorWorkerTimetable tt = rrpr2.raptorWorkerData.timetablesForPattern.get(0);
    // should be no frequency variation because trips on all patterns are considered for frequencies.
    // there should be no travel time variation because only trips on the dominant pattern are considered
    // for travel time.
    assertEquals(FakeGraph.FREQUENCY, tt.headwaySecs[0]);
    assertEquals(FakeGraph.TRAVEL_TIME, tt.frequencyTrips[0][2]);
    // now try it with groupings by pattern
    ConvertToFrequency ctf3 = new ConvertToFrequency();
    ctf3.groupBy = ConvertToFrequency.ConversionGroup.PATTERN;
    ctf3.routeId = new String[] { "route" };
    ctf3.windowStart = 5 * 3600;
    ctf3.windowEnd = 10 * 3600;
    ProfileRequest pr3 = new ProfileRequest();
    pr3.date = new LocalDate(2015, 6, 10);
    pr3.fromTime = 7 * 3600;
    pr3.toTime = 9 * 3600;
    pr3.fromLat = pr2.toLat = 39.9621;
    pr3.fromLon = pr2.toLon = -83.0007;
    pr3.accessModes = pr2.egressModes = pr2.directModes = new QualifiedModeSet("WALK");
    pr3.transitModes = new TraverseModeSet("TRANSIT");
    pr3.scenario = new Scenario(0);
    pr3.scenario.modifications = Arrays.asList(ctf3);
    RepeatedRaptorProfileRouter rrpr3 = new RepeatedRaptorProfileRouter(gg, pr3);
    rrpr3.route();
    assertTrue(rrpr3.raptorWorkerData.hasFrequencies);
    // should be converted to two independent patterns
    assertEquals(2, rrpr3.raptorWorkerData.timetablesForPattern.size());
    RaptorWorkerTimetable shrt, lng;
    if (rrpr3.raptorWorkerData.timetablesForPattern.get(0).nStops == 2) {
        shrt = rrpr3.raptorWorkerData.timetablesForPattern.get(0);
        lng = rrpr3.raptorWorkerData.timetablesForPattern.get(1);
    } else {
        lng = rrpr3.raptorWorkerData.timetablesForPattern.get(0);
        shrt = rrpr3.raptorWorkerData.timetablesForPattern.get(1);
    }
    assertEquals(3, lng.nStops);
    assertEquals(2, shrt.nStops);
    assertEquals(675, lng.headwaySecs[0]);
    assertEquals((int) (FakeGraph.FREQUENCY / 0.1), shrt.headwaySecs[0]);
    // make sure that the hop time is always FakeGraph.TRAVEL_TIME
    assertEquals(FakeGraph.TRAVEL_TIME, shrt.frequencyTrips[0][2]);
    assertEquals(FakeGraph.TRAVEL_TIME, lng.frequencyTrips[0][2]);
    assertEquals(FakeGraph.TRAVEL_TIME * 2, lng.frequencyTrips[0][4]);
}
Also used : RepeatedRaptorProfileRouter(org.opentripplanner.profile.RepeatedRaptorProfileRouter) FakeGraph(org.opentripplanner.graph_builder.module.FakeGraph) Graph(org.opentripplanner.routing.graph.Graph) ConvertToFrequency(org.opentripplanner.analyst.scenario.ConvertToFrequency) QualifiedModeSet(org.opentripplanner.api.parameter.QualifiedModeSet) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) RaptorWorkerTimetable(org.opentripplanner.profile.RaptorWorkerTimetable) ProfileRequest(org.opentripplanner.profile.ProfileRequest) LocalDate(org.joda.time.LocalDate) Scenario(org.opentripplanner.analyst.scenario.Scenario) Test(org.junit.Test)

Example 9 with QualifiedModeSet

use of org.opentripplanner.api.parameter.QualifiedModeSet in project OpenTripPlanner by opentripplanner.

the class RepeatedRaptorComparison method main.

public static void main(String... args) {
    if (args.length == 0) {
        System.err.println("too few arguments.");
        return;
    }
    // build a graph
    File graphDir = new File(args[0]);
    Graph graph = buildGraph(graphDir);
    DB comparisonDb = null;
    BTreeMap<Fun.Tuple3<String, String, ResultEnvelope.Which>, Integer> comparison = null;
    // open the comparison file, if we have one.
    if (args.length > 1) {
        comparisonDb = DBMaker.newFileDB(new File(args[1])).readOnly().transactionDisable().closeOnJvmShutdown().cacheSize(24).asyncWriteEnable().make();
        comparison = comparisonDb.getTreeMap("results");
    }
    String outputName = args.length > 2 ? args[2] : MavenVersion.VERSION.commit + ".db";
    DB outputDb = DBMaker.newFileDB(new File(outputName)).transactionDisable().cacheSize(48).closeOnJvmShutdown().make();
    final BTreeMap<Fun.Tuple3<String, String, ResultEnvelope.Which>, Integer> output = outputDb.createTreeMap("results").valueSerializer(Serializer.JAVA).makeOrGet();
    // if we have a comparison file, get the pointset from it. Otherwise choose some randomly.
    Collection<String> vertexLabels;
    PointSet pset;
    if (comparison != null) {
        // clooge, pointset is stored in its own map in db.
        pset = comparisonDb.<String, PointSet>getTreeMap("pointset").get("pointset");
    } else {
        // choose some vertices
        List<Vertex> vertices = graph.getVertices().stream().filter(v -> v.getLabel().startsWith("osm:node:")).limit(1000).collect(Collectors.toList());
        // make a pointset
        pset = new PointSet(vertices.size());
        int featIdx = 0;
        for (Vertex v : vertices) {
            PointFeature pf = new PointFeature();
            pf.setId(v.getLabel());
            pf.setLat(v.getLat() + OFFSET_Y);
            pf.setLon(v.getLon() + OFFSET_X);
            pset.addFeature(pf, featIdx++);
        }
        outputDb.createTreeMap("pointset").<String, PointSet>make().put("pointset", pset);
    }
    SampleSet ss = new SampleSet(pset, graph.getSampleFactory());
    final BTreeMap<Fun.Tuple3<String, String, ResultEnvelope.Which>, Integer> comparisonResults = comparison;
    Histogram bestCaseHisto = new Histogram("Best case");
    Histogram avgCaseHisto = new Histogram("Average");
    Histogram worstCaseHisto = new Histogram("Worst case");
    ProfileRequest template = new ProfileRequest();
    template.accessModes = new QualifiedModeSet("WALK");
    template.analyst = true;
    template.maxWalkTime = 20 * 60;
    template.walkSpeed = 1.3f;
    template.fromTime = 7 * 3600;
    template.toTime = 9 * 3600;
    template.date = new LocalDate(2015, 8, 4);
    RaptorWorkerData data = RepeatedRaptorProfileRouter.getRaptorWorkerData(template, graph, ss, new TaskStatistics());
    // do the computation and comparison
    IntStream.range(0, pset.featureCount()).parallel().forEach(idx -> {
        if (idx % 100 == 0)
            System.out.println(idx + " points complete");
        Coordinate coord = pset.getCoordinate(idx);
        String origin = pset.getFeature(idx).getId();
        ProfileRequest req;
        try {
            req = template.clone();
        } catch (CloneNotSupportedException e) {
            /* can't happen */
            throw new RuntimeException(e);
        }
        req.maxWalkTime = 20 * 60;
        req.fromLat = req.toLat = coord.y;
        req.fromLon = req.toLon = coord.x;
        // 7 to 9 AM
        req.fromTime = 7 * 3600;
        req.toTime = 9 * 3600;
        req.transitModes = new TraverseModeSet("TRANSIT");
        RepeatedRaptorProfileRouter rrpr = new RepeatedRaptorProfileRouter(graph, req, ss);
        rrpr.raptorWorkerData = data;
        rrpr.includeTimes = true;
        // TODO we really want to disable both isochrone and accessibility generation here.
        // Because a sampleSet is provided it's going to make accessibility information (not isochrones).
        ResultEnvelope results = new ResultEnvelope();
        try {
            results = rrpr.route();
        } catch (Exception e) {
            LOG.error("Exception during routing", e);
            return;
        }
        for (ResultEnvelope.Which which : new ResultEnvelope.Which[] { ResultEnvelope.Which.BEST_CASE, ResultEnvelope.Which.AVERAGE, ResultEnvelope.Which.WORST_CASE }) {
            Histogram histogram;
            ResultSet resultSet;
            switch(which) {
                case BEST_CASE:
                    histogram = bestCaseHisto;
                    resultSet = results.bestCase;
                    break;
                case WORST_CASE:
                    histogram = worstCaseHisto;
                    resultSet = results.worstCase;
                    break;
                case AVERAGE:
                    histogram = avgCaseHisto;
                    resultSet = results.avgCase;
                    break;
                default:
                    histogram = null;
                    resultSet = null;
            }
            // comparison.
            for (int i = 0; i < resultSet.times.length; i++) {
                int time = resultSet.times[i];
                // TODO this is creating a PointFeature obj to hold the id at each call
                // Cache?
                String dest = pset.getFeature(i).getId();
                Fun.Tuple3<String, String, ResultEnvelope.Which> key = new Fun.Tuple3<>(origin, dest, which);
                output.put(key, time);
                if (time < 0) {
                    LOG.error("Path from {}  to {} has negative time {}", origin, dest, time);
                }
                if (comparisonResults != null) {
                    int time0 = comparisonResults.get(key);
                    int deltaMinutes;
                    if (time0 == RaptorWorker.UNREACHED && time != RaptorWorker.UNREACHED)
                        deltaMinutes = (time / 60) - 120;
                    else if (time == RaptorWorker.UNREACHED && time0 != RaptorWorker.UNREACHED)
                        deltaMinutes = 120 - (time0 / 60);
                    else
                        deltaMinutes = (time - time0) / 60;
                    // histograms are not threadsafe
                    synchronized (histogram) {
                        histogram.add(deltaMinutes);
                    }
                }
            }
        }
    });
    output.close();
    if (comparisonDb != null) {
        comparisonDb.close();
        bestCaseHisto.displayHorizontal();
        System.out.println("mean: " + bestCaseHisto.mean());
        avgCaseHisto.displayHorizontal();
        System.out.println("mean: " + avgCaseHisto.mean());
        worstCaseHisto.displayHorizontal();
        System.out.println("mean: " + worstCaseHisto.mean());
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) Histogram(org.opentripplanner.common.Histogram) QualifiedModeSet(org.opentripplanner.api.parameter.QualifiedModeSet) LocalDate(org.joda.time.LocalDate) RepeatedRaptorProfileRouter(org.opentripplanner.profile.RepeatedRaptorProfileRouter) TaskStatistics(org.opentripplanner.analyst.cluster.TaskStatistics) RaptorWorkerData(org.opentripplanner.profile.RaptorWorkerData) ResultEnvelope(org.opentripplanner.analyst.cluster.ResultEnvelope) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) ProfileRequest(org.opentripplanner.profile.ProfileRequest) Graph(org.opentripplanner.routing.graph.Graph) Coordinate(com.vividsolutions.jts.geom.Coordinate) File(java.io.File)

Example 10 with QualifiedModeSet

use of org.opentripplanner.api.parameter.QualifiedModeSet in project OpenTripPlanner by opentripplanner.

the class ProfileRouter method findClosestStops.

/**
 * Perform an on-street search around a point with each of several modes to find nearby stops.
 * @return one or more paths to each reachable stop using the various modes.
 */
private Multimap<StopCluster, StopAtDistance> findClosestStops(boolean dest) {
    Multimap<StopCluster, StopAtDistance> pathsByStop = ArrayListMultimap.create();
    QualifiedModeSet qModes = dest ? request.egressModes : request.accessModes;
    for (QualifiedMode qmode : qModes.qModes) {
        LOG.info("{} mode {}", dest ? "egress" : "access", qmode);
        for (StopAtDistance sd : findClosestStops(qmode, dest)) {
            pathsByStop.put(sd.stopCluster, sd);
        }
    }
    return pathsByStop;
}
Also used : QualifiedModeSet(org.opentripplanner.api.parameter.QualifiedModeSet) QualifiedMode(org.opentripplanner.api.parameter.QualifiedMode)

Aggregations

QualifiedModeSet (org.opentripplanner.api.parameter.QualifiedModeSet)11 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)8 LocalDate (org.joda.time.LocalDate)7 ProfileRequest (org.opentripplanner.profile.ProfileRequest)7 RepeatedRaptorProfileRouter (org.opentripplanner.profile.RepeatedRaptorProfileRouter)7 Graph (org.opentripplanner.routing.graph.Graph)7 Test (org.junit.Test)5 FakeGraph (org.opentripplanner.graph_builder.module.FakeGraph)5 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)5 TIntIntMap (gnu.trove.map.TIntIntMap)3 TaskStatistics (org.opentripplanner.analyst.cluster.TaskStatistics)3 ConvertToFrequency (org.opentripplanner.analyst.scenario.ConvertToFrequency)3 Scenario (org.opentripplanner.analyst.scenario.Scenario)3 RaptorWorkerData (org.opentripplanner.profile.RaptorWorkerData)3 RaptorWorkerTimetable (org.opentripplanner.profile.RaptorWorkerTimetable)3 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)3 TIntIntIterator (gnu.trove.iterator.TIntIntIterator)2 GenericLocation (org.opentripplanner.common.model.GenericLocation)2 AStar (org.opentripplanner.routing.algorithm.AStar)2 Vertex (org.opentripplanner.routing.graph.Vertex)2