Ocean | R Documentation |
R6 Object for simulating a population of whales.
R6Class
object.
Object of R6Class
with methods for simulation.
dimensions
A vector of length two giving the dimensions of the ocean.
males
A list of R6 objects of class Male containing the current population of male whales.
females
A list of R6 objects of class Female containing the current population of female whales.
malePop
Current number of males in the population.
femalePop
Current number of females in the population.
starveParameter
Helps determine probability for each whale to die by starvation in the current generation.
distance()
Compute distance between two whales.
Ocean$distance(a, b)
a
one whale
b
another whale
A number
new()
Create a new Ocean object.
Ocean$new(dims = c(100, 100), males = 10, females = 10, starve = 5)
dims
numerical vector of length 2 giving the dimensions of the ocean
males
initial number of males
females
initial number of females
starve
the starvation parameter
A new 'Ocean' object.
starvationProbability()
Compute the starvation probability.
Ocean$starvationProbability(popDensity)
popDensity
population density
a number.
advance()
advance the 'Ocean' one step.
Ocean$advance()
the 'Ocean' object.
plot()
plot the 'Ocean'.
Ocean$plot()
side effect
clone()
The objects of this class are cloneable with this method.
Ocean$clone(deep = FALSE)
deep
Whether to make a deep clone.
Homer White homerhanumat@gmail.com
## Not run:
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 ( animate ) ocean$plot()
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)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.