Search in sources :

Example 91 with Edge

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

the class AStar method iterate.

boolean iterate() {
    // print debug info
    if (verbose) {
        double w = runState.pq.peek_min_key();
        System.out.println("pq min key = " + w);
    }
    // interleave some heuristic-improving work (single threaded)
    runState.heuristic.doSomeWork();
    // get the lowest-weight state in the queue
    runState.u = runState.pq.extract_min();
    // and mark vertex as visited
    if (!runState.spt.visit(runState.u)) {
        // not in any optimal path. drop it on the floor and try the next one.
        return false;
    }
    if (traverseVisitor != null) {
        traverseVisitor.visitVertex(runState.u);
    }
    runState.u_vertex = runState.u.getVertex();
    if (verbose)
        System.out.println("   vertex " + runState.u_vertex);
    runState.nVisited += 1;
    Collection<Edge> edges = runState.options.arriveBy ? runState.u_vertex.getIncoming() : runState.u_vertex.getOutgoing();
    for (Edge edge : edges) {
        // returning NULL), the iteration is over. TODO Use this to board multiple trips.
        for (State v = edge.traverse(runState.u); v != null; v = v.getNextResult()) {
            if (traverseVisitor != null) {
                traverseVisitor.visitEdge(edge, v);
            }
            double remaining_w = runState.heuristic.estimateRemainingWeight(v);
            if (remaining_w < 0 || Double.isInfinite(remaining_w)) {
                continue;
            }
            double estimate = v.getWeight() + remaining_w;
            if (verbose) {
                System.out.println("      edge " + edge);
                System.out.println("      " + runState.u.getWeight() + " -> " + v.getWeight() + "(w) + " + remaining_w + "(heur) = " + estimate + " vert = " + v.getVertex());
            }
            // avoid enqueuing useless branches
            if (estimate > runState.options.maxWeight) {
                // too expensive to get here
                if (verbose)
                    System.out.println("         too expensive to reach, not enqueued. estimated weight = " + estimate);
                continue;
            }
            if (isWorstTimeExceeded(v, runState.options)) {
                // too much time to get here
                if (verbose)
                    System.out.println("         too much time to reach, not enqueued. time = " + v.getTimeSeconds());
                continue;
            }
            // spt.add returns true if the state is hopeful; enqueue state if it's hopeful
            if (runState.spt.add(v)) {
                // report to the visitor if there is one
                if (traverseVisitor != null)
                    traverseVisitor.visitEnqueue(v);
                // LOG.info("u.w={} v.w={} h={}", runState.u.weight, v.weight, remaining_w);
                runState.pq.insert(v, estimate);
            }
        }
    }
    return true;
}
Also used : State(org.opentripplanner.routing.core.State) Edge(org.opentripplanner.routing.graph.Edge)

Example 92 with Edge

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

the class GenericDijkstra method getShortestPathTree.

public ShortestPathTree getShortestPathTree(State initialState) {
    Vertex target = null;
    if (options.rctx != null) {
        target = initialState.getOptions().rctx.target;
    }
    ShortestPathTree spt = new DominanceFunction.MinimumWeight().getNewShortestPathTree(options);
    BinHeap<State> queue = new BinHeap<State>(1000);
    spt.add(initialState);
    queue.insert(initialState, initialState.getWeight());
    while (!queue.empty()) {
        // Until the priority queue is empty:
        State u = queue.extract_min();
        Vertex u_vertex = u.getVertex();
        if (traverseVisitor != null) {
            traverseVisitor.visitVertex(u);
        }
        if (!spt.getStates(u_vertex).contains(u)) {
            continue;
        }
        if (verbose) {
            System.out.println("min," + u.getWeight());
            System.out.println(u_vertex);
        }
        if (searchTerminationStrategy != null && searchTerminationStrategy.shouldSearchTerminate(initialState.getVertex(), null, u, spt, options)) {
            break;
        }
        for (Edge edge : options.arriveBy ? u_vertex.getIncoming() : u_vertex.getOutgoing()) {
            if (skipEdgeStrategy != null && skipEdgeStrategy.shouldSkipEdge(initialState.getVertex(), null, u, edge, spt, options)) {
                continue;
            }
            // returning NULL), the iteration is over.
            for (State v = edge.traverse(u); v != null; v = v.getNextResult()) {
                if (skipTraverseResultStrategy != null && skipTraverseResultStrategy.shouldSkipTraversalResult(initialState.getVertex(), null, u, v, spt, options)) {
                    continue;
                }
                if (traverseVisitor != null) {
                    traverseVisitor.visitEdge(edge, v);
                }
                if (verbose) {
                    System.out.printf("  w = %f + %f = %f %s", u.getWeight(), v.getWeightDelta(), v.getWeight(), v.getVertex());
                }
                if (v.exceedsWeightLimit(options.maxWeight))
                    continue;
                if (spt.add(v)) {
                    double estimate = heuristic.estimateRemainingWeight(v);
                    queue.insert(v, v.getWeight() + estimate);
                    if (traverseVisitor != null)
                        traverseVisitor.visitEnqueue(v);
                }
            }
        }
    }
    return spt;
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) BinHeap(org.opentripplanner.common.pqueue.BinHeap) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction) Edge(org.opentripplanner.routing.graph.Edge)

