moth: R6 class representing an individual moth

Description Public fields Methods Examples

Description

R6 class representing an individual moth.

Public fields

colour

Integer. Colour of the moth. Can be either 0 (white) or 1 (black)

mutation_rate

Probability. Probability of mutation in genes that control colour.

world

R6 object of class world. World in which moth lives.

fitness

Numeric. Fitness of individual in current world. Ranges between 0 and 1.

Methods

Public methods


Method new()

Initialize new R6 object of class 'moth'.

Usage
moth$new(mutation_rate = 1e-06, world)
Arguments
mutation_rate

Probability. Probability of mutation in genes that control colour.

world

R6 object of class world. World in which moth lives.

Returns

An R6 object of class 'moth'


Method mutation()

Change moth colour

Usage
moth$mutation()
Details

Moth colour will change with a probability defined by attribute 'mutation_rate'

Examples
#Moth with low mutation rate is very unlikely to change colour
set.seed(123L)
my_world <- world$new()
low_mutation_moth  <- moth$new(mutation_rate = 1e-10, world = my_world)
low_mutation_moth$colour
low_mutation_moth$mutation()
low_mutation_moth$colour

#Moth with mutation rate 1 is almost guaranteed to change colour
high_mutation_moth <- moth$new(mutation_rate = 1, world = my_world)
high_mutation_moth$colour
high_mutation_moth$mutation()
high_mutation_moth$colour

Method computefitness()

Compute fitness of individual in current environment.

Usage
moth$computefitness()
Details

Compare colour of moth to the colour of the world and update attribute 'fitness' accordingly. Fitness can be either 0.5 or 1.

Examples
set.seed(123L)
my_world <- world$new()
my_moth  <- moth$new(world = my_world)

#World is black (i.e. colour = 1) and the moth is white (i.e. colour = 0)
#Therefore, fitness is lower (0.5)
my_world$colour
my_moth$colour
my_moth$computefitness()
my_moth$fitness

#If the world were also white, moth fitness is 1.
my_world$colour <- 0
my_moth$computefitness()
my_moth$fitness

Method clone()

The objects of this class are cloneable with this method.

Usage
moth$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

 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
#Create a world in which the moth lives
example_world <- world$new()

#Create new moth object
example_moth <- moth$new(world = example_world)

#Moth has empty fitness initially
example_moth$fitness

#Compute fitness and then print it
example_moth$computefitness()
example_moth$fitness

## ------------------------------------------------
## Method `moth$mutation`
## ------------------------------------------------

#Moth with low mutation rate is very unlikely to change colour
set.seed(123L)
my_world <- world$new()
low_mutation_moth  <- moth$new(mutation_rate = 1e-10, world = my_world)
low_mutation_moth$colour
low_mutation_moth$mutation()
low_mutation_moth$colour

#Moth with mutation rate 1 is almost guaranteed to change colour
high_mutation_moth <- moth$new(mutation_rate = 1, world = my_world)
high_mutation_moth$colour
high_mutation_moth$mutation()
high_mutation_moth$colour

## ------------------------------------------------
## Method `moth$computefitness`
## ------------------------------------------------

set.seed(123L)
my_world <- world$new()
my_moth  <- moth$new(world = my_world)

#World is black (i.e. colour = 1) and the moth is white (i.e. colour = 0)
#Therefore, fitness is lower (0.5)
my_world$colour
my_moth$colour
my_moth$computefitness()
my_moth$fitness

#If the world were also white, moth fitness is 1.
my_world$colour <- 0
my_moth$computefitness()
my_moth$fitness

data-zoo-gang/ABMR6 documentation built on Dec. 19, 2021, 8:12 p.m.