-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable initial DRT estimators (#2968)
* add interface and implementation for initial drt estimates * remove left over toods
- Loading branch information
Showing
7 changed files
with
256 additions
and
24 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...ensions/src/main/java/org/matsim/contrib/drt/extension/estimator/DrtInitialEstimator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.matsim.contrib.drt.extension.estimator; | ||
|
||
/** | ||
* This interface is used to provide an initial estimate for the drt service. | ||
* Supposed to be used when no data is available from the simulation yet. | ||
* The interface is exactly the same as {@link DrtEstimator}, but this class won't be called with update events. | ||
*/ | ||
public interface DrtInitialEstimator extends DrtEstimator { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...s/src/main/java/org/matsim/contrib/drt/extension/estimator/impl/ConstantDrtEstimator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.matsim.contrib.drt.extension.estimator.impl; | ||
|
||
import org.matsim.contrib.drt.extension.estimator.DrtInitialEstimator; | ||
import org.matsim.contrib.drt.fare.DrtFareParams; | ||
import org.matsim.contrib.drt.routing.DrtRoute; | ||
import org.matsim.contrib.drt.run.DrtConfigGroup; | ||
import org.matsim.core.utils.misc.OptionalTime; | ||
|
||
/** | ||
* Estimates using a constant detour factor and waiting time. | ||
*/ | ||
public class ConstantDrtEstimator implements DrtInitialEstimator { | ||
|
||
private final DrtConfigGroup drtConfig; | ||
|
||
/** | ||
* Detour factor for the estimate. 1.0 means no detour, 2.0 means twice the distance. | ||
*/ | ||
private final double detourFactor; | ||
|
||
/** | ||
* Constant waiting time estimate in seconds. | ||
*/ | ||
private final double waitingTime; | ||
|
||
public ConstantDrtEstimator(DrtConfigGroup drtConfig, double detourFactor, double waitingTime) { | ||
this.drtConfig = drtConfig; | ||
this.detourFactor = detourFactor; | ||
this.waitingTime = waitingTime; | ||
} | ||
|
||
@Override | ||
public Estimate estimate(DrtRoute route, OptionalTime departureTime) { | ||
|
||
double distance = route.getDistance() * detourFactor; | ||
double travelTime = route.getDirectRideTime() * detourFactor; | ||
|
||
double fare = 0; | ||
if (drtConfig.getDrtFareParams().isPresent()) { | ||
DrtFareParams fareParams = drtConfig.getDrtFareParams().get(); | ||
fare = fareParams.distanceFare_m * distance | ||
+ fareParams.timeFare_h * travelTime / 3600.0 | ||
+ fareParams.baseFare; | ||
|
||
fare = Math.max(fare, fareParams.minFarePerTrip); | ||
} | ||
|
||
return new Estimate(distance, travelTime, waitingTime, fare, 0); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...rc/main/java/org/matsim/contrib/drt/extension/estimator/impl/PessimisticDrtEstimator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.matsim.contrib.drt.extension.estimator.impl; | ||
|
||
import org.matsim.contrib.drt.extension.estimator.DrtInitialEstimator; | ||
import org.matsim.contrib.drt.fare.DrtFareParams; | ||
import org.matsim.contrib.drt.routing.DrtRoute; | ||
import org.matsim.contrib.drt.run.DrtConfigGroup; | ||
import org.matsim.core.utils.misc.OptionalTime; | ||
|
||
/** | ||
* Uses the upper bounds from config for the initial estimate. | ||
*/ | ||
public class PessimisticDrtEstimator implements DrtInitialEstimator { | ||
private final DrtConfigGroup drtConfig; | ||
|
||
public PessimisticDrtEstimator(DrtConfigGroup drtConfig) { | ||
this.drtConfig = drtConfig; | ||
} | ||
|
||
@Override | ||
public Estimate estimate(DrtRoute route, OptionalTime departureTime) { | ||
// If not estimates are present, use travel time alpha as detour | ||
// beta is not used, because estimates are supposed to be minimums and not worst cases | ||
double travelTime = Math.min(route.getDirectRideTime() + drtConfig.maxAbsoluteDetour, | ||
route.getDirectRideTime() * drtConfig.maxTravelTimeAlpha); | ||
|
||
double fare = 0; | ||
if (drtConfig.getDrtFareParams().isPresent()) { | ||
DrtFareParams fareParams = drtConfig.getDrtFareParams().get(); | ||
fare = fareParams.distanceFare_m * route.getDistance() | ||
+ fareParams.timeFare_h * route.getDirectRideTime() / 3600.0 | ||
+ fareParams.baseFare; | ||
|
||
fare = Math.max(fare, fareParams.minFarePerTrip); | ||
} | ||
|
||
// for distance, also use the max travel time alpha | ||
return new Estimate(route.getDistance() * drtConfig.maxTravelTimeAlpha, travelTime, drtConfig.maxWaitTime, fare, 0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...st/java/org/matsim/contrib/drt/extension/estimator/MultiModaFixedDrtLegEstimatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.matsim.contrib.drt.extension.estimator; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.matsim.application.MATSimApplication; | ||
import org.matsim.contrib.drt.extension.DrtTestScenario; | ||
import org.matsim.contrib.drt.extension.estimator.impl.ConstantDrtEstimator; | ||
import org.matsim.contrib.drt.extension.estimator.run.DrtEstimatorConfigGroup; | ||
import org.matsim.contrib.drt.extension.estimator.run.DrtEstimatorModule; | ||
import org.matsim.contrib.drt.extension.estimator.run.MultiModeDrtEstimatorConfigGroup; | ||
import org.matsim.core.config.Config; | ||
import org.matsim.core.config.ConfigUtils; | ||
import org.matsim.core.config.groups.ReplanningConfigGroup; | ||
import org.matsim.core.controler.Controler; | ||
import org.matsim.modechoice.InformedModeChoiceModule; | ||
import org.matsim.modechoice.ModeOptions; | ||
import org.matsim.modechoice.estimators.DefaultLegScoreEstimator; | ||
import org.matsim.modechoice.estimators.FixedCostsEstimator; | ||
import org.matsim.testcases.MatsimTestUtils; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class MultiModaFixedDrtLegEstimatorTest { | ||
|
||
@Rule | ||
public MatsimTestUtils utils = new MatsimTestUtils(); | ||
|
||
private Controler controler; | ||
|
||
private static void prepare(Controler controler) { | ||
InformedModeChoiceModule.Builder builder = InformedModeChoiceModule.newBuilder() | ||
.withFixedCosts(FixedCostsEstimator.DailyConstant.class, "car") | ||
.withLegEstimator(DefaultLegScoreEstimator.class, ModeOptions.AlwaysAvailable.class, "bike", "walk", "pt") | ||
.withLegEstimator(DefaultLegScoreEstimator.class, ModeOptions.ConsiderYesAndNo.class, "car") | ||
.withLegEstimator(MultiModalDrtLegEstimator.class, ModeOptions.AlwaysAvailable.class, "drt", "av"); | ||
|
||
controler.addOverridingModule(builder.build()); | ||
controler.addOverridingModule(new DrtEstimatorModule() | ||
.withInitialEstimator(cfg -> new ConstantDrtEstimator(cfg, 1.05, 300), "drt", "av")); | ||
} | ||
|
||
private static void prepare(Config config) { | ||
|
||
MultiModeDrtEstimatorConfigGroup estimators = ConfigUtils.addOrGetModule(config, MultiModeDrtEstimatorConfigGroup.class); | ||
|
||
estimators.addParameterSet(new DrtEstimatorConfigGroup("drt") | ||
.withEstimator(DrtEstimatorConfigGroup.EstimatorType.INITIAL)); | ||
estimators.addParameterSet(new DrtEstimatorConfigGroup("av")); | ||
|
||
// Set subtour mode selection as strategy | ||
List<ReplanningConfigGroup.StrategySettings> strategies = config.replanning().getStrategySettings().stream() | ||
.filter(s -> !s.getStrategyName().toLowerCase().contains("mode") | ||
).collect(Collectors.toList()); | ||
|
||
strategies.add(new ReplanningConfigGroup.StrategySettings() | ||
.setStrategyName(InformedModeChoiceModule.SELECT_SUBTOUR_MODE_STRATEGY) | ||
.setSubpopulation("person") | ||
.setWeight(0.2)); | ||
|
||
config.replanning().clearStrategySettings(); | ||
strategies.forEach(s -> config.replanning().addStrategySettings(s)); | ||
|
||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
|
||
Config config = DrtTestScenario.loadConfig(utils); | ||
|
||
config.controller().setLastIteration(3); | ||
|
||
controler = MATSimApplication.prepare(new DrtTestScenario(MultiModaFixedDrtLegEstimatorTest::prepare, MultiModaFixedDrtLegEstimatorTest::prepare), config); | ||
} | ||
|
||
@Test | ||
public void run() { | ||
|
||
String out = utils.getOutputDirectory(); | ||
|
||
controler.run(); | ||
|
||
assertThat(new File(out, "kelheim-mini-drt.drt_estimates_drt.csv")) | ||
.exists() | ||
.isNotEmpty(); | ||
|
||
|
||
} | ||
} |