Search in sources :

Example 36 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class TestGraph method testGetStreetEdgesSeveral.

public void testGetStreetEdgesSeveral() {
    Graph g = new Graph();
    StreetVertex a = new IntersectionVertex(g, "A", 5, 5);
    StreetVertex b = new IntersectionVertex(g, "B", 6, 6);
    StreetVertex c = new IntersectionVertex(g, "C", 3, 2);
    Set<Edge> allStreetEdges = new HashSet<Edge>(4);
    allStreetEdges.add(edge(a, b, 1.0));
    allStreetEdges.add(edge(b, c, 1.0));
    allStreetEdges.add(edge(c, b, 1.0));
    allStreetEdges.add(edge(c, a, 1.0));
    Set<StreetEdge> edges = new HashSet<StreetEdge>(g.getStreetEdges());
    assertEquals(4, edges.size());
    assertEquals(allStreetEdges, edges);
}
Also used : Graph(org.opentripplanner.routing.graph.Graph) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) FreeEdge(org.opentripplanner.routing.edgetype.FreeEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 37 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class TestGraph method testGetStreetEdgesNone.

public void testGetStreetEdgesNone() {
    Graph g = new Graph();
    Vertex a = new IntersectionVertex(g, "A", 5, 5);
    Vertex b = new IntersectionVertex(g, "B", 6, 6);
    Vertex c = new IntersectionVertex(g, "C", 3, 2);
    Set<Edge> allEdges = new HashSet<Edge>(4);
    allEdges.add(new FreeEdge(a, b));
    allEdges.add(new FreeEdge(b, c));
    allEdges.add(new FreeEdge(c, b));
    allEdges.add(new FreeEdge(c, a));
    Set<StreetEdge> edges = new HashSet<StreetEdge>(g.getStreetEdges());
    assertEquals(0, edges.size());
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) Graph(org.opentripplanner.routing.graph.Graph) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) FreeEdge(org.opentripplanner.routing.edgetype.FreeEdge) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) FreeEdge(org.opentripplanner.routing.edgetype.FreeEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 38 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class TestOnBoardRouting method testOnBoardRouting.

/**
 * Compute a set of path between two random stop locations in a test GTFS.
 *
 * For each departure/arrival location, compute a normal path (depart alighted). Then re-run the
 * same itinerary but with departure while on-board at a randomly-picked up trip alongside the
 * path.
 *
 * We assert that the two itineraries will arrive at the same time, at the same place, with at
 * least one less boarding, and take a less or equals amount of time.
 */
@SuppressWarnings("deprecation")
public void testOnBoardRouting() throws Exception {
    String feedId = graph.getFeedIds().iterator().next();
    // Seed the random generator to make consistent set of tests
    Random rand = new Random(42);
    // Number of tests to run
    final int NTESTS = 100;
    int n = 0;
    while (true) {
        /* Compute a normal path between two random stops... */
        Vertex origin, destination;
        do {
            /* See FAKE_GTFS for available locations */
            origin = graph.getVertex(feedId + ":" + (char) (65 + rand.nextInt(20)));
            destination = graph.getVertex(feedId + ":" + (char) (65 + rand.nextInt(20)));
        } while (origin.equals(destination));
        /* ...at a random date/time */
        RoutingRequest options = new RoutingRequest();
        options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 5 + rand.nextInt(4), 1 + rand.nextInt(20), 4 + rand.nextInt(10), rand.nextInt(60), 0);
        ShortestPathTree spt;
        GraphPath path;
        options.setRoutingContext(graph, origin, destination);
        spt = aStar.getShortestPathTree(options);
        path = spt.getPath(destination, false);
        if (path == null)
            continue;
        System.out.println("Testing path between " + origin.getLabel() + " and " + destination.getLabel() + " at " + new Date(options.dateTime * 1000));
        long arrivalTime1 = 0L;
        long elapsedTime1 = 0L;
        int numBoardings1 = 0;
        Vertex arrivalVertex1 = null;
        if (verbose)
            System.out.println("PATH 1 ---------------------");
        for (State s : path.states) {
            if (verbose)
                System.out.println(s + " [" + s.getVertex().getClass().getName() + "]");
            arrivalTime1 = s.getTimeSeconds();
            arrivalVertex1 = s.getVertex();
            elapsedTime1 = s.getElapsedTimeSeconds();
            numBoardings1 = s.getNumBoardings();
        }
        /* Get a random transit hop from the computed path */
        Stop end = null;
        PatternStopVertex nextV = null;
        TripTimes tripTimes = null;
        int stopIndex = 0;
        long newStart = 0L;
        int nhop = 0;
        for (State s : path.states) {
            if (s.getVertex() instanceof PatternArriveVertex && s.getBackEdge() instanceof PatternHop)
                nhop++;
        }
        int hop = rand.nextInt(nhop);
        nhop = 0;
        float k = rand.nextFloat();
        for (State s : path.states) {
            Vertex v = s.getVertex();
            if (v instanceof PatternArriveVertex && s.getBackEdge() instanceof PatternHop) {
                if (hop == nhop) {
                    PatternArriveVertex pav = (PatternArriveVertex) v;
                    end = pav.getStop();
                    nextV = pav;
                    PatternHop phe = (PatternHop) s.getBackEdge();
                    stopIndex = phe.getStopIndex();
                    tripTimes = s.getTripTimes();
                    int hopDuration = tripTimes.getRunningTime(stopIndex);
                    /*
                         * New start time at k% of hop. Note: do not try to make: round(time +
                         * k.hop) as it will be off few seconds due to floating-point rounding
                         * errors.
                         */
                    newStart = s.getBackState().getTimeSeconds() + Math.round(hopDuration * k);
                    break;
                }
                nhop++;
            }
        }
        System.out.println("Boarded depart: trip=" + tripTimes.trip + ", nextStop=" + nextV.getStop() + " stopIndex=" + stopIndex + " startTime=" + new Date(newStart * 1000L));
        /* And use it for onboard departure */
        double lat = end.getLat();
        // Mock location, not really important here.
        double lon = end.getLon();
        OnboardDepartVertex onboardOrigin = new OnboardDepartVertex("OnBoard_Origin", lat, lon);
        @SuppressWarnings("unused") OnBoardDepartPatternHop currentHop = new OnBoardDepartPatternHop(onboardOrigin, nextV, tripTimes, options.rctx.serviceDays.get(1), stopIndex, k);
        options.dateTime = newStart;
        options.setRoutingContext(graph, onboardOrigin, destination);
        spt = aStar.getShortestPathTree(options);
        /* Re-compute a new path starting boarded */
        GraphPath path2 = spt.getPath(destination, false);
        assertNotNull(path2);
        if (verbose)
            System.out.println("PATH 2 ---------------------");
        long arrivalTime2 = 0L;
        long elapsedTime2 = 0L;
        int numBoardings2 = 0;
        Vertex arrivalVertex2 = null;
        for (State s : path2.states) {
            if (verbose)
                System.out.println(s + " [" + s.getVertex().getClass().getName() + "]");
            arrivalTime2 = s.getTimeSeconds();
            arrivalVertex2 = s.getVertex();
            elapsedTime2 = s.getElapsedTimeSeconds();
            numBoardings2 = s.getNumBoardings();
        }
        /* Arrival time and vertex *must* match */
        assertEquals(arrivalTime1, arrivalTime2);
        assertEquals(arrivalVertex1, destination);
        assertEquals(arrivalVertex2, destination);
        /* On-board *must* be shorter in time */
        assertTrue(elapsedTime2 <= elapsedTime1);
        /* On-board *must* have less boardings */
        assertTrue(numBoardings2 < numBoardings1);
        /* Cleanup edges */
        for (Edge edge : onboardOrigin.getOutgoing()) {
            graph.removeEdge(edge);
        }
        n++;
        if (n > NTESTS)
            break;
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) OnboardDepartVertex(org.opentripplanner.routing.vertextype.OnboardDepartVertex) PatternArriveVertex(org.opentripplanner.routing.vertextype.PatternArriveVertex) PatternStopVertex(org.opentripplanner.routing.vertextype.PatternStopVertex) Stop(org.onebusaway.gtfs.model.Stop) GraphPath(org.opentripplanner.routing.spt.GraphPath) OnboardDepartVertex(org.opentripplanner.routing.vertextype.OnboardDepartVertex) Date(java.util.Date) Random(java.util.Random) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) PatternHop(org.opentripplanner.routing.edgetype.PatternHop) OnBoardDepartPatternHop(org.opentripplanner.routing.edgetype.OnBoardDepartPatternHop) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) PatternStopVertex(org.opentripplanner.routing.vertextype.PatternStopVertex) PatternArriveVertex(org.opentripplanner.routing.vertextype.PatternArriveVertex) OnBoardDepartPatternHop(org.opentripplanner.routing.edgetype.OnBoardDepartPatternHop) Edge(org.opentripplanner.routing.graph.Edge)

