From f029ca06cc5f8cb0359fbc0b404e35b1ecd60a77 Mon Sep 17 00:00:00 2001 From: nkuehnel Date: Mon, 27 May 2024 21:22:49 +0200 Subject: [PATCH] simplify method, rename to lateDiversionThreshold --- .../DrtOptimizationConstraintsSet.java | 2 +- .../DefaultInsertionCostCalculator.java | 65 ++++++++++--------- .../InsertionCostCalculatorTest.java | 10 ++- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/DrtOptimizationConstraintsSet.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/DrtOptimizationConstraintsSet.java index 9e7f63c4940..63a03eaa014 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/DrtOptimizationConstraintsSet.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/DrtOptimizationConstraintsSet.java @@ -95,7 +95,7 @@ public DrtOptimizationConstraintsSet() { "3 minutes away from her destination, even though her time window would allow it." + " Delayed detours just before arrival are usually perceived very negatively.") @PositiveOrZero - public double allowDetourBeforeArrivalThreshold = 0; // [s]; + public double lateDiversionthreshold = 0; // [s]; @Override protected void checkConsistency(Config config) { diff --git a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/insertion/DefaultInsertionCostCalculator.java b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/insertion/DefaultInsertionCostCalculator.java index 14ec7ceb3d0..adbe51364c9 100644 --- a/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/insertion/DefaultInsertionCostCalculator.java +++ b/contribs/drt/src/main/java/org/matsim/contrib/drt/optimizer/insertion/DefaultInsertionCostCalculator.java @@ -20,6 +20,7 @@ package org.matsim.contrib.drt.optimizer.insertion; import org.matsim.contrib.drt.optimizer.DrtOptimizationConstraintsSet; +import org.matsim.contrib.drt.optimizer.VehicleEntry; import org.matsim.contrib.drt.optimizer.Waypoint; import org.matsim.contrib.drt.optimizer.insertion.InsertionDetourTimeCalculator.DetourTimeInfo; import org.matsim.contrib.drt.passenger.DrtRequest; @@ -72,34 +73,9 @@ public double calculate(DrtRequest drtRequest, Insertion insertion, DetourTimeIn return INFEASIBLE_SOLUTION_COST; } - // all stops after the new (potential) pickup but before the new dropoff - // are delayed by pickupDetourTimeLoss - double timeLoss = detourTimeInfo.pickupDetourInfo.pickupTimeLoss; - if(vEntry.stops != null) { - for (int s = insertion.pickup.index; s < vEntry.stops.size(); s++) { - if(timeLoss == 0) { - continue; - } - Waypoint.Stop stop = vEntry.stops.get(s); - // passengers are being dropped off == may be close to arrival - if (!stop.task.getDropoffRequests().isEmpty()) { - double nextArrival = stop.getArrivalTime(); - double departureTime = insertion.vehicleEntry.start.getDepartureTime(); - if (nextArrival - departureTime < constraintsSet.allowDetourBeforeArrivalThreshold) { - //arrival is too soon to allow further diversion - return INFEASIBLE_SOLUTION_COST; - } else if (nextArrival - departureTime >= constraintsSet.allowDetourBeforeArrivalThreshold) { - // all following stops are above the threshold - break; - } - } - if (s == insertion.dropoff.index) { - // all stops after the new (potential) dropoff are delayed by totalTimeLoss - timeLoss = detourTimeInfo.getTotalTimeLoss(); - if(timeLoss == 0) { - break; - } - } + if (vEntry.stops != null && !vEntry.stops.isEmpty() && constraintsSet.lateDiversionthreshold > 0) { + if(violatesLateDiversion(insertion, detourTimeInfo, vEntry, effectiveDropoffTimeLoss)) { + return INFEASIBLE_SOLUTION_COST; } } @@ -111,4 +87,35 @@ private boolean violatesMaxRideDuration(DrtRequest drtRequest, InsertionDetourTi double rideDuration = detourTimeInfo.dropoffDetourInfo.arrivalTime - detourTimeInfo.pickupDetourInfo.departureTime; return drtRequest.getMaxRideDuration() < rideDuration; } -} + + private boolean violatesLateDiversion(Insertion insertion, DetourTimeInfo detourTimeInfo, + VehicleEntry vEntry, double effectiveDropoffTimeLoss) { + if (detourTimeInfo.pickupDetourInfo.pickupTimeLoss > 0) { + if (checkStopsForDetour(vEntry, insertion.pickup.index, insertion.dropoff.index, + constraintsSet.lateDiversionthreshold)) { + return true; + } + } + if (effectiveDropoffTimeLoss > 0) { + if (checkStopsForDetour(vEntry, insertion.dropoff.index, vEntry.stops.size(), + constraintsSet.lateDiversionthreshold)) { + return true; + } + } + return false; + } + + private boolean checkStopsForDetour(VehicleEntry vehicleEntry, int start, int end, double lateDiversionThreshold) { + for (int s = start; s < end; s++) { + Waypoint.Stop stop = vehicleEntry.stops.get(s); + if (!stop.task.getDropoffRequests().isEmpty()) { + double remainingRideDuration = stop.getArrivalTime() - vehicleEntry.start.getDepartureTime(); + if (remainingRideDuration < lateDiversionThreshold) { + return true; + } + break; + } + } + return false; + } +} \ No newline at end of file diff --git a/contribs/drt/src/test/java/org/matsim/contrib/drt/optimizer/insertion/InsertionCostCalculatorTest.java b/contribs/drt/src/test/java/org/matsim/contrib/drt/optimizer/insertion/InsertionCostCalculatorTest.java index 206ed0b747d..8fab605b992 100644 --- a/contribs/drt/src/test/java/org/matsim/contrib/drt/optimizer/insertion/InsertionCostCalculatorTest.java +++ b/contribs/drt/src/test/java/org/matsim/contrib/drt/optimizer/insertion/InsertionCostCalculatorTest.java @@ -35,8 +35,6 @@ import org.matsim.contrib.drt.schedule.DrtStopTask; import org.matsim.testcases.fakes.FakeLink; -import java.util.Collections; - import static org.assertj.core.api.Assertions.assertThat; import static org.matsim.contrib.drt.optimizer.insertion.InsertionCostCalculator.INFEASIBLE_SOLUTION_COST; import static org.matsim.contrib.drt.optimizer.insertion.InsertionDetourTimeCalculator.*; @@ -108,7 +106,7 @@ public void testAllowDetourBeforeArrivalThreshold() { DrtOptimizationConstraintsSet drtOptimizationConstraintsSet = drtConfigGroup.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet(); // new insertion before dropoff of boarded passenger within threshold - infeasible solution - drtOptimizationConstraintsSet.allowDetourBeforeArrivalThreshold = 180; + drtOptimizationConstraintsSet.lateDiversionthreshold = 180; assertCalculate(insertion, new DetourTimeInfo(new PickupDetourInfo(60, 30), new DropoffDetourInfo(300, 30)), INFEASIBLE_SOLUTION_COST, drtRequest, drtOptimizationConstraintsSet); @@ -117,7 +115,7 @@ public void testAllowDetourBeforeArrivalThreshold() { 30, drtRequest, drtOptimizationConstraintsSet); // new insertion before dropoff of boarded passenger, but outside of threshold - feasible solution - drtOptimizationConstraintsSet.allowDetourBeforeArrivalThreshold = 120; + drtOptimizationConstraintsSet.lateDiversionthreshold = 120; assertCalculate(insertion, new DetourTimeInfo(new PickupDetourInfo(60, 30), new DropoffDetourInfo(300, 30)), 60, drtRequest, drtOptimizationConstraintsSet); @@ -166,12 +164,12 @@ public void testAllowDetourBeforeArrivalThreshold2() { // new insertion before dropoff of boarded passenger within threshold - infeasible solution DrtOptimizationConstraintsSet constraintsSet = drtConfigGroup.addOrGetDrtOptimizationConstraintsParams().addOrGetDefaultDrtOptimizationConstraintsSet(); - constraintsSet.allowDetourBeforeArrivalThreshold = 300; + constraintsSet.lateDiversionthreshold = 300; assertCalculate(insertion, new DetourTimeInfo(new PickupDetourInfo(60, 60), new DropoffDetourInfo(300, 60)), INFEASIBLE_SOLUTION_COST, drtRequest, constraintsSet); // new insertion before dropoff of boarded passenger outside of threshold - feasible solution - constraintsSet.allowDetourBeforeArrivalThreshold = 200; + constraintsSet.lateDiversionthreshold = 200; assertCalculate(insertion, new DetourTimeInfo(new PickupDetourInfo(60, 60), new DropoffDetourInfo(300, 60)), 120, drtRequest, constraintsSet); }