population: R6 class representing population of multiple moths

Description Public fields Methods Examples

Description

R6 class representing population of multiple moths.

Public fields

individuals

List. List of R6 objects of class 'moth'.

Methods

Public methods


Method new()

Initialize new R6 object of class 'population'.

Usage
population$new(N = 100, mutation_rate = 1e-06, world)
Arguments
N

Integer. Number of individual moths in the population.

mutation_rate

Probability. Probability that moths can change colour in a timestep.

world

R6 object of class world. World in which moths live.

Returns

An R6 object of class 'population'


Method computefitness()

Compute fitness of all moths in the population.

Usage
population$computefitness()
Details

Run the method 'computefitness' of each moth object in the population.

Examples
set.seed(123L)
my_world <- world$new()
my_pop   <- population$new(N = 50, world = my_world)

#Fitness of all moths is NA on initialisation
my_pop$individuals[[1]]$fitness

#Compute fitness of all moths
my_pop$computefitness()

#Plot fitness of moths in the world
all_fitness <- sapply(my_pop$individuals, function(x){x$fitness}) 
hist(all_fitness)

Method mutation()

Change colour of all moths.

Usage
population$mutation()
Details

Run the method 'mutation' of each moth object in the population.

Examples
set.seed(123L)
my_world <- world$new()
my_pop   <- population$new(N = 50, mutation_rate = 0.75, world = my_world)

#Colour of moths should be ~equal at start
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

#Change colour of all moths with probability defined by `mutation_rate`
#Colours have changed
my_pop$mutation()
all_colour <- sapply(my_pop$individuals, function(x){x$colour}) 
hist(all_colour)

Method reproduction()

Moths reproduce with a probability defined by their fitness in the environment.

Usage
population$reproduction()
Details

Offspring are clones of their parents (i.e. they have the same colour!)

Examples
set.seed(123L)
my_world <- world$new()
my_pop   <- population$new(N = 50, world = my_world)

#At initialization colour of moths is ~even
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

#The world is black (i.e. colour = 1)
#So black moths will have higher fitness than white moths
my_world$colour
my_pop$computefitness()

#Black moths have higher fitness and more more likely to reproduce
#After reproduction, black individuals are much more common
my_pop$reproduction()
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

Method generation()

Run all demographic methods.

Usage
population$generation()
Details

Compute fitness, mutation and reproduction for all moths.

Examples
set.seed(123L)
my_world <- world$new()
my_pop   <- population$new(N = 50, world = my_world)

#At initialization colour of moths is ~even
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

#The world is black (i.e. colour = 1)
#So black moths will have higher fitness than white moths
my_world$colour
my_pop$generation()

#There will be more black moths in the next generation
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

Method clone()

The objects of this class are cloneable with this method.

Usage
population$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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 set.seed(2L)
 my_world <- world$new()
 my_population <- population$new(N = 4, world = my_world)
 my_population$individuals[[1]]$colour <- 1
 my_population$computefitness()
 print(unlist(lapply(my_population$individuals, function(i) i$colour)))
 my_population$reproduction()
 print(unlist(lapply(my_population$individuals, function(i) i$colour)))

## ------------------------------------------------
## Method `population$computefitness`
## ------------------------------------------------

set.seed(123L)
my_world <- world$new()
my_pop   <- population$new(N = 50, world = my_world)

#Fitness of all moths is NA on initialisation
my_pop$individuals[[1]]$fitness

#Compute fitness of all moths
my_pop$computefitness()

#Plot fitness of moths in the world
all_fitness <- sapply(my_pop$individuals, function(x){x$fitness}) 
hist(all_fitness)

## ------------------------------------------------
## Method `population$mutation`
## ------------------------------------------------

set.seed(123L)
my_world <- world$new()
my_pop   <- population$new(N = 50, mutation_rate = 0.75, world = my_world)

#Colour of moths should be ~equal at start
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

#Change colour of all moths with probability defined by `mutation_rate`
#Colours have changed
my_pop$mutation()
all_colour <- sapply(my_pop$individuals, function(x){x$colour}) 
hist(all_colour)

## ------------------------------------------------
## Method `population$reproduction`
## ------------------------------------------------

set.seed(123L)
my_world <- world$new()
my_pop   <- population$new(N = 50, world = my_world)

#At initialization colour of moths is ~even
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

#The world is black (i.e. colour = 1)
#So black moths will have higher fitness than white moths
my_world$colour
my_pop$computefitness()

#Black moths have higher fitness and more more likely to reproduce
#After reproduction, black individuals are much more common
my_pop$reproduction()
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

## ------------------------------------------------
## Method `population$generation`
## ------------------------------------------------

set.seed(123L)
my_world <- world$new()
my_pop   <- population$new(N = 50, world = my_world)

#At initialization colour of moths is ~even
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

#The world is black (i.e. colour = 1)
#So black moths will have higher fitness than white moths
my_world$colour
my_pop$generation()

#There will be more black moths in the next generation
all_colour <- sapply(my_pop$individuals, function(x){x$colour})
hist(all_colour)

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