world: R6 class representing the world in which the moths live

Description Details Public fields Methods Examples

Description

R6 class representing the world in which the moths live

Details

Just like moths, the world has a colour either 0 (white) or 1 (black). During a simulation, the colour of the world will change with a frequency defined by the attribute 'period'.

Public fields

time

Integer. Number of time steps that have passed in the world.

colour

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

period

Integer. Number of time steps after which colour of world changes.

Methods

Public methods


Method new()

Initialize new R6 object of class 'world'.

Usage
world$new(period = 100)
Arguments
period

Integer. Number of time steps after which colour of world changes.

Returns

An R6 object of class 'world'


Method computecolour()

Change colour of the world.

Usage
world$computecolour()
Details

World will change colour at a given frequency specified by the attribute 'period'.

Examples
#Create a world that should change colour every 50 timesteps
set.seed(123L)
my_world <- world$new(period = 50)

#Colour of world starts as black (i.e. 1)
my_world$colour

#Once time reaches 50, colour will change to white (i.e. 0)
my_world$time <- 50
my_world$computecolour()
my_world$colour

Method moveforward()

Advance time by 1.

Usage
world$moveforward()
Details

Time of the world will move forward by 1, after which we check to see if the colour of the world should change given the attribute 'period'.

Examples
my_world <- world$new(period = 20)
#Starts with time 1
my_world$time

#Moves to time 2 after moveforward
my_world$moveforward()
my_world$time

Method clone()

The objects of this class are cloneable with this method.

Usage
world$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
my_world <- world$new(period = 50)


plot(NULL, ylim = c(0, 1), xlim = c(1, 1000), xlab = "time", ylab = "", axes = FALSE)
axis(1)

#Move the world forward 1000 time steps
#World will change colour every 50
for (t in 1:1000) {
  my_world$moveforward()
  abline(v = t, col = my_world$colour, lwd = 5)
}

## ------------------------------------------------
## Method `world$computecolour`
## ------------------------------------------------

#Create a world that should change colour every 50 timesteps
set.seed(123L)
my_world <- world$new(period = 50)

#Colour of world starts as black (i.e. 1)
my_world$colour

#Once time reaches 50, colour will change to white (i.e. 0)
my_world$time <- 50
my_world$computecolour()
my_world$colour

## ------------------------------------------------
## Method `world$moveforward`
## ------------------------------------------------

my_world <- world$new(period = 20)
#Starts with time 1
my_world$time

#Moves to time 2 after moveforward
my_world$moveforward()
my_world$time

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