diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/DrtVehicleDistanceStats.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/DrtVehicleDistanceStats.java index 42a452c3a0c..d6c613a4313 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/DrtVehicleDistanceStats.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/analysis/DrtVehicleDistanceStats.java @@ -106,10 +106,10 @@ public void reset(int iteration) { @Override public void handleEvent(VehicleAddedEvent event) { - var state = vehicleStates.get(Id.createVehicleId(event.getVehicleId())); + var state = vehicleStates.get(Id.createVehicleId(event.getDvrpVehicleId())); if (state == null) { - vehicleStates.put(Id.createVehicleId(event.getVehicleId()), new VehicleState(event.getCapacity(), Double.NaN)); + vehicleStates.put(Id.createVehicleId(event.getDvrpVehicleId()), new VehicleState(event.getCapacity(), Double.NaN)); } else { state.active = true; Verify.verify(state.maxCapacity == event.getCapacity()); @@ -118,7 +118,7 @@ public void handleEvent(VehicleAddedEvent event) { @Override public void handleEvent(VehicleRemovedEvent event) { - Objects.requireNonNull(vehicleStates.get(Id.createVehicleId(event.getVehicleId()))).active = false; + Objects.requireNonNull(vehicleStates.get(Id.createVehicleId(event.getDvrpVehicleId()))).active = false; } @Override diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/FleetSizeProfileCalculator.java b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/FleetSizeProfileCalculator.java index ff3a8931307..703a85f4908 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/FleetSizeProfileCalculator.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/FleetSizeProfileCalculator.java @@ -55,14 +55,14 @@ public FleetSizeProfileCalculator(String dvrpMode, FleetSpecification fleet, int @Override public void handleEvent(VehicleAddedEvent event) { - if (event.getMode().equals(dvrpMode)) { + if (event.getDvrpMode().equals(dvrpMode)) { increment(event.getTime()); } } @Override public void handleEvent(VehicleRemovedEvent event) { - if (event.getMode().equals(dvrpMode)) { + if (event.getDvrpMode().equals(dvrpMode)) { decrement(event.getTime()); } } diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/VehicleOccupancyProfileCalculator.java b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/VehicleOccupancyProfileCalculator.java index 837e2b38e6c..6c4bc47eb8f 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/VehicleOccupancyProfileCalculator.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/VehicleOccupancyProfileCalculator.java @@ -39,6 +39,10 @@ import org.matsim.contrib.dvrp.fleet.DvrpVehicle; import org.matsim.contrib.dvrp.fleet.DvrpVehicleSpecification; import org.matsim.contrib.dvrp.fleet.FleetSpecification; +import org.matsim.contrib.dvrp.fleet.VehicleAddedEvent; +import org.matsim.contrib.dvrp.fleet.VehicleAddedEventHandler; +import org.matsim.contrib.dvrp.fleet.VehicleRemovedEvent; +import org.matsim.contrib.dvrp.fleet.VehicleRemovedEventHandler; import org.matsim.contrib.dvrp.schedule.Task; import org.matsim.contrib.dvrp.vrpagent.TaskEndedEvent; import org.matsim.contrib.dvrp.vrpagent.TaskEndedEventHandler; @@ -53,10 +57,11 @@ /** * @author michalm (Michal Maciejewski) + * @author Sebastian Hörl (sebhoerl), IRT SystemX */ public class VehicleOccupancyProfileCalculator implements PersonEntersVehicleEventHandler, PersonLeavesVehicleEventHandler, TaskStartedEventHandler, - TaskEndedEventHandler { + TaskEndedEventHandler, VehicleAddedEventHandler, VehicleRemovedEventHandler { private static class VehicleState { private Task.TaskType taskType; @@ -161,7 +166,12 @@ public TimeDiscretizer getTimeDiscretizer() { } private void increment(VehicleState state, double endTime) { - Verify.verify(state.taskType != null); + if (state.taskType == null) { + // will be null if a vehicle has been added to the fleet, but the task that has + // ended came from a different dvrp mode + return; + } + Verify.verify(state.occupancy >= 0); boolean servingPassengers = passengerServingTaskTypes.contains(state.taskType) || state.occupancy > 0; @@ -197,18 +207,30 @@ private void increment(double[] values, double beginTime, double endTime) { /* Event handling starts here */ @Override - public void handleEvent(TaskStartedEvent event) { + public void handleEvent(VehicleAddedEvent event) { if (!event.getDvrpMode().equals(dvrpMode)) { return; } + + vehicleStates.put(event.getDvrpVehicleId(), new VehicleState()); + } - final VehicleState state; - if (event.getTaskIndex() == 0) { - state = new VehicleState(); - vehicleStates.put(event.getDvrpVehicleId(), state); - } else { - state = vehicleStates.get(event.getDvrpVehicleId()); + @Override + public void handleEvent(VehicleRemovedEvent event) { + if (!event.getDvrpMode().equals(dvrpMode)) { + return; } + + increment(vehicleStates.remove(event.getDvrpVehicleId()), event.getTime()); + } + + @Override + public void handleEvent(TaskStartedEvent event) { + if (!event.getDvrpMode().equals(dvrpMode)) { + return; + } + + VehicleState state = vehicleStates.get(event.getDvrpVehicleId()); state.taskType = event.getTaskType(); state.beginTime = event.getTime(); } diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/VehicleTaskProfileCalculator.java b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/VehicleTaskProfileCalculator.java index 673eae38188..4c51de9c1a0 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/VehicleTaskProfileCalculator.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/VehicleTaskProfileCalculator.java @@ -27,6 +27,10 @@ import org.matsim.contrib.dvrp.fleet.DvrpVehicle; import org.matsim.contrib.dvrp.fleet.DvrpVehicleSpecification; import org.matsim.contrib.dvrp.fleet.FleetSpecification; +import org.matsim.contrib.dvrp.fleet.VehicleAddedEvent; +import org.matsim.contrib.dvrp.fleet.VehicleAddedEventHandler; +import org.matsim.contrib.dvrp.fleet.VehicleRemovedEvent; +import org.matsim.contrib.dvrp.fleet.VehicleRemovedEventHandler; import org.matsim.contrib.dvrp.schedule.Task; import org.matsim.contrib.common.timeprofile.TimeDiscretizer; import org.matsim.contrib.dvrp.vrpagent.TaskEndedEvent; @@ -36,15 +40,15 @@ import org.matsim.core.config.groups.QSimConfigGroup; import com.google.common.base.Preconditions; -import com.google.common.base.Verify; /** * Collects task profiles of DVRP vehicles. Based on {@link VehicleOccupancyProfileCalculator} but only collects tasks. * * @author nkuehnel / MOIA + * @author Sebastian Hörl (sebhoerl), IRT SystemX */ public class VehicleTaskProfileCalculator implements TaskStartedEventHandler, - TaskEndedEventHandler { + TaskEndedEventHandler, VehicleAddedEventHandler, VehicleRemovedEventHandler { private static class VehicleState { private Task.TaskType taskType; @@ -114,7 +118,11 @@ public TimeDiscretizer getTimeDiscretizer() { } private void increment(VehicleState state, double endTime) { - Verify.verify(state.taskType != null); + if (state.taskType == null) { + // will be null if a vehicle has been added to the fleet, but the task that has + // ended came from a different dvrp mode + return; + } double[] profile = taskProfiles.computeIfAbsent(state.taskType, v -> new double[timeDiscretizer.getIntervalCount()]); @@ -145,18 +153,30 @@ private void increment(double[] values, double beginTime, double endTime) { /* Event handling starts here */ @Override - public void handleEvent(TaskStartedEvent event) { + public void handleEvent(VehicleAddedEvent event) { if (!event.getDvrpMode().equals(dvrpMode)) { return; } + + vehicleStates.put(event.getDvrpVehicleId(), new VehicleState()); + } - final VehicleState state; - if (event.getTaskIndex() == 0) { - state = new VehicleState(); - vehicleStates.put(event.getDvrpVehicleId(), state); - } else { - state = vehicleStates.get(event.getDvrpVehicleId()); + @Override + public void handleEvent(VehicleRemovedEvent event) { + if (!event.getDvrpMode().equals(dvrpMode)) { + return; } + + increment(vehicleStates.remove(event.getDvrpVehicleId()), event.getTime()); + } + + @Override + public void handleEvent(TaskStartedEvent event) { + if (!event.getDvrpMode().equals(dvrpMode)) { + return; + } + + VehicleState state = vehicleStates.get(event.getDvrpVehicleId()); state.taskType = event.getTaskType(); state.beginTime = event.getTime(); } diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/fleet/AbstractFleetEvent.java b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/fleet/AbstractFleetEvent.java index bd51eeaa171..d46685010b8 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/fleet/AbstractFleetEvent.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/fleet/AbstractFleetEvent.java @@ -23,11 +23,11 @@ protected AbstractFleetEvent(double time, String mode, Id vehicleId this.vehicleId = vehicleId; } - public String getMode() { + public String getDvrpMode() { return mode; } - public Id getVehicleId() { + public Id getDvrpVehicleId() { return vehicleId; }