simulation_loop: A premade simulation loop

Description Usage Arguments Examples

View source: R/simulation.R

Description

Run a simulation where event listeners take precedence over processes for state changes.

Usage

1
2
3
4
5
6
simulation_loop(
  variables = list(),
  events = list(),
  processes = list(),
  timesteps
)

Arguments

variables

a list of Variables

events

a list of Events

processes

a list of processes to execute on each timestep

timesteps

the number of timesteps to simulate

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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()

individual documentation built on Oct. 25, 2021, 9:09 a.m.