Skip to content

Commit

Permalink
Merge pull request #14 from matsim-scenarios/additional_policy_cases
Browse files Browse the repository at this point in the history
Additional policy cases
  • Loading branch information
simei94 authored Sep 26, 2024
2 parents 3562d86 + 1ebe6eb commit 649e004
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
output/
scenarios/output
test/output
runs/

*.gz
*.pbf
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.matsim.dashboards;

import org.matsim.core.config.Config;
import org.matsim.core.population.PersonUtils;
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.DashboardProvider;
import org.matsim.simwrapper.SimWrapper;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/matsim/run/RunLausitzDrtScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.matsim.core.controler.Controler;
import org.matsim.core.network.algorithms.MultimodalNetworkCleaner;
import org.matsim.core.utils.geometry.geotools.MGC;
import org.matsim.run.scenarios.LausitzScenario;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleCapacity;
import org.matsim.vehicles.VehicleType;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/matsim/run/RunLausitzPtScenario.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.matsim.run;

import org.matsim.application.MATSimApplication;
import org.matsim.run.scenarios.LausitzPtScenario;

/**
* Run the Lausitz PT scenario policy case.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/matsim/run/RunLausitzScenario.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.matsim.run;

import org.matsim.application.MATSimApplication;
import org.matsim.run.scenarios.LausitzScenario;

/**
* Run the Lausitz scenario with default configuration.
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/matsim/run/RunLausitzSingleModeScenario.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.matsim.run;

import org.matsim.application.MATSimApplication;
import org.matsim.run.scenarios.LausitzSingleModeScenario;

/**
* Run the Lausitz single mode scenario policy case.
*/
public final class RunLausitzSingleModeScenario {

private RunLausitzSingleModeScenario() {
}

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

}
18 changes: 18 additions & 0 deletions src/main/java/org/matsim/run/RunLausitzSpeedReductionScenario.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.matsim.run;

import org.matsim.application.MATSimApplication;
import org.matsim.run.scenarios.LausitzSpeedReductionScenario;

