Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bicycle example #1140

Open
wants to merge 15 commits into
base: 2024.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/mvn-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: 17
java-version: 21
distribution: 'temurin'
cache: maven
- name: Build with Maven
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<id>jzy3d</id>
<url>https://maven.jzy3d.org/releases/</url>
</repository>
<!-- for glugen dependency -->
<repository>
<id>jogamp</id>
<url>https://jogamp.org/deployment/maven</url>
</repository>
</repositories>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,80 @@
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.Leg;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.contrib.bicycle.BicycleConfigGroup;
import org.matsim.contrib.bicycle.BicycleModule;
import org.matsim.contrib.bicycle.BicycleUtils;
import org.matsim.contrib.otfvis.OTFVisLiveModule;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.core.config.groups.QSimConfigGroup;
import org.matsim.core.config.groups.ReplanningConfigGroup;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;
import org.matsim.vehicles.VehicleType;
import org.matsim.vehicles.VehicleUtils;
import org.matsim.vehicles.VehiclesFactory;
import org.matsim.vis.otfvis.OTFVisConfigGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.net.URL;
import java.util.*;

public final class RunBicycleContribExample{
private static final Logger LOG = LogManager.getLogger( RunBicycleContribExample.class );

private static final String BICYCLE_MODE = "bicycle";

public static void main(String[] args) {
Config config;
if (args.length >= 1) {
LOG.info("A user-specified config.xml file was provided. Using it...");
config = ConfigUtils.loadConfig(args, new BicycleConfigGroup() );
} else {
LOG.info("No config.xml file was provided. Using 'standard' example files given in this contrib's resources folder.");
// Setting the context like this works when the data is stored under "/matsim/contribs/bicycle/src/main/resources/bicycle_example"
config = ConfigUtils.createConfig("bicycle_example/");
config.addModule(new BicycleConfigGroup());
config.network().setInputFile("network_lane.xml"); // Modify this
config.plans().setInputFile("population_1200.xml");
LOG.info("No config.xml file was provided. Using bicycle_example from ExamplesUtils.");

config = ConfigUtils.createConfig( ExamplesUtils.getTestScenarioURL( "bicycle_example" ) );

config.qsim().setLinkDynamics( QSimConfigGroup.LinkDynamics.PassingQ );
config.qsim().setTrafficDynamics( QSimConfigGroup.TrafficDynamics.kinematicWaves );

config.network().setInputFile("network_normal.xml"); // change this to any of the others that are provided
config.plans().setInputFile("population_3.xml");

config.replanning().addStrategySettings( new ReplanningConfigGroup.StrategySettings().setStrategyName("ChangeExpBeta" ).setWeight(0.8 ) );
config.replanning().addStrategySettings( new ReplanningConfigGroup.StrategySettings().setStrategyName("ReRoute" ).setWeight(0.2 ) );
// config.replanning().addStrategySettings( new ReplanningConfigGroup.StrategySettings().setStrategyName("ChangeExpBeta" ).setWeight(0.8 ) );
// config.replanning().addStrategySettings( new ReplanningConfigGroup.StrategySettings().setStrategyName("ReRoute" ).setWeight(0.2 ) );

config.scoring().addActivityParams( new ScoringConfigGroup.ActivityParams("home").setTypicalDuration(12*60*60 ) );
config.scoring().addActivityParams( new ScoringConfigGroup.ActivityParams("work").setTypicalDuration(8*60*60 ) );

config.scoring().addModeParams( new ScoringConfigGroup.ModeParams("bicycle").setConstant(0. ).setMarginalUtilityOfDistance(-0.0004 ).setMarginalUtilityOfTraveling(-6.0 ).setMonetaryDistanceRate(0. ) );
config.scoring().addModeParams( new ScoringConfigGroup.ModeParams(
BICYCLE_MODE ).setConstant(0. ).setMarginalUtilityOfDistance(-0.0004 ).setMarginalUtilityOfTraveling(-6.0 ).setMonetaryDistanceRate(0. ) );

config.global().setNumberOfThreads(1 );
config.controller().setOverwriteFileSetting( OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists );

config.controller().setLastIteration(100); // Modify if motorized interaction is used
config.controller().setLastIteration(0);
}

BicycleConfigGroup bicycleConfigGroup = ConfigUtils.addOrGetModule( config, BicycleConfigGroup.class );
bicycleConfigGroup.setBicycleMode( "bicycle" );
bicycleConfigGroup.setMarginalUtilityOfInfrastructure_m(-0.0002);
bicycleConfigGroup.setMarginalUtilityOfComfort_m(-0.0002);
bicycleConfigGroup.setMarginalUtilityOfGradient_m_100m(-0.02);
bicycleConfigGroup.setMaxBicycleSpeedForRouting(4.16666666);
BicycleConfigGroup bicycleConfig = ConfigUtils.addOrGetModule( config, BicycleConfigGroup.class );
bicycleConfig.setBicycleMode( BICYCLE_MODE );
// bicycleConfig.setMarginalUtilityOfInfrastructure_m(-0.0002);
// bicycleConfig.setMarginalUtilityOfComfort_m(-0.0002);
// bicycleConfig.setMarginalUtilityOfGradient_m_100m(-0.02);
// bicycleConfig.setMaxBicycleSpeedForRouting(4.16666666);

List<String> mainModeList = Arrays.asList( "bicycle", TransportMode.car);
Set<String> mainModeSet = new LinkedHashSet<>( Arrays.asList( BICYCLE_MODE, TransportMode.car ) );

config.qsim().setMainModes(mainModeList );
config.qsim().setMainModes(mainModeSet );

config.routing().setNetworkModes(mainModeList );
config.routing().setNetworkModes(mainModeSet );

// ===

Expand All @@ -73,16 +87,32 @@ public static void main(String[] args) {
// set config such that the mode vehicles come from vehicles data:
scenario.getConfig().qsim().setVehiclesSource( QSimConfigGroup.VehiclesSource.modeVehicleTypesFromVehiclesData );

// now put hte mode vehicles into the vehicles data:
// now put the mode vehicles into the vehicles data:
final VehiclesFactory vf = VehicleUtils.getFactory();
scenario.getVehicles().addVehicleType( vf.createVehicleType( Id.create(TransportMode.car, VehicleType.class ) ).setNetworkMode( TransportMode.car ) );
scenario.getVehicles().addVehicleType( vf.createVehicleType(Id.create("bicycle", VehicleType.class ) ).setNetworkMode( "bicycle" ).setMaximumVelocity(4.16666666 ).setPcuEquivalents(0.25 ) );
scenario.getVehicles().addVehicleType( vf.createVehicleType(Id.create( BICYCLE_MODE, VehicleType.class ) ).setNetworkMode( BICYCLE_MODE ).setMaximumVelocity(4.16666666 ).setPcuEquivalents(0.25 ) );

// // create a bicycle expressway
// scenario.getNetwork().getLinks().get( Id.createLinkId( 2 ) ).getAttributes().putAttribute( BicycleUtils.BICYCLE_INFRASTRUCTURE_SPEED_FACTOR, 10. );

// // allow cars on all links ... and switch one person to car:
// for( Link link : scenario.getNetwork().getLinks().values() ){
// link.setAllowedModes( mainModeSet );
// }
// Plan plan = scenario.getPopulation().getPersons().get( Id.createPersonId( 1 ) ).getSelectedPlan();
// for( Leg leg : TripStructureUtils.getLegs( plan ) ){
// leg.setMode( TransportMode.car );
// }

// ===

Controler controler = new Controler(scenario);
controler.addOverridingModule(new BicycleModule() );

controler.addOverridingModule( new OTFVisLiveModule() );
OTFVisConfigGroup otfConfig = ConfigUtils.addOrGetModule( config, OTFVisConfigGroup.class );
otfConfig.setAgentSize( otfConfig.getAgentSize()*2 );

controler.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.Person;
import org.matsim.contrib.common.zones.systems.grid.square.SquareGridZoneSystemParams;
import org.matsim.contrib.drt.extension.insertion.DrtInsertionModule;
import org.matsim.contrib.drt.optimizer.insertion.DrtInsertionSearchParams;
import org.matsim.contrib.drt.optimizer.insertion.extensive.ExtensiveInsertionSearchParams;
import org.matsim.contrib.drt.prebooking.PrebookingParams;
import org.matsim.contrib.drt.prebooking.logic.ProbabilityBasedPrebookingLogic;
import org.matsim.contrib.drt.routing.DrtRoute;
import org.matsim.contrib.drt.routing.DrtRouteFactory;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.run.*;
import org.matsim.contrib.drt.run.DrtConfigGroup.OperationalScheme;
import org.matsim.contrib.drt.run.DrtConfigs;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.drt.run.MultiModeDrtModule;
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.fleet.FleetSpecification;
import org.matsim.contrib.dvrp.fleet.FleetSpecificationImpl;
Expand Down Expand Up @@ -81,7 +79,9 @@ public static class RunSettings {

public static void runSingle(File populationPath, File networkPath, File outputPath, RunSettings settings) {
// configuration
Config config = ConfigUtils.createConfig(new MultiModeDrtConfigGroup(), new DvrpConfigGroup());
DvrpConfigGroup dvrpConfigGroup = new DvrpConfigGroup();
dvrpConfigGroup.getTravelTimeMatrixParams().addParameterSet(new SquareGridZoneSystemParams());
Config config = ConfigUtils.createConfig(new MultiModeDrtConfigGroup(), dvrpConfigGroup);

config.qsim().setSimStarttimeInterpretation(StarttimeInterpretation.onlyUseStarttime);
config.qsim().setStartTime(0.0);
Expand Down Expand Up @@ -117,9 +117,10 @@ public static void runSingle(File populationPath, File networkPath, File outputP
drtConfig.mode = "drt";
drtConfig.operationalScheme = OperationalScheme.door2door;
drtConfig.stopDuration = 60.0;
drtConfig.maxWaitTime = settings.maxWaitTime;
drtConfig.maxTravelTimeAlpha = settings.maxTravelTimeAlpha;
drtConfig.maxTravelTimeBeta = settings.maxWaitTime;
DrtOptimizationConstraintsParams constraintsParam = drtConfig.getDrtOptimizationConstraintsParam();
constraintsParam.maxWaitTime = settings.maxWaitTime;
constraintsParam.maxTravelTimeAlpha = settings.maxTravelTimeAlpha;
constraintsParam.maxTravelTimeBeta = settings.maxWaitTime;

DrtInsertionSearchParams insertionSearchParams = new ExtensiveInsertionSearchParams();
drtConfig.addDrtInsertionSearchParams(insertionSearchParams);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
package org.matsim.codeexamples.extensions.dvrp;

import org.apache.logging.log4j.Logger;
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.events.Event;
import org.matsim.contrib.common.zones.systems.grid.square.SquareGridZoneSystemParams;
import org.matsim.contrib.drt.optimizer.insertion.extensive.ExtensiveInsertionSearchParams;
import org.matsim.contrib.drt.routing.DrtRoute;
import org.matsim.contrib.drt.routing.DrtRouteFactory;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.run.DrtConfigs;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.drt.run.MultiModeDrtModule;
import org.matsim.contrib.drt.run.*;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpModule;
import org.matsim.contrib.dvrp.run.DvrpQSimComponents;
import org.matsim.contrib.otfvis.OTFVisLiveModule;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.ScoringConfigGroup.ModeParams;
import org.matsim.core.config.groups.QSimConfigGroup;
import org.matsim.core.config.groups.QSimConfigGroup.SnapshotStyle;
import org.matsim.core.config.groups.ReplanningConfigGroup.StrategySettings;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.config.groups.ScoringConfigGroup.ModeParams;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting;
import org.matsim.core.events.handler.BasicEventHandler;
import org.matsim.core.events.handler.EventHandler;
import org.matsim.core.replanning.strategies.DefaultPlanStrategiesModule.DefaultSelector;
import org.matsim.core.replanning.strategies.DefaultPlanStrategiesModule.DefaultStrategy;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;
import org.matsim.vis.otfvis.OTFVisConfigGroup;

class RunDrtExample{
Expand Down Expand Up @@ -71,17 +63,18 @@ public static void run(boolean otfvis, String... args ){

@SuppressWarnings("unused")
DvrpConfigGroup dvrpConfig = ConfigUtils.addOrGetModule( config, DvrpConfigGroup.class );
dvrpConfig.getTravelTimeMatrixParams().addParameterSet(new SquareGridZoneSystemParams());
// (config group needs to be "materialized")

MultiModeDrtConfigGroup multiModeDrtCfg = ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class);
{
DrtConfigGroup drtConfig = new DrtConfigGroup();
drtConfig.mode = DRT_A;
drtConfig.stopDuration = 60.;
drtConfig.maxWaitTime=900;
drtConfig.maxTravelTimeAlpha = 1.3;
drtConfig.maxTravelTimeBeta=10. * 60.;
drtConfig.rejectRequestIfMaxWaitOrTravelTimeViolated= false ;
drtConfig.getDrtOptimizationConstraintsParam().maxWaitTime=900;
drtConfig.getDrtOptimizationConstraintsParam().maxTravelTimeAlpha = 1.3;
drtConfig.getDrtOptimizationConstraintsParam().maxTravelTimeBeta=10. * 60.;
drtConfig.getDrtOptimizationConstraintsParam().rejectRequestIfMaxWaitOrTravelTimeViolated= false ;
drtConfig.vehiclesFile="one_shared_taxi_vehicles_A.xml";
drtConfig.changeStartLinkToLastLinkInSchedule=true;
drtConfig.addParameterSet( new ExtensiveInsertionSearchParams() );
Expand All @@ -91,10 +84,10 @@ public static void run(boolean otfvis, String... args ){
DrtConfigGroup drtConfig = new DrtConfigGroup();
drtConfig.mode = DRT_B;
drtConfig.stopDuration = 60.;
drtConfig.maxWaitTime=900;
drtConfig.maxTravelTimeAlpha = 1.3;
drtConfig.maxTravelTimeBeta=10. * 60.;
drtConfig.rejectRequestIfMaxWaitOrTravelTimeViolated= false ;
drtConfig.getDrtOptimizationConstraintsParam().maxWaitTime=900;
drtConfig.getDrtOptimizationConstraintsParam().maxTravelTimeAlpha = 1.3;
drtConfig.getDrtOptimizationConstraintsParam().maxTravelTimeBeta=10. * 60.;
drtConfig.getDrtOptimizationConstraintsParam().rejectRequestIfMaxWaitOrTravelTimeViolated= false ;
drtConfig.vehiclesFile="one_shared_taxi_vehicles_B.xml";
drtConfig.changeStartLinkToLastLinkInSchedule=true;
drtConfig.addParameterSet( new ExtensiveInsertionSearchParams() );
Expand All @@ -104,10 +97,11 @@ public static void run(boolean otfvis, String... args ){
DrtConfigGroup drtConfig = new DrtConfigGroup();
drtConfig.mode = DRT_C;
drtConfig.stopDuration = 60.;
drtConfig.maxWaitTime=900;
drtConfig.maxTravelTimeAlpha = 1.3;
drtConfig.maxTravelTimeBeta=10. * 60.;
drtConfig.rejectRequestIfMaxWaitOrTravelTimeViolated= false ;
DrtOptimizationConstraintsParams constraintsParam = drtConfig.getDrtOptimizationConstraintsParam();
constraintsParam.maxWaitTime=900;
constraintsParam.maxTravelTimeAlpha = 1.3;
constraintsParam.maxTravelTimeBeta=10. * 60.;
constraintsParam.rejectRequestIfMaxWaitOrTravelTimeViolated= false ;
drtConfig.vehiclesFile="one_shared_taxi_vehicles_C.xml";
drtConfig.changeStartLinkToLastLinkInSchedule=true;
drtConfig.addParameterSet( new ExtensiveInsertionSearchParams() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,13 +65,27 @@ static Scenario prepareScenario( Config config ){
Set<String> 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;
}

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( ) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void insertAgentsIntoMobsim(){
qsim.insertAgentIntoMobsim( ag );

// insert vehicle:
final Vehicle vehicle = VehicleUtils.getFactory().createVehicle( Id.create( ag.getId(), Vehicle.class ), VehicleUtils.getDefaultVehicleType() );
final Vehicle vehicle = VehicleUtils.getFactory().createVehicle( Id.create( ag.getId(), Vehicle.class ), VehicleUtils.createDefaultVehicleType() );
QVehicleImpl qVeh = new QVehicleImpl( vehicle );
qsim.addParkedVehicle( qVeh, ag.getCurrentLinkId() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void insertAgentsIntoMobsim() {
qsim.insertAgentIntoMobsim(ag);

// insert vehicle:
final Vehicle vehicle = VehicleUtils.getFactory().createVehicle(Id.create(ag.getId(), Vehicle.class), VehicleUtils.getDefaultVehicleType());
final Vehicle vehicle = VehicleUtils.getFactory().createVehicle(Id.create(ag.getId(), Vehicle.class), VehicleUtils.createDefaultVehicleType());
final Id<Link> linkId4VehicleInsertion = Id.createLinkId(1);
// qsim.createAndParkVehicleOnLink(vehicle, linkId4VehicleInsertion);
final QVehicle qVeh = new QVehicleImpl( vehicle ) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void insertAgentsIntoMobsim() {

// insert vehicle:
final Id<Vehicle> vehId = Id.create("myVeh", Vehicle.class);
final VehicleType vehType = VehicleUtils.getDefaultVehicleType();
final VehicleType vehType = VehicleUtils.createDefaultVehicleType();
final VehiclesFactory vehFactory = VehicleUtils.getFactory();
final Vehicle vehicle = vehFactory.createVehicle(vehId, vehType);
// qsim.createAndParkVehicleOnLink(vehicle, startingLinkId);
Expand Down
Loading
Loading