forked from SBOHVM/RPiR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_simple.Rd
52 lines (48 loc) · 1.84 KB
/
run_simple.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/run_simple.R
\name{run_simple}
\alias{run_simple}
\title{Simplest code to run a simulation loop}
\usage{
run_simple(step_function, initial.pop, end.time, ...)
}
\arguments{
\item{step_function}{Function to run a timestep (\code{step_function()})
which returns a list containing elements \code{updated.pop} with the
updated population and \code{end.experiment} which is TRUE if the
experiment has ended (FALSE if not)}
\item{initial.pop}{Initial population data frame with columns corresponding
to function requirements. This *must* include a \code{time} column so that
\code{run_simple()} can check whether the \code{end.time} has been reached.}
\item{end.time}{End time of simulation}
\item{...}{(optionally) any other arguments for \code{step_function()},
e.g. parameters or timestep}
}
\value{
Data frame containing population history of simulation over time
}
\description{
A generic function to run a simulation loop for a fixed period of time.
\code{run_simple()} will call \code{step_function(initial.pop, ...)} over
and over until the time ends or \code{step_function()} reports that the
experiment has ended.
}
\examples{
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)
}
\seealso{
\code{\link{run_simulation}} if you want a more flexible version
of this function that will allow your \code{step_function()} to return just a
data frame and will print some debugging information on request.
}