Skip to content

Commit

Permalink
Remove the need of binding to use DRT detour constraints (#3171)
Browse files Browse the repository at this point in the history
* Refactoring work

* Update DrtOfferAcceptor.java

* Remove original DEFAULT_ACCEPTOR

* Improve binding
  • Loading branch information
luchengqi7 authored Mar 18, 2024
1 parent 95bdede commit 4180bcd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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.passenger.DefaultOfferAcceptor;
import org.matsim.contrib.drt.prebooking.PrebookingActionCreator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.schedule.DrtTaskFactory;
Expand Down Expand Up @@ -178,7 +179,8 @@ public EmptyVehicleRelocator get() {
getter.getModal(StopTimeCalculator.class), scheduleWaitBeforeDrive)))
.asEagerSingleton();

bindModal(DrtOfferAcceptor.class).toInstance(DrtOfferAcceptor.DEFAULT_ACCEPTOR);
bindModal(DefaultOfferAcceptor.class).toProvider(modalProvider(getter -> new DefaultOfferAcceptor(drtCfg.maxAllowedPickupDelay)));
bindModal(DrtOfferAcceptor.class).to(modalKey(DefaultOfferAcceptor.class));

bindModal(ScheduleTimingUpdater.class).toProvider(modalProvider(
getter -> new ScheduleTimingUpdater(getter.get(MobsimTimer.class),
Expand All @@ -187,11 +189,11 @@ public EmptyVehicleRelocator get() {
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)
Expand All @@ -201,9 +203,9 @@ public EmptyVehicleRelocator get() {
// + 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 @@ -37,6 +37,7 @@
import org.matsim.contrib.drt.optimizer.insertion.selective.SelectiveInsertionSearchQSimModule;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy;
import org.matsim.contrib.drt.passenger.DrtOfferAcceptor;
import org.matsim.contrib.drt.passenger.DefaultOfferAcceptor;
import org.matsim.contrib.drt.prebooking.PrebookingActionCreator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.schedule.DrtStayTaskEndTimeCalculator;
Expand Down Expand Up @@ -138,7 +139,7 @@ public EmptyVehicleRelocator get() {
}).asEagerSingleton();

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),
Expand All @@ -147,7 +148,8 @@ public EmptyVehicleRelocator get() {
getter.getModal(StopTimeCalculator.class), scheduleWaitBeforeDrive)))
.asEagerSingleton();

bindModal(DrtOfferAcceptor.class).toInstance(DrtOfferAcceptor.DEFAULT_ACCEPTOR);
bindModal(DefaultOfferAcceptor.class).toProvider(modalProvider(getter -> new DefaultOfferAcceptor(drtCfg.maxAllowedPickupDelay)));
bindModal(DrtOfferAcceptor.class).to(modalKey(DefaultOfferAcceptor.class));

