Skip to content

Commit

Permalink
changes due to small scale comemrcial traffic demand generation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed Aug 28, 2024
1 parent 6c1ae3e commit 5170064
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 25 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ input/plans-longHaulFreight.xml.gz: input/$V/$N-$V-network.xml.gz
--target-crs $(CRS)\
--shp input/shp/lausitz.shp --shp-crs $(CRS)\
--cut-on-boundary\
--LegMode "longDistanceFreight"\
--output $@
# create facilities for commercial traffic
input/commercialFacilities.xml.gz:
Expand Down
16 changes: 8 additions & 8 deletions input/v1.1/lausitz-v1.1-10pct.config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

<module name="network">
<param name="inputNetworkFile"
value="https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v1.0/lausitz-v1.0-network-with-pt.xml.gz"/>
value="https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v1.1/lausitz-v1.1-network-with-pt.xml.gz"/>
<param name="laneDefinitionsFile" value="null"/>
</module>

<module name="plans">
<param name="inputPlansFile"
value="https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v1.0/lausitz-v1.0-10pct.plans.xml.gz"/>
value="https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v1.1/lausitz-v1.1-10pct.plans-initial.xml.gz"/>
<param name="removingUnnecessaryPlanAttributes" value="true"/>
</module>

Expand All @@ -53,7 +53,7 @@
<module name="routing">
<!-- Some typical teleportation routing params are set by default, such as for walk and bike. Setting this switch to "true" will clear them. Note that this will also clear settings for helper modes such as for non_network_walk -->
<param name="clearDefaultTeleportedModeParams" value="true"/>
<param name="networkModes" value="car,ride,bike,freight"/>
<param name="networkModes" value="car,ride,bike,longDistanceFreight"/>
<parameterset type="teleportedModeParameters">
<param name="beelineDistanceFactor" value="1.3"/>
<param name="mode" value="walk"/>
Expand All @@ -65,7 +65,7 @@
<param name="endTime" value="36:00:00"/>
<param name="flowCapacityFactor" value="0.1"/>
<param name="storageCapacityFactor" value="0.1"/>
<param name="mainMode" value="car,bike,freight"/>
<param name="mainMode" value="car,bike,longDistanceFreight"/>
<param name="numberOfThreads" value="12"/>
<param name="startTime" value="00:00:00"/>
<param name="stuckTime" value="30.0"/>
Expand Down Expand Up @@ -132,12 +132,12 @@

<parameterset type="strategysettings">
<param name="strategyName" value="ChangeExpBeta"/>
<param name="subpopulation" value="freight"/>
<param name="subpopulation" value="longDistanceFreight"/>
<param name="weight" value="0.95"/>
</parameterset>
<parameterset type="strategysettings">
<param name="strategyName" value="ReRoute"/>
<param name="subpopulation" value="freight"/>
<param name="subpopulation" value="longDistanceFreight"/>
<param name="weight" value="0.05"/>
</parameterset>
</module>
Expand All @@ -164,7 +164,7 @@
<!-- This parameter decides which modes are analyzed, when separateModes = false && filterModes = true. The 2 latter parameters are only here for backwards compability.
standard is separateModes = true (see below), so analyzedModes is ignored.
In case of separateModes = false && filterModes = true: travel time measurement for all vehicle types together (seperatemodes = false), BUT filtered (filterModes = true) for the modes in analyzedModes-->
<param name="analyzedModes" value="car,freight"/>
<param name="analyzedModes" value="car,longDistanceFreight"/>

<param name="separateModes" value="true"/>
<param name="calculateLinkToLinkTravelTimes" value="false"/>
Expand Down Expand Up @@ -212,7 +212,7 @@

<parameterset type="modeParams">
<param name="marginalUtilityOfTraveling_util_hr" value="-10.0"/>
<param name="mode" value="freight"/>
<param name="mode" value="longDistanceFreight"/>
<param name="monetaryDistanceRate" value="-0.002"/>
<!-- -->
</parameterset>
Expand Down
37 changes: 28 additions & 9 deletions src/main/java/org/matsim/run/LausitzScenario.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.matsim.run;

import com.google.common.collect.Sets;
import com.google.inject.Key;
import com.google.inject.name.Names;
import org.matsim.analysis.personMoney.PersonMoneyEventsAnalysisModule;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.population.Person;
import org.matsim.application.MATSimApplication;
import org.matsim.application.analysis.CheckPopulation;
import org.matsim.application.analysis.traffic.LinkStats;
Expand All @@ -31,9 +31,8 @@
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.population.PopulationUtils;
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.run.analysis.CommuterAnalysis;
import org.matsim.run.prepare.PrepareNetwork;
Expand All @@ -51,6 +50,7 @@
import java.util.List;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;

