matsim-org / matsim-code-examples

A repository containing code examples around MATSim
GNU General Public License v3.0
83 stars 180 forks source link

Using vehicles from vehicles data #361

Open ZhiweiUSF opened 4 years ago

ZhiweiUSF commented 4 years ago

Hi all,

Following the example config-with-all-vehicles-from-file.xml in https://github.com/matsim-org/matsim-code-examples/tree/12.x/scenarios/equil-mixedTraffic, I was trying to figure out how to create a vehicle fleet for the simulation. Yet, I ran into an exception when running the model using this configuration file (see below for the exception below). It seems that coding is needed to reach what I want to do. Is there a way to do this by adding another input xml file? Did anyone know how I should address this issue? Also, I am wondering why we need to assign a vehicle for each mode for each person? What if some people that do not have cars?

Exception in thread "main" java.lang.RuntimeException: Could not retrieve vehicle id from person: 1 for mode: car. If you are not using config.qsim().getVehicleSource() with 'defaultVehicle' or 'modeVehicleTypesFromVehiclesData' you have to provide a vehicle for each mode for each person. Attach a map of mode:String -> id:Id with key 'vehicles' as person attribute to each person. VehicleUtils.insertVehicleIdIntoAttributes does this for you. at org.matsim.vehicles.VehicleUtils.getVehicleId(VehicleUtils.java:143) at org.matsim.core.mobsim.qsim.agents.PopulationAgentSource.insertVehicles(PopulationAgentSource.java:95) at org.matsim.core.mobsim.qsim.agents.PopulationAgentSource.insertAgentsIntoMobsim(PopulationAgentSource.java:72) at org.matsim.core.mobsim.qsim.QSim.createAgents(QSim.java:306) at org.matsim.core.mobsim.qsim.QSim.prepareSim(QSim.java:291) at org.matsim.core.mobsim.qsim.QSim.run(QSim.java:250) at org.matsim.core.controler.NewControler.runMobSim(NewControler.java:124) at org.matsim.core.controler.AbstractController$8.run(AbstractController.java:198) at org.matsim.core.controler.AbstractController.iterationStep(AbstractController.java:230) at org.matsim.core.controler.AbstractController.mobsim(AbstractController.java:194) at org.matsim.core.controler.AbstractController.iteration(AbstractController.java:145) at org.matsim.core.controler.AbstractController.doIterations(AbstractController.java:113) at org.matsim.core.controler.AbstractController$1.run(AbstractController.java:80) at org.matsim.core.controler.MatsimRuntimeModifications.run(MatsimRuntimeModifications.java:69) at org.matsim.core.controler.MatsimRuntimeModifications.run(MatsimRuntimeModifications.java:52) at org.matsim.core.controler.AbstractController.run(AbstractController.java:88) at org.matsim.core.controler.NewControler.run(NewControler.java:82) at org.matsim.core.controler.Controler.run(Controler.java:249) at org.matsim.run.Controler.run(Controler.java:54) at org.matsim.run.Controler.main(Controler.java:58)

Janekdererste commented 4 years ago

If you want to use vehicles from a vehicles file you actually have to assign those vehicles to agents. This can only be done in code.

// iterate over all persons
for (Person person : scenario.getPopulation().getPersons().values()) {

    // retreive vehicleIds which were previously read in from vehicles.xml. -- insert your own logic here
    var carFromFile= scenario.getVehicles().getVehicleTypes().get(Id.create("some-car-id", VehicleType.class));
    var bikeFromFile= scenario.getVehicles().getVehicleTypes().get(Id.create("some-bike-id", VehicleType.class));

    // link vehicles to different modes
    var modeToVehicle = Map.of("car", carFromFile, "bike", bikeFromFile);

    // assign vehicles to agent
    VehicleUtils.insertVehicleIdsIntoAttributes(person, modeToVehicle);
}

If you simply define whicle types for certain modes, the start up logic will assign a vehicle to each agent for each mode.

Persons only need vehicles for modes they are allowed to choose. So, if you have agents who can't drive a car, you don't have to assign one. Though I do know that this is possible I have never used this feature and therefore don't know how to achieve this.