/**
* Run the Lausitz speed reduction scenario policy case.
*/
public final class RunLausitzSpeedReductionScenario {

private RunLausitzSpeedReductionScenario() {
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Map;
import java.util.Set;

import static org.matsim.run.LausitzScenario.*;
import static org.matsim.run.scenarios.LausitzScenario.*;

@CommandLine.Command(
name = "adapt-freight-plans",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/matsim/run/prepare/PrepareNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import java.util.Set;

import static org.matsim.run.LausitzScenario.*;
import static org.matsim.run.scenarios.LausitzScenario.*;

@CommandLine.Command(
name = "network",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.matsim.run;
package org.matsim.run.scenarios;

import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
Expand Down Expand Up @@ -46,15 +46,15 @@ public static void main(String[] args) {

@Nullable
@Override
protected Config prepareConfig(Config config) {
public Config prepareConfig(Config config) {
// apply all config changes from base scenario class
baseScenario.prepareConfig(config);

return config;
}

@Override
protected void prepareScenario(Scenario scenario) {
public void prepareScenario(Scenario scenario) {
// apply all scenario changes from base scenario class
baseScenario.prepareScenario(scenario);

Expand Down Expand Up @@ -147,7 +147,7 @@ protected void prepareScenario(Scenario scenario) {
}

@Override
protected void prepareControler(Controler controler) {
public void prepareControler(Controler controler) {
// apply all controller changes from base scenario class
baseScenario.prepareControler(controler);
// TODO: add potential new Listeners here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.matsim.run;
package org.matsim.run.scenarios;

import com.google.common.collect.Sets;
import org.matsim.analysis.personMoney.PersonMoneyEventsAnalysisModule;
Expand Down Expand Up @@ -108,7 +108,7 @@ public static void main(String[] args) {

@Nullable
@Override
protected Config prepareConfig(Config config) {
public Config prepareConfig(Config config) {

// Add all activity types with time bins
SnzActivities.addScoringParams(config);
Expand Down Expand Up @@ -184,7 +184,7 @@ protected Config prepareConfig(Config config) {
}

@Override
protected void prepareScenario(Scenario scenario) {
public void prepareScenario(Scenario scenario) {
// add freight and truck as allowed modes together with car
PrepareNetwork.prepareFreightNetwork(scenario.getNetwork());

Expand All @@ -197,7 +197,7 @@ protected void prepareScenario(Scenario scenario) {
}

@Override
protected void prepareControler(Controler controler) {
public void prepareControler(Controler controler) {

//analyse PersonMoneyEvents
controler.addOverridingModule(new PersonMoneyEventsAnalysisModule());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.matsim.run.scenarios;

import com.google.common.collect.Lists;
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.api.core.v01.population.*;
import org.matsim.core.config.Config;
import org.matsim.core.config.groups.ReplanningConfigGroup;
import org.matsim.core.controler.Controler;
import org.matsim.core.population.algorithms.TripsToLegsAlgorithm;
import org.matsim.core.router.RoutingModeMainModeIdentifier;
import picocli.CommandLine;

import javax.annotation.Nullable;
import java.util.Collection;

/**
* Lausitz scenario including the simulation of 1 single transport mode.
* All legs are changed to --transport-mode and mode choice is switched off.
* All necessary configs will be made in this class.
*/
public class LausitzSingleModeScenario extends LausitzScenario {
Logger log = LogManager.getLogger(LausitzSingleModeScenario.class);

private final LausitzScenario baseScenario = new LausitzScenario(sample, emissions);

@CommandLine.Option(names = "--transport-mode", description = "Transport mode to which all legs should be changed.", defaultValue = TransportMode.car)
private String mode;

public LausitzSingleModeScenario(@Nullable Config config) {
super(config);
}

public LausitzSingleModeScenario(@Nullable String args) {
super(args);
}

public LausitzSingleModeScenario() {
super(String.format("input/v%s/lausitz-v%s-10pct.config.xml", LausitzScenario.VERSION, LausitzScenario.VERSION));
}

@Nullable
@Override
public Config prepareConfig(Config config) {
// apply all config changes from base scenario class
baseScenario.prepareConfig(config);

config.subtourModeChoice().setModes(new String[0]);

// remove smc strategy, only one mode for this scenario
Collection<ReplanningConfigGroup.StrategySettings> settings = config.replanning().getStrategySettings();
config.replanning().clearStrategySettings();

settings.stream()
.filter(setting -> !setting.getStrategyName().equals("SubtourModeChoice"))
.forEach(setting -> config.replanning().addStrategySettings(setting));

return config;
}

@Override
public void prepareScenario(Scenario scenario) {
// apply all scenario changes from base scenario class
baseScenario.prepareScenario(scenario);

// TODO: replace code segment that selects, cleans routes and converts trips 2 legs with call of CleanPopulation PR3496
TripsToLegsAlgorithm trips2Legs = new TripsToLegsAlgorithm(new RoutingModeMainModeIdentifier());

for (Person person : scenario.getPopulation().getPersons().values()) {
Plan selected = person.getSelectedPlan();
for (Plan plan : Lists.newArrayList(person.getPlans())) {
if (plan != selected)
person.removePlan(plan);
}

for (Plan plan : person.getPlans()) {
trips2Legs.run(plan);

for (PlanElement el : plan.getPlanElements()) {
if (el instanceof Leg leg) {
leg.setRoute(null);
leg.setMode(mode);
}
}
}
}
log.info("For all agents: Unselected plans have been removed. Trips were converted to legs. Routes have been removed." +
" For every leg, the mode was changed to {}", mode);
}

@Override
public void prepareControler(Controler controler) {
// apply all controller changes from base scenario class
baseScenario.prepareControler(controler);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.matsim.run.scenarios;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.locationtech.jts.geom.prep.PreparedGeometry;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
import org.matsim.core.config.Config;
import org.matsim.core.controler.Controler;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.utils.gis.shp2matsim.ShpGeometryUtils;
import picocli.CommandLine;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Lausitz scenario including speed reductions on network links of all types except motorways.
* Speed reduction can be done relatively or by absolute value.
* All necessary configs will be made in this class.
*/
public class LausitzSpeedReductionScenario extends LausitzScenario {
Logger log = LogManager.getLogger(LausitzSpeedReductionScenario.class);

private final LausitzScenario baseScenario = new LausitzScenario(sample, emissions);

@CommandLine.Option(names = "--slow-speed-shp", description = "Path to shp file for adaption of link speeds.", defaultValue = "../shp/lausitz.shp")
private String slowSpeedAreaShp;
@CommandLine.Option(names = "--slow-speed-relative-change", description = "provide a value that is bigger than 0.0 and smaller than 1.0." +
"The default is set to 0.6, such that roads with an allowed speed of 50kmh are reduced to 30kmh.", defaultValue = "0.6")
private double relativeSpeedChange;

public LausitzSpeedReductionScenario(@Nullable Config config) {
super(config);
}

public LausitzSpeedReductionScenario(@Nullable String args) {
super(args);
}

public LausitzSpeedReductionScenario() {
super(String.format("input/v%s/lausitz-v%s-10pct.config.xml", LausitzScenario.VERSION, LausitzScenario.VERSION));
}

@Nullable
@Override
public Config prepareConfig(Config config) {
// apply all config changes from base scenario class
baseScenario.prepareConfig(config);
return config;
}

@Override
public void prepareScenario(Scenario scenario) {
// apply all scenario changes from base scenario class
baseScenario.prepareScenario(scenario);

List<PreparedGeometry> geometries = ShpGeometryUtils.loadPreparedGeometries(IOUtils.extendUrl(scenario.getConfig().getContext(), slowSpeedAreaShp));

Set<? extends Link> carLinksInArea = scenario.getNetwork().getLinks().values().stream()
//filter car links
.filter(link -> link.getAllowedModes().contains(TransportMode.car))
//spatial filter
.filter(link -> ShpGeometryUtils.isCoordInPreparedGeometries(link.getCoord(), geometries))
//we won't change motorways and motorway_links
.filter(link -> !((String) link.getAttributes().getAttribute("type")).contains("motorway"))
.collect(Collectors.toSet());

if (relativeSpeedChange >= 0.0 && relativeSpeedChange < 1.0) {
log.info("reduce speed relatively by a factor of: {}", relativeSpeedChange);

//apply speed reduction to all roads but motorways
carLinksInArea.forEach(link -> link.setFreespeed(link.getFreespeed() * relativeSpeedChange));
} else {
log.fatal("Speed reduction value of {} is invalid. Please put a 0.0 <= value < 1.0", relativeSpeedChange);
throw new IllegalArgumentException();
}
}

@Override
public void prepareControler(Controler controler) {
// apply all controller changes from base scenario class
baseScenario.prepareControler(controler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.dashboards.LausitzSimWrapperRunner;
import org.matsim.run.scenarios.LausitzScenario;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import org.matsim.testcases.MatsimTestUtils;

Expand Down
Loading

0 comments on commit 649e004

Please sign in to comment.