Skip to content

Commit

Permalink
some further cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl committed Oct 16, 2023
1 parent 35d7b6d commit 05ff372
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 105 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class PrebookingActionCreator implements VrpAgentLogic.DynActionCreator {
private final VrpAgentLogic.DynActionCreator delegate;
private final PrebookingPassengerEngine passengerEngine;
private final PassengerStopDurationProvider stopDurationProvider;
private final EnteringHandler enteringHandler;
private final PrebookingManager prebookingManager;

public PrebookingActionCreator(PrebookingPassengerEngine passengerEngine, VrpAgentLogic.DynActionCreator delegate,
PassengerStopDurationProvider stopDurationProvider, EnteringHandler enteringHandler) {
PassengerStopDurationProvider stopDurationProvider, PrebookingManager prebookingManager) {
this.delegate = delegate;
this.passengerEngine = passengerEngine;
this.stopDurationProvider = stopDurationProvider;
this.enteringHandler = enteringHandler;
this.prebookingManager = prebookingManager;
}

@Override
Expand All @@ -44,7 +44,7 @@ public DynAction createAction(DynAgent dynAgent, DvrpVehicle vehicle, double now
DrtStopTask stopTask = (DrtStopTask) task;
return new PrebookingStopActivity(passengerEngine, dynAgent, stopTask, stopTask.getDropoffRequests(),
stopTask.getPickupRequests(), DrtActionCreator.DRT_STOP_NAME, stopDurationProvider, vehicle,
enteringHandler);
prebookingManager);
}

return delegate.createAction(dynAgent, vehicle, now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.contrib.drt.extension.prebooking.events.PassengerEnteringVehicleEvent;
import org.matsim.contrib.drt.extension.prebooking.events.PassengerRequestBookedEvent;
import org.matsim.contrib.drt.passenger.AcceptedDrtRequest;
import org.matsim.contrib.dvrp.optimizer.Request;
import org.matsim.contrib.dvrp.optimizer.VrpOptimizer;
import org.matsim.contrib.dvrp.passenger.PassengerRequest;
Expand Down Expand Up @@ -62,9 +64,6 @@ public class PrebookingManager implements MobsimEngine {
private final List<PrebookingQueueItem> prebookingQueue = new LinkedList<>();
private final IdMap<Request, PassengerRequest> prebookedRequests = new IdMap<>(Request.class);

private final List<Leg> prebookedLegs = new LinkedList<>();
private boolean cleanupFinished = false;

public PrebookingManager(String mode, Network network, PassengerRequestCreator requestCreator,
VrpOptimizer optimizer, PassengerRequestValidator requestValidator, EventsManager eventsManager) {
this.network = network;
Expand Down Expand Up @@ -97,7 +96,7 @@ public boolean isPrebookedRequest(Id<Request> requestId) {
}

public void prebook(Person person, Leg leg, double earliestDepartureTime) {
Verify.verify(!cleanupFinished, "Attempting to prebook a request after Mobsim has finished");
Verify.verify(leg.getMode().equals(mode), "Invalid mode for this prebooking manager");

synchronized (prebookingQueue) {
this.prebookingQueue.add(new PrebookingQueueItem(person, leg, earliestDepartureTime));
Expand Down Expand Up @@ -143,22 +142,20 @@ public void doSimStep(double now) {

item.leg.getAttributes().putAttribute(requestAttribute, request.getId().toString());
prebookedRequests.put(request.getId(), request);
prebookedLegs.add(item.leg);
}
}

prebookingQueue.clear();
}
}

void notifyEntering(double now, AcceptedDrtRequest request) {
eventsManager
.processEvent(new PassengerEnteringVehicleEvent(now, mode, request.getId(), request.getPassengerId()));
}

@Override
public void afterSim() {
// reset leg attributes for next iteration
cleanupFinished = true;

for (Leg leg : prebookedLegs) {
leg.getAttributes().removeAttribute(requestAttribute);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void install() {
}));

// install QSim bindings / overrides
installOverridingQSimModule(new PrebookingModeQSimModule(getMode(), isElectric));
installOverridingQSimModule(new PrebookingModeQSimModule(getMode(), drtConfig, isElectric));

// analysis
install(new PrebookingModeAnalysisModule(getMode()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@
import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.extension.edrt.EDrtActionCreator;
import org.matsim.contrib.drt.extension.prebooking.electric.ElectricPrebookingActionCreator;
import org.matsim.contrib.drt.passenger.DrtRequest;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.stops.PassengerStopDurationProvider;
import org.matsim.contrib.drt.vrpagent.DrtActionCreator;
import org.matsim.contrib.dvrp.optimizer.VrpOptimizer;
import org.matsim.contrib.dvrp.passenger.DefaultRequestQueue;
import org.matsim.contrib.dvrp.passenger.PassengerEngine;
import org.matsim.contrib.dvrp.passenger.PassengerHandler;
import org.matsim.contrib.dvrp.passenger.PassengerRequestCreator;
import org.matsim.contrib.dvrp.passenger.PassengerRequestValidator;
import org.matsim.contrib.dvrp.passenger.RequestQueue;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeQSimModule;
import org.matsim.contrib.dvrp.vrpagent.VrpAgentLogic;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.mobsim.framework.MobsimTimer;

import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;

class PrebookingModeQSimModule extends AbstractDvrpModeQSimModule {
private final boolean isElectric;
private final DrtConfigGroup drtConfig;

PrebookingModeQSimModule(String mode, boolean isElectric) {
PrebookingModeQSimModule(String mode, DrtConfigGroup drtConfig, boolean isElectric) {
super(mode);

this.isElectric = isElectric;
this.drtConfig = drtConfig;
}

@Override
Expand All @@ -36,9 +43,9 @@ protected void configureQSim() {
DrtActionCreator delegate = getter.getModal(DrtActionCreator.class);
PassengerStopDurationProvider stopDurationProvider = getter
.getModal(PassengerStopDurationProvider.class);
EnteringHandler enteringHandler = getter.getModal(EnteringHandler.class);
PrebookingManager prebookingManager = getter.getModal(PrebookingManager.class);

return new PrebookingActionCreator(passengerEngine, delegate, stopDurationProvider, enteringHandler);
return new PrebookingActionCreator(passengerEngine, delegate, stopDurationProvider, prebookingManager);
})).in(Singleton.class);
bindModal(VrpAgentLogic.DynActionCreator.class).to(modalKey(PrebookingActionCreator.class));
} else {
Expand All @@ -51,19 +58,14 @@ protected void configureQSim() {
PassengerStopDurationProvider stopDurationProvider = getter
.getModal(PassengerStopDurationProvider.class);
MobsimTimer timer = getter.get(MobsimTimer.class);
EnteringHandler enteringHandler = getter.getModal(EnteringHandler.class);
PrebookingManager prebookingManager = getter.getModal(PrebookingManager.class);

return new ElectricPrebookingActionCreator(passengerEngine, delegate, stopDurationProvider, timer,
enteringHandler);
prebookingManager);
})).in(Singleton.class);
bindModal(VrpAgentLogic.DynActionCreator.class).to(modalKey(ElectricPrebookingActionCreator.class));
}