@CommandLine.Command(header = ":: Open Lausitz Scenario ::", version = LausitzScenario.VERSION, mixinStandardHelpOptions = true)
Expand All @@ -66,8 +66,11 @@
public class LausitzScenario extends MATSimApplication {

public static final String VERSION = "1.1";
private static final String FREIGHT = "freight";
public static final String FREIGHT = "longDistanceFreight";
private static final String AVERAGE = "average";
public static final String HEAVY_MODE = "truck40t";
public static final String MEDIUM_MODE = "truck18t";
public static final String LIGHT_MODE = "truck8t";

// To decrypt hbefa input files set MATSIM_DECRYPTION_PASSWORD as environment variable. ask VSP for access.
private static final String HBEFA_2020_PATH = "https://svn.vsp.tu-berlin.de/repos/public-svn/3507bb3997e5657ab9da76dbedbb13c9b5991d3e/0e73947443d68f95202b71a156b337f7f71604ae/";
Expand Down Expand Up @@ -182,6 +185,25 @@ protected Config prepareConfig(Config config) {
@Override
protected void prepareScenario(Scenario scenario) {


for (Person person : scenario.getPopulation().getPersons().values()) {

if (PopulationUtils.getSubpopulation(person).contains("commercialPersonTraffic") ||
PopulationUtils.getSubpopulation(person).contains("goodsTraffic")) {

Map<String, Id<VehicleType>> types = VehicleUtils.getVehicleTypes(person);

for (Map.Entry<String, Id<VehicleType>> entry : types.entrySet()) {
if (Set.of(HEAVY_MODE, MEDIUM_MODE, LIGHT_MODE).contains(entry.getKey())) {
types.put(entry.getKey(), Id.create(entry.getKey(), VehicleType.class));
}
}
}

}



// add freight and truck as allowed modes together with car
PrepareNetwork.prepareFreightNetwork(scenario.getNetwork());

Expand Down Expand Up @@ -210,8 +232,6 @@ 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)));
// we do not need to add SwissRailRaptor explicitely! this is done in core
}