Example 39 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class TestOpenStreetMapGraphBuilder method testGraphBuilder.

@Test
public void testGraphBuilder() throws Exception {
    Graph gg = new Graph();
    OpenStreetMapModule loader = new OpenStreetMapModule();
    loader.setDefaultWayPropertySetSource(new DefaultWayPropertySetSource());
    FileBasedOpenStreetMapProviderImpl provider = new FileBasedOpenStreetMapProviderImpl();
    File file = new File(URLDecoder.decode(getClass().getResource("map.osm.gz").getFile(), "UTF-8"));
    provider.setPath(file);
    loader.setProvider(provider);
    loader.buildGraph(gg, extra);
    // Kamiennogorska at south end of segment
    Vertex v1 = gg.getVertex("osm:node:280592578");
    // Kamiennogorska at Mariana Smoluchowskiego
    Vertex v2 = gg.getVertex("osm:node:288969929");
    // Mariana Smoluchowskiego, north end
    Vertex v3 = gg.getVertex("osm:node:280107802");
    // Mariana Smoluchowskiego, south end (of segment connected to v2)
    Vertex v4 = gg.getVertex("osm:node:288970952");
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v3);
    assertNotNull(v4);
    Edge e1 = null, e2 = null, e3 = null;
    for (Edge e : v2.getOutgoing()) {
        if (e.getToVertex() == v1) {
            e1 = e;
        } else if (e.getToVertex() == v3) {
            e2 = e;
        } else if (e.getToVertex() == v4) {
            e3 = e;
        }
    }
    assertNotNull(e1);
    assertNotNull(e2);
    assertNotNull(e3);
    assertTrue("name of e1 must be like \"Kamiennog\u00F3rska\"; was " + e1.getName(), e1.getName().contains("Kamiennog\u00F3rska"));
    assertTrue("name of e2 must be like \"Mariana Smoluchowskiego\"; was " + e2.getName(), e2.getName().contains("Mariana Smoluchowskiego"));
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) Graph(org.opentripplanner.routing.graph.Graph) FileBasedOpenStreetMapProviderImpl(org.opentripplanner.openstreetmap.impl.FileBasedOpenStreetMapProviderImpl) File(java.io.File) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge) Test(org.junit.Test)

