Skip to content

Commit

Permalink
feat: prebooking for drt (#2826)
Browse files Browse the repository at this point in the history
* feat: prebooking for drt

* fix taxi contrib

* fix drt-extensions

* fix recovery of DefaultRequestQueue

* update taxi unit tests with new event format

* fix fiss integration test

* add earliestDepartureTime to output and fix typo

* cleanup for multi-iteration simulations

* restructuring some elements

* rename PassengerEnteringEvent -> PassengerEnteringVehicleEvent

* move events

* add prebooking analysis

* some further cleanup

* backport & simplifications

* fixes for 'passenger waiting' event

* trying to pass electric test with Id.resetCaches

* trying to make population iteration order deterministic if not already ...

* revert changes for testing

* reduce edrt testing footprint

* clean analysis handler

* remove unused method

* adjust PrebookingManager

* add comment for wait scheduling config

* factor out RequestQueue

* specific unplannedRequests for taxi optimizer

* factor out advanceRequestHorizon

* merge PrebookingPassengerEngine into DefaultPassengerEngine, introduce AdvanceRequestProvider

* phase out DrtWaitTask

* forgot to fix shift factories

* fix two unit tests

* change some Verify to Preconditions

* update handling of occupany analysis

* move remaining prebooking code to drt, integrate in DI

* small fix for test

* clean up prebooking logic

* fix latest changes in shifts

* fix latest changes in shifts

* revert some formatting and visibility changes
  • Loading branch information
sebhoerl authored Nov 16, 2023
1 parent 05e5f2f commit 2b684a2
Show file tree
Hide file tree
Showing 93 changed files with 3,436 additions and 517 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@

package org.matsim.contrib.drt.extension;

import java.util.Optional;

import javax.annotation.Nullable;

import org.matsim.contrib.drt.extension.companions.DrtCompanionParams;
import org.matsim.contrib.drt.extension.operations.DrtOperationsParams;
import org.matsim.contrib.drt.run.DrtConfigGroup;

import javax.annotation.Nullable;
import java.util.Optional;

/**
* @author Steffen Axer
*
Expand Down Expand Up @@ -56,5 +57,5 @@ public Optional<DrtCompanionParams> getDrtCompanionParams() {
public Optional<DrtOperationsParams> getDrtOperationsParams() {
return Optional.ofNullable(drtOperationsParams);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@

package org.matsim.contrib.drt.extension.edrt;

import org.matsim.contrib.drt.vrpagent.DrtActionCreator;
import org.matsim.contrib.drt.extension.edrt.schedule.EDrtChargingTask;
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.passenger.PassengerHandler;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.dvrp.schedule.DriveTask;
import org.matsim.contrib.dvrp.schedule.Task;
import org.matsim.contrib.dvrp.tracker.OnlineDriveTaskTracker;
Expand All @@ -33,7 +31,6 @@
import org.matsim.contrib.dvrp.vrpagent.VrpLeg;
import org.matsim.contrib.dynagent.DynAction;
import org.matsim.contrib.dynagent.DynAgent;
import org.matsim.contrib.drt.extension.edrt.schedule.EDrtChargingTask;
import org.matsim.contrib.evrp.ChargingActivity;
import org.matsim.contrib.evrp.ChargingTask;
import org.matsim.contrib.evrp.EvDvrpVehicle;
Expand All @@ -45,12 +42,12 @@
* @author michalm
*/
public class EDrtActionCreator implements VrpAgentLogic.DynActionCreator {
private final DrtActionCreator drtActionCreator;
private final VrpAgentLogic.DynActionCreator delegate;
private final MobsimTimer timer;

public EDrtActionCreator(PassengerHandler passengerHandler, MobsimTimer timer, DvrpConfigGroup dvrpCfg) {
public EDrtActionCreator(VrpAgentLogic.DynActionCreator delegate, MobsimTimer timer) {
this.timer = timer;
drtActionCreator = new DrtActionCreator(passengerHandler, v -> createLeg(dvrpCfg.mobsimMode, v, timer));
this.delegate = delegate;
}

@Override
Expand All @@ -60,15 +57,15 @@ public DynAction createAction(DynAgent dynAgent, DvrpVehicle vehicle, double now
task.initTaskTracker(new OfflineETaskTracker((EvDvrpVehicle)vehicle, timer));
return new ChargingActivity((ChargingTask)task);
} else {
DynAction dynAction = drtActionCreator.createAction(dynAgent, vehicle, now);
DynAction dynAction = delegate.createAction(dynAgent, vehicle, now);
if (task.getTaskTracker() == null) {
task.initTaskTracker(new OfflineETaskTracker((EvDvrpVehicle)vehicle, timer));
}
return dynAction;
}
}

private static VrpLeg createLeg(String mobsimMode, DvrpVehicle vehicle, MobsimTimer timer) {
public static VrpLeg createLeg(String mobsimMode, DvrpVehicle vehicle, MobsimTimer timer) {
DriveTask driveTask = (DriveTask)vehicle.getSchedule().getCurrentTask();
VrpLeg leg = new VrpLeg(mobsimMode, driveTask.getPath());
OnlineDriveTaskTracker onlineTracker = new OnlineDriveTaskTrackerImpl(vehicle, leg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@

import java.util.List;

import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.extension.edrt.schedule.EDrtChargingTask;
import org.matsim.contrib.drt.optimizer.VehicleDataEntryFactoryImpl;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.schedule.Schedule;
import org.matsim.contrib.dvrp.schedule.Schedule.ScheduleStatus;
import org.matsim.contrib.dvrp.schedule.Task;
import org.matsim.contrib.dvrp.schedule.Task.TaskStatus;
import org.matsim.contrib.drt.extension.edrt.schedule.EDrtChargingTask;
import org.matsim.contrib.ev.fleet.Battery;
import org.matsim.contrib.evrp.ETask;
import org.matsim.contrib.evrp.EvDvrpVehicle;
import org.matsim.contrib.evrp.tracker.ETaskTracker;
import org.matsim.contrib.ev.fleet.Battery;

import com.google.inject.Provider;

Expand All @@ -52,17 +51,13 @@ public EVehicleEntry(VehicleEntry entry, double socBeforeFinalStay) {
private final double minimumRelativeSoc;
private final VehicleDataEntryFactoryImpl entryFactory;

public EDrtVehicleDataEntryFactory(DrtConfigGroup drtCfg, double minimumRelativeSoc) {
public EDrtVehicleDataEntryFactory(double minimumRelativeSoc) {
this.minimumRelativeSoc = minimumRelativeSoc;
entryFactory = new VehicleDataEntryFactoryImpl(drtCfg);
entryFactory = new VehicleDataEntryFactoryImpl();
}

@Override
public VehicleEntry create(DvrpVehicle vehicle, double currentTime) {
if (entryFactory.isNotEligibleForRequestInsertion(vehicle, currentTime)) {
return null;
}

Schedule schedule = vehicle.getSchedule();
int taskCount = schedule.getTaskCount();
if (taskCount > 1) {
Expand Down Expand Up @@ -100,17 +95,15 @@ public VehicleEntry create(DvrpVehicle vehicle, double currentTime) {
}

public static class EDrtVehicleDataEntryFactoryProvider implements Provider<VehicleEntry.EntryFactory> {
private final DrtConfigGroup drtCfg;
private final double minimumRelativeSoc;

public EDrtVehicleDataEntryFactoryProvider(DrtConfigGroup drtCfg, double minimumRelativeSoc) {
this.drtCfg = drtCfg;
public EDrtVehicleDataEntryFactoryProvider(double minimumRelativeSoc) {
this.minimumRelativeSoc = minimumRelativeSoc;
}

@Override
public EDrtVehicleDataEntryFactory get() {
return new EDrtVehicleDataEntryFactory(drtCfg, minimumRelativeSoc);
return new EDrtVehicleDataEntryFactory(minimumRelativeSoc);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,24 @@
import org.matsim.contrib.drt.optimizer.insertion.UnplannedRequestInserter;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy;
import org.matsim.contrib.drt.passenger.DrtOfferAcceptor;
import org.matsim.contrib.drt.prebooking.PrebookingActionCreator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.schedule.DrtTaskFactory;
import org.matsim.contrib.drt.scheduler.DefaultRequestInsertionScheduler;
import org.matsim.contrib.drt.scheduler.DrtScheduleInquiry;
import org.matsim.contrib.drt.scheduler.EmptyVehicleRelocator;
import org.matsim.contrib.drt.scheduler.RequestInsertionScheduler;
import org.matsim.contrib.drt.stops.PassengerStopDurationProvider;
import org.matsim.contrib.drt.stops.StopTimeCalculator;
import org.matsim.contrib.drt.vrpagent.DrtActionCreator;
import org.matsim.contrib.dvrp.fleet.Fleet;
import org.matsim.contrib.dvrp.optimizer.VrpOptimizer;
import org.matsim.contrib.dvrp.passenger.PassengerHandler;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeQSimModule;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpModes;
import org.matsim.contrib.dvrp.schedule.ScheduleTimingUpdater;
import org.matsim.contrib.dvrp.vrpagent.VrpAgentLogic;
import org.matsim.contrib.dvrp.vrpagent.VrpLegFactory;
import org.matsim.contrib.ev.infrastructure.ChargingInfrastructure;
import org.matsim.contrib.ev.infrastructure.ChargingInfrastructureUtils;
import org.matsim.core.api.experimental.events.EventsManager;
Expand All @@ -69,6 +72,7 @@
import org.matsim.core.router.util.TravelTime;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
* @author Michal Maciejewski (michalm)
Expand All @@ -88,11 +92,13 @@ protected void configureQSim() {
getter.getModal(EmptyVehicleChargingScheduler.class))));

bindModal(DefaultDrtOptimizer.class).toProvider(modalProvider(
getter -> new DefaultDrtOptimizer(drtCfg, getter.getModal(Fleet.class), getter.get(MobsimTimer.class),
getter -> {
return new DefaultDrtOptimizer(drtCfg, getter.getModal(Fleet.class), getter.get(MobsimTimer.class),
getter.getModal(DepotFinder.class), getter.getModal(RebalancingStrategy.class),
getter.getModal(DrtScheduleInquiry.class), getter.getModal(ScheduleTimingUpdater.class),
getter.getModal(EmptyVehicleRelocator.class), getter.getModal(UnplannedRequestInserter.class),
getter.getModal(DrtRequestInsertionRetryQueue.class)))).asEagerSingleton();
getter.getModal(DrtRequestInsertionRetryQueue.class));
})).asEagerSingleton();

bindModal(ChargingInfrastructure.class).toProvider(modalProvider(
getter -> ChargingInfrastructureUtils.createModalNetworkChargers(getter.get(ChargingInfrastructure.class ),
Expand Down Expand Up @@ -129,7 +135,8 @@ public EmptyVehicleChargingScheduler get() {
getter.getModal(RequestInsertionScheduler.class),
getter.getModal(VehicleEntry.EntryFactory.class), getter.getModal(DrtInsertionSearch.class),
getter.getModal(DrtRequestInsertionRetryQueue.class), getter.getModal(DrtOfferAcceptor.class),
getter.getModal(QSimScopeForkJoinPoolHolder.class).getPool()))).asEagerSingleton();
getter.getModal(QSimScopeForkJoinPoolHolder.class).getPool(),
getter.getModal(PassengerStopDurationProvider.class)))).asEagerSingleton();

bindModal(InsertionCostCalculator.class).toProvider(modalProvider(
getter -> new DefaultInsertionCostCalculator(getter.getModal(CostCalculationStrategy.class))));
Expand Down Expand Up @@ -163,11 +170,12 @@ public EmptyVehicleRelocator get() {

bindModal(DrtScheduleInquiry.class).to(DrtScheduleInquiry.class).asEagerSingleton();

boolean scheduleWaitBeforeDrive = drtCfg.getPrebookingParams().map(p -> p.scheduleWaitBeforeDrive).orElse(false);
bindModal(RequestInsertionScheduler.class).toProvider(modalProvider(
getter -> new DefaultRequestInsertionScheduler(getter.getModal(Fleet.class),
getter.get(MobsimTimer.class), getter.getModal(TravelTime.class),
getter.getModal(ScheduleTimingUpdater.class), getter.getModal(DrtTaskFactory.class),
getter.getModal(StopTimeCalculator.class))))
getter.getModal(StopTimeCalculator.class), scheduleWaitBeforeDrive)))
.asEagerSingleton();

bindModal(DrtOfferAcceptor.class).toInstance(DrtOfferAcceptor.DEFAULT_ACCEPTOR);
Expand All @@ -176,10 +184,26 @@ public EmptyVehicleRelocator get() {
getter -> new ScheduleTimingUpdater(getter.get(MobsimTimer.class),
new EDrtStayTaskEndTimeCalculator(getter.getModal(StopTimeCalculator.class))))).asEagerSingleton();

bindModal(VrpAgentLogic.DynActionCreator.class).toProvider(modalProvider(
getter -> new EDrtActionCreator(getter.getModal(PassengerHandler.class), getter.get(MobsimTimer.class),
getter.get(DvrpConfigGroup.class)))).asEagerSingleton();

bindModal(VrpLegFactory.class).toProvider(modalProvider(getter -> {
DvrpConfigGroup dvrpCfg = getter.get(DvrpConfigGroup.class);
MobsimTimer timer = getter.get(MobsimTimer.class);

// Makes basic DrtActionCreator create legs with consumption tracker
return v -> EDrtActionCreator.createLeg(dvrpCfg.mobsimMode, v, timer);
})).in(Singleton.class);

bindModal(EDrtActionCreator.class).toProvider(modalProvider(getter -> {
VrpAgentLogic.DynActionCreator delegate = drtCfg.getPrebookingParams().isPresent()
? getter.getModal(PrebookingActionCreator.class)
: getter.getModal(DrtActionCreator.class);

// EDrtActionCreator wraps around delegate and initializes consumption trackers
// + adds ChargingActivity
return new EDrtActionCreator(delegate, getter.get(MobsimTimer.class));
})).asEagerSingleton();

bindModal(VrpAgentLogic.DynActionCreator.class).to(modalKey(EDrtActionCreator.class));

bindModal(VrpOptimizer.class).to(modalKey(DrtOptimizer.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public static void run(URL configUrl, boolean otfvis) {
new OTFVisConfigGroup(), new EvConfigGroup()), otfvis);
}

public static void run(Config config, boolean otfvis) {
public static Controler createControler(Config config, boolean otfvis) {
Controler controler = EDrtControlerCreator.createControler(config, otfvis);
for (DrtConfigGroup drtCfg : MultiModeDrtConfigGroup.get(config).getModalElements()) {
controler.addOverridingModule(new AbstractDvrpModeModule(drtCfg.getMode()) {
@Override
public void install() {
bind(EDrtVehicleDataEntryFactoryProvider.class).toInstance(
new EDrtVehicleDataEntryFactoryProvider(drtCfg, MIN_RELATIVE_SOC));
new EDrtVehicleDataEntryFactoryProvider(MIN_RELATIVE_SOC));
}
});
}
Expand All @@ -75,6 +75,11 @@ public void install() {
bind(TemperatureService.class).toInstance(linkId -> TEMPERATURE);
}
});
controler.run();

return controler;
}

public static void run(Config config, boolean otfvis) {
createControler(config, otfvis).run();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.matsim.contrib.drt.extension.operations.eshifts.run;

import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.extension.edrt.EDrtActionCreator;
import org.matsim.contrib.drt.extension.edrt.optimizer.EDrtVehicleDataEntryFactory;
import org.matsim.contrib.drt.extension.edrt.schedule.EDrtTaskFactoryImpl;
import org.matsim.contrib.drt.extension.edrt.scheduler.EmptyVehicleChargingScheduler;
Expand All @@ -20,23 +21,29 @@
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DrtShiftDispatcher;
import org.matsim.contrib.drt.extension.operations.shifts.dispatcher.DrtShiftDispatcherImpl;
import org.matsim.contrib.drt.extension.operations.shifts.optimizer.ShiftVehicleDataEntryFactory;
import org.matsim.contrib.drt.extension.operations.shifts.schedule.ShiftDrtActionCreator;
import org.matsim.contrib.drt.extension.operations.shifts.schedule.ShiftDrtTaskFactory;
import org.matsim.contrib.drt.extension.operations.shifts.scheduler.ShiftTaskScheduler;
import org.matsim.contrib.drt.extension.operations.shifts.shift.DrtShifts;
import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.prebooking.PrebookingActionCreator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.schedule.DrtTaskFactory;
import org.matsim.contrib.drt.vrpagent.DrtActionCreator;
import org.matsim.contrib.dvrp.fleet.Fleet;
import org.matsim.contrib.dvrp.passenger.PassengerHandler;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeQSimModule;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.dvrp.vrpagent.VrpAgentLogic;
import org.matsim.contrib.dvrp.vrpagent.VrpLegFactory;
import org.matsim.contrib.ev.infrastructure.ChargingInfrastructure;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.mobsim.framework.MobsimTimer;
import org.matsim.core.router.costcalculators.TravelDisutilityFactory;
import org.matsim.core.router.util.TravelTime;

import com.google.inject.Singleton;

/**
* @author nkuehnel / MOIA
*/
Expand Down Expand Up @@ -69,7 +76,7 @@ drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
new EDrtAssignShiftToVehicleLogic(new DefaultAssignShiftToVehicleLogic(drtShiftParams), drtShiftParams)),
getter.getModal(Fleet.class)))).asEagerSingleton();

