R6 class for running individual model simulations via a
simulation function, storing results, and generating success/error statuses.
Super class
poems::GenericClass -> ModelSimulator
Active bindings
simulation_modelA SimulationModel object or an inherited class object.
simulation_functionName (character string) or direct assignment (assigned or loaded via source path) of the simulation function, which takes a
SimulationModel(or inherited class) as an input and returns the simulation results.sample_idAn identifier for the simulation sample.
resultsA list of result structures.
Methods
Method new()
Initialization method sets the population model, and optionally the simulation function, the sample ID, and any attached attributes listed individually.
Usage
ModelSimulator$new(
simulation_model = NULL,
simulation_function = NULL,
sample_id = NULL,
...
)Arguments
simulation_modelA
SimulationModel(or inherited class) object (can be set later).simulation_functionOptional name (character string) or direct assignment (assigned or loaded via source path) of the simulation function, which takes a
SimulationModel(or inherited class) as an input and returns the simulation results.sample_idOptional identifier for the simulation sample.
...Additional parameters passed individually are attached.
Method new_clone()
Creates a new (re-initialized) object of the current (inherited) object class with optionally passed parameters.
Method run()
Runs a model simulator (function), stores the results, and creates a status log entry as a list.
Examples
# Simulation model
model1 <- SimulationModel$new(
time_steps = 10,
model_attributes = c("time_steps", "a", "b"),
params = list(a = 1:7)
)
model1$required_attributes <- model1$model_attributes
# Simulation function
test_simulator <- function(model) {
sum(unlist(model$get_attributes(model$required_attributes)))
}
# Model simulator
simulator1 <- ModelSimulator$new(
simulation_model = model1,
simulation_function = test_simulator
)
simulator1$run()
#> $successful
#> [1] FALSE
#>
#> $message
#> [1] "Model %s attributes are incomplete/inconsistent: a, b"
#>
model1$set_attributes(a = 1:10, b = 15)
model1$get_attributes(model1$required_attributes)
#> $time_steps
#> [1] 10
#>
#> $a
#> [1] 1 2 3 4 5 6 7 8 9 10
#>
#> $b
#> [1] 15
#>
simulator1$run()
#> $successful
#> [1] TRUE
#>
#> $message
#> [1] "Model %s simulation ran successfully"
#>
simulator1$results
#> [1] 80