Skip to content

Commit

Permalink
Merge pull request #3331 from moia-oss/multimodeDrtShifts
Browse files Browse the repository at this point in the history
Make drt shifts multi mode drt compatible
  • Loading branch information
nkuehnel authored Jun 23, 2024
2 parents 1ac6e04 + b739633 commit 1663f22
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ protected void configureQSim() {

bindModal(DrtShiftDispatcher.class).toProvider(modalProvider(
getter -> new EDrtShiftDispatcherImpl(((EShiftTaskScheduler) getter.getModal(ShiftTaskScheduler.class)), getter.getModal(ChargingInfrastructure.class),
drtShiftParams, getter.getModal(OperationFacilities.class), new DrtShiftDispatcherImpl(getter.getModal(DrtShifts.class), getter.getModal(Fleet.class),
drtShiftParams, getter.getModal(OperationFacilities.class), new DrtShiftDispatcherImpl(getMode(),
getter.getModal(DrtShifts.class), getter.getModal(Fleet.class),
getter.get(MobsimTimer.class), getter.getModal(OperationFacilities.class), getter.getModal(OperationFacilityFinder.class),
getter.getModal(ShiftTaskScheduler.class), getter.getModal(Network.class), getter.get(EventsManager.class),
drtShiftParams, new EDrtShiftStartLogic(new DefaultShiftStartLogic()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
*/
public class BreakCorridorXY implements DrtShiftBreakStartedEventHandler, DrtShiftBreakEndedEventHandler {

private final String mode;

private final Provider<DrtShiftsSpecification> shifts;
private final Map<Id<DrtShift>, Tuple<Double,Double>> shift2plannedVsActualBreakStart = new HashMap<>();
private final Map<Id<DrtShift>, Tuple<Double,Double>> shift2plannedVsActualBreakEnd = new HashMap<>();

public BreakCorridorXY(Provider<DrtShiftsSpecification> shifts) {
public BreakCorridorXY(String mode, Provider<DrtShiftsSpecification> shifts) {
super();
this.mode = mode;
this.shifts = shifts;
reset(0);
}
Expand All @@ -38,16 +41,20 @@ public BreakCorridorXY(Provider<DrtShiftsSpecification> shifts) {

@Override
public void handleEvent(DrtShiftBreakStartedEvent event) {
final DrtShiftBreakSpecification drtShiftBreak = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
final double earliestBreakStartTime = drtShiftBreak.getEarliestBreakStartTime();
shift2plannedVsActualBreakStart.put(event.getShiftId(), new Tuple<>(earliestBreakStartTime, event.getTime()));
if (event.getMode().equals(mode)) {
final DrtShiftBreakSpecification drtShiftBreak = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
final double earliestBreakStartTime = drtShiftBreak.getEarliestBreakStartTime();
shift2plannedVsActualBreakStart.put(event.getShiftId(), new Tuple<>(earliestBreakStartTime, event.getTime()));
}
}

@Override
public void handleEvent(DrtShiftBreakEndedEvent event) {
final DrtShiftBreakSpecification drtShiftBreak = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
final double latestBreakEndTime = drtShiftBreak.getLatestBreakEndTime();
shift2plannedVsActualBreakEnd.put(event.getShiftId(), new Tuple<>(latestBreakEndTime, event.getTime()));
if (event.getMode().equals(mode)) {
final DrtShiftBreakSpecification drtShiftBreak = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
final double latestBreakEndTime = drtShiftBreak.getLatestBreakEndTime();
shift2plannedVsActualBreakEnd.put(event.getShiftId(), new Tuple<>(latestBreakEndTime, event.getTime()));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,51 @@ public class ShiftDurationXY implements DrtShiftStartedEventHandler, DrtShiftEnd
private final Map<Id<DrtShift>, Tuple<Double,Double>> shift2plannedVsActualDuration = new HashMap<>();
private final Map<Id<DrtShift>, Tuple<Double,Double>> shift2plannedVsActualBreakDuration = new HashMap<>();

public ShiftDurationXY(Provider<DrtShiftsSpecification> shifts) {
private final String mode;

public ShiftDurationXY(Provider<DrtShiftsSpecification> shifts, String mode) {
super();
this.shifts = shifts;
this.mode = mode;
reset(0);
}

/* Implementation of EventHandler-Interfaces */

@Override
public void handleEvent(final DrtShiftStartedEvent event) {
shift2StartTime.put(event.getShiftId(), event.getTime());
if (event.getMode().equals(mode)) {
shift2StartTime.put(event.getShiftId(), event.getTime());
}
}

@Override
public void handleEvent(DrtShiftBreakStartedEvent event) {
shift2BreakStartTime.put(event.getShiftId(), event.getTime());
if (event.getMode().equals(mode)) {
shift2BreakStartTime.put(event.getShiftId(), event.getTime());
}
}

@Override
public void handleEvent(final DrtShiftEndedEvent event) {
final Double start = shift2StartTime.get(event.getShiftId());
double duration = event.getTime() - start;
final DrtShiftSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId());
double plannedDuration = drtShift.getEndTime() - drtShift.getStartTime();
shift2plannedVsActualDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
if (event.getMode().equals(mode)) {
final Double start = shift2StartTime.get(event.getShiftId());
double duration = event.getTime() - start;
final DrtShiftSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId());
double plannedDuration = drtShift.getEndTime() - drtShift.getStartTime();
shift2plannedVsActualDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
}
}

@Override
public void handleEvent(DrtShiftBreakEndedEvent event) {
final Double start = shift2BreakStartTime.get(event.getShiftId());
double duration = event.getTime() - start;
final DrtShiftBreakSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
double plannedDuration = drtShift.getDuration();
shift2plannedVsActualBreakDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
if (event.getMode().equals(mode)) {
final Double start = shift2BreakStartTime.get(event.getShiftId());
double duration = event.getTime() - start;
final DrtShiftBreakSpecification drtShift = shifts.get().getShiftSpecifications().get(event.getShiftId()).getBreak().orElseThrow();
double plannedDuration = drtShift.getDuration();
shift2plannedVsActualBreakDuration.put(event.getShiftId(), new Tuple<>(plannedDuration, duration));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
public class ShiftHistogram implements DrtShiftStartedEventHandler, DrtShiftEndedEventHandler,
DrtShiftBreakStartedEventHandler, DrtShiftBreakEndedEventHandler {

private final String mode;

public static final int DEFAULT_END_TIME = 30 * 3600;
public static final int DEFAULT_BIN_SIZE = 300;

Expand All @@ -30,9 +32,10 @@ public class ShiftHistogram implements DrtShiftStartedEventHandler, DrtShiftEnde
private DataFrame data = null;


public ShiftHistogram(Population population, Config config) {
public ShiftHistogram(Population population, String mode, Config config) {
super();
this.binSize = DEFAULT_BIN_SIZE;
this.mode = mode;
this.binSize = DEFAULT_BIN_SIZE;
this.nofBins = ((int) config.qsim().getEndTime().orElse(DEFAULT_END_TIME) ) / this.binSize + 1;
reset(0);
if (population == null) {
Expand All @@ -46,8 +49,9 @@ public ShiftHistogram(Population population, Config config) {
* @param binSize The size of a time bin in seconds.
* @param nofBins The number of time bins for this analysis.
*/
public ShiftHistogram(final int binSize, final int nofBins) {
public ShiftHistogram(String mode, final int binSize, final int nofBins) {
super();
this.mode = mode;
this.binSize = binSize;
this.nofBins = nofBins;
reset(0);
Expand All @@ -57,34 +61,42 @@ public ShiftHistogram(final int binSize, final int nofBins) {

@Override
public void handleEvent(final DrtShiftStartedEvent event) {
int index = getBinIndex(event.getTime());
if ((this.shiftIds == null || this.shiftIds.contains(event.getShiftId()))) {
DataFrame dataFrame = getData();
dataFrame.countsStart[index]++;
if (event.getMode().equals(mode)) {
int index = getBinIndex(event.getTime());
if ((this.shiftIds == null || this.shiftIds.contains(event.getShiftId()))) {
DataFrame dataFrame = getData();
dataFrame.countsStart[index]++;
}
}
}

@Override
public void handleEvent(final DrtShiftEndedEvent event) {
int index = getBinIndex(event.getTime());
if ((this.shiftIds == null || this.shiftIds.contains(event.getShiftId()))) {
DataFrame dataFrame = getData();
dataFrame.countsEnd[index]++;
if (event.getMode().equals(mode)) {
int index = getBinIndex(event.getTime());
if ((this.shiftIds == null || this.shiftIds.contains(event.getShiftId()))) {
DataFrame dataFrame = getData();
dataFrame.countsEnd[index]++;
}
}
}

@Override
public void handleEvent(DrtShiftBreakStartedEvent event) {
int index = getBinIndex(event.getTime());
DataFrame dataFrame = getData();
dataFrame.countsBreaksStart[index]++;
if (event.getMode().equals(mode)) {
int index = getBinIndex(event.getTime());
DataFrame dataFrame = getData();
dataFrame.countsBreaksStart[index]++;
}
}

@Override
public void handleEvent(DrtShiftBreakEndedEvent event) {
int index = getBinIndex(event.getTime());
DataFrame dataFrame = getData();
dataFrame.countsBreaksEnd[index]++;
if (event.getMode().equals(mode)) {
int index = getBinIndex(event.getTime());
DataFrame dataFrame = getData();
dataFrame.countsBreaksEnd[index]++;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public DrtShiftEfficiencyModeModule(DrtConfigGroup drtConfigGroup) {
@Override
public void install() {
bindModal(ShiftEfficiencyTracker.class).toProvider(modalProvider(getter ->
new ShiftEfficiencyTracker())).asEagerSingleton();
new ShiftEfficiencyTracker(getMode()))).asEagerSingleton();
addEventHandlerBinding().to(modalKey(ShiftEfficiencyTracker.class));
bindModal(ShiftEfficiencyAnalysisControlerListener.class).toProvider(modalProvider(getter ->
new ShiftEfficiencyAnalysisControlerListener(drtConfigGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
public final class ShiftEfficiencyTracker implements PersonMoneyEventHandler,
PassengerDroppedOffEventHandler, DrtShiftStartedEventHandler, DrtShiftEndedEventHandler {

private final String mode;
private Map<Id<DrtShift>, Double> revenueByShift;
private Map<Id<Request>, Id<DrtShift>> shiftByRequest;

Expand All @@ -63,44 +64,53 @@ public Map<Id<DrtShift>, Id<DvrpVehicle>> getFinishedShifts() {
}
}

public ShiftEfficiencyTracker() {
this.revenueByShift = new HashMap<>();
public ShiftEfficiencyTracker(String mode) {
this.mode = mode;
this.revenueByShift = new HashMap<>();
this.shiftByRequest = new HashMap<>();
this.finishedShifts = new HashMap<>();
this.currentRecord = new Record(revenueByShift, shiftByRequest, finishedShifts);
}

@Override
public void handleEvent(PersonMoneyEvent personMoneyEvent) {
if (DrtFareHandler.PERSON_MONEY_EVENT_PURPOSE_DRT_FARE.equals(personMoneyEvent.getPurpose())) {
Id<DrtShift> key = shiftByRequest.get(Id.create(personMoneyEvent.getReference(), DrtRequest.class));
if(key != null) {
revenueByShift.merge(key, -personMoneyEvent.getAmount(), Double::sum);
if(personMoneyEvent.getTransactionPartner().equals(mode)) {
if (DrtFareHandler.PERSON_MONEY_EVENT_PURPOSE_DRT_FARE.equals(personMoneyEvent.getPurpose())) {
Id<DrtShift> key = shiftByRequest.get(Id.create(personMoneyEvent.getReference(), DrtRequest.class));
if (key != null) {
revenueByShift.merge(key, -personMoneyEvent.getAmount(), Double::sum);
}
}
}
}

@Override
public void handleEvent(PassengerDroppedOffEvent event) {
Id<DvrpVehicle> vehicleId = event.getVehicleId();
Gbl.assertIf(activeShifts.containsKey(vehicleId));
Id<DrtShift> drtShiftId = activeShifts.get(vehicleId);
shiftByRequest.put(event.getRequestId(), drtShiftId);
if(event.getMode().equals(mode)) {
Id<DvrpVehicle> vehicleId = event.getVehicleId();
Gbl.assertIf(activeShifts.containsKey(vehicleId));
Id<DrtShift> drtShiftId = activeShifts.get(vehicleId);
shiftByRequest.put(event.getRequestId(), drtShiftId);
}
}

@Override
public void handleEvent(DrtShiftStartedEvent event) {
revenueByShift.put(event.getShiftId(), 0.);
if(activeShifts.containsKey(event.getVehicleId())) {
throw new RuntimeException("Vehicle is already registered for another shift");
if(event.getMode().equals(mode)) {
revenueByShift.put(event.getShiftId(), 0.);
if (activeShifts.containsKey(event.getVehicleId())) {
throw new RuntimeException("Vehicle is already registered for another shift");
}
activeShifts.put(event.getVehicleId(), event.getShiftId());
}
activeShifts.put(event.getVehicleId(), event.getShiftId());
}

@Override
public void handleEvent(DrtShiftEndedEvent event) {
activeShifts.remove(event.getVehicleId());
finishedShifts.put(event.getShiftId(), event.getVehicleId());
if(event.getMode().equals(mode)) {
activeShifts.remove(event.getVehicleId());
finishedShifts.put(event.getShiftId(), event.getVehicleId());
}
}

@Override
Expand Down
Loading

0 comments on commit 1663f22

Please sign in to comment.