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

make bicycle example work; improve it #1139

Open
wants to merge 3 commits into
base: dev.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 pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Therefore we need to use a fixed value here and then refer to the matsim version with ${project.parent.version} -->
<!-- Options: stable build based on weekly (e.g. 15.0-PR2344), PR-based (e.g. 15.0-2023w01) or official (e.g. 14.0) releases -->
<!-- <version>16.0-2024w02</version>-->
<version>2025.0-2024w21</version>
<version>2024.0</version>
<!--<version>15.0-SNAPSHOT</version>-->
<!-- <version>16.0-SNAPSHOT</version>-->
<relativePath/>
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
Loading