Skip to content

Commit

Permalink
update matsim dependency and run script
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Feb 13, 2024
1 parent 6047a78 commit ff1bb1f
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 190 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>matsim-all</artifactId>

<!-- PR-labelled release -->
<version>16.0-PR3085</version>
<version>16.0-PR3108</version>

<!-- snapshot == not recommended: rather use PR-labelled release!-->
<!-- <version>16.0-SNAPSHOT</version>-->
Expand Down Expand Up @@ -149,6 +149,11 @@
<artifactId>roadpricing</artifactId>
<version>${matsim.version}</version>
</dependency>
<dependency>
<groupId>org.matsim.contrib</groupId>
<artifactId>small-scale-traffic-generation</artifactId>
<version>${matsim.version}</version>
</dependency>

<dependency>
<groupId>com.github.matsim-vsp</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.matsim.api.core.v01.TransportMode;
import org.matsim.application.ApplicationUtils;
import org.matsim.core.config.Config;
import org.matsim.run.RunOpenBerlinScenario;
import org.matsim.run.OpenBerlinScenario;
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.DashboardProvider;
import org.matsim.simwrapper.SimWrapper;
Expand All @@ -26,7 +26,7 @@ public List<Dashboard> getDashboards(Config config, SimWrapper simWrapper) {

return List.of(
trips,
new TravelTimeComparisonDashboard(ApplicationUtils.resolve(config.getContext(), "berlin-v" + RunOpenBerlinScenario.VERSION + "-routes-ref.csv.gz")),
new TravelTimeComparisonDashboard(ApplicationUtils.resolve(config.getContext(), "berlin-v" + OpenBerlinScenario.VERSION + "-routes-ref.csv.gz")),
new TrafficCountsDashboard()
.withModes(TransportMode.car, Set.of(TransportMode.car))
.withModes(TransportMode.truck, Set.of(TransportMode.truck, "freight"))
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/matsim/prepare/ExtractFacilityShp.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.locationtech.jts.index.strtree.STRtree;
import org.matsim.application.MATSimAppCommand;
import org.matsim.application.options.CrsOptions;
import org.matsim.run.RunOpenBerlinScenario;
import org.matsim.run.OpenBerlinScenario;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CRSAuthorityFactory;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class ExtractFacilityShp implements MATSimAppCommand {
@CommandLine.Option(names = "--activity-mapping", description = "Path to activity napping json", required = true)
private Path mappingPath;
@CommandLine.Mixin
private CrsOptions crs = new CrsOptions("EPSG:4326", RunOpenBerlinScenario.CRS);
private CrsOptions crs = new CrsOptions("EPSG:4326", OpenBerlinScenario.CRS);
/**
* Maps types to feature index.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/matsim/prepare/FilterRelevantAgents.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.matsim.facilities.ActivityFacilities;
import org.matsim.facilities.FacilitiesUtils;
import org.matsim.facilities.MatsimFacilitiesReader;
import org.matsim.run.RunOpenBerlinScenario;
import org.matsim.run.OpenBerlinScenario;
import picocli.CommandLine;

import java.nio.file.Path;
Expand Down Expand Up @@ -89,10 +89,10 @@ public Integer call() throws Exception {
filter.filter(network, Set.of(TransportMode.car));

geometry = shp.getGeometry();
ct = shp.createTransformation(RunOpenBerlinScenario.CRS);
ct = shp.createTransformation(OpenBerlinScenario.CRS);

facilities = FacilitiesUtils.createActivityFacilities();
new MatsimFacilitiesReader(RunOpenBerlinScenario.CRS, RunOpenBerlinScenario.CRS, facilities)
new MatsimFacilitiesReader(OpenBerlinScenario.CRS, OpenBerlinScenario.CRS, facilities)
.readFile(facilityPath.toString());

ctxs = ThreadLocal.withInitial(() -> this.createRouter(network));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import org.matsim.prepare.opt.SelectPlansFromIndex;
import org.matsim.prepare.population.*;
import org.matsim.run.Activities;
import org.matsim.run.RunOpenBerlinScenario;
import org.matsim.run.OpenBerlinScenario;
import org.matsim.run.scoring.VspScoringModule;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import org.matsim.simwrapper.SimWrapperModule;
Expand All @@ -72,7 +72,7 @@
/**
* This scenario class is used for run a MATSim scenario in various stages of the calibration process.
*/
@CommandLine.Command(header = ":: Open Berlin Calibration ::", version = RunOpenBerlinScenario.VERSION, mixinStandardHelpOptions = true)
@CommandLine.Command(header = ":: Open Berlin Calibration ::", version = OpenBerlinScenario.VERSION, mixinStandardHelpOptions = true)
@MATSimApplication.Prepare({
CreateLandUseShp.class, CreateBerlinPopulation.class, CreateBrandenburgPopulation.class, MergePopulations.class,
LookupRegioStaR.class, ExtractFacilityShp.class, DownSamplePopulation.class, DownloadCommuterStatistic.class,
Expand Down Expand Up @@ -398,7 +398,7 @@ public void install() {
}
});

controler.addOverridingModule(new RunOpenBerlinScenario.TravelTimeBinding());
controler.addOverridingModule(new OpenBerlinScenario.TravelTimeBinding());
controler.addOverridingModule(new VspScoringModule());
controler.addOverridingModule(new SimWrapperModule());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.network.filter.NetworkFilterManager;
import org.matsim.core.utils.geometry.geotools.MGC;
import org.matsim.run.RunOpenBerlinScenario;
import org.matsim.run.OpenBerlinScenario;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.MathTransform;
Expand Down Expand Up @@ -117,7 +117,7 @@ public Integer call() throws Exception {
// Compare two line strings
index.setDistanceCalculator(NetworkIndex::minHausdorffDistance);

MathTransform transformation = getCoordinateTransformation(shp.getShapeCrs(), RunOpenBerlinScenario.CRS);
MathTransform transformation = getCoordinateTransformation(shp.getShapeCrs(), OpenBerlinScenario.CRS);

log.info("Processing features.");
int counter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.matsim.core.utils.geometry.geotools.MGC;
import org.matsim.core.utils.geometry.transformations.GeotoolsTransformation;
import org.matsim.prepare.RunOpenBerlinCalibration;
import org.matsim.run.RunOpenBerlinScenario;
import org.matsim.run.OpenBerlinScenario;
import org.opengis.feature.simple.SimpleFeature;
import picocli.CommandLine;

Expand Down Expand Up @@ -155,7 +155,7 @@ public Integer call() throws Exception {

PopulationUtils.sortPersons(population);

ProjectionUtils.putCRS(population, RunOpenBerlinScenario.CRS);
ProjectionUtils.putCRS(population, OpenBerlinScenario.CRS);
PopulationUtils.writePopulation(population, output.toString());

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.matsim.core.population.PersonUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.scenario.ProjectionUtils;
import org.matsim.run.RunOpenBerlinScenario;
import org.matsim.run.OpenBerlinScenario;
import org.opengis.feature.simple.SimpleFeature;
import picocli.CommandLine;

Expand Down Expand Up @@ -124,7 +124,7 @@ public Integer call() throws Exception {

PopulationUtils.sortPersons(population);

ProjectionUtils.putCRS(population, RunOpenBerlinScenario.CRS);
ProjectionUtils.putCRS(population, OpenBerlinScenario.CRS);
PopulationUtils.writePopulation(population, output.toString());

return 0;
Expand Down Expand Up @@ -170,7 +170,7 @@ private void addPersons(CSVRecord r, String code, String ars, MultiPolygon geom)
// All persons will be employed until employed population is empty.
PersonUtils.setEmployed(person, employed.subtract(1 / sample, age));

Coord coord = CreateBerlinPopulation.sampleHomeCoordinate(geom, RunOpenBerlinScenario.CRS, landuse, rnd);
Coord coord = CreateBerlinPopulation.sampleHomeCoordinate(geom, OpenBerlinScenario.CRS, landuse, rnd);

person.getAttributes().putAttribute(Attributes.HOME_X, coord.getX());
person.getAttributes().putAttribute(Attributes.HOME_Y, coord.getY());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.matsim.facilities.ActivityFacility;
import org.matsim.facilities.FacilitiesUtils;
import org.matsim.facilities.MatsimFacilitiesReader;
import org.matsim.run.RunOpenBerlinScenario;
import org.matsim.run.OpenBerlinScenario;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -33,7 +33,7 @@ final class FacilityIndex {

FacilityIndex(String facilityPath) {

new MatsimFacilitiesReader(RunOpenBerlinScenario.CRS, RunOpenBerlinScenario.CRS, all)
new MatsimFacilitiesReader(OpenBerlinScenario.CRS, OpenBerlinScenario.CRS, all)
.readFile(facilityPath);

Set<String> activities = all.getFacilities().values().stream()
Expand Down
140 changes: 140 additions & 0 deletions src/main/java/org/matsim/run/OpenBerlinScenario.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package org.matsim.run;

import com.google.inject.Key;
import com.google.inject.name.Names;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.application.MATSimApplication;
import org.matsim.application.options.SampleOptions;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.ReplanningConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.replanning.strategies.DefaultPlanStrategiesModule;
import org.matsim.core.router.costcalculators.TravelDisutilityFactory;
import org.matsim.core.router.util.TravelTime;
import org.matsim.core.scoring.functions.ScoringParametersForPerson;
import org.matsim.prepare.RunOpenBerlinCalibration;
import org.matsim.prepare.population.AssignIncome;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import org.matsim.simwrapper.SimWrapperModule;
import picocli.CommandLine;
import playground.vsp.scoring.IncomeDependentUtilityOfMoneyPersonScoringParameters;
import java.util.List;
import org.matsim.run.scoring.VspScoringModule;

@CommandLine.Command(header = ":: Open Berlin Scenario ::", version = OpenBerlinScenario.VERSION, mixinStandardHelpOptions = true)
public class OpenBerlinScenario extends MATSimApplication {

private static final Logger log = LogManager.getLogger(RunOpenBerlinCalibration.class);

public static final String VERSION = "6.1";
public static final String CRS = "EPSG:25832";
@CommandLine.Mixin
private final SampleOptions sample = new SampleOptions(10, 25, 3, 1);

public OpenBerlinScenario() {
super(String.format("input/v%s/berlin-v%s.config.xml", VERSION, VERSION));
}

public static void main(String[] args) {
MATSimApplication.run(OpenBerlinScenario.class, args);
}

@Override
protected Config prepareConfig(Config config) {

SimWrapperConfigGroup sw = ConfigUtils.addOrGetModule(config, SimWrapperConfigGroup.class);

if (sample.isSet()) {
double sampleSize = sample.getSample();

config.qsim().setFlowCapFactor(sampleSize);
config.qsim().setStorageCapFactor(sampleSize);

// Counts can be scaled with sample size
config.counts().setCountsScaleFactor(sampleSize);
sw.sampleSize = sampleSize;

config.controller().setRunId(sample.adjustName(config.controller().getRunId()));
config.controller().setOutputDirectory(sample.adjustName(config.controller().getOutputDirectory()));
config.plans().setInputFile(sample.adjustName(config.plans().getInputFile()));
}

Activities.addScoringParams(config, true);

// Required for all calibration strategies
for (String subpopulation : List.of("person", "freight", "goodsTraffic", "commercialPersonTraffic", "commercialPersonTraffic_service")) {
config.replanning().addStrategySettings(
new ReplanningConfigGroup.StrategySettings()
.setStrategyName(DefaultPlanStrategiesModule.DefaultSelector.ChangeExpBeta)
.setWeight(1.0)
.setSubpopulation(subpopulation)
);

config.replanning().addStrategySettings(
new ReplanningConfigGroup.StrategySettings()
.setStrategyName(DefaultPlanStrategiesModule.DefaultStrategy.ReRoute)
.setWeight(0.15)
.setSubpopulation(subpopulation)
);
}

config.replanning().addStrategySettings(
new ReplanningConfigGroup.StrategySettings()
.setStrategyName(DefaultPlanStrategiesModule.DefaultStrategy.TimeAllocationMutator)
.setWeight(0.15)
.setSubpopulation("person")
);

config.replanning().addStrategySettings(
new ReplanningConfigGroup.StrategySettings()
.setStrategyName(DefaultPlanStrategiesModule.DefaultStrategy.SubtourModeChoice)
.setWeight(0.15)
.setSubpopulation("person")
);

return config;
}

@Override
protected void prepareScenario(Scenario scenario) {

AssignIncome income = new AssignIncome();

// Calculate the income for each person, in next versions this might also be done during creation of the population
scenario.getPopulation().getPersons().values().forEach(income::run);

}

@Override
protected void prepareControler(Controler controler) {

controler.addOverridingModule(new SimWrapperModule());

controler.addOverridingModule(new TravelTimeBinding());

controler.addOverridingModule(new VspScoringModule());

}

/**
* Add travel time bindings for ride and freight modes, which are not actually network modes.
*/
public static final class TravelTimeBinding extends AbstractModule {
@Override
public void install() {
addTravelTimeBinding(TransportMode.ride).to(networkTravelTime());
addTravelDisutilityFactoryBinding(TransportMode.ride).to(carTravelDisutilityFactoryKey());

addTravelTimeBinding("freight").to(Key.get(TravelTime.class, Names.named(TransportMode.truck)));
addTravelDisutilityFactoryBinding("freight").to(Key.get(TravelDisutilityFactory.class, Names.named(TransportMode.truck)));

bind(ScoringParametersForPerson.class).to(IncomeDependentUtilityOfMoneyPersonScoringParameters.class).asEagerSingleton();
}
}

}
Loading

0 comments on commit ff1bb1f

Please sign in to comment.