bindModal(EnteringHandler.class).toProvider(modalProvider(getter -> {
EventsManager eventsManager = getter.get(EventsManager.class);
return new EnteringHandler(eventsManager, getMode());
})).in(Singleton.class);

// override the PassengerEngine
bindModal(PrebookingPassengerEngine.class).toProvider(modalProvider(getter -> {
EventsManager eventsManager = getter.get(EventsManager.class);
Expand Down Expand Up @@ -92,5 +94,15 @@ protected void configureQSim() {
eventsManager);
})).in(Singleton.class);
addModalQSimComponentBinding().to(modalKey(PrebookingManager.class));

bindModal(new TypeLiteral<RequestQueue<DrtRequest>>() {
}).toProvider(modalProvider(getter -> {
PrebookingManager prebookingManager = getter.getModal(PrebookingManager.class);
RequestQueue<DrtRequest> delegate = DefaultRequestQueue
.withLimitedAdvanceRequestPlanningHorizon(drtConfig.advanceRequestPlanningHorizon);

return new PrebookingRequestQueue<>(prebookingManager, delegate);
})).in(Singleton.class);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class PrebookingRequestQueue<R extends PassengerRequest> implements
private final PrebookingManager prebookingManager;
private final RequestQueue<R> delegate;

private final List<R> queue = new LinkedList<>();
private final List<R> schedulableRequests = new LinkedList<>();

