From db8271472d4f0f5fcc7d6b1500b654e4f819ae46 Mon Sep 17 00:00:00 2001 From: sime94 Date: Mon, 23 Sep 2024 16:41:03 +0200 Subject: [PATCH] re-structuring --- .../org/matsim/run/LausitzDrtScenario.java | 102 ++++++++++++++++++ .../org/matsim/run/RunLausitzDrtScenario.java | 95 +--------------- .../org/matsim/run/RunIntegrationTest.java | 2 +- 3 files changed, 107 insertions(+), 92 deletions(-) create mode 100644 src/main/java/org/matsim/run/LausitzDrtScenario.java diff --git a/src/main/java/org/matsim/run/LausitzDrtScenario.java b/src/main/java/org/matsim/run/LausitzDrtScenario.java new file mode 100644 index 0000000..b2153c2 --- /dev/null +++ b/src/main/java/org/matsim/run/LausitzDrtScenario.java @@ -0,0 +1,102 @@ +package org.matsim.run; + +import org.matsim.api.core.v01.Scenario; +import org.matsim.application.MATSimApplication; +import org.matsim.contrib.drt.estimator.DrtEstimatorModule; +import org.matsim.contrib.drt.estimator.impl.DirectTripBasedDrtEstimator; +import org.matsim.contrib.drt.estimator.impl.distribution.NormalDistributionGenerator; +import org.matsim.contrib.drt.estimator.impl.trip_estimation.ConstantRideDurationEstimator; +import org.matsim.contrib.drt.estimator.impl.waiting_time_estimation.ConstantWaitingTimeEstimator; +import org.matsim.contrib.drt.run.DrtConfigGroup; +import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup; +import org.matsim.contrib.drt.run.MultiModeDrtModule; +import org.matsim.contrib.dvrp.run.DvrpModule; +import org.matsim.contrib.dvrp.run.DvrpQSimComponents; +import org.matsim.core.config.Config; +import org.matsim.core.config.ConfigUtils; +import org.matsim.core.controler.AbstractModule; +import org.matsim.core.controler.Controler; +import picocli.CommandLine; + +import javax.annotation.Nullable; + +/** + * Run the Lausitz scenario including a regional DRT service. + * All necessary configs will be made in this class. + */ +public final class LausitzDrtScenario extends LausitzScenario { + +// run params re drt are contained in separate class DrtOptions + @CommandLine.ArgGroup(heading = "%nDrt options%n", exclusive = false, multiplicity = "0..1") + private final DrtOptions drtOpt = new DrtOptions(); + + private final LausitzScenario baseScenario = new LausitzScenario(sample, emissions); + +// this constructor is needed when this class is to be called from external classes with a given Config (e.g. for testing). + public LausitzDrtScenario(Config config) { + super(config); + } + + public LausitzDrtScenario() { + super(String.format("input/v%s/lausitz-v%s-10pct.config.xml", LausitzScenario.VERSION, LausitzScenario.VERSION)); + } + + public static void main(String[] args) { + MATSimApplication.run(LausitzDrtScenario.class, args); + } + + @Nullable + @Override + protected Config prepareConfig(Config config) { +// apply all config changes from base scenario class + baseScenario.prepareConfig(config); + +// apply all necessary config changes for drt simulation + drtOpt.configureDrtConfig(config); + + return config; + } + + @Override + protected void prepareScenario(Scenario scenario) { +// apply all scenario changes from base scenario class + baseScenario.prepareScenario(scenario); + +// apply all necessary scenario changes for drt simulation + drtOpt.configureDrtScenario(scenario); + } + + @Override + protected void prepareControler(Controler controler) { + Config config = controler.getConfig(); + +// apply all controller changes from base scenario class + baseScenario.prepareControler(controler); + + controler.addOverridingModule(new DvrpModule()); + controler.addOverridingModule(new MultiModeDrtModule()); + +// the following cannot be "experts only" (like requested from KN) because without it DRT would not work +// here, the DynActivityEngine, PreplanningEngine + DvrpModule for each drt mode are added to the qsim components +// this is necessary for drt / dvrp to work! + controler.configureQSimComponents(DvrpQSimComponents.activateAllModes(ConfigUtils.addOrGetModule(controler.getConfig(), MultiModeDrtConfigGroup.class))); + + MultiModeDrtConfigGroup multiModeDrtConfigGroup = MultiModeDrtConfigGroup.get(config); + for (DrtConfigGroup drtConfigGroup : multiModeDrtConfigGroup.getModalElements()) { + controler.addOverridingModule(new AbstractModule() { + @Override + public void install() { + DrtEstimatorModule.bindEstimator(binder(), drtConfigGroup.mode).toInstance( + new DirectTripBasedDrtEstimator.Builder() + .setWaitingTimeEstimator(new ConstantWaitingTimeEstimator(drtOpt.typicalWaitTime)) + .setWaitingTimeDistributionGenerator(new NormalDistributionGenerator(1, drtOpt.waitTimeStd)) + .setRideDurationEstimator(new ConstantRideDurationEstimator(drtOpt.rideTimeAlpha, drtOpt.rideTimeBeta)) + .setRideDurationDistributionGenerator(new NormalDistributionGenerator(2, drtOpt.rideTimeStd)) + .build() + ); + } + }); + } + + } +} diff --git a/src/main/java/org/matsim/run/RunLausitzDrtScenario.java b/src/main/java/org/matsim/run/RunLausitzDrtScenario.java index 0a914f2..0d06f83 100644 --- a/src/main/java/org/matsim/run/RunLausitzDrtScenario.java +++ b/src/main/java/org/matsim/run/RunLausitzDrtScenario.java @@ -1,104 +1,17 @@ package org.matsim.run; -import org.matsim.api.core.v01.Scenario; import org.matsim.application.MATSimApplication; -import org.matsim.contrib.drt.estimator.DrtEstimatorModule; -import org.matsim.contrib.drt.estimator.impl.DirectTripBasedDrtEstimator; -import org.matsim.contrib.drt.estimator.impl.distribution.NormalDistributionGenerator; -import org.matsim.contrib.drt.estimator.impl.trip_estimation.ConstantRideDurationEstimator; -import org.matsim.contrib.drt.estimator.impl.waiting_time_estimation.ConstantWaitingTimeEstimator; -import org.matsim.contrib.drt.run.DrtConfigGroup; -import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup; -import org.matsim.contrib.drt.run.MultiModeDrtModule; -import org.matsim.contrib.dvrp.run.DvrpModule; -import org.matsim.contrib.dvrp.run.DvrpQSimComponents; -import org.matsim.core.config.Config; -import org.matsim.core.config.ConfigUtils; -import org.matsim.core.controler.AbstractModule; -import org.matsim.core.controler.Controler; -import picocli.CommandLine; - -import javax.annotation.Nullable; /** - * Run the Lausitz scenario including a regional DRT service. - * All necessary configs will be made in this class. + * Run the Lausitz DRT scenario policy case. */ -public final class RunLausitzDrtScenario extends MATSimApplication { - -// TODO: re-structure like pt scenario run class - -// run params re drt are contained in separate class DrtOptions - @CommandLine.ArgGroup(heading = "%nDrt options%n", exclusive = false, multiplicity = "0..1") - private final DrtOptions drtOpt = new DrtOptions(); - - private final LausitzScenario baseScenario = new LausitzScenario(); - -// this constructor is needed when this class is to be called from external classes with a given Config (e.g. for testing). - public RunLausitzDrtScenario(Config config) { - super(config); - } +public final class RunLausitzDrtScenario { - public RunLausitzDrtScenario() { - super(String.format("input/v%s/lausitz-v%s-10pct.config.xml", LausitzScenario.VERSION, LausitzScenario.VERSION)); + private RunLausitzDrtScenario() { } public static void main(String[] args) { - MATSimApplication.run(RunLausitzDrtScenario.class, args); - } - - @Nullable - @Override - protected Config prepareConfig(Config config) { -// apply all config changes from base scenario class - baseScenario.prepareConfig(config); - -// apply all necessary config changes for drt simulation - drtOpt.configureDrtConfig(config); - - return config; + MATSimApplication.execute(LausitzDrtScenario.class, args); } - @Override - protected void prepareScenario(Scenario scenario) { -// apply all scenario changes from base scenario class - baseScenario.prepareScenario(scenario); - -// apply all necessary scenario changes for drt simulation - drtOpt.configureDrtScenario(scenario); - } - - @Override - protected void prepareControler(Controler controler) { - Config config = controler.getConfig(); - -// apply all controller changes from base scenario class - baseScenario.prepareControler(controler); - - controler.addOverridingModule(new DvrpModule()); - controler.addOverridingModule(new MultiModeDrtModule()); - -// the following cannot be "experts only" (like requested from KN) because without it DRT would not work -// here, the DynActivityEngine, PreplanningEngine + DvrpModule for each drt mode are added to the qsim components -// this is necessary for drt / dvrp to work! - controler.configureQSimComponents(DvrpQSimComponents.activateAllModes(ConfigUtils.addOrGetModule(controler.getConfig(), MultiModeDrtConfigGroup.class))); - - MultiModeDrtConfigGroup multiModeDrtConfigGroup = MultiModeDrtConfigGroup.get(config); - for (DrtConfigGroup drtConfigGroup : multiModeDrtConfigGroup.getModalElements()) { - controler.addOverridingModule(new AbstractModule() { - @Override - public void install() { - DrtEstimatorModule.bindEstimator(binder(), drtConfigGroup.mode).toInstance( - new DirectTripBasedDrtEstimator.Builder() - .setWaitingTimeEstimator(new ConstantWaitingTimeEstimator(drtOpt.typicalWaitTime)) - .setWaitingTimeDistributionGenerator(new NormalDistributionGenerator(1, drtOpt.waitTimeStd)) - .setRideDurationEstimator(new ConstantRideDurationEstimator(drtOpt.rideTimeAlpha, drtOpt.rideTimeBeta)) - .setRideDurationDistributionGenerator(new NormalDistributionGenerator(2, drtOpt.rideTimeStd)) - .build() - ); - } - }); - } - - } } diff --git a/src/test/java/org/matsim/run/RunIntegrationTest.java b/src/test/java/org/matsim/run/RunIntegrationTest.java index 10f2f79..c6cce20 100644 --- a/src/test/java/org/matsim/run/RunIntegrationTest.java +++ b/src/test/java/org/matsim/run/RunIntegrationTest.java @@ -69,7 +69,7 @@ void runScenarioIncludingDrt() { createDrtTestPopulation(inputPath); - assert MATSimApplication.execute(RunLausitzDrtScenario.class, config, + assert MATSimApplication.execute(LausitzDrtScenario.class, config, "--1pct", "--iterations", "1", "--config:plans.inputPlansFile", inputPath.toString(),