Description Public fields Methods Examples
R6 class representing population of multiple moths.
individuals
List. List of R6 objects of class 'moth'.
new()
Initialize new R6 object of class 'population'.
population$new(N = 100, mutation_rate = 1e-06, world)
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.
An R6 object of class 'population'
computefitness()
Compute fitness of all moths in the population.
population$computefitness()
Run the method 'computefitness' of each moth object in the population.
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)
mutation()
Change colour of all moths.
population$mutation()
Run the method 'mutation' of each moth object in the population.
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)
reproduction()
Moths reproduce with a probability defined by their fitness in the environment.
population$reproduction()
Offspring are clones of their parents (i.e. they have the same colour!)
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)
generation()
Run all demographic methods.
population$generation()
Compute fitness, mutation and reproduction for all moths.
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)
clone()
The objects of this class are cloneable with this method.
population$clone(deep = FALSE)
deep
Whether to make a deep clone.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.