public PrebookingRequestQueue(PrebookingManager prebookingManager, RequestQueue<R> delegate) {
this.prebookingManager = prebookingManager;
Expand All @@ -31,21 +31,16 @@ public void updateQueuesOnNextTimeSteps(double currentTime) {
public void addRequest(R request) {
if (prebookingManager.isPrebookedRequest(request.getId())) {
// add to queue in the order of submission
queue.add(request);
schedulableRequests.add(request);
} else {
delegate.addRequest(request);
}
}

@Override
public Collection<R> getSchedulableRequests() {
// prebooked requests, they are treated when and in the order they are submitted
List<R> requests = new LinkedList<>(queue);
queue.clear();

// other requests treated according to sorting default logic
requests.addAll(delegate.getSchedulableRequests());

return requests;
schedulableRequests.addAll(delegate.getSchedulableRequests());
delegate.getSchedulableRequests().clear();
return schedulableRequests;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,23 @@ public class PrebookingStopActivity extends FirstLastSimStepDynActivity implemen
private final IdMap<Request, Double> leaveTimes = new IdMap<>(Request.class);
private final Set<Id<Request>> enteredRequests = new HashSet<>();

private final PrebookingManager prebookingManager;
private final PrebookingPassengerEngine passengerEngine;
private final PassengerStopDurationProvider stopDurationProvider;

private final EnteringHandler enteringHandler;

public PrebookingStopActivity(PrebookingPassengerEngine passengerEngine, DynAgent driver, StayTask task,
Map<Id<Request>, ? extends AcceptedDrtRequest> dropoffRequests,
Map<Id<Request>, ? extends AcceptedDrtRequest> pickupRequests, String activityType,
PassengerStopDurationProvider stopDurationProvider, DvrpVehicle vehicle,
EnteringHandler enteringHandler) {
PrebookingManager prebookingManager) {
super(activityType);
this.passengerEngine = passengerEngine;
this.driver = driver;
this.dropoffRequests = dropoffRequests;
this.pickupRequests = pickupRequests;
this.stopDurationProvider = stopDurationProvider;
this.vehicle = vehicle;
this.enteringHandler = enteringHandler;
this.prebookingManager = prebookingManager;
}

@Override
Expand Down Expand Up @@ -119,7 +118,7 @@ private boolean updatePickupRequests(double now) {
}

private void queuePickup(AcceptedDrtRequest request, double now) {
enteringHandler.sendEnteringEvent(now, request);
prebookingManager.notifyEntering(now, request);

double enterTime = now + stopDurationProvider.calcPickupDuration(vehicle, request.getRequest());
enterTimes.put(request.getId(), enterTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.matsim.contrib.drt.extension.prebooking.electric;

import org.matsim.contrib.drt.extension.prebooking.dvrp.EnteringHandler;
import org.matsim.contrib.drt.extension.prebooking.dvrp.PrebookingManager;
import org.matsim.contrib.drt.extension.prebooking.dvrp.PrebookingPassengerEngine;
import org.matsim.contrib.drt.extension.prebooking.dvrp.PrebookingStopActivity;
import org.matsim.contrib.drt.schedule.DrtStopTask;
Expand All @@ -27,16 +27,16 @@ public class ElectricPrebookingActionCreator implements VrpAgentLogic.DynActionC
private final PrebookingPassengerEngine passengerEngine;
private final PassengerStopDurationProvider stopDurationProvider;
private final MobsimTimer timer;
private final EnteringHandler enteringHandler;
private final PrebookingManager prebookingManager;

public ElectricPrebookingActionCreator(PrebookingPassengerEngine passengerEngine,
VrpAgentLogic.DynActionCreator delegate, PassengerStopDurationProvider stopDurationProvider,
MobsimTimer timer, EnteringHandler enteringHandler) {
MobsimTimer timer, PrebookingManager prebookingManager) {
this.delegate = delegate;
this.passengerEngine = passengerEngine;
this.stopDurationProvider = stopDurationProvider;
this.timer = timer;
this.enteringHandler = enteringHandler;
this.prebookingManager = prebookingManager;
}

@Override
Expand All @@ -51,7 +51,7 @@ public DynAction createAction(DynAgent dynAgent, DvrpVehicle vehicle, double now

return new PrebookingStopActivity(passengerEngine, dynAgent, stopTask, stopTask.getDropoffRequests(),
stopTask.getPickupRequests(), DrtActionCreator.DRT_STOP_NAME, stopDurationProvider, vehicle,
enteringHandler);
prebookingManager);
}

return delegate.createAction(dynAgent, vehicle, now);
Expand Down
Loading

0 comments on commit 05ff372

Please sign in to comment.