Skip to content

Commit

Permalink
feat(drt): handle DetourTimeEstimator through dependency injection (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl authored Feb 9, 2024
1 parent bd7d012 commit 499e0c3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import java.util.stream.Collectors;

import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.optimizer.insertion.*;
import org.matsim.contrib.drt.optimizer.insertion.DetourTimeEstimator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionCostCalculator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionGenerator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionGenerator.Insertion;
import org.matsim.contrib.drt.optimizer.insertion.InsertionWithDetourData;
import org.matsim.contrib.drt.passenger.DrtRequest;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.stops.PassengerStopDurationProvider;
import org.matsim.contrib.drt.stops.StopTimeCalculator;
import org.matsim.contrib.zone.skims.TravelTimeMatrix;
import org.matsim.core.router.util.TravelTime;

import com.google.common.annotations.VisibleForTesting;

Expand All @@ -44,12 +44,9 @@
*/
class ExtensiveInsertionProvider {
static ExtensiveInsertionProvider create(DrtConfigGroup drtCfg, InsertionCostCalculator insertionCostCalculator,
TravelTimeMatrix travelTimeMatrix, TravelTime travelTime, ForkJoinPool forkJoinPool,
StopTimeCalculator stopTimeCalculator) {
var insertionParams = (ExtensiveInsertionSearchParams)drtCfg.getDrtInsertionSearchParams();
var admissibleTimeEstimator = DetourTimeEstimator.createMatrixBasedEstimator(
insertionParams.admissibleBeelineSpeedFactor, travelTimeMatrix, travelTime);
return new ExtensiveInsertionProvider((ExtensiveInsertionSearchParams)drtCfg.getDrtInsertionSearchParams(),
ForkJoinPool forkJoinPool, StopTimeCalculator stopTimeCalculator,
DetourTimeEstimator admissibleTimeEstimator) {
return new ExtensiveInsertionProvider((ExtensiveInsertionSearchParams) drtCfg.getDrtInsertionSearchParams(),
insertionCostCalculator, new InsertionGenerator(stopTimeCalculator, admissibleTimeEstimator),
forkJoinPool);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.optimizer.QSimScopeForkJoinPoolHolder;
import org.matsim.contrib.drt.optimizer.insertion.DetourTimeEstimator;
import org.matsim.contrib.drt.optimizer.insertion.DrtInsertionSearch;
import org.matsim.contrib.drt.optimizer.insertion.InsertionCostCalculator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.stops.PassengerStopDurationProvider;
import org.matsim.contrib.drt.stops.StopTimeCalculator;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeQSimModule;
import org.matsim.contrib.dvrp.run.DvrpModes;
Expand All @@ -48,11 +48,19 @@ public ExtensiveInsertionSearchQSimModule(DrtConfigGroup drtCfg) {

@Override
protected void configureQSim() {
bindModal(DetourTimeEstimator.class).toProvider(modalProvider(getter -> {
var insertionParams = (ExtensiveInsertionSearchParams) drtCfg.getDrtInsertionSearchParams();
var admissibleTimeEstimator = DetourTimeEstimator.createMatrixBasedEstimator(
insertionParams.admissibleBeelineSpeedFactor, getter.getModal(TravelTimeMatrix.class),
getter.getModal(TravelTime.class));
return admissibleTimeEstimator;
}));

bindModal(DrtInsertionSearch.class).toProvider(modalProvider(getter -> {
var insertionCostCalculator = getter.getModal(InsertionCostCalculator.class);
var provider = ExtensiveInsertionProvider.create(drtCfg, insertionCostCalculator,
getter.getModal(TravelTimeMatrix.class), getter.getModal(TravelTime.class),
getter.getModal(QSimScopeForkJoinPoolHolder.class).getPool(), getter.getModal(StopTimeCalculator.class));
getter.getModal(QSimScopeForkJoinPoolHolder.class).getPool(),
getter.getModal(StopTimeCalculator.class), getter.getModal(DetourTimeEstimator.class));
return new ExtensiveInsertionSearch(provider, getter.getModal(MultiInsertionDetourPathCalculator.class),
insertionCostCalculator, getter.getModal(StopTimeCalculator.class));
})).asEagerSingleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@

package org.matsim.contrib.drt.optimizer.insertion.repeatedselective;

import com.google.common.annotations.VisibleForTesting;
import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.optimizer.insertion.*;
import org.matsim.contrib.drt.optimizer.insertion.BestInsertionFinder.InsertionWithCost;
import static org.matsim.contrib.drt.optimizer.insertion.BestInsertionFinder.INSERTION_WITH_COST_COMPARATOR;
import org.matsim.contrib.drt.passenger.DrtRequest;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.stops.StopTimeCalculator;
import org.matsim.contrib.zone.skims.AdaptiveTravelTimeMatrix;
import org.matsim.core.router.util.TravelTime;

import static org.matsim.contrib.drt.optimizer.insertion.InsertionCostCalculator.INFEASIBLE_SOLUTION_COST;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.optimizer.insertion.BestInsertionFinder.InsertionWithCost;
import org.matsim.contrib.drt.optimizer.insertion.DetourTimeEstimator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionCostCalculator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionGenerator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionWithDetourData;
import org.matsim.contrib.drt.passenger.DrtRequest;
import org.matsim.contrib.drt.stops.StopTimeCalculator;

import com.google.common.annotations.VisibleForTesting;

/**
* @author steffenaxer
*/
class RepeatedSelectiveInsertionProvider {
private static final double SPEED_FACTOR = 1.;
private final InsertionCostCalculator insertionCostCalculator;
private final InsertionGenerator insertionGenerator;
private final ForkJoinPool forkJoinPool;
Expand All @@ -54,14 +54,11 @@ class RepeatedSelectiveInsertionProvider {
this.forkJoinPool = forkJoinPool;
}

public static RepeatedSelectiveInsertionProvider create( InsertionCostCalculator insertionCostCalculator, AdaptiveTravelTimeMatrix updatableTravelTimeMatrix,
TravelTime travelTime,
ForkJoinPool forkJoinPool, StopTimeCalculator stopTimeCalculator) {
var detourTimeEstimatorWithUpdatedTravelTimes = DetourTimeEstimatorWithAdaptiveTravelTimes.create(
SPEED_FACTOR, updatableTravelTimeMatrix, travelTime);
return new RepeatedSelectiveInsertionProvider(insertionCostCalculator,
new InsertionGenerator(stopTimeCalculator, detourTimeEstimatorWithUpdatedTravelTimes), forkJoinPool);
}
public static RepeatedSelectiveInsertionProvider create(InsertionCostCalculator insertionCostCalculator,
ForkJoinPool forkJoinPool, StopTimeCalculator stopTimeCalculator, DetourTimeEstimator detourTimeEstimator) {
return new RepeatedSelectiveInsertionProvider(insertionCostCalculator,
new InsertionGenerator(stopTimeCalculator, detourTimeEstimator), forkJoinPool);
}

public List<InsertionWithDetourData> getInsertions(DrtRequest drtRequest, Collection<VehicleEntry> vehicleEntries) {
// Parallel outer stream over vehicle entries. The inner stream (flatmap) is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.optimizer.QSimScopeForkJoinPoolHolder;
import org.matsim.contrib.drt.optimizer.insertion.DetourTimeEstimator;
import org.matsim.contrib.drt.optimizer.insertion.DrtInsertionSearch;
import org.matsim.contrib.drt.optimizer.insertion.InsertionCostCalculator;
import org.matsim.contrib.drt.optimizer.insertion.selective.SingleInsertionDetourPathCalculator;
Expand All @@ -41,6 +42,8 @@
* @author steffenaxer
*/
public class RepeatedSelectiveInsertionSearchQSimModule extends AbstractDvrpModeQSimModule {
private static final double SPEED_FACTOR = 1.;

private final DrtConfigGroup drtCfg;

public RepeatedSelectiveInsertionSearchQSimModule(DrtConfigGroup drtCfg) {
Expand All @@ -50,11 +53,17 @@ public RepeatedSelectiveInsertionSearchQSimModule(DrtConfigGroup drtCfg) {

@Override
protected void configureQSim() {
bindModal(DetourTimeEstimator.class).toProvider(modalProvider(getter -> {
var detourTimeEstimatorWithUpdatedTravelTimes = DetourTimeEstimatorWithAdaptiveTravelTimes.create(
SPEED_FACTOR, getter.getModal(AdaptiveTravelTimeMatrix.class), getter.getModal(TravelTime.class));
return detourTimeEstimatorWithUpdatedTravelTimes;
}));

addModalComponent(RepeatedSelectiveInsertionSearch.class, modalProvider(getter -> {
RepeatedSelectiveInsertionProvider provider = RepeatedSelectiveInsertionProvider.create(
getter.getModal(InsertionCostCalculator.class), getter.getModal(AdaptiveTravelTimeMatrix.class),
getter.getModal(TravelTime.class), getter.getModal(QSimScopeForkJoinPoolHolder.class).getPool(),
getter.getModal(StopTimeCalculator.class));
RepeatedSelectiveInsertionProvider provider = RepeatedSelectiveInsertionProvider.create(
getter.getModal(InsertionCostCalculator.class),
getter.getModal(QSimScopeForkJoinPoolHolder.class).getPool(),
getter.getModal(StopTimeCalculator.class), getter.getModal(DetourTimeEstimator.class));
var insertionCostCalculator = getter.getModal(InsertionCostCalculator.class);
return new RepeatedSelectiveInsertionSearch(provider,
getter.getModal(SingleInsertionDetourPathCalculator.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,23 @@
import java.util.concurrent.ForkJoinPool;

import org.matsim.contrib.drt.optimizer.VehicleEntry;
import org.matsim.contrib.drt.optimizer.insertion.*;
import org.matsim.contrib.drt.optimizer.insertion.BestInsertionFinder;
import org.matsim.contrib.drt.optimizer.insertion.DetourTimeEstimator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionCostCalculator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionGenerator;
import org.matsim.contrib.drt.optimizer.insertion.InsertionWithDetourData;
import org.matsim.contrib.drt.passenger.DrtRequest;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.stops.PassengerStopDurationProvider;
import org.matsim.contrib.drt.stops.StopTimeCalculator;
import org.matsim.contrib.zone.skims.TravelTimeMatrix;
import org.matsim.core.router.util.TravelTime;

import com.google.common.annotations.VisibleForTesting;

/**
* @author michalm
*/
class SelectiveInsertionProvider {
public static SelectiveInsertionProvider create(DrtConfigGroup drtCfg,
InsertionCostCalculator insertionCostCalculator, TravelTimeMatrix travelTimeMatrix, TravelTime travelTime,
ForkJoinPool forkJoinPool, StopTimeCalculator stopTimeCalculator) {
var insertionParams = (SelectiveInsertionSearchParams)drtCfg.getDrtInsertionSearchParams();
var restrictiveDetourTimeEstimator = DetourTimeEstimator.createMatrixBasedEstimator(
insertionParams.restrictiveBeelineSpeedFactor, travelTimeMatrix, travelTime);
public static SelectiveInsertionProvider create(InsertionCostCalculator insertionCostCalculator,
ForkJoinPool forkJoinPool, StopTimeCalculator stopTimeCalculator,
DetourTimeEstimator restrictiveDetourTimeEstimator) {
return new SelectiveInsertionProvider(new BestInsertionFinder(insertionCostCalculator),
new InsertionGenerator(stopTimeCalculator, restrictiveDetourTimeEstimator), forkJoinPool);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.optimizer.QSimScopeForkJoinPoolHolder;
import org.matsim.contrib.drt.optimizer.insertion.DetourTimeEstimator;
import org.matsim.contrib.drt.optimizer.insertion.DrtInsertionSearch;
import org.matsim.contrib.drt.optimizer.insertion.InsertionCostCalculator;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.stops.PassengerStopDurationProvider;
import org.matsim.contrib.drt.stops.StopTimeCalculator;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeQSimModule;
import org.matsim.contrib.dvrp.run.DvrpModes;
Expand All @@ -49,10 +49,19 @@ public SelectiveInsertionSearchQSimModule(DrtConfigGroup drtCfg) {

@Override
protected void configureQSim() {
bindModal(DetourTimeEstimator.class).toProvider(modalProvider(getter -> {
var insertionParams = (SelectiveInsertionSearchParams) drtCfg.getDrtInsertionSearchParams();
var restrictiveDetourTimeEstimator = DetourTimeEstimator.createMatrixBasedEstimator(
insertionParams.restrictiveBeelineSpeedFactor, getter.getModal(TravelTimeMatrix.class),
getter.getModal(TravelTime.class));
return restrictiveDetourTimeEstimator;
}));

addModalComponent(SelectiveInsertionSearch.class, modalProvider(getter -> {
SelectiveInsertionProvider provider = SelectiveInsertionProvider.create(drtCfg,
getter.getModal(InsertionCostCalculator.class), getter.getModal(TravelTimeMatrix.class),
getter.getModal(TravelTime.class), getter.getModal(QSimScopeForkJoinPoolHolder.class).getPool(), getter.getModal(StopTimeCalculator.class));
SelectiveInsertionProvider provider = SelectiveInsertionProvider.create(
getter.getModal(InsertionCostCalculator.class),
getter.getModal(QSimScopeForkJoinPoolHolder.class).getPool(),
getter.getModal(StopTimeCalculator.class), getter.getModal(DetourTimeEstimator.class));
// Use 0 as the cost for the selected insertion:
// - In the selective strategy, there is at most 1 insertion pre-selected. So no need to compute as there is
// no other insertion to compare with.
Expand Down

0 comments on commit 499e0c3

Please sign in to comment.