bindModal(ScheduleTimingUpdater.class).toProvider(modalProvider(
getter -> new ScheduleTimingUpdater(getter.get(MobsimTimer.class),
Expand All @@ -160,7 +162,7 @@ public EmptyVehicleRelocator get() {
return v -> VrpLegFactory.createWithOnlineTracker(dvrpCfg.mobsimMode, v, OnlineTrackerListener.NO_LISTENER,
timer);
})).in(Singleton.class);

if (drtCfg.getPrebookingParams().isEmpty()) {
bindModal(VrpAgentLogic.DynActionCreator.class).to(modalKey(DrtActionCreator.class));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

import java.util.Optional;

public class MaxDetourOfferAcceptor implements DrtOfferAcceptor{
public class DefaultOfferAcceptor implements DrtOfferAcceptor{
private final double maxAllowedPickupDelay;

public MaxDetourOfferAcceptor(double maxAllowedPickupDelay) {
/**
* Generate Default offer acceptor with max allowed pickup delay.
* @param maxAllowedPickupDelay: maximum allowed delay since the initially assigned pickup time.
*/
public DefaultOfferAcceptor(double maxAllowedPickupDelay) {
this.maxAllowedPickupDelay = maxAllowedPickupDelay;
}

/**
* Generate Default offer acceptor.
*/
public DefaultOfferAcceptor() {
this.maxAllowedPickupDelay = Double.POSITIVE_INFINITY;
}

@Override
public Optional<AcceptedDrtRequest> acceptDrtOffer(DrtRequest request, double departureTime, double arrivalTime) {
double updatedLatestStartTime = Math.min(departureTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
* @author Michal Maciejewski (michalm)
*/
public interface DrtOfferAcceptor {
DrtOfferAcceptor DEFAULT_ACCEPTOR = (request, departureTime, arrivalTime) -> Optional.of(
AcceptedDrtRequest.createFromOriginalRequest(request));

/**
* @param request drt request
* @param departureTime offered departure time for the new request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static DrtConfigGroup getSingleModeDrtConfig(Config config) {
@Comment(
"Defines the maximum delay allowed from the initial scheduled pick up time. Once the initial pickup time is offered, the latest promised"
+ "pickup time is calculated based on initial scheduled pickup time + maxAllowedPickupDelay. "
+ "By default, this limit is disabled. If enabled, a value between 120 and 240 is a good choice.")
+ "By default, this limit is disabled. If enabled, a value between 0 and 240 is a good choice.")
@PositiveOrZero
public double maxAllowedPickupDelay = Double.POSITIVE_INFINITY;// [s]

Expand Down Expand Up @@ -323,6 +323,12 @@ protected void checkConsistency(Config config) {
if (useModeFilteredSubnetwork) {
DvrpModeRoutingNetworkModule.checkUseModeFilteredSubnetworkAllowed(config, mode);
}

if ((maxDetourAlpha != Double.POSITIVE_INFINITY && maxDetourBeta != Double.POSITIVE_INFINITY) || maxAbsoluteDetour != Double.POSITIVE_INFINITY) {
Verify.verify(maxAllowedPickupDelay != Double.POSITIVE_INFINITY, "Detour constraints are activated, " +
"maxAllowedPickupDelay must be specified! A value between 0 and 240 seconds can be a good choice for maxAllowedPickupDelay.");
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.contrib.drt.passenger.DrtOfferAcceptor;
import org.matsim.contrib.drt.passenger.MaxDetourOfferAcceptor;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.run.DrtControlerCreator;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeQSimModule;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
Expand All @@ -29,7 +26,6 @@ public void testMaxDetourConstraint() {
URL configUrl = IOUtils.extendUrl(ExamplesUtils.getTestScenarioURL("mielec"), "mielec_drt_config.xml");
Config config = ConfigUtils.loadConfig(configUrl, new MultiModeDrtConfigGroup(), new DvrpConfigGroup(),
new OTFVisConfigGroup());
MultiModeDrtConfigGroup multiModeDrtConfigGroup = MultiModeDrtConfigGroup.get(config);
DrtConfigGroup drtConfigGroup = DrtConfigGroup.getSingleModeDrtConfig(config);

// Max wait time
Expand All @@ -50,15 +46,6 @@ public void testMaxDetourConstraint() {

Controler controler = DrtControlerCreator.createControler(config, false);

for (DrtConfigGroup drtCfg : multiModeDrtConfigGroup.getModalElements()) {
controler.addOverridingQSimModule(new AbstractDvrpModeQSimModule(drtCfg.mode) {
@Override
protected void configureQSim() {
bindModal(DrtOfferAcceptor.class).toProvider(modalProvider(getter -> new MaxDetourOfferAcceptor(drtCfg.maxAllowedPickupDelay)));
}
});
}

controler.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.matsim.contrib.drt.optimizer.DrtRequestInsertionRetryQueue;
import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.passenger.AcceptedDrtRequest;
import org.matsim.contrib.drt.passenger.DefaultOfferAcceptor;
import org.matsim.contrib.drt.passenger.DrtOfferAcceptor;
import org.matsim.contrib.drt.passenger.DrtRequest;
import org.matsim.contrib.drt.schedule.DefaultDrtStopTask;
Expand Down Expand Up @@ -285,7 +286,7 @@ private DefaultUnplannedRequestInserter newInserter(Fleet fleet, double now,
VehicleEntry.EntryFactory vehicleEntryFactory, DrtRequestInsertionRetryQueue insertionRetryQueue,
DrtInsertionSearch insertionSearch, RequestInsertionScheduler insertionScheduler) {
return new DefaultUnplannedRequestInserter(mode, fleet, () -> now, eventsManager, insertionScheduler,
vehicleEntryFactory, insertionRetryQueue, insertionSearch, DrtOfferAcceptor.DEFAULT_ACCEPTOR,
vehicleEntryFactory, insertionRetryQueue, insertionSearch, new DefaultOfferAcceptor(),
forkJoinPoolExtension.forkJoinPool, StaticPassengerStopDurationProvider.of(10.0, 0.0));
}

Expand Down

0 comments on commit 4180bcd

Please sign in to comment.