Skip to content

Commit

Permalink
improve performance by reusing objects instead of recreating them
Browse files Browse the repository at this point in the history
  • Loading branch information
mrieser committed May 30, 2024
1 parent 0904608 commit 52712f6
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public RaptorRoute calcLeastCostRoute(double depTime, Facility fromFacility, Fac
final int maxTransfersAfterFirstArrival = 2;

reset();
CachingTransferProvider transferProvider = this.data.new CachingTransferProvider();

Map<TransitStopFacility, InitialStop> destinationStops = new HashMap<>();

Expand Down Expand Up @@ -235,7 +236,7 @@ public RaptorRoute calcLeastCostRoute(double depTime, Facility fromFacility, Fac
BitSet initialRouteStopIndices = new BitSet();
initialRouteStopIndices.or(this.improvedRouteStopIndices);

handleTransfers(true, parameters);
handleTransfers(true, parameters, transferProvider);
this.improvedRouteStopIndices.or(initialRouteStopIndices);
}

Expand All @@ -246,7 +247,7 @@ public RaptorRoute calcLeastCostRoute(double depTime, Facility fromFacility, Fac
// but because we re-use the earliestArrivalTime-array, we don't have to do anything.

// second stage: process routes
exploreRoutes(parameters, person);
exploreRoutes(parameters, person, transferProvider);

PathElement leastCostPath = findLeastCostArrival(destinationStops);

Expand All @@ -262,7 +263,7 @@ public RaptorRoute calcLeastCostRoute(double depTime, Facility fromFacility, Fac
}

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

// final stage: check stop criterion
if (this.improvedRouteStopIndices.isEmpty()) {
Expand All @@ -284,6 +285,7 @@ public List<RaptorRoute> calcRoutes(double earliestDepTime, double desiredDepTim

reset();

CachingTransferProvider transferProvider = this.data.new CachingTransferProvider();
double marginalUtilityOfWaitingPt_utl_s = parameters.getMarginalUtilityOfWaitingPt_utl_s();

PathElement lastFoundBestPath = null;
Expand Down Expand Up @@ -381,7 +383,7 @@ public List<RaptorRoute> calcRoutes(double earliestDepTime, double desiredDepTim
// but because we re-use the earliestArrivalTime-array, we don't have to do anything.

// second stage: process routes
exploreRoutes(parameters, person);
exploreRoutes(parameters, person, transferProvider);

PathElement leastCostPath = findLeastCostArrival(destinationStops);
if (leastCostPath != null && (lastFoundBestPath == null || leastCostPath.comingFrom != lastFoundBestPath.comingFrom)) {
Expand All @@ -407,7 +409,7 @@ public List<RaptorRoute> calcRoutes(double earliestDepTime, double desiredDepTim
}

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

// final stage: check stop criterion
if (this.improvedRouteStopIndices.isEmpty()) {
Expand Down Expand Up @@ -493,6 +495,7 @@ public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime
public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime, Collection<InitialStop> startStops, RaptorParameters parameters, Person person, RaptorObserver observer) {
reset();

CachingTransferProvider transferProvider = this.data.new CachingTransferProvider();
BitSet initialRouteStopIndices = new BitSet();
BitSet initialStopIndices = new BitSet();
for (InitialStop stop : startStops) {
Expand Down Expand Up @@ -538,7 +541,7 @@ public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime
// but because we re-use the earliestArrivalTime-array, we don't have to do anything.

// second stage: process routes
exploreRoutes(parameters, person);
exploreRoutes(parameters, person, transferProvider);

if (this.improvedStops.isEmpty()) {
break;
Expand Down Expand Up @@ -571,7 +574,7 @@ public Map<Id<TransitStopFacility>, TravelInfo> calcLeastCostTree(double depTime
}

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

// final stage: check stop criterion
Expand Down Expand Up @@ -625,12 +628,10 @@ private TravelInfo getTravelInfo(PathElement destination, RaptorParameters param
return new TravelInfo(departureStopId, departureTimeAtFirstStop, arrivalTimeAtLastStop, travelCost, accessTime, accessCost, transferCount, waitingTime, waitingCost, destination);
}

private void exploreRoutes(RaptorParameters parameters, Person person) {
private void exploreRoutes(RaptorParameters parameters, Person person, CachingTransferProvider transferProvider) {
this.improvedStops.clear();
this.reachedRouteStopIndices.clear();

CachingTransferProvider transferProvider = this.data.new CachingTransferProvider();

double marginalUtilityOfWaitingPt_utl_s = parameters.getMarginalUtilityOfWaitingPt_utl_s();
boolean useTransportModeUtilities = parameters.isUseTransportModeUtilities();

Expand Down Expand Up @@ -783,12 +784,11 @@ private int findNextDepartureIndexWithConstraints(RRoute route, RRouteStop route
return this.data.occupancyData.getNextAvailableDeparture(this.data, routeStop, time);
}

private void handleTransfers(boolean strict, RaptorParameters raptorParams) {
private void handleTransfers(boolean strict, RaptorParameters raptorParams, CachingTransferProvider transferProvider) {
this.improvedRouteStopIndices.clear();
this.tmpImprovedStops.clear();

double margUtilityTransitWalk = raptorParams.getMarginalUtilityOfTravelTime_utl_s(TransportMode.walk); // replaced TransportMode.transit_walk with walk
CachingTransferProvider transferProvider = this.data.new CachingTransferProvider();

for (int stopIndex = this.improvedStops.nextSetBit(0); stopIndex >= 0; stopIndex = this.improvedStops.nextSetBit(stopIndex + 1)) {
PathElement fromPE = this.arrivalPathPerStop[stopIndex];
Expand Down

0 comments on commit 52712f6

Please sign in to comment.