Whale: Whales in an Ocean

WhaleR Documentation

Whales in an Ocean

Description

R6 Objects for modelling a whale. Female and Male inherit from whale. Use Female and Male if you want to provide custom lists of male and female whales when you instantiate an ocean.

Format

R6Class object.

Value

Object of R6Class with methods for simulation.

Boolean

sex of the resulting baby

Public fields

position

A numeric vector of length 2 giving the initial position. (Make sure that it's within the dimensions of the ocean.)

age

initial age of the whale

lifespan

lifespan of the whale

range

Distance at which a female can detect an eligible male.

maturity

Age of whale at which reproduction is possible.

stepSize

Number of units the whale moves in each generation.

Methods

Public methods


Method new()

initialize a 'Whale'.

Usage
Whale$new(
  position = NA,
  age = 3,
  lifespan = 40,
  range = 5,
  maturity = 10,
  stepSize = 5
)
Arguments
position

position of whale

age

age of whale

lifespan

lifespan of whale

range

range of whale

maturity

how old whale must be in order to mate

stepSize

how far whale moves in one step

Returns

a new 'Whale' object


Method move()

move a whale

Usage
Whale$move(dims, r = self$stepSize)
Arguments
dims

dimensions of the ocean

r

how far the whale wants to move


Method clone()

The objects of this class are cloneable with this method.

Usage
Whale$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Super class

bcscr::Whale -> Male

Public fields

sex

sex of the whale

Methods

Public methods

Inherited methods

Method clone()

The objects of this class are cloneable with this method.

Usage
Male$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Super class

bcscr::Whale -> Female

Public fields

sex

sex of the whale

timeToFertility

how long until whale can breed

infertilityPeriod

number of steps until a female can breed again after having borne a child

Methods

Public methods

Inherited methods

Method maleNear()

determine whether a male is near

Usage
Female$maleNear(males, dist)
Arguments
males

the males in the ocean

dist

the distance-fidning function


Method mate()

make female whale mate

Usage
Female$mate()

Method clone()

The objects of this class are cloneable with this method.

Usage
Female$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Author(s)

Homer White homerhanumat@gmail.com

Examples

## Not run: 
initialMales <- vector(mode = "list", length = 10)
ages <- c(rep(3, 5), c(rep(10, 5)))
for (i in 1:10) {
  initialMales[[i]] <- Male$new(
    position = runif(2, min = 0, max = 100),
    age = ages[i],
    lifespan = 40,
    range = 12,
    maturity = 10,
    stepSize = 7
  )
}
initialFemales <- vector(mode = "list", length = 10)
for (i in 1:10) {
  initialFemales[[i]] <- Female$new(
    position = runif(2, min = 0, max = 100),
    age = ages[i],
    lifespan = 40,
    maturity = 10,
    range = 12,
    stepSize = 3
  )
}

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(males = initialMales, females = initialFemales, seed = 5050)

## End(Not run)

homerhanumat/bcscr documentation built on Dec. 18, 2024, 9:29 a.m.