Skip to content

Commit

Permalink
Merge branch 'master' into insertionSchedulerUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel authored Aug 29, 2024
2 parents 27bf881 + ac59969 commit 52e838a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ void testWithPrebooking() {
new OTFVisConfigGroup(), new EvConfigGroup());

DrtConfigGroup drtConfig = DrtConfigGroup.getSingleModeDrtConfig(config);
drtConfig.addParameterSet(new PrebookingParams());
PrebookingParams prebookingParams = new PrebookingParams();
prebookingParams.abortRejectedPrebookings = false;
drtConfig.addParameterSet(prebookingParams);

Controler controller = RunEDrtScenario.createControler(config, false);
ProbabilityBasedPrebookingLogic.install(controller, drtConfig, 0.5, 4.0 * 3600.0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.matsim.contrib.drt.extension.operations.shifts.run;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.matsim.api.core.v01.Coord;
Expand Down Expand Up @@ -62,11 +65,77 @@ public class RunPrebookingShiftDrtScenarioIT {


@Test
void test() {
void testWithReattempts() {

MultiModeDrtConfigGroup multiModeDrtConfigGroup = new MultiModeDrtConfigGroup(DrtWithExtensionsConfigGroup::new);
DrtWithExtensionsConfigGroup drtWithShiftsConfigGroup = (DrtWithExtensionsConfigGroup) multiModeDrtConfigGroup.createParameterSet("drt");
final Controler run = prepare(drtWithShiftsConfigGroup, multiModeDrtConfigGroup);

Multiset<Id<Person>> rejectedPersons = HashMultiset.create();
run.addOverridingModule(new AbstractModule() {
@Override
public void install() {
addEventHandlerBinding().toInstance((PassengerRequestRejectedEventHandler) event -> rejectedPersons.addAll(event.getPersonIds()));
}
});

PrebookingParams prebookingParams = new PrebookingParams();
prebookingParams.maximumPassengerDelay = 600;
prebookingParams.unschedulingMode = PrebookingParams.UnschedulingMode.Routing;
prebookingParams.scheduleWaitBeforeDrive = true;
prebookingParams.abortRejectedPrebookings = false;
drtWithShiftsConfigGroup.addParameterSet(prebookingParams);

run.addOverridingQSimModule(new PrebookingModeQSimModule(drtWithShiftsConfigGroup.getMode(),
prebookingParams));

run.run();

Assertions.assertEquals(0, rejectedPersons.count(Id.createPersonId(1) ));
Assertions.assertEquals(0, rejectedPersons.count(Id.createPersonId(3)));
Assertions.assertEquals(0, rejectedPersons.count(Id.createPersonId(5)));
Assertions.assertEquals(1, rejectedPersons.count(Id.createPersonId(2)));
Assertions.assertEquals(1, rejectedPersons.count(Id.createPersonId(4)));
Assertions.assertEquals(2, rejectedPersons.count(Id.createPersonId(6)));
}

@Test
void testWithoutReattempts() {

MultiModeDrtConfigGroup multiModeDrtConfigGroup = new MultiModeDrtConfigGroup(DrtWithExtensionsConfigGroup::new);
DrtWithExtensionsConfigGroup drtWithShiftsConfigGroup = (DrtWithExtensionsConfigGroup) multiModeDrtConfigGroup.createParameterSet("drt");
final Controler run = prepare(drtWithShiftsConfigGroup, multiModeDrtConfigGroup);

Multiset<Id<Person>> rejectedPersons = HashMultiset.create();
run.addOverridingModule(new AbstractModule() {
@Override
public void install() {
addEventHandlerBinding().toInstance((PassengerRequestRejectedEventHandler) event -> rejectedPersons.addAll(event.getPersonIds()));
}
});

PrebookingParams prebookingParams = new PrebookingParams();
prebookingParams.maximumPassengerDelay = 600;
prebookingParams.unschedulingMode = PrebookingParams.UnschedulingMode.Routing;
prebookingParams.scheduleWaitBeforeDrive = true;
prebookingParams.abortRejectedPrebookings = true;
drtWithShiftsConfigGroup.addParameterSet(prebookingParams);

run.addOverridingQSimModule(new PrebookingModeQSimModule(drtWithShiftsConfigGroup.getMode(),
prebookingParams));

run.run();

Assertions.assertEquals(0, rejectedPersons.count(Id.createPersonId(1) ));
Assertions.assertEquals(0, rejectedPersons.count(Id.createPersonId(3)));
Assertions.assertEquals(0, rejectedPersons.count(Id.createPersonId(5)));
Assertions.assertEquals(1, rejectedPersons.count(Id.createPersonId(2)));
Assertions.assertEquals(1, rejectedPersons.count(Id.createPersonId(4)));
Assertions.assertEquals(1, rejectedPersons.count(Id.createPersonId(6)));
}

@NotNull
private Controler prepare(DrtWithExtensionsConfigGroup drtWithShiftsConfigGroup, MultiModeDrtConfigGroup multiModeDrtConfigGroup) {
drtWithShiftsConfigGroup.mode = TransportMode.drt;
DefaultDrtOptimizationConstraintsSet defaultConstraintsSet =
(DefaultDrtOptimizationConstraintsSet) drtWithShiftsConfigGroup.addOrGetDrtOptimizationConstraintsParams()
Expand Down Expand Up @@ -148,11 +217,6 @@ void test() {
shiftsParams.shiftEndLookAhead = 900.;
drtWithShiftsConfigGroup.addParameterSet(operationsParams);

PrebookingParams prebookingParams = new PrebookingParams();
prebookingParams.maximumPassengerDelay = 600;
prebookingParams.unschedulingMode = PrebookingParams.UnschedulingMode.Routing;
prebookingParams.scheduleWaitBeforeDrive = true;
drtWithShiftsConfigGroup.addParameterSet(prebookingParams);

Scenario scenario = DrtControlerCreator.createScenarioWithDrtRouteFactory(config);
prepareNetwork(scenario);
Expand All @@ -161,27 +225,9 @@ void test() {
final Controler run = DrtOperationsControlerCreator.createControler(config, scenario, false);
prepareOperations(run, drtWithShiftsConfigGroup);

run.addOverridingQSimModule(new PrebookingModeQSimModule(drtWithShiftsConfigGroup.getMode(),
prebookingParams));
AttributeBasedPrebookingLogic.install(run, drtWithShiftsConfigGroup);

Set<Id<Person>> rejectedPersons = new HashSet<>();
run.addOverridingModule(new AbstractModule() {
@Override
public void install() {
addEventHandlerBinding().toInstance((PassengerRequestRejectedEventHandler) event -> rejectedPersons.addAll(event.getPersonIds()));
}
});

run.run();


Assertions.assertFalse(rejectedPersons.contains(Id.createPersonId(1)));
Assertions.assertFalse(rejectedPersons.contains(Id.createPersonId(3)));
Assertions.assertFalse(rejectedPersons.contains(Id.createPersonId(5)));
Assertions.assertTrue(rejectedPersons.contains(Id.createPersonId(2)));
Assertions.assertTrue(rejectedPersons.contains(Id.createPersonId(4)));
Assertions.assertTrue(rejectedPersons.contains(Id.createPersonId(6)));
AttributeBasedPrebookingLogic.install(run, drtWithShiftsConfigGroup);
return run;
}

private void preparePopulation(Scenario scenario) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.matsim.api.core.v01.events.handler.PersonStuckEventHandler;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
Expand All @@ -21,10 +22,12 @@
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.mobsim.framework.MobsimAgent;
import org.matsim.core.mobsim.framework.MobsimAgent.State;
import org.matsim.core.mobsim.framework.MobsimPassengerAgent;
import org.matsim.core.mobsim.framework.MobsimTimer;
import org.matsim.core.mobsim.framework.events.MobsimAfterSimStepEvent;
import org.matsim.core.mobsim.framework.listeners.MobsimAfterSimStepListener;
import org.matsim.core.mobsim.qsim.InternalInterface;
import org.matsim.core.mobsim.qsim.agents.HasModifiablePlan;
import org.matsim.core.mobsim.qsim.agents.WithinDayAgentUtils;
import org.matsim.core.mobsim.qsim.interfaces.MobsimEngine;

Expand Down Expand Up @@ -59,12 +62,16 @@ public class PrebookingManager implements MobsimEngine, MobsimAfterSimStepListen

private final VrpOptimizer optimizer;
private final RequestUnscheduler unscheduler;
private final boolean abortRejectedPrebookings;

private final MobsimTimer mobsimTimer;

private InternalInterface internalInterface;


public PrebookingManager(String mode, Network network, PassengerRequestCreator requestCreator,
VrpOptimizer optimizer, MobsimTimer mobsimTimer, PassengerRequestValidator requestValidator,
EventsManager eventsManager, RequestUnscheduler unscheduler) {
VrpOptimizer optimizer, MobsimTimer mobsimTimer, PassengerRequestValidator requestValidator,
EventsManager eventsManager, RequestUnscheduler unscheduler, boolean abortRejectedPrebookings) {
this.network = network;
this.mode = mode;
this.requestCreator = requestCreator;
Expand All @@ -74,6 +81,7 @@ public PrebookingManager(String mode, Network network, PassengerRequestCreator r
this.mobsimTimer = mobsimTimer;
this.eventsManager = eventsManager;
this.unscheduler = unscheduler;
this.abortRejectedPrebookings = abortRejectedPrebookings;
}

// Functionality for ID management
Expand Down Expand Up @@ -386,6 +394,19 @@ private void processRejections(double now) {
} else {
unscheduleUponVehicleAssignment.add(requestId);
}

if(abortRejectedPrebookings) {
for (Id<Person> passengerId : item.request.getPassengerIds()) {
MobsimAgent agent = internalInterface.getMobsim().getAgents().get(passengerId);
((Activity) WithinDayAgentUtils.getCurrentPlanElement(agent)).setEndTime(Double.POSITIVE_INFINITY);
((Activity) WithinDayAgentUtils.getCurrentPlanElement(agent)).setMaximumDurationUndefined();
((HasModifiablePlan) agent).resetCaches();
internalInterface.getMobsim().rescheduleActivityEnd(agent);
eventsManager.processEvent(new PersonStuckEvent(now, agent.getId(), agent.getCurrentLinkId(),
this.mode));
internalInterface.getMobsim().getAgentCounter().incLost();
}
}
}
}

Expand Down Expand Up @@ -454,5 +475,6 @@ public void afterSim() {

@Override
public void setInternalInterface(InternalInterface internalInterface) {
this.internalInterface = internalInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void configureQSim() {
MobsimTimer mobsimTimer = getter.get(MobsimTimer.class);

return new PrebookingManager(getMode(), network, requestCreator, optimizer, mobsimTimer, requestValidator,
eventsManager, requestUnscheduler);
eventsManager, requestUnscheduler, prebookingParams.abortRejectedPrebookings);
})).in(Singleton.class);
addModalQSimComponentBinding().to(modalKey(PrebookingManager.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ public enum UnschedulingMode {
@NotNull
public UnschedulingMode unschedulingMode = UnschedulingMode.StopBased;

@Parameter
@Comment("Defines whether agents are set to stuck and abort when a prebooked request is rejected." +
"If false, prebooked agents will re-attempt to ride at their initially planned departure time." +
"Note that additional passenger events for submission, scheduling etc. will occur for re-attempts.")
public boolean abortRejectedPrebookings = true;

}
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ void testRunDrtWithPrebooking() {
config.controller().setOutputDirectory(utils.getOutputDirectory());

DrtConfigGroup drtConfig = DrtConfigGroup.getSingleModeDrtConfig(config);
drtConfig.addParameterSet(new PrebookingParams());
PrebookingParams prebookingParams = new PrebookingParams();
prebookingParams.abortRejectedPrebookings = false;
drtConfig.addParameterSet(prebookingParams);

Controler controller = DrtControlerCreator.createControler(config, false);
ProbabilityBasedPrebookingLogic.install(controller, drtConfig, 0.5, 4.0 * 3600.0);
Expand Down

0 comments on commit 52e838a

Please sign in to comment.