Provides methods for initializing the synthetic population, parallel simulation, calibration and monitoring.
new()
Create a new Simulator object.
Simulator$new( initializer = NULL, acc_events = list(), man_events = list(), seeds = 123, keep_init = FALSE, ... )
initializer
A function that generates the initial population.
acc_events
A list of AccumulationEvent objects.
man_events
A list of ManipulationEvent objects.
seeds
Seed values for random number generation.
keep_init
A logical value indicating whether the initial status should be stored.
If TRUE
the initial population can be retrieved with the method get_init.
...
Additional arguments to initializer
,
A new Simulator
object.
start_cluster()
Start a parallel computation cluster.
Simulator$start_cluster( cl, nc, export, packages, interface = c("doParallel", "doMPI") )
cl
A cluster object.
nc
Size of the cluster (available computing cores).
export
A character vector giving the names of objects from the global environment that should be exported to the workers.
packages
A character vector giving the names of packages that should be loaded on the workers.
interface
Interface to "foreach", must match the cluster type ("doParallel" and "doMPI" are currently supported).
stop_cluster()
Stop a parallel computation cluster.
Simulator$stop_cluster()
run()
Progress the simulation.
Simulator$run(N, show_progress = TRUE)
N
An integer indicating the length of the simulation.
show_progress
A logical value.
If TRUE
shows a progress bar that tracks the completion of the simulation
(for non-parallel computations only)
unit
A character string giving the time unit that t_sim
corresponds to.
run_scenarios()
Run various scenarios with the same initial population using different interventions with replications.
Simulator$run_scenarios( interventions, replications, N, seeds_override = NULL, output, ... )
interventions
A list of interventions corresponding to the scenarios to run, see 'details'.
replications
An integer describing how many times each scenario should be replicated.
N
A single integer or an integer vector describing the simulation runtime for each scenario.
seeds_override
If NULL
, a different seed is automatically used for each scenario.
If provided, should be an integer matrix with replications
rows and length(interventions)
columns.
output
A function to apply to the population after each scenario.
...
Additional arguments passed to output
.
Each element of interventions
should be a named list with the following elements:
acc_events
, acc_pars
, man_events
, man_pars
and pars
.
The element acc_events
should be a list of objects of class AccumulationEvent
and
acc_pars
should be a list of the same length of numeric vectors giving the parameter values for these events.
Similarly, man_events
should be a list of object of class ManipulationEvent
with man_pars
being a list of the same length of numeric vectors giving the parameter values.
Finally, pars
should be a list of parameter values (see configure
).
For additional details, see intervene
.
reconfigure()
Configure the parameters of Event objects.
Simulator$reconfigure(pars)
pars
A named list of parameters, see 'details'.
The parameter list pars
should be structured as follows:
Each element should have a name of an Event object and each
element should be a named list where the names correspond
to the names of the parameters of whose values are to be configured.
configure()
Configure events, run a simulation and compute a summary statistic for calibration. The original status is restored after completion.
Simulator$configure(N, pars, output, seeds = 123, ...)
N
A numeric value indicating the length of the simulation
pars
A list of parameter values, see 'details'.
output
A function that computes a summary statistic from the simulation.
seeds
Seed values for random number generation.
...
Additional arguments passed to output
.
The parameter list pars
should be structured as follows:
Each element should have a name of an Event object and each
element should be a named list where the names correspond
to the names of the parameters of whose values are to be configured.
The output of output_function
evaluated for the resulting population.
intervene()
Carry out an intervention that applies a set of accumulation events and manipulation events, and changes the values of specific parameters. By default, the events are applied first, then the parameters are changed.
acc_events
A list of accumulation events to apply.
man_events
A list of manipulation events to apply.
pars
A list of parameter values to set, see 'details'.
events_first
A logical value. If TRUE
(the default) then the events are applied before any changes to the parameters. If FALSE
the parameter values are changed first.
The parameter list pars
should be structured as follows:
Each element should have a name of an Event object and each
element should be a named list where the names correspond
to the names of the parameters of whose values are to be configured.
get_status()
Get the status of the current population.
Simulator$get_status()
A data.table of the current status.
get_init()
Get the initial status of the population, if available.
Simulator$get_init()
A data.table of the initial status if it was recorded, otherwise NULL.
get_sample()
. Get a sample of the current population
Simulator$get_sample(sampler, ...)
sampler
A function that constructs the output sample.
...
Additional arguments passed to sampler
.
The sample generated by sampler
.
get_history()
Get the simulation history as recorded by the monitor.
Simulator$get_history()
If the monitor is enabled, A list with an element for each evaluation of the monitor aggregators at their specified intervals These elements are lists as well, where the first element is the time index, and the remaining elements correspond to the values returned by the evaluated aggregators of the monitor. If no monitor is specified, returns NULL (invisibly).
initialize_monitor()
Initialize monitors to track specific status variables throughout the simulation at certain intervals.
Simulator$initialize_monitor(aggregators, intervals, combiners = NULL)
aggregators
A list of functions to compute aggregate summary statistics from the simulated population.
intervals
A numeric vector giving the time interval after which each aggregator should be evaluated.
combiners
If parallel computation is used, this is a list of functions that combine the aggregator results from each parallel worker.
reset()
Reset the simulated population to the initial state (if initial status was kept). Note that any changes to event parameters are kept as is and have to be configured manually, if so desired.
Simulator$reset(force = FALSE, seeds = NULL)
force
If TRUE
, reset the simulator even if the initial status is not available.
seeds
An integer vector. If given, sets the pseudo-RNG seeds. This will then reset the seed numbers, and the simulation time.
clone()
The objects of this class are cloneable with this method.
Simulator$clone(deep = FALSE)
deep
Whether to make a deep clone.