Run a simulation where event listeners take precedence over processes for state changes.
a list of Variables
a list of Events
a list of processes to execute on each timestep
the end timestep of the simulation. If state
is not NULL, timesteps must be greater than state$timestep
a checkpoint from which to resume the simulation
if TRUE, restore R's global random number generator's state from the checkpoint.
Invisibly, the saved state at the end of the simulation, suitable for later resuming.
population <- 4
timesteps <- 5
state <- CategoricalVariable$new(c('S', 'I', 'R'), rep('S', population))
renderer <- Render$new(timesteps)
transition <- function(from, to, rate) {
return(function(t) {
from_state <- state$get_index_of(from)
state$queue_update(
to,
from_state$sample(rate)
)
})
}
processes <- list(
transition('S', 'I', .2),
transition('I', 'R', .1),
transition('R', 'S', .05),
categorical_count_renderer_process(renderer, state, c('S', 'I', 'R'))
)
simulation_loop(variables=list(state), processes=processes, timesteps=timesteps)
renderer$to_dataframe()
#> timestep S_count I_count R_count
#> 1 1 4 0 0
#> 2 2 4 0 0
#> 3 3 4 0 0
#> 4 4 3 1 0
#> 5 5 1 3 0