Example 40 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class GTFSPatternHopFactoryTest method testBikesAllowed.

@Test
public void testBikesAllowed() throws IOException {
    MockGtfs gtfs = MockGtfs.create();
    gtfs.putAgencies(1);
    gtfs.putRoutes(1);
    gtfs.putStops(2);
    gtfs.putCalendars(1);
    gtfs.putTrips(1, "r0", "sid0", "bikes_allowed=1");
    gtfs.putStopTimes("t0", "s0,s1");
    gtfs.putLines("frequencies.txt", "trip_id,start_time,end_time,headway_secs", "t0,09:00:00,17:00:00,300");
    GtfsFeedId feedId = new GtfsFeedId.Builder().id("FEED").build();
    GTFSPatternHopFactory factory = new GTFSPatternHopFactory(GtfsLibrary.createContext(feedId, gtfs.read()));
    Graph graph = new Graph();
    factory.run(graph);
    for (Edge edge : graph.getEdges()) {
        if (edge instanceof TransitBoardAlight) {
            TripPattern pattern = ((TransitBoardAlight) edge).getPattern();
        // TODO assertTrue(pattern.getBikesAllowed());
        }
    }
}
Also used : Graph(org.opentripplanner.routing.graph.Graph) GtfsFeedId(org.opentripplanner.graph_builder.module.GtfsFeedId) TransitBoardAlight(org.opentripplanner.routing.edgetype.TransitBoardAlight) MockGtfs(org.onebusaway.gtfs.services.MockGtfs) Edge(org.opentripplanner.routing.graph.Edge) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) Test(org.junit.Test)

Aggregations

Edge (org.opentripplanner.routing.graph.Edge)113 Vertex (org.opentripplanner.routing.graph.Vertex)61 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)53 IntersectionVertex (org.opentripplanner.routing.vertextype.IntersectionVertex)26 HashSet (java.util.HashSet)23 State (org.opentripplanner.routing.core.State)22 Coordinate (com.vividsolutions.jts.geom.Coordinate)19 Graph (org.opentripplanner.routing.graph.Graph)19 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)18 Test (org.junit.Test)17 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)17 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)17 ArrayList (java.util.ArrayList)16 LineString (com.vividsolutions.jts.geom.LineString)15 GraphPath (org.opentripplanner.routing.spt.GraphPath)15 StreetVertex (org.opentripplanner.routing.vertextype.StreetVertex)12 PathwayEdge (org.opentripplanner.routing.edgetype.PathwayEdge)11 Geometry (com.vividsolutions.jts.geom.Geometry)9 Stop (org.onebusaway.gtfs.model.Stop)9 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)9