Skip to content

Commit

Permalink
factor out advanceRequestHorizon
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl committed Nov 11, 2023
1 parent 42fe5ae commit 0730ed2
Show file tree
Hide file tree
Showing 13 changed files with 12 additions and 56 deletions.
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 @@ -61,7 +61,7 @@ public static void run(Config config, boolean otfvis) {
@Override
public void install() {
bind(EDrtVehicleDataEntryFactoryProvider.class).toInstance(
new EDrtVehicleDataEntryFactoryProvider(drtCfg, MIN_RELATIVE_SOC));
new EDrtVehicleDataEntryFactoryProvider(MIN_RELATIVE_SOC));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ shiftsParams, new DefaultShiftStartLogic(), new DefaultAssignShiftToVehicleLogic
getter -> new ShiftInsertionCostCalculator(getter.get(MobsimTimer.class),
new DefaultInsertionCostCalculator(getter.getModal(CostCalculationStrategy.class)))));

bindModal(VehicleEntry.EntryFactory.class).toInstance(new ShiftVehicleDataEntryFactory(new VehicleDataEntryFactoryImpl(drtCfg)));
bindModal(VehicleEntry.EntryFactory.class).toInstance(new ShiftVehicleDataEntryFactory(new VehicleDataEntryFactoryImpl()));

