Skip to content

Commit

Permalink
re-structuring
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed Sep 23, 2024
1 parent fadbfa6 commit db82714
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 92 deletions.
102 changes: 102 additions & 0 deletions src/main/java/org/matsim/run/LausitzDrtScenario.java
Original file line number Diff line number Diff line change
@@ -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()
);
}
});
}

}
}
95 changes: 4 additions & 91 deletions src/main/java/org/matsim/run/RunLausitzDrtScenario.java
Original file line number Diff line number Diff line change
@@ -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()
);
}
});
}

}
}
2 changes: 1 addition & 1 deletion src/test/java/org/matsim/run/RunIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit db82714

Please sign in to comment.