R6 Object for simulating a population of whales.
Author
Homer White homerhanumat@gmail.com
Public fields
dimensionsA vector of length two giving the dimensions of the ocean.
malesA list of R6 objects of class Male containing the current population of male whales.
femalesA list of R6 objects of class Female containing the current population of female whales.
malePopCurrent number of males in the population.
femalePopCurrent number of females in the population.
starveParameterHelps determine probability for each whale to die by starvation in the current generation.
Methods
Method new()
Create a new Ocean object.
Arguments
dimsnumerical vector of length 2 giving the dimensions of the ocean
maleseither a numeric vector of length 1 giving the initial number of male whales, or a list of R6 objects of class Male inheriting from Whale. If the former, then the specified number of whales of class Male are instantiated, with certain default characteristics. If the latter, then the given whales are cloned.
femaleseither a numeric vector of length 1 giving the initial number of female whales, or a list of R6 objects of class Female inheriting from Whale. If the former, then the specified number of whales of class Female are instantiated, with certain default characteristics. If the latter, then the given whales are cloned.
starvethe starvation parameter
Examples
if (FALSE) { # \dontrun{
library(ggplot2)
oceanSim <- function(
steps = 100, males = 10,
females = 10, starve = 5,
animate = FALSE, seed = NULL
) {
if ( !is.null(seed) ) {
set.seed(seed)
}
ocean <- Ocean$new(dims = c(100, 100), males = males,
females = females, starve = starve)
population <-numeric(steps)
for ( i in 1:steps ) {
population[i] <- ocean$malePop + ocean$femalePop
if ( population[i] == 0 ) break
ocean$advance()
if ( animate ) {
ocean$plot()
Sys.sleep(0.5)
}
}
pop <- population[1:i]
df <- data.frame(time = 1:length(pop),
pop)
ggplot(df, aes(x = time, y = pop)) + geom_line() +
labs(x = "Time", y = "Whale Population")
}
oceanSim(seed = 5050)
} # }