Example 93 with Edge

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

the class StreetNotesService method getNotes.

/**
 * Return the set of notes applicable for this state / backedge pair.
 *
 * @param state
 * @return The set of notes or null if empty.
 */
public Set<Alert> getNotes(State state) {
    Edge edge = state.getBackEdge();
    Set<MatcherAndAlert> maas = new HashSet<MatcherAndAlert>();
    for (StreetNotesSource source : sources) {
        Set<MatcherAndAlert> maas2 = source.getNotes(edge);
        if (maas2 != null)
            maas.addAll(maas2);
    }
    if (maas == null || maas.isEmpty()) {
        return null;
    }
    Set<Alert> notes = new HashSet<Alert>(maas.size());
    for (MatcherAndAlert maa : maas) {
        if (maa.getMatcher().matches(state))
            notes.add(maa.getNote());
    }
    if (notes.isEmpty())
        return null;
    return notes;
}
Also used : Alert(org.opentripplanner.routing.alertpatch.Alert) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 94 with Edge

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

the class SPTWalker method walk.

/**
 * Walk over a SPT. Call a visitor for each visited point.
 */
public void walk(SPTVisitor visitor, double d0) {
    int nTotal = 0, nSkippedDupEdge = 0, nSkippedNoGeometry = 0;
    Collection<? extends State> allStates = spt.getAllStates();
    Set<Vertex> allVertices = new HashSet<Vertex>(spt.getVertexCount());
    for (State s : allStates) {
        allVertices.add(s.getVertex());
    }
    Set<Edge> processedEdges = new HashSet<Edge>(allVertices.size());
    for (Vertex v : allVertices) {
        State s0 = spt.getState(v);
        if (s0 == null || !s0.isFinal())
            continue;
        for (Edge e : s0.getVertex().getIncoming()) {
            // Take only street
            if (e != null && visitor.accept(e)) {
                State s1 = spt.getState(e.getFromVertex());
                if (s1 == null || !s1.isFinal())
                    continue;
                if (e.getFromVertex() != null && e.getToVertex() != null) {
                    // Hack alert: e.hashCode() throw NPE
                    if (processedEdges.contains(e)) {
                        nSkippedDupEdge++;
                        continue;
                    }
                    processedEdges.add(e);
                }
                Vertex vx0 = s0.getVertex();
                Vertex vx1 = s1.getVertex();
                LineString lineString = e.getGeometry();
                if (lineString == null) {
                    nSkippedNoGeometry++;
                    continue;
                }
                // Compute speed along edge
                double speedAlongEdge = spt.getOptions().walkSpeed;
                if (e instanceof StreetEdge) {
                    StreetEdge se = (StreetEdge) e;
                    /*
                         * Compute effective speed, taking into account end state mode (car, bike,
                         * walk...) and edge properties (car max speed, slope, etc...)
                         */
                    TraverseMode mode = s0.getNonTransitMode();
                    speedAlongEdge = se.calculateSpeed(spt.getOptions(), mode, s0.getTimeInMillis());
                    if (mode != TraverseMode.CAR)
                        speedAlongEdge = speedAlongEdge * se.getDistance() / se.getSlopeSpeedEffectiveLength();
                    double avgSpeed = se.getDistance() / Math.abs(s0.getTimeInMillis() - s1.getTimeInMillis()) * 1000;
                    if (avgSpeed < 1e-10)
                        avgSpeed = 1e-10;
                    /*
                         * We can't go faster than the average speed on the edge. We can go slower
                         * however, that simply means that one end vertice has a time higher than
                         * the other end vertice + time to traverse the edge (can happen due to
                         * max walk clamping).
                         */
                    if (speedAlongEdge > avgSpeed)
                        speedAlongEdge = avgSpeed;
                }
                // Length of linestring
                double lineStringLen = SphericalDistanceLibrary.fastLength(lineString);
                visitor.visit(e, vx0.getCoordinate(), s0, s1, 0.0, lineStringLen, speedAlongEdge);
                visitor.visit(e, vx1.getCoordinate(), s0, s1, lineStringLen, 0.0, speedAlongEdge);
                nTotal += 2;
                Coordinate[] pList = lineString.getCoordinates();
                boolean reverse = vx1.getCoordinate().equals(pList[0]);
                // Split the linestring in nSteps
                if (lineStringLen > d0) {
                    // Number of steps
                    int nSteps = (int) Math.floor(lineStringLen / d0) + 1;
                    // Length of step
                    double stepLen = lineStringLen / nSteps;
                    // Distance at start of current seg
                    double startLen = 0;
                    // Distance cursor
                    double curLen = stepLen;
                    int ns = 1;
                    for (int i = 0; i < pList.length - 1; i++) {
                        Coordinate p0 = pList[i];
                        Coordinate p1 = pList[i + 1];
                        double segLen = SphericalDistanceLibrary.fastDistance(p0, p1);
                        while (curLen - startLen < segLen) {
                            double k = (curLen - startLen) / segLen;
                            Coordinate p = new Coordinate(p0.x * (1 - k) + p1.x * k, p0.y * (1 - k) + p1.y * k);
                            visitor.visit(e, p, reverse ? s1 : s0, reverse ? s0 : s1, curLen, lineStringLen - curLen, speedAlongEdge);
                            nTotal++;
                            curLen += stepLen;
                            ns++;
                        }
                        startLen += segLen;
                        if (ns >= nSteps)
                            break;
                    }
                }
            }
        }
    }
    LOG.info("SPTWalker: Generated {} points ({} dup edges, {} no geometry) from {} vertices / {} states.", nTotal, nSkippedDupEdge, nSkippedNoGeometry, allVertices.size(), allStates.size());
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) LineString(com.vividsolutions.jts.geom.LineString) Coordinate(com.vividsolutions.jts.geom.Coordinate) State(org.opentripplanner.routing.core.State) TraverseMode(org.opentripplanner.routing.core.TraverseMode) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 95 with Edge

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

