Skip to content

Commit

Permalink
Merge pull request #43 from matsim-scenarios/drtSpeedUp
Browse files Browse the repository at this point in the history
Use DrtSpeedUp
  • Loading branch information
tschlenther authored Feb 23, 2024
2 parents e1632a1 + 0a1ad14 commit a473a93
Show file tree
Hide file tree
Showing 14 changed files with 1,159 additions and 244 deletions.
Binary file modified input/v1.3/drtServiceArea/leipzig_flexa_service_area_2021.dbf
Binary file not shown.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions src/main/java/org/matsim/run/RunLeipzigScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import playground.vsp.simpleParkingCostHandler.ParkingCostConfigGroup;

import javax.annotation.Nullable;
import java.net.URISyntaxException;
import java.util.*;

/**
Expand Down Expand Up @@ -93,9 +92,6 @@ public class RunLeipzigScenario extends MATSimApplication {
@CommandLine.Option(names = "--parking-cost-time-period-end", defaultValue = "0", description = "End of time period for which parking cost will be charged.")
private Double parkingCostTimePeriodEnd;

@CommandLine.Option(names = "--drt-case", defaultValue = "oneServiceArea", description = "Defines how drt is modelled. For a more detailed description see class DrtCaseSetup.")
private DrtCaseSetup.DrtCase drtCase;

@CommandLine.Option(names = "--intermodality", defaultValue = "drtAndPtSeparateFromEachOther", description = "Define if drt should be used as access and egress mode for pt.")
private DrtCaseSetup.PtDrtIntermodality ptDrtIntermodality;

Expand Down Expand Up @@ -190,11 +186,7 @@ protected Config prepareConfig(Config config) {

if (networkOpt.hasDrtArea()) {
//drt
try {
DrtCaseSetup.prepareConfig(config, drtCase, new ShpOptions(networkOpt.getDrtArea(), null, null));
} catch (URISyntaxException e) {
log.fatal(e);
}
DrtCaseSetup.prepareConfig(config, /* drtCase, */ new ShpOptions(networkOpt.getDrtArea(), null, null));
}

config.qsim().setUsingTravelTimeCheckInTeleportation(true);
Expand Down Expand Up @@ -280,7 +272,7 @@ protected void prepareScenario(Scenario scenario) {
// (passt das Netz an aus den mitgegebenen shape files, z.B. parking area, car-free area, ...)

if (networkOpt.hasDrtArea()) {
DrtCaseSetup.prepareScenario(scenario, drtCase, new ShpOptions(networkOpt.getDrtArea(), null, null), VERSION);
DrtCaseSetup.prepareScenario(scenario, new ShpOptions(networkOpt.getDrtArea(), null, null), VERSION);
}

}
Expand Down Expand Up @@ -328,7 +320,7 @@ public void install() {
});

if (networkOpt.hasDrtArea()) {
DrtCaseSetup.prepareControler(controler, drtCase, new ShpOptions(networkOpt.getDrtArea(), null, null), ptDrtIntermodality);
DrtCaseSetup.prepareControler(controler, new ShpOptions(networkOpt.getDrtArea(), null, null), ptDrtIntermodality);
}

