Skip to content

Commit

Permalink
Merge branch 'master' into 2817-default-SpeedyALT
Browse files Browse the repository at this point in the history
  • Loading branch information
mrieser authored Oct 10, 2023
2 parents 04b1743 + e89c773 commit d454c16
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.drt.optimizer.depot.DepotFinder;
import org.matsim.contrib.drt.optimizer.depot.Depots;
import org.matsim.contrib.drt.optimizer.insertion.UnplannedRequestInserter;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy.Relocation;
Expand All @@ -43,6 +42,8 @@
import org.matsim.core.mobsim.framework.MobsimTimer;
import org.matsim.core.mobsim.framework.events.MobsimBeforeSimStepEvent;

import static org.matsim.contrib.drt.schedule.DrtTaskBaseType.STAY;

/**
* @author michalm
*/
Expand Down Expand Up @@ -95,6 +96,7 @@ public void notifyMobsimBeforeSimStep(@SuppressWarnings("rawtypes") MobsimBefore
requestInserter.scheduleUnplannedRequests(unplannedRequests.getSchedulableRequests());
}

relocateVehiclesToDepot(drtCfg.returnToDepotEvaluationInterval, drtCfg.returnToDepotTimeout);
if (rebalancingInterval != null && e.getSimulationTime() % rebalancingInterval == 0) {
if (!scheduleTimingUpdated) {
for (DvrpVehicle v : fleet.getVehicles().values()) {
Expand All @@ -116,7 +118,7 @@ private void rebalanceFleet() {
for (Relocation r : relocations) {
Link currentLink = ((DrtStayTask)r.vehicle.getSchedule().getCurrentTask()).getLink();
if (currentLink != r.link) {
relocator.relocateVehicle(r.vehicle, r.link);
relocator.relocateVehicle(r.vehicle, r.link, EmptyVehicleRelocator.RELOCATE_VEHICLE_TASK_TYPE);
}
}
}
Expand All @@ -130,15 +132,29 @@ public void requestSubmitted(Request request) {
@Override
public void nextTask(DvrpVehicle vehicle) {
scheduleTimingUpdater.updateBeforeNextTask(vehicle);

vehicle.getSchedule().nextTask();
}

// if STOP->STAY then choose the best depot
if (drtCfg.idleVehiclesReturnToDepots && Depots.isSwitchingFromStopToStay(vehicle)) {
Link depotLink = depotFinder.findDepot(vehicle);
if (depotLink != null) {
relocator.relocateVehicle(vehicle, depotLink);
}
private void relocateVehiclesToDepot(double evaluationInterval, double timeout) {
if (drtCfg.idleVehiclesReturnToDepots && mobsimTimer.getTimeOfDay() % evaluationInterval == 0) {
fleet.getVehicles().values().stream()
.filter(scheduleInquiry::isIdle)
.filter(v -> stayTimeoutExceeded(v, timeout))
.forEach(v -> {
Link depotLink = depotFinder.findDepot(v);
if (depotLink != null) {
relocator.relocateVehicle(v, depotLink, EmptyVehicleRelocator.RELOCATE_VEHICLE_TO_DEPOT_TASK_TYPE);
}
});
}
}

boolean stayTimeoutExceeded(DvrpVehicle vehicle, double timeout) {
if (STAY.isBaseTypeOf(vehicle.getSchedule().getCurrentTask())) {
double now = mobsimTimer.getTimeOfDay();
double taskStart = vehicle.getSchedule().getCurrentTask().getBeginTime();
return (now - taskStart) > timeout;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ public static DrtConfigGroup getSingleModeDrtConfig(Config config) {
@Comment("Idle vehicles return to the nearest of all start links. See: DvrpVehicle.getStartLink()")
public boolean idleVehiclesReturnToDepots = false;

@Parameter
@Comment("Specifies the duration (seconds) a vehicle needs to be idle in order to get send back to the depot." +
"Please be aware, that returnToDepotEvaluationInterval describes the minimal time a vehicle will be idle before it gets send back to depot.")
public double returnToDepotTimeout = 60;

@Parameter
@Comment("Specifies the time interval (seconds) a vehicle gets evaluated to be send back to depot.")
public double returnToDepotEvaluationInterval = 60;

public enum OperationalScheme {
stopbased, door2door, serviceAreaBased
}
Expand Down Expand Up @@ -273,6 +282,11 @@ protected void checkConsistency(Config config) {
+ " in order to speed up the DRT route update during the replanning phase.");
}

if (this.idleVehiclesReturnToDepots && this.returnToDepotTimeout < this.returnToDepotEvaluationInterval) {
log.warn("idleVehiclesReturnToDepots is active and returnToDepotTimeout < returnToDepotEvaluationInterval. " +
"Vehicles will be send back to depot after {} seconds",returnToDepotEvaluationInterval);
}

Verify.verify(getParameterSets(MinCostFlowRebalancingStrategyParams.SET_NAME).size() <= 1,
"More than one rebalancing parameter sets is specified");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
*/
public class EmptyVehicleRelocator {
public static final DrtTaskType RELOCATE_VEHICLE_TASK_TYPE = new DrtTaskType("RELOCATE", DRIVE);
public static final DrtTaskType RELOCATE_VEHICLE_TO_DEPOT_TASK_TYPE = new DrtTaskType("RELOCATE_TO_DEPOT", DRIVE);

private final TravelTime travelTime;
private final MobsimTimer timer;
Expand All @@ -55,28 +56,28 @@ public EmptyVehicleRelocator(Network network, TravelTime travelTime, TravelDisut
router = new SpeedyALTFactory().createPathCalculator(network, travelDisutility, travelTime);
}

public void relocateVehicle(DvrpVehicle vehicle, Link link) {
public void relocateVehicle(DvrpVehicle vehicle, Link link, DrtTaskType relocationTaskType) {
DrtStayTask currentTask = (DrtStayTask)vehicle.getSchedule().getCurrentTask();
Link currentLink = currentTask.getLink();

if (currentLink != link) {
VrpPathWithTravelData path = VrpPaths.calcAndCreatePath(currentLink, link, timer.getTimeOfDay(), router,
travelTime);
if (path.getArrivalTime() < vehicle.getServiceEndTime()) {
relocateVehicleImpl(vehicle, path);
relocateVehicleImpl(vehicle, path, relocationTaskType);
}
}
}

private void relocateVehicleImpl(DvrpVehicle vehicle, VrpPathWithTravelData vrpPath) {
private void relocateVehicleImpl(DvrpVehicle vehicle, VrpPathWithTravelData vrpPath, DrtTaskType relocationTaskType) {
Schedule schedule = vehicle.getSchedule();
DrtStayTask stayTask = (DrtStayTask)schedule.getCurrentTask();
if (stayTask.getTaskIdx() != schedule.getTaskCount() - 1) {
throw new IllegalStateException("The current STAY task is not last. Not possible without prebooking");
}

stayTask.setEndTime(vrpPath.getDepartureTime()); // finish STAY
schedule.addTask(taskFactory.createDriveTask(vehicle, vrpPath, RELOCATE_VEHICLE_TASK_TYPE)); // add RELOCATE
schedule.addTask(taskFactory.createDriveTask(vehicle, vrpPath, relocationTaskType)); // add RELOCATE
// append STAY
schedule.addTask(taskFactory.createStayTask(vehicle, vrpPath.getArrivalTime(), vehicle.getServiceEndTime(),
vrpPath.getToLink()));
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.5.0</version>
<version>5.6.0</version>
<scope>test</scope>
</dependency>

Expand All @@ -308,7 +308,7 @@
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.8</version>
<version>1.14.9</version>
<scope>test</scope>
</dependency>

Expand Down

0 comments on commit d454c16

Please sign in to comment.