Skip to content

Commit

Permalink
improve start time calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Dec 30, 2024
1 parent 763b912 commit 030475e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.matsim.modechoice;

import com.google.inject.Inject;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.config.groups.PlansConfigGroup;
import org.matsim.core.router.AnalysisMainModeIdentifier;
import org.matsim.core.router.TripRouter;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.utils.timing.TimeInterpretation;
Expand All @@ -23,14 +24,18 @@ public final class EstimateRouter {

private final TripRouter tripRouter;
private final ActivityFacilities facilities;
private final AnalysisMainModeIdentifier mmi;
private final TimeInterpretation timeInterpretation;

@Inject
public EstimateRouter(TripRouter tripRouter, ActivityFacilities facilities,
TimeInterpretation timeInterpretation) {
AnalysisMainModeIdentifier mmi) {
this.tripRouter = tripRouter;
this.facilities = facilities;
this.timeInterpretation = timeInterpretation;
this.mmi = mmi;
// ignore the travel times of individual legs
this.timeInterpretation = TimeInterpretation.create(PlansConfigGroup.ActivityDurationInterpretation.tryEndTimeThenDuration,
PlansConfigGroup.TripDurationHandling.ignoreDelays);
}

/**
Expand Down Expand Up @@ -73,14 +78,14 @@ public void routeModes(PlanModel model, Collection<String> modes) {
}
*/

// Use the end-time of an activity or the time tracker if not available
final List<? extends PlanElement> newTrip = tripRouter.calcRoute(
mode, from, to,
oldTrip.getOriginActivity().getEndTime().orElse(timeTracker.getTime().seconds()),
model.getPerson(),
oldTrip.getTripAttributes()
);

// update time tracker, however it will be updated before each iteration
timeTracker.addElements(newTrip);

// store and increment
Expand Down Expand Up @@ -131,12 +136,6 @@ public void routeSingleTrip(PlanModel model, Collection<String> modes, int idx)
.map(el -> (Leg) el)
.collect(Collectors.toList());

// not a real pt trip, see reasoning above
if (mode.equals(TransportMode.pt) && ll.stream().noneMatch(l -> l.getMode().equals(TransportMode.pt))) {
model.setLegs(mode, new List[model.trips()]);
continue;
}

List<Leg>[] legs = new List[model.trips()];
legs[idx] = ll;

Expand Down Expand Up @@ -168,10 +167,13 @@ private double advanceTimetracker(TimeTracker timeTracker, TripStructureUtils.Tr
List<Leg> oldLegs = oldTrip.getLegsOnly();
boolean undefined = oldLegs.stream().anyMatch(l -> timeInterpretation.decideOnLegTravelTime(l).isUndefined());

// If no time is known the previous trips need to be routed
// If no time is known the previous trips needs to be routed
if (undefined) {
String routingMode = TripStructureUtils.getRoutingMode(oldLegs.get(0));
List<? extends PlanElement> legs = routeTrip(oldTrip, plan, routingMode != null ? routingMode : oldLegs.get(0).getMode(), timeTracker);
String routingMode = TripStructureUtils.getRoutingMode(oldLegs.getFirst());
if (routingMode == null)
routingMode = mmi.identifyMainMode(oldLegs);

List<? extends PlanElement> legs = routeTrip(oldTrip, plan, routingMode, timeTracker);
timeTracker.addElements(legs);

} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.matsim.core.config.groups.PlansConfigGroup;
import org.matsim.core.controler.ControlerListenerManager;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.router.DefaultAnalysisMainModeIdentifier;
import org.matsim.core.router.TripRouter;
import org.matsim.core.scoring.functions.ScoringParameters;
import org.matsim.core.scoring.functions.ScoringParametersForPerson;
Expand Down Expand Up @@ -207,7 +208,8 @@ else if (invocationOnMock.getArgument(0).equals(TransportMode.walk)) {

bind(EstimateRouter.class).toInstance(new EstimateRouter(router,
FacilitiesUtils.createActivityFacilities(),
TimeInterpretation.create(PlansConfigGroup.ActivityDurationInterpretation.minOfDurationAndEndTime, PlansConfigGroup.TripDurationHandling.shiftActivityEndTimes)));
new DefaultAnalysisMainModeIdentifier()
));

MapBinder<String, ModeOptions> optionBinder = MapBinder.newMapBinder(binder(), new TypeLiteral<>() {}, new TypeLiteral<>(){});
optionBinder.addBinding(TransportMode.car).toInstance(new ModeOptions.AlwaysAvailable());
Expand Down

0 comments on commit 030475e

Please sign in to comment.