Skip to content

Commit

Permalink
feat: compatibility of PrebookingActionCreator with CapacityChangeTask
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarek Chouaki committed Dec 8, 2024
1 parent 88b4495 commit fcb77e2
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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<DvrpLoad> vehicleCapacities = new ArrayList<>();

List<? extends Task> 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;
Expand Down

0 comments on commit fcb77e2

Please sign in to comment.