if (bike == BicycleHandling.onNetworkWithBicycleContrib) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void processNetworkForStopCreation(Network network, boolean modeFilteredN

for (Node node : network.getNodes().values()) {
//we dont want pt nodes included as pt has a separate network + no dead ends
//ts feb' 24: why more than 2 links and not >=2 links?? also, the spatial lookup is made before in the network preprocessing...
if (MGC.coord2Point(node.getCoord()).within(drtServiceArea) && (node.getInLinks().size() + node.getOutLinks().size() > 2)
&& !node.getId().toString().contains("pt_")) {

Expand Down
260 changes: 105 additions & 155 deletions src/main/java/org/matsim/run/prepare/DrtCaseSetup.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public final class LeipzigDrtVehicleCreator implements MATSimAppCommand {
@CommandLine.Option(names = "--no-vehicles", description = "no of vehicles per service area to create", required = true)
private int noVehiclesPerArea;

@CommandLine.Option(names = "--service-start-time", description = "start of vehicle service time in seconds", defaultValue = "18000")
private double serviceStartTime;
@CommandLine.Option(names = "--service-start-time", description = "start of vehicle service time in seconds. default is 18000.")
private double serviceStartTime = 18000;

@CommandLine.Option(names = "--service-end-time", description = "end of vehicle service time in seconds", defaultValue = "86400")
private double serviceEndTime;
@CommandLine.Option(names = "--service-end-time", description = "end of vehicle service time in seconds. default is 864000.")
private double serviceEndTime = 86400;

public static void main(String[] args) throws IOException {
new LeipzigDrtVehicleCreator().execute(args);
Expand Down Expand Up @@ -151,11 +151,7 @@ public void createDrtVehiclesForSingleArea(Vehicles vehicles, Network network, S
}
}

Network filteredNetwork = NetworkUtils.createNetwork();
TransportModeNetworkFilter filter = new TransportModeNetworkFilter(network);
filter.filter(filteredNetwork, Sets.newHashSet(drtMode));

createVehiclesByRandomPointInShape(feature, filteredNetwork, noVehiclesPerArea, serviceStartTime,
createVehiclesByRandomPointInShape(feature, network, noVehiclesPerArea, serviceStartTime,
serviceEndTime, 1, drtType, drtMode, vehicles);
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/matsim/run/prepare/NetworkOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class NetworkOptions {

@CommandLine.Option(names = "--drt-area", description = "Path to SHP file specifying where DRT mode is allowed")
private Path drtArea;
@CommandLine.Option(names = "--drt-modes", description = "List of modes to add. Use comma as delimiter", defaultValue = TransportMode.drt)
private String drtModes;
@CommandLine.Option(names = "--car-free-area", description = "Path to SHP file specifying car-free area")
private Path carFreeArea;
@CommandLine.Option(names = "--car-free-modes", description = "List of modes to remove. Use comma as delimiter", defaultValue = TransportMode.car)
Expand Down Expand Up @@ -65,7 +63,7 @@ public void prepare(Network network) {
if (!Files.exists(drtArea)) {
throw new IllegalArgumentException("Path to drt area not found: " + drtArea);
} else {
PrepareNetwork.prepareDRT(network, new ShpOptions(drtArea, null, null), drtModes);
PrepareNetwork.prepareDRT(network, new ShpOptions(drtArea, null, null));
}
}

Expand Down
59 changes: 20 additions & 39 deletions src/main/java/org/matsim/run/prepare/PrepareNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,61 +57,42 @@ public Integer call() throws Exception {
* Adapt network to one or more drt service areas. Therefore, a shape file of the wished service area + a list
* of drt modes are needed.
*/
static void prepareDRT(Network network, ShpOptions shp, String modes) {

Set<String> modesToAdd = new HashSet<>(Arrays.asList(modes.split(",")));
Geometry drtOperationArea = null;
Geometry avOperationArea = null;
static void prepareDRT(Network network, ShpOptions shp) {

List<SimpleFeature> features = shp.readFeatures();
Map<String, Geometry> modeGeoms = new HashMap<>();
for (SimpleFeature feature : features) {
if (feature.getAttribute("mode").equals("drt")) {
if (drtOperationArea == null) {
drtOperationArea = (Geometry) feature.getDefaultGeometry();
} else {
drtOperationArea = drtOperationArea.union((Geometry) feature.getDefaultGeometry());
}
} else {
drtOperationArea = avOperationArea.getFactory().createPoint();
}

if (feature.getAttribute("mode").equals("av")) {
if (avOperationArea == null) {
avOperationArea = (Geometry) feature.getDefaultGeometry();
} else {
avOperationArea = avOperationArea.union((Geometry) feature.getDefaultGeometry());
}
} else {
avOperationArea = drtOperationArea.getFactory().createPoint();
String mode = String.valueOf( feature.getAttribute("mode") );
if (mode.equals("null")) {
throw new IllegalArgumentException("could not find 'mode' attribute in the given shape file at " + shp.getShapeFile().toString());
}
modeGeoms.compute(mode, (m, geom) -> geom == null ? ((Geometry) feature.getDefaultGeometry()) : geom.union((Geometry) feature.getDefaultGeometry()));

}

for (Link link : network.getLinks().values()) {
if (!link.getAllowedModes().contains("car")) {
continue;
}

boolean isDrtAllowed = MGC.coord2Point(link.getFromNode().getCoord()).within(drtOperationArea) &&
MGC.coord2Point(link.getToNode().getCoord()).within(drtOperationArea);
boolean isAvAllowed = MGC.coord2Point(link.getFromNode().getCoord()).within(avOperationArea) &&
MGC.coord2Point(link.getToNode().getCoord()).within(avOperationArea);

if (isDrtAllowed) {
Set<String> allowedModes = new HashSet<>(link.getAllowedModes());
allowedModes.addAll(modesToAdd);
link.setAllowedModes(allowedModes);
}

if (isAvAllowed) {
Set<String> allowedModes = new HashSet<>(link.getAllowedModes());
allowedModes.addAll(modesToAdd);
link.setAllowedModes(allowedModes);
for (Map.Entry<String, Geometry> modeGeometryEntry : modeGeoms.entrySet()) {
if (MGC.coord2Point(link.getFromNode().getCoord()).within(modeGeometryEntry.getValue()) &&
MGC.coord2Point(link.getToNode().getCoord()).within(modeGeometryEntry.getValue())){
Set<String> allowedModes = new HashSet<>(link.getAllowedModes());
allowedModes.add(modeGeometryEntry.getKey());
link.setAllowedModes(allowedModes);
}
}
}

//we have to call the MultiModalNetworkCleaner for each mode individually, because otherwise the individual subnetworks might not get cleaned
MultimodalNetworkCleaner multimodalNetworkCleaner = new MultimodalNetworkCleaner(network);
multimodalNetworkCleaner.run(modesToAdd);
for (String mode : modeGeoms.keySet()) {
multimodalNetworkCleaner.run(Set.of(mode));
}

log.log(Level.INFO, "The following modes have been added to the network: {}", modes);
log.log(Level.INFO, "The following modes have been added to the network: {}", modeGeoms.keySet());
}

/**
Expand Down
78 changes: 51 additions & 27 deletions src/test/java/org/matsim/run/RunLeipzigIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.matsim.run;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.matsim.analysis.ParkingLocation;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Network;
import org.matsim.application.MATSimApplication;
import org.matsim.contrib.drt.fare.DrtFareParams;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.drt.speedup.DrtSpeedUpParams;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.OutputDirectoryHierarchy;
Expand All @@ -21,18 +23,20 @@
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class RunLeipzigIntegrationTest {


private static final String URL = String.format("https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/leipzig/leipzig-v%s/input/",
RunLeipzigScenario.VERSION);
private static final String exampleShp = String.format("input/v%s/drtServiceArea/Leipzig_stadt.shp",RunLeipzigScenario.VERSION);
private static final String stadtShp = String.format("input/v%s/drtServiceArea/Leipzig_stadt.shp", RunLeipzigScenario.VERSION);
private static final String flexaShp = String.format("input/v%s/drtServiceArea/leipzig_flexa_service_area_2021.shp", RunLeipzigScenario.VERSION);

@Rule
public MatsimTestUtils utils = new MatsimTestUtils();


@Test
public final void runPoint1pctIntegrationTest() {

Expand All @@ -47,19 +51,21 @@ public final void runPoint1pctIntegrationTest() {

ConfigUtils.addOrGetModule(config, SimWrapperConfigGroup.class).defaultDashboards = SimWrapperConfigGroup.Mode.disabled;

MATSimApplication.execute(RunLeipzigScenario.class, config, "run", "--1pct", "--slow-speed-area", exampleShp,
"--slow-speed-relative-change", "0.5", "--drt-area", exampleShp, "--post-processing", "disabled"
MATSimApplication.execute(RunLeipzigScenario.class, config, "run", "--1pct", "--slow-speed-area", stadtShp,
"--slow-speed-relative-change", "0.5","--drt-area", flexaShp, "--post-processing", "disabled"
);

assertThat(EventsUtils.compareEventsFiles(
new File(utils.getOutputDirectory(), "leipzig-1pct.output_events.xml.gz").toString(),
new File(utils.getClassInputDirectory(), "runPoint1pctIntegrationTest_events.xml.zst").toString()
new File(utils.getClassInputDirectory(), "runPoint1pctIntegrationTest_events.xml.gz").toString()
)).isEqualTo(EventsFileComparator.Result.FILES_ARE_EQUAL);


Network network = NetworkUtils.readNetwork(utils.getOutputDirectory() + "/" + config.controler().getRunId() + ".output_network.xml.gz");
assertTrue(network.getLinks().get(Id.createLinkId("24232899")).getFreespeed() < 12.501000000000001);
assertTrue(network.getLinks().get(Id.createLinkId("24675139")).getFreespeed() < 7.497);

testDrt(config);
}

@Test
Expand All @@ -75,7 +81,7 @@ public final void runPoint1pctParkingIntegrationTest() {
ConfigUtils.addOrGetModule(config, SimWrapperConfigGroup.class).defaultDashboards = SimWrapperConfigGroup.Mode.disabled;
config.plans().setInputFile(URL + String.format("leipzig-v%s-0.1pct.plans-initial.xml.gz",RunLeipzigScenario.VERSION));

MATSimApplication.execute(RunLeipzigScenario.class, config, "run", "--1pct", "--drt-area", exampleShp, "--post-processing", "disabled",
MATSimApplication.execute(RunLeipzigScenario.class, config, "run", "--1pct", "--drt-area", flexaShp, "--post-processing", "disabled",
"--parking-cost-area", "input/v" + RunLeipzigScenario.VERSION + "/parkingCostArea/Bewohnerparken_2020.shp",
"--parking", "--intermodality", "drtAsAccessEgressForPt");

Expand All @@ -85,38 +91,56 @@ public final void runPoint1pctParkingIntegrationTest() {

assertThat(EventsUtils.compareEventsFiles(
new File(utils.getOutputDirectory(), "leipzig-1pct.output_events.xml.gz").toString(),
new File(utils.getClassInputDirectory(), "runPoint1pctParkingIntegrationTest_events.xml.zst").toString()
new File(utils.getClassInputDirectory(), "runPoint1pctParkingIntegrationTest_events.xml.gz").toString()
)).isEqualTo(EventsFileComparator.Result.FILES_ARE_EQUAL);


Network network = NetworkUtils.readNetwork(utils.getOutputDirectory() + "/" + config.controler().getRunId() + ".output_network.xml.gz");
assertTrue(network.getLinks().get(Id.createLinkId("24232899")).getFreespeed() < 12.501000000000001);
assertTrue(network.getLinks().get(Id.createLinkId("24675139")).getFreespeed() < 7.497);

testDrt(config);

new ParkingLocation().execute("--directory", output);
}

@Test
@Ignore("Opt Drt is not used right now.")
public final void runOptDrtExamplePopulationTest() {
Config config = ConfigUtils.loadConfig("input/v1.2/leipzig-test.with-drt.config.xml");
private static void testDrt(Config config) {
//TODO add more tests, drt trips, etc.

config.global().setNumberOfThreads(1);
config.qsim().setNumberOfThreads(1);
config.controler().setLastIteration(1);
config.controler().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
config.network().setInputFile(URL + "drt-base-case/leipzig-v1.1-network-with-pt-drt.xml.gz");
config.plans().setInputFile(URL + "leipzig-v1.1-0.1pct.plans.xml.gz");
config.transit().setTransitScheduleFile(URL + "leipzig-v1.1-transitSchedule.xml.gz");
config.transit().setVehiclesFile(URL + "leipzig-v1.1-transitVehicles.xml.gz");
config.vehicles().setVehiclesFile(URL + "drt-base-case/leipzig-v1.1-vehicle-types-with-drt-scaledFleet.xml");
LeipzigPtFareModule ptFareModule = new LeipzigPtFareModule();

ConfigUtils.addOrGetModule(config, SimWrapperConfigGroup.class).defaultDashboards = SimWrapperConfigGroup.Mode.disabled;
//set fare params; flexa has the same prices as leipzig PT: Values taken out of LeipzigPtFareModule -sm0522
Double ptBaseFare = ptFareModule.getNormalPtBaseFare();
Double ptDistanceFare = ptFareModule.getNormalDistanceBasedFare();

assertNotNull(DvrpConfigGroup.get(config));
MultiModeDrtConfigGroup multiModeDrtCfg = MultiModeDrtConfigGroup.get(config);
assertNotNull(multiModeDrtCfg);
multiModeDrtCfg.getModalElements().forEach(drtConfigGroup -> {

//assume DrtFareParams to be configured
assertTrue(drtConfigGroup.getDrtFareParams().isPresent());
DrtFareParams fareParams = drtConfigGroup.getDrtFareParams().get();
assertEquals(ptBaseFare.doubleValue(), fareParams.baseFare, 0.);
assertEquals(ptDistanceFare.doubleValue(), fareParams.distanceFare_m, 0.);

MATSimApplication.execute(RunLeipzigScenario.class, config, "run", "--1pct", "--drt-area", exampleShp,
"--drt-modes", "drtNorth,drtSoutheast", "--waiting-time-threshold-optDrt", "600", "--post-processing", "disabled");
//assume speed up params to be configured and a few specific values
assertTrue(drtConfigGroup.getDrtSpeedUpParams().isPresent());
DrtSpeedUpParams speedUpParams = drtConfigGroup.getDrtSpeedUpParams().get();
assertTrue(config.controler().getLastIteration() <= speedUpParams.firstSimulatedDrtIterationToReplaceInitialDrtPerformanceParams);
assertEquals(0., speedUpParams.fractionOfIterationsSwitchOn,0.);
assertEquals(1., speedUpParams.fractionOfIterationsSwitchOff, 0.);
});


testDrtNetwork(config);
}

Assert.assertNotNull(config.getModules().get("multiModeOptDrt"));
Assert.assertNotNull(config.getModules().get("multiModeOptDrt").getParameterSets());
private static void testDrtNetwork(Config config) {
Network network = NetworkUtils.readNetwork(config.controler().getOutputDirectory() + "/" + config.controler().getRunId() + ".output_network.xml.gz");
assertFalse(network.getLinks().get(Id.createLinkId("24232899")).getAllowedModes().contains("drtNorth"));
assertFalse(network.getLinks().get(Id.createLinkId("24232899")).getAllowedModes().contains("drtSoutheast"));
assertTrue(network.getLinks().get(Id.createLinkId("307899688#1")).getAllowedModes().contains("drtNorth"));
assertTrue(network.getLinks().get(Id.createLinkId("26588307#0")).getAllowedModes().contains("drtSoutheast"));
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit a473a93

Please sign in to comment.