Ocean: Whales in an Ocean

OceanR Documentation

Whales in an Ocean

Description

R6 Object for simulating a population of whales.

Format

R6Class object.

Value

Object of R6Class with methods for simulation.

Properties

  • 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: Computes distance between any two whales.

Methods

  • new: Instantiates an Ocean object. Parameters are:

    • dims: A numeric vector of length 2 setting the length and width of the ocean.

    • males: An integer giving the number of males (to be created with defaults) or a list of Male whale objects.

    • females: An integer giving the number of females (to be created with defaults) or a list of Female whale objects.

    • starve: A non-negative number, used to determine probability that an individual starves in a given generation. The larger the value, the lower the carrying-capacity of the population will be.

  • advance: Advances the simulation by one generation. Takes no arguments.

  • plot: Plots the current population. Takes no arguments.

Methods

Public methods


Method distance()

Usage
Ocean$distance(a, b)

Method new()

Usage
Ocean$new(dims = c(100, 100), males = 10, females = 10, starve = 5)

Method starvationProbability()

Usage
Ocean$starvationProbability(popDensity)

Method advance()

Usage
Ocean$advance()

Method plot()

Usage
Ocean$plot()

Method clone()

The objects of this class are cloneable with this method.

Usage
Ocean$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Author(s)

Homer White homerhanumat@gmail.com

Examples

## 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)

homerhanumat/bcscr documentation built on Jan. 14, 2023, 4 a.m.