Description Public fields Methods Examples
R6 class representing an individual moth.
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.
new()
Initialize new R6 object of class 'moth'.
moth$new(mutation_rate = 1e-06, world)
mutation_rate
Probability. Probability of mutation in genes that control colour.
world
R6 object of class world. World in which moth lives.
An R6 object of class 'moth'
mutation()
Change moth colour
moth$mutation()
Moth colour will change with a probability defined by attribute 'mutation_rate'
#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
computefitness()
Compute fitness of individual in current environment.
moth$computefitness()
Compare colour of moth to the colour of the world and update attribute 'fitness' accordingly. Fitness can be either 0.5 or 1.
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
clone()
The objects of this class are cloneable with this method.
moth$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 | #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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.