diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/PrebookingActionCreator.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/PrebookingActionCreator.java index 0a0f1bb3d9a..066e3fb5ba4 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/PrebookingActionCreator.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/PrebookingActionCreator.java @@ -2,8 +2,10 @@ import static org.matsim.contrib.drt.schedule.DrtTaskBaseType.getBaseTypeOrElseThrow; +import java.util.ArrayList; import java.util.List; +import com.google.common.base.Verify; import org.matsim.contrib.drt.passenger.AcceptedDrtRequest; import org.matsim.contrib.drt.prebooking.abandon.AbandonVoter; import org.matsim.contrib.drt.schedule.DrtStopTask; @@ -13,6 +15,8 @@ import org.matsim.contrib.dvrp.fleet.DvrpVehicle; import org.matsim.contrib.dvrp.fleet.dvrp_load.DvrpLoad; import org.matsim.contrib.dvrp.passenger.PassengerHandler; +import org.matsim.contrib.dvrp.passenger.VehicleCapacityChangeActivity; +import org.matsim.contrib.dvrp.schedule.CapacityChangeTask; import org.matsim.contrib.dvrp.schedule.Task; import org.matsim.contrib.dvrp.vrpagent.VrpAgentLogic; import org.matsim.contrib.dynagent.DynAction; @@ -50,6 +54,9 @@ public DynAction createAction(DynAgent dynAgent, DvrpVehicle vehicle, double now Task task = vehicle.getSchedule().getCurrentTask(); if (getBaseTypeOrElseThrow(task).equals(DrtTaskBaseType.STOP)) { + if(task instanceof CapacityChangeTask capacityChangeTask) { + return new VehicleCapacityChangeActivity(DrtActionCreator.DRT_CAPACITY_CHANGE_NAME, vehicle, capacityChangeTask.getNewVehicleCapacity(), task.getEndTime()); + } DrtStopTask stopTask = (DrtStopTask) task; DvrpLoad incomingCapacity = getIncomingOccupancy(vehicle, stopTask); @@ -63,21 +70,39 @@ public DynAction createAction(DynAgent dynAgent, DvrpVehicle vehicle, double now private DvrpLoad getIncomingOccupancy(DvrpVehicle vehicle, DrtStopTask referenceTask) { DvrpLoad incomingOccupancy = vehicle.getCapacity().getType().getEmptyLoad(); + List vehicleCapacities = new ArrayList<>(); List tasks = vehicle.getSchedule().getTasks(); + vehicleCapacities.add(vehicle.getCapacity()); + boolean encounteredCurrentTask = false; + for(Task task : tasks) { + if(task == vehicle.getSchedule().getCurrentTask()) { + encounteredCurrentTask = true; + } + if(encounteredCurrentTask && task instanceof CapacityChangeTask capacityChangeTask) { + vehicleCapacities.add(capacityChangeTask.getNewVehicleCapacity()); + } + } + + int currentCapacityIndex = vehicleCapacities.size() - 1; + incomingOccupancy = vehicleCapacities.get(currentCapacityIndex).getType().getEmptyLoad(); int index = tasks.size() - 1; while (index >= 0) { Task task = tasks.get(index); - if (task instanceof DrtStopTask) { + if(task instanceof CapacityChangeTask) { + Verify.verify(incomingOccupancy.isEmpty()); + currentCapacityIndex--; + incomingOccupancy = vehicleCapacities.get(currentCapacityIndex).getType().getEmptyLoad(); + } else if (task instanceof DrtStopTask) { DrtStopTask stopTask = (DrtStopTask) task; incomingOccupancy = incomingOccupancy.add(stopTask.getDropoffRequests().values().stream() - .map(AcceptedDrtRequest::getLoad).reduce(DvrpLoad::add).orElse(vehicle.getCapacity().getType().getEmptyLoad())); + .map(AcceptedDrtRequest::getLoad).reduce(DvrpLoad::add).orElse(incomingOccupancy.getType().getEmptyLoad())); incomingOccupancy = incomingOccupancy.subtract(stopTask.getPickupRequests().values().stream() - .map(AcceptedDrtRequest::getLoad).reduce(DvrpLoad::add).orElse(vehicle.getCapacity().getType().getEmptyLoad())); + .map(AcceptedDrtRequest::getLoad).reduce(DvrpLoad::add).orElse(incomingOccupancy.getType().getEmptyLoad())); if (task == referenceTask) { return incomingOccupancy;