final ShiftDrtTaskFactoryImpl taskFactory = new ShiftDrtTaskFactoryImpl(new DrtTaskFactoryImpl());
bindModal(DrtTaskFactory.class).toInstance(taskFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public void install() {
DrtWithExtensionsConfigGroup extensionsConfig = (DrtWithExtensionsConfigGroup) modeConfig;

if (extensionsConfig.getDrtPrebookingParams().isPresent()) {
Verify.verify(modeConfig.advanceRequestPlanningHorizon == Double.POSITIVE_INFINITY,
"Prebooked modes should have advanceRequestPlanningHorizon set to Double.POSITIVE_INFINITY. The submission time to the optimizer will be managed by the prebooking logic.");

install(new PrebookingModeModule(modeConfig.getMode(), isElectric));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void testDrtWithPrebooking() {
for (var drtCfg : MultiModeDrtConfigGroup.get(config).getModalElements()) {
DrtWithExtensionsConfigGroup extensionsConfig = (DrtWithExtensionsConfigGroup) drtCfg;
DrtPrebookingParams prebookingParams = new DrtPrebookingParams();
extensionsConfig.advanceRequestPlanningHorizon = Double.POSITIVE_INFINITY;
extensionsConfig.addParameterSet(prebookingParams);
}

Expand Down Expand Up @@ -95,13 +94,12 @@ public void testElectricDrtWithPrebooking() {
@Override
public void install() {
bind(EDrtVehicleDataEntryFactoryProvider.class)
.toInstance(new EDrtVehicleDataEntryFactoryProvider(drtCfg, 0.2));
.toInstance(new EDrtVehicleDataEntryFactoryProvider(0.2));
}
});

DrtWithExtensionsConfigGroup extensionsConfig = (DrtWithExtensionsConfigGroup) drtCfg;
DrtPrebookingParams prebookingParams = new DrtPrebookingParams();
extensionsConfig.advanceRequestPlanningHorizon = Double.POSITIVE_INFINITY;
extensionsConfig.addParameterSet(prebookingParams);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ private void installPrebooking(Controler controller) {
DrtWithExtensionsConfigGroup drtConfig = (DrtWithExtensionsConfigGroup) MultiModeDrtConfigGroup
.get(controller.getConfig()).getModalElements().stream().findFirst().get();
DrtPrebookingParams prebookingParams = new DrtPrebookingParams();
drtConfig.advanceRequestPlanningHorizon = Double.POSITIVE_INFINITY;
drtConfig.addParameterSet(prebookingParams);
controller.addOverridingModule(new PrebookingModule());
AttributePrebookingLogic.install("drt", controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ private void buildConfig(Config config) {
modeConfig.stopDuration = stopDuration;
modeConfig.idleVehiclesReturnToDepots = false;
modeConfig.vehiclesFile = null;
modeConfig.advanceRequestPlanningHorizon = planningHorizon;

DrtInsertionSearchParams searchParams = new SelectiveInsertionSearchParams();
modeConfig.addDrtInsertionSearchParams(searchParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected void configureQSim() {

install(getInsertionSearchQSimModule(drtCfg));

bindModal(VehicleEntry.EntryFactory.class).toInstance(new VehicleDataEntryFactoryImpl(drtCfg));
bindModal(VehicleEntry.EntryFactory.class).toInstance(new VehicleDataEntryFactoryImpl());

bindModal(CostCalculationStrategy.class).to(drtCfg.rejectRequestIfMaxWaitOrTravelTimeViolated ?
CostCalculationStrategy.RejectSoftConstraintViolations.class :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.ArrayList;
import java.util.List;

import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.schedule.DrtStopTask;
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.schedule.DriveTask;
Expand All @@ -37,33 +36,13 @@
import org.matsim.contrib.dvrp.tracker.OnlineDriveTaskTracker;
import org.matsim.contrib.dvrp.util.LinkTimePair;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

/**
* @author michalm
*/
public class VehicleDataEntryFactoryImpl implements VehicleEntry.EntryFactory {
private final double lookAhead;

public VehicleDataEntryFactoryImpl(DrtConfigGroup drtCfg) {
if (drtCfg.rejectRequestIfMaxWaitOrTravelTimeViolated && drtCfg.advanceRequestPlanningHorizon == 0.0) {
lookAhead = drtCfg.maxWaitTime - drtCfg.stopDuration;
Preconditions.checkArgument(lookAhead >= 0,
"maxWaitTime must not be smaller than stopDuration");
} else {
// if no rejection due to max wait time, the look ahead is infinite
// also if prebooking is used, because we may want to schedule prebooked
// requests to vehicles with service times starting in the future
lookAhead = Double.POSITIVE_INFINITY;
}
}

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

Schedule schedule = vehicle.getSchedule();
final LinkTimePair start;
final Task startTask;
Expand Down Expand Up @@ -126,10 +105,6 @@ public VehicleEntry create(DvrpVehicle vehicle, double currentTime) {
ImmutableList.copyOf(stops), slackTimes, precedingStayTimes, currentTime);
}

public boolean isNotEligibleForRequestInsertion(DvrpVehicle vehicle, double currentTime) {
return currentTime + lookAhead < vehicle.getServiceBeginTime() || currentTime >= vehicle.getServiceEndTime();
}

static double[] computeSlackTimes(DvrpVehicle vehicle, double now, Waypoint.Stop[] stops, Waypoint.Stop start, List<Double> precedingStayTimes) {
double[] slackTimes = new double[stops.length + 2];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@ public enum OperationalScheme {
@Comment("Store planned unshared drt route as a link sequence")
public boolean storeUnsharedPath = false; // If true, the planned unshared path is stored and exported in plans

@PositiveOrZero
public double advanceRequestPlanningHorizon = 0; // beta-feature; planning horizon for advance (prebooked) requests

@Parameter
@Comment("When working with prebooked requests, defines whether vehicles drive immediately to the next"
+ "(prebooked) future task and wait for the planned stop to begin, or wait at the current"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public static DrtConfigGroup convertTaxiToDrtCfg(TaxiConfigGroup taxiCfg) {
drtCfg.drtServiceAreaShapeFile = null;
drtCfg.plotDetailedCustomerStats = taxiCfg.detailedStats || taxiCfg.timeProfiles;
drtCfg.numberOfThreads = taxiCfg.numberOfThreads;
drtCfg.advanceRequestPlanningHorizon = 0;
drtCfg.storeUnsharedPath = false;

taxiCfg.getTaxiFareParams().ifPresent(taxiFareParams -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ public void testPtAlongALineWithRaptorAndDrtServiceArea() {
drtConfigGroup.maxWaitTime = Double.MAX_VALUE;
drtConfigGroup.rejectRequestIfMaxWaitOrTravelTimeViolated = false;
drtConfigGroup.useModeFilteredSubnetwork = true;
drtConfigGroup.advanceRequestPlanningHorizon = 99999;

drtConfigGroup.addParameterSet(new ExtensiveInsertionSearchParams());
mm.addParameterSet(drtConfigGroup);
Expand Down

0 comments on commit 0730ed2

Please sign in to comment.