This module provides the following functionalities:
runControl.R
: run MATSim using a MATSim config file.createVISTADemand.R
: assign daily activity patterns to a
population and write it as a MATSim plan_v4 file.helpers.R
: reader and edit matsim’s config, create a MATSim
network_v2 file from shpfiles, assign random geographical locations
to activities.runControler
has new requirements in model
. It can now set the
path to a matsim.jar file and the maximum amount of memory for
MATSim to use using matsim_path_to_jar
and matsim_max_memory
,
respectively. matsim_config_params
accepts a named list which will
be used to modify a given matsim_config
. See its documentation
section for more details.use_rJava
argument to runControler
this allows the event to
be configured how it should run MATSim’s controler.You will need to install Java. It is recommended to use Java 8 to run MATSim (see related issues here: https://github.com/matsim-org/matsim-code-examples/issues/176, https://github.com/matsim-org/matsim-code-examples/issues/16).
The matsim module doesn’t shipped with a MATSim java executable file
that is required to make this module works. The user must download
MATSim from MATSim website. I
recommend to use the latest stable release (version 0.10.0 as of
2020-01-25) with this module. Once, you have downloaded MATSim you
must extract the matsim zip file into your matsim module folder
(usually under the root folder of your active R project
modules/matsim
). Then rename the extracted file as ‘matsim’. Then
you are good to go! :)
Run MATSim using a MATSim config file.
dymium always makes a copy of the original MATSim config file and saves
the copy in the same location with a suffix -dymium
, incase it needs
to modify some settings. If you are using dymium’s scenario, your matsim
output will be automatically saved inside the outputs
folder of your
active scenario.
event_matsim_runcontroler <- modules::use(here::here("modules/matsim/runControler.R"))
#> Masking (modules:sf):
#> `%>%` from: modules:dymiumCore
#> run:
#> function(world, model = NULL, target = NULL, time_steps = NULL, use_rJava = TRUE)
#>
#>
#> REQUIRED_MODELS:
#> chr [1:4] "matsim_config" "matsim_config_params" "matsim_path_to_jar" ...
matsim_config
. From the example below, controler
is a module name and firstIteration
and lastIteration
are
param names that belong to the controler
module. We set
firstIteration
to 0 and lastIteration
to 10. To learn more
about what parameters can be set please see a MATSim
documentation.model <- list(
matsim_config = "path/to/config.xml",
matsim_config_params = list(
controler = list(
firstIteration = 0,
lastIteration = 10
)),
matsim_path_to_jar = "path/to/matsim.jar",
matsim_max_memory = "-Xmx1024m" # 1024mbs or 1gb which is also the default value.
)
TRUE
, this determine
the method that the function uses to run MATSim. By default, the
function will attempt to use rJava to start MATSim’s controler. If
FALSE, then it will call MATSim using a commandline command. Note
that, calling MATSim from the commandline only possible on a UNIX
machine if you are running this through a Windows machine then you
must use rJava to call MATSim. An error will be raised if this is
set to FALSE on a Windows machine.The following example runs an example MATSim scenario for five iterations.
event_matsim_runcontroler <-
modules::use(here::here('modules/matsim/runControler.R'))
create_toy_world()
for (i in 1:1) {
world$start_iter(time_step = i, unit = 'year') %>%
event_matsim_runcontroler$run(model = list(
matsim_config = here::here("modules/matsim/matsim/examples/equil/config.xml"),
matsim_config_params = list(controler = list(
firstIteration = 0,
lastIteration = 5
))
))
}
This event function assigns daily travel activity patterns to Dymium individuals using a statistical matching technique. It uses a travel survey in this case the 2009 VISTA household travel survey of Victoria state, Australia (link). It then parses the fused travel activities to an xml file that follows the format of MATSim’s population_V6 and save it to your matsim input folder.
The implementation of this function is very specific to the 2009 VISTA data hence it’s most likely not going to work if you replace the vista data with other data. However, this can be used as a template for implementing other travel surveys or can be replaced this step entirely with a proper activity-based model if available.
event_matsim_createVISTADemand <- modules::use('modules/matsim/createVISTADemand.R')
#> Masking (modules:sf):
#> `%>%` from: modules:dymiumCore
#> run:
#> function(world, model = NULL, target = NULL, time_steps = NULL)
#>
#>
#> REQUIRED_MODELS:
#> chr [1:2] "vista_persons" "vista_trips"
model <- list(vista_persons = data.frame(),
vista_trips = data.frame())
See the helpers.R
script for more details.
Additionally, there are some functions for binding MATSim’s histogram
plots into a single animated histogram by iteration number in plots.R
The MATSim melbourne scenario has the following unresolved issues
MATSim works best with Java 8. If you are running newer versions of Java there is a high chance that some dependencies might be missing one of which is the JAXB libraries as discussed here.
(2020-01-12) Java JDK 11.0.1 must be installed in order for the rJava
pacakge to work. This issue has been discussed
here and
here. This is only required
if you would like to run the MATSim module using rJava as implemented in
.run_matsim_rjava
function inside runControler.R
. Otherwise, you can
use .run_matsim_system()
which basically invoke your matsim.jar
from
the OS command, this option requires you to make sure that Java8+ is
installed and is callable using the OS command.
To install the required version of Java click on this link and download Java SE Development Kit 11.0.1 that works on your machine.
You will need to run R CMD javareconf
on terminal to make sure R
detects your new java installation. Make sure that all R sessions are
close before running this command.
If you get the following error..
> library(rJava)
# Error: package or namespace load failed for 'rJava':
# .onLoad failed in loadNamespace() for 'rJava', details:
# call: dyn.load(file, DLLpath = DLLpath, ...)
# error: unable to load shared object '/Users/kevin/Library/R/3.4/library/rJava/libs/rJava.so':
# dlopen(/Users/kevin/Library/R/3.4/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
# Referenced from: /Users/kevin/Library/R/3.4/library/rJava/libs/rJava.so
# Reason: image not found
It may be a good idea to run the following command in your Terminal.
R CMD javareconf
Then quit RStudio and launch it again to try library(rJava)
. If the
error still persist then Google search is your best friend!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.