Expand All @@ -223,7 +243,7 @@ public void install() {
*/
public static void prepareCommercialTrafficConfig(Config config) {

Set<String> modes = Set.of(FREIGHT, TransportMode.truck);
Set<String> modes = Set.of(HEAVY_MODE, MEDIUM_MODE, LIGHT_MODE);

modes.forEach(mode -> {
ScoringConfigGroup.ModeParams thisModeParams = new ScoringConfigGroup.ModeParams(mode);
Expand All @@ -244,7 +264,6 @@ public static void prepareCommercialTrafficConfig(Config config) {
config.scoring().addActivityParams(new ScoringConfigGroup.ActivityParams("freight_start").setTypicalDuration(30 * 60.));
config.scoring().addActivityParams(new ScoringConfigGroup.ActivityParams("freight_end").setTypicalDuration(30 * 60.));

//TODO: add freight and remove from config or change freight subpopulation and mode for long distance freight
for (String subpopulation : List.of("commercialPersonTraffic", "commercialPersonTraffic_service", "goodsTraffic")) {
config.replanning().addStrategySettings(
new ReplanningConfigGroup.StrategySettings()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.matsim.run.prepare;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.api.core.v01.population.Population;
import org.matsim.application.MATSimAppCommand;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.vehicles.VehicleType;
import org.matsim.vehicles.VehicleUtils;
import picocli.CommandLine;

import java.nio.file.Path;
import java.util.Map;
import java.util.Set;

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

@CommandLine.Command(
name = "adapt-freight-plans",
description = "Adapt all freight plans (including small scall commercial traffic) to new standards."
)
public class AdaptFreightTrafficToDetailedModes implements MATSimAppCommand {

Logger log = LogManager.getLogger(AdaptFreightTrafficToDetailedModes.class);

@CommandLine.Parameters(arity = "1", paramLabel = "INPUT", description = "Path to input population")
private Path input;

@CommandLine.Option(names = "--output", description = "Path to output population", required = true)
private Path output;

public static void main(String[] args) {
new AdaptFreightTrafficToDetailedModes().execute(args);
}

@Override
public Integer call() throws Exception {

Population population = PopulationUtils.readPopulation(input.toString());

for (Person person : population.getPersons().values()) {
if (PopulationUtils.getSubpopulation(person).equals("freight")) {
// rename freight subpop to longDistanceFreight
person.getAttributes().removeAttribute("subpopulation");
person.getAttributes().putAttribute("subpopulation", FREIGHT);

// rename each leg mode freight to longDistanceFreight
for (Plan plan : person.getPlans()) {
for (Leg leg : TripStructureUtils.getLegs(plan)) {
if (leg.getMode().equals("freight")) {
leg.setMode(FREIGHT);
}
}
}
} else if (PopulationUtils.getSubpopulation(person).equals(FREIGHT)) {
for (Plan plan : person.getPlans()) {
for (Leg leg : TripStructureUtils.getLegs(plan)) {
if (!leg.getMode().equals(FREIGHT)) {
leg.setMode(FREIGHT);
}
}
}

}

if (PopulationUtils.getSubpopulation(person).contains("commercialPersonTraffic") ||
PopulationUtils.getSubpopulation(person).contains("goodsTraffic")) {

Map<String, Id<VehicleType>> types = VehicleUtils.getVehicleTypes(person);

for (Plan plan : person.getPlans()) {
for (Leg leg : TripStructureUtils.getLegs(plan)) {
if (leg.getMode().equals(TransportMode.truck)) {
String vehicleTypes = person.getAttributes().getAttribute("vehicleTypes").toString();
if (vehicleTypes.contains("light8t")) {
leg.setMode(LIGHT_MODE);
} else if (vehicleTypes.contains("medium18t")) {
leg.setMode(MEDIUM_MODE);
} else if (vehicleTypes.contains("heavy40t")) {
leg.setMode(HEAVY_MODE);
} else {
log.error("Unknown vehicle type in: {}", vehicleTypes);
return 2;
}
} else if (leg.getMode().equals(TransportMode.car)) {
// TODO
}
}
}

for (Map.Entry<String, Id<VehicleType>> entry : types.entrySet()) {
if (Set.of(HEAVY_MODE, MEDIUM_MODE, LIGHT_MODE).contains(entry.getKey())) {
types.put(entry.getKey(), Id.create(entry.getKey(), VehicleType.class));
}
}
}
}




PopulationUtils.writePopulation(population, output.toString());

return 0;
}

private static @NotNull Population removeSmallScaleCommercialTrafficFromPopulation(Population population) {
Population newPop = PopulationUtils.createPopulation(ConfigUtils.createConfig());

for (Person person : population.getPersons().values()) {
if (PopulationUtils.getSubpopulation(person).contains("commercialPersonTraffic")
|| PopulationUtils.getSubpopulation(person).contains("goodsTraffic")) {
// do not add commercial or goods traffic from RE
continue;
}
newPop.addPerson(person);
}
return newPop;
}
}
12 changes: 7 additions & 5 deletions src/main/java/org/matsim/run/prepare/PrepareNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import java.util.Set;

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

@CommandLine.Command(
name = "network",
description = "Prepare network / link attributes."
Expand All @@ -23,8 +25,6 @@ public class PrepareNetwork implements MATSimAppCommand {

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

private static final String FREIGHT = "freight";

@CommandLine.Option(names = "--network", description = "Path to network file", required = true)
private String networkFile;

Expand Down Expand Up @@ -61,17 +61,19 @@ public static void prepareFreightNetwork(Network network) {
if (modes.contains(TransportMode.car)) {
Set<String> newModes = Sets.newHashSet(modes);

newModes.add(HEAVY_MODE);
newModes.add(MEDIUM_MODE);
newModes.add(LIGHT_MODE);
newModes.add(FREIGHT);
newModes.add(TransportMode.truck);

link.setAllowedModes(newModes);
linkCount++;
}
}

log.info("For {} links {} and {} has been added as an allowed mode.", linkCount, FREIGHT, TransportMode.truck);
log.info("For {} links {}, {}, {} and {} has been added as an allowed mode.", linkCount, HEAVY_MODE, MEDIUM_MODE, LIGHT_MODE, FREIGHT);

new MultimodalNetworkCleaner(network).run(Set.of(FREIGHT, TransportMode.truck));
new MultimodalNetworkCleaner(network).run(Set.of(HEAVY_MODE, MEDIUM_MODE, LIGHT_MODE, FREIGHT));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/matsim/run/prepare/PreparePopulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ private void prepare(Person person) {
}

// Assign income to person (skip the freight agents)
if (person.getId().toString().startsWith("freight")) {
if (person.getId().toString().startsWith("freight") || person.getId().toString().startsWith("commercial")
|| person.getId().toString().startsWith("goods")) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/matsim/run/RunIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void runScenario() {
assert MATSimApplication.execute(LausitzScenario.class, config,
"--1pct",
"--iterations", "1",
"--config:plans.inputPlansFile", "https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v1.0/lausitz-v1.0-1pct.plans-initial.xml.gz",
"--config:plans.inputPlansFile", "https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v1.1/lausitz-v1.1-1pct.plans-initial.xml.gz",
"--output", utils.getOutputDirectory(),
"--config:controller.overwriteFiles=deleteDirectoryIfExists") == 0 : "Must return non error code";
}
Expand All @@ -62,7 +62,7 @@ void runScenarioIncludingDrt() {
assert MATSimApplication.execute(RunLausitzDrtScenario.class, config,
"--1pct",
"--iterations", "1",
"--config:plans.inputPlansFile", "https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v1.0/lausitz-v1.0-1pct.plans-initial.xml.gz",
"--config:plans.inputPlansFile", "https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v1.1/lausitz-v1.1-1pct.plans-initial.xml.gz",
"--output", utils.getOutputDirectory(),
"--config:controller.overwriteFiles=deleteDirectoryIfExists") == 0 : "Must return non error code";

Expand Down

0 comments on commit 5170064

Please sign in to comment.