Skip to content

Commit

Permalink
add option to limit number of transfers in calcTree
Browse files Browse the repository at this point in the history
  • Loading branch information
mrieser committed May 12, 2024
1 parent 8c9a192 commit fab1ed6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Expand Up @@ -231,21 +231,21 @@ public Map<Id<TransitStopFacility>, SwissRailRaptorCore.TravelInfo> calcTree(Col
for (TransitStopFacility stop : fromStops) {
accessStops.add(new InitialStop(stop, 0, 0, 0, null));
}
return this.calcLeastCostTree(accessStops, departureTime, parameters, person, null);
return this.calcLeastCostTree(accessStops, departureTime, parameters, Integer.MAX_VALUE, person, null);
}

public Map<Id<TransitStopFacility>, SwissRailRaptorCore.TravelInfo> calcTree(Facility fromFacility, double departureTime, Person person, Attributes routingAttributes) {
RaptorParameters parameters = this.parametersForPerson.getRaptorParameters(person);
List<InitialStop> accessStops = findAccessStops(fromFacility, fromFacility, person, departureTime, routingAttributes, parameters);
return this.calcLeastCostTree(accessStops, departureTime, parameters, person, null);
return this.calcLeastCostTree(accessStops, departureTime, parameters, Integer.MAX_VALUE, person, null);
}

/** Calculates a least-cost-tree for every actual departure time between <code>earliestDepartureTime</code>
* and <code>latestDepartureTime</code> at the provided stop-facility.
* This method returns nothing, instead users have to use the <code>observer</code> to collect
* relevant results.
*/
public void calcTreesObservable(TransitStopFacility stopFacility, double earliestDepartureTime, double latestStartTime, RaptorParameters parameters, Person person, RaptorObserver observer) {
public void calcTreesObservable(TransitStopFacility stopFacility, double earliestDepartureTime, double latestStartTime, RaptorParameters parameters, int maxTransfers, Person person, RaptorObserver observer) {
if (this.data.config.getOptimization() != RaptorStaticConfig.RaptorOptimization.OneToAllRouting && !this.treeWarningShown) {
log.warn("SwissRailRaptorData was not initialized with full support for tree calculations and may result in unexpected results. Use `RaptorStaticConfig.setOptimization(RaptorOptimization.OneToAllRouting)` to fix this issue.");
this.treeWarningShown = true;
Expand Down Expand Up @@ -274,14 +274,14 @@ public void calcTreesObservable(TransitStopFacility stopFacility, double earlies
double lastDepTime = -1;
for (double departureTime : departureTimes) {
if (departureTime > lastDepTime) {
calcLeastCostTree(accessStops, departureTime, parameters, person, observer);
calcLeastCostTree(accessStops, departureTime, parameters, maxTransfers, person, observer);
lastDepTime = departureTime;
}
}
}

private Map<Id<TransitStopFacility>, SwissRailRaptorCore.TravelInfo> calcLeastCostTree(Collection<InitialStop> accessStops, double departureTime, RaptorParameters parameters, Person person, RaptorObserver observer) {
return this.raptor.calcLeastCostTree(departureTime, accessStops, parameters, person, observer);
private Map<Id<TransitStopFacility>, SwissRailRaptorCore.TravelInfo> calcLeastCostTree(Collection<InitialStop> accessStops, double departureTime, RaptorParameters parameters, int maxTransfers, Person person, RaptorObserver observer) {
return this.raptor.calcLeastCostTree(departureTime, accessStops, parameters, maxTransfers, person, observer);
}

public SwissRailRaptorData getUnderlyingData() {
Expand Down
Expand Up @@ -487,10 +487,10 @@ private List<RaptorRoute> filterRoutes(List<RaptorRoute> allRoutes) {
}

public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime, Collection<InitialStop> startStops, RaptorParameters parameters, Person person) {
return this.calcLeastCostTree(depTime, startStops, parameters, person, null);
return this.calcLeastCostTree(depTime, startStops, parameters, Integer.MAX_VALUE, person, null);
}

public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime, Collection<InitialStop> startStops, RaptorParameters parameters, Person person, RaptorObserver observer) {
public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime, Collection<InitialStop> startStops, RaptorParameters parameters, int maxTransfers, Person person, RaptorObserver observer) {
reset();

BitSet initialRouteStopIndices = new BitSet();
Expand All @@ -514,6 +514,7 @@ public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime
}

// the main loop
int transfers = 0;
while (true) {
// first stage (according to paper) is to set earliestArrivalTime_k(stop) = earliestArrivalTime_k-1(stop)
// but because we re-use the earliestArrivalTime-array, we don't have to do anything.
Expand All @@ -532,8 +533,13 @@ public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime
initialStopIndices = null;
}

if (transfers >= maxTransfers) {
break;
}

// third stage (according to paper): handle footpaths / transfers
handleTransfers(true, parameters);
transfers++;

// final stage: check stop criterion
if (this.improvedRouteStopIndices.isEmpty()) {
Expand Down

0 comments on commit fab1ed6

Please sign in to comment.