the class BusRouteStreetMatcher method buildGraph.

/*
       The "extra" parameter is a mechanism for passing arbitrary things between graph builder modules.
       Whether or not this is a good idea is open to debate, but that's what it is.
       An EdgesForRoute instance is generated by MapBuilder and StreetMatcher, then retrieved later by the
       NetworkLinkerLibrary later (actually in LinkRequests).
     */
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
    // Mapbuilder needs transit index
    graph.index(new DefaultStreetVertexIndexFactory());
    StreetMatcher matcher = new StreetMatcher(graph);
    EdgesForRoute edgesForRoute = new EdgesForRoute();
    extra.put(EdgesForRoute.class, edgesForRoute);
    log.info("Finding corresponding street edges for trip patterns...");
    // Why do we need to iterate over the routes? Why not just patterns?
    for (Route route : graph.index.routeForId.values()) {
        for (TripPattern pattern : graph.index.patternsForRoute.get(route)) {
            if (pattern.mode == TraverseMode.BUS) {
                /* we can only match geometry to streets on bus routes */
                log.debug("Matching {}", pattern);
                // that is why pattern.geometry is null in that case
                if (pattern.geometry == null) {
                    continue;
                }
                List<Edge> edges = matcher.match(pattern.geometry);
                if (edges == null || edges.isEmpty()) {
                    log.warn("Could not match to street network: {}", pattern);
                    continue;
                }
                List<Coordinate> coordinates = new ArrayList<Coordinate>();
                for (Edge e : edges) {
                    coordinates.addAll(Arrays.asList(e.getGeometry().getCoordinates()));
                    edgesForRoute.edgesForRoute.put(route, e);
                }
                Coordinate[] coordinateArray = new Coordinate[coordinates.size()];
                LineString ls = GeometryUtils.getGeometryFactory().createLineString(coordinates.toArray(coordinateArray));
                // Replace the pattern's geometry from GTFS with that of the equivalent OSM edges.
                pattern.geometry = ls;
            }
        }
    }
}
Also used : EdgesForRoute(org.opentripplanner.extra_graph.EdgesForRoute) Coordinate(com.vividsolutions.jts.geom.Coordinate) LineString(com.vividsolutions.jts.geom.LineString) ArrayList(java.util.ArrayList) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) Edge(org.opentripplanner.routing.graph.Edge) EdgesForRoute(org.opentripplanner.extra_graph.EdgesForRoute) Route(org.onebusaway.gtfs.model.Route) TripPattern(org.opentripplanner.routing.edgetype.TripPattern)

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