From d04e35bf04c14d1748ec241f93a7494d2ae8c6af Mon Sep 17 00:00:00 2001 From: Paul Heinrich Date: Thu, 27 Jun 2024 11:18:31 +0200 Subject: [PATCH] add vehicles source option to example --- .../integration/RunMultipleModesExample.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/org/matsim/codeexamples/integration/RunMultipleModesExample.java b/src/main/java/org/matsim/codeexamples/integration/RunMultipleModesExample.java index 363cbf7c..5851247f 100644 --- a/src/main/java/org/matsim/codeexamples/integration/RunMultipleModesExample.java +++ b/src/main/java/org/matsim/codeexamples/integration/RunMultipleModesExample.java @@ -2,21 +2,26 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +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.network.Link; import org.matsim.api.core.v01.population.Person; import org.matsim.core.config.Config; import org.matsim.core.config.ConfigUtils; +import org.matsim.core.config.groups.QSimConfigGroup; import org.matsim.core.config.groups.ReplanningConfigGroup.StrategySettings; import org.matsim.core.controler.AbstractModule; import org.matsim.core.controler.Controler; +import org.matsim.core.controler.OutputDirectoryHierarchy; import org.matsim.core.replanning.strategies.DefaultPlanStrategiesModule; import org.matsim.core.router.util.TravelTime; import org.matsim.core.scenario.ScenarioUtils; import org.matsim.core.utils.io.IOUtils; import org.matsim.examples.ExamplesUtils; import org.matsim.vehicles.Vehicle; +import org.matsim.vehicles.VehicleType; +import org.matsim.vehicles.VehicleUtils; import java.net.URL; import java.util.Arrays; @@ -60,6 +65,16 @@ static Scenario prepareScenario( Config config ){ Set modes = new LinkedHashSet<>( Arrays.asList( TransportMode.car, TransportMode.bike )); link.setAllowedModes( modes ); } + + // add vehicle types for both modes (they are only used, if the vehicles source is set correspondingly) + scenario.getVehicles().addVehicleType( VehicleUtils.getFactory().createVehicleType(Id.create("car", VehicleType.class)) ); + + VehicleType bike = VehicleUtils.getFactory().createVehicleType(Id.create("bike", VehicleType.class)) + .setNetworkMode("bike") + .setMaximumVelocity(5.); + + scenario.getVehicles().addVehicleType(bike); + return scenario; } @@ -67,6 +82,10 @@ static Config prepareConfig(){ final URL url = IOUtils.extendUrl( ExamplesUtils.getTestScenarioURL( "equil" ), "config.xml" ); log.warn("url=" + url) ; Config config = ConfigUtils.loadConfig( url ); + config.controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists); + + // in case you want the different modes to use different vehicle types, you need to set this + config.qsim().setVehiclesSource(QSimConfigGroup.VehiclesSource.modeVehicleTypesFromVehiclesData); { // add strategy that switches between car and bike: StrategySettings stratSets = new StrategySettings( ) ;