run_simple: Simplest code to run a simulation loop

Description Usage Arguments Value Examples

View source: R/run_simple.R

Description

A generic function to run a simulation loop for a fixed period of time.

Usage

1
run_simple(step_function, initial.pop, end.time, ...)

Arguments

step_function

Function to run a timestep (step_function()) which returns a list containing elements updated.pop with the updated population and end.experiment which is TRUE if the experiment has ended (FALSE if not)

initial.pop

Initial population data frame with columns corresponding to function requirements

end.time

End time of simulation

...

(optionally) any other arguments for step_function(), e.g. parameters or timestep

Value

Data frame containing population history of simulation over time

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
growth <- function(latest.df, growth.rate) {
current.count <- latest.df$count
growth.num <- current.count * growth.rate
next.count <- current.count + growth.num
next.time <- latest.df$time + 1
new.df <- data.frame(time=next.time, count=next.count)
finished <- next.count == 0
list(updated.pop=new.df, end.experiment=finished)
}
df <- data.frame(time=0, count=1)
results <- run_simple(growth, df, 100, growth.rate=0.1)
plot_populations(results)

boydorr/ProgInR documentation built on Nov. 16, 2021, 6:32 p.m.