Skip to content

Commit

Permalink
simplify method, rename to lateDiversionThreshold
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel committed May 27, 2024
1 parent 4ad9ca2 commit f029ca0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit f029ca0

Please sign in to comment.