Skip to content

Commit

Permalink
Walk heuristics to limit access/egress search to 150 stops
Browse files Browse the repository at this point in the history
  • Loading branch information
vesameskanen committed May 23, 2023
1 parent fc3a451 commit 910ef84
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.opentripplanner.routing.api.request.StreetMode.CAR_RENTAL;
import static org.opentripplanner.routing.api.request.StreetMode.CAR_TO_PARK;
import static org.opentripplanner.routing.api.request.StreetMode.SCOOTER_RENTAL;
import static org.opentripplanner.routing.api.request.StreetMode.WALK;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -24,6 +25,8 @@
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.RegularStop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This strategy terminates when enough "important" stops are found.
Expand All @@ -42,18 +45,21 @@
*/
public class VehicleToStopSkipEdgeStrategy implements SkipEdgeStrategy<State, Edge> {

private static final Logger LOG = LoggerFactory.getLogger(VehicleToStopSkipEdgeStrategy.class);

public static final Set<StreetMode> applicableModes = Set.of(
BIKE_TO_PARK,
BIKE_RENTAL,
CAR_TO_PARK,
CAR_PICKUP,
CAR_HAILING,
CAR_RENTAL,
SCOOTER_RENTAL
SCOOTER_RENTAL,
WALK
);
private final Function<RegularStop, Collection<TripPattern>> getPatternsForStop;
private final int maxScore;
private final List<TransitFilter> filters;
private final Collection<TransitFilter> filters;
private double sumOfScores;

private final Set<FeedScopedId> stopsCounted = new HashSet<>();
Expand All @@ -62,48 +68,38 @@ public VehicleToStopSkipEdgeStrategy(
Function<RegularStop, Collection<TripPattern>> getPatternsForStop,
Collection<TransitFilter> filters
) {
this.filters = new ArrayList<>(filters);
this.maxScore = 300;
this.filters = filters;
this.maxScore = 150;
this.getPatternsForStop = getPatternsForStop;
}

@Override
public boolean shouldSkipEdge(State current, Edge edge) {
if (current.getNonTransitMode().isWalking()) {
if (
current.getVertex() instanceof TransitStopVertex stopVertex &&
!stopsCounted.contains(stopVertex.getStop().getId())
) {
// TODO: 2022-12-05 filters: check performance on that and verify that this is right. Previously we were filtering just on modes
var stop = stopVertex.getStop();
if (
current.getVertex() instanceof TransitStopVertex stopVertex &&
!stopsCounted.contains(stopVertex.getStop().getId())
) {
// TODO: 2022-12-05 filters: check performance on that and verify that this is right. Previously we were filtering just on modes
var stop = stopVertex.getStop();

// Not using streams. Performance is important here
var patterns = getPatternsForStop.apply(stop);
var score = 0;
for (var pattern : patterns) {
for (var filter : filters) {
if (filter.matchTripPattern(pattern)) {
score += VehicleToStopSkipEdgeStrategy.score(pattern.getMode());
break;
}
// Not using streams. Performance is important here
var patterns = getPatternsForStop.apply(stop);
var score = 0;
for (var pattern : patterns) {
for (var filter : filters) {
if (filter.matchTripPattern(pattern)) {
score = 1;
break;
}
}
if (score > 0) break;
}

stopsCounted.add(stop.getId());
stopsCounted.add(stop.getId());

sumOfScores = sumOfScores + score;
}
return false;
} else {
return sumOfScores >= maxScore;
sumOfScores = sumOfScores + score;
}
}

private static int score(TransitMode mode) {
return switch (mode) {
case RAIL, FERRY, SUBWAY -> 20;
case BUS -> 1;
default -> 2;
};
return sumOfScores >= maxScore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.service.TransitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* These library functions are used by the streetless and streetful stop linkers, and in profile
Expand All @@ -56,6 +58,7 @@
*/
public class NearbyStopFinder {

private static final Logger LOG = LoggerFactory.getLogger(NearbyStopFinder.class);
public final boolean useStreets;

private final TransitService transitService;
Expand Down Expand Up @@ -147,6 +150,7 @@ public Set<NearbyStop> findNearbyStopsConsideringPatterns(
Set<NearbyStop> uniqueStops = new HashSet<>();
uniqueStops.addAll(closestStopForFlexTrip.values());
uniqueStops.addAll(closestStopForPattern.values());

return uniqueStops;
}

Expand Down Expand Up @@ -260,6 +264,7 @@ public List<NearbyStop> findNearbyStopsViaStreets(
stopsFound.add(NearbyStop.nearbyStopForState(min, areaStop));
}
}
LOG.debug("findNearbyStopsViaStreets found {} stops", stopsFound.size());

return stopsFound;
}
Expand All @@ -285,7 +290,7 @@ private SkipEdgeStrategy<State, Edge> getSkipEdgeStrategy(
// what are "good" stops for those accesses. if we have reached a threshold of "good" stops
// we stop the access search.
if (
!reverseDirection &&
// !reverseDirection &&
OTPFeature.VehicleToStopHeuristics.isOn() &&
VehicleToStopSkipEdgeStrategy.applicableModes.contains(
routingRequest.journey().access().mode()
Expand Down

0 comments on commit 910ef84

Please sign in to comment.