bindModal(VehicleEntry.EntryFactory.class).toProvider(modalProvider(getter -> new ShiftVehicleDataEntryFactory(new EDrtVehicleDataEntryFactory(drtCfg, 0)))).asEagerSingleton();
bindModal(VehicleEntry.EntryFactory.class).toProvider(modalProvider(getter -> new ShiftVehicleDataEntryFactory(new EDrtVehicleDataEntryFactory(0)))).asEagerSingleton();

final ShiftEDrtTaskFactoryImpl taskFactory = new ShiftEDrtTaskFactoryImpl(new EDrtTaskFactoryImpl());
bindModal(DrtTaskFactory.class).toInstance(taskFactory);
Expand All @@ -81,10 +88,26 @@ drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
getter.get(MobsimTimer.class), taskFactory, drtShiftParams, getter.getModal(ChargingInfrastructure.class),
getter.getModal(OperationFacilities.class), getter.getModal(Fleet.class))
)).asEagerSingleton();

// See EDrtModeOptimizerQSimModule
bindModal(VrpLegFactory.class).toProvider(modalProvider(getter -> {
DvrpConfigGroup dvrpCfg = getter.get(DvrpConfigGroup.class);
MobsimTimer timer = getter.get(MobsimTimer.class);

// Makes basic DrtActionCreator create legs with consumption tracker
return v -> EDrtActionCreator.createLeg(dvrpCfg.mobsimMode, v, timer);
})).in(Singleton.class);

bindModal(ShiftEDrtActionCreator.class).toProvider(modalProvider(getter -> {
VrpAgentLogic.DynActionCreator delegate = drtCfg.getPrebookingParams().isPresent()
? getter.getModal(PrebookingActionCreator.class)
: getter.getModal(DrtActionCreator.class);

bindModal(VrpAgentLogic.DynActionCreator.class).toProvider(modalProvider(
getter -> new ShiftEDrtActionCreator(getter.getModal(PassengerHandler.class),
getter.get(MobsimTimer.class), getter.get(DvrpConfigGroup.class)))
).asEagerSingleton();
return new ShiftEDrtActionCreator(
new ShiftDrtActionCreator(getter.getModal(PassengerHandler.class), delegate),
getter.get(MobsimTimer.class), getter.getModal(PassengerHandler.class));
})).asEagerSingleton();

bindModal(VrpAgentLogic.DynActionCreator.class).to(modalKey(ShiftEDrtActionCreator.class));
}
}
Loading

0 comments on commit 2b684a2

Please sign in to comment.