simulation models for economics world is not just black and white.pdf · the idea of using a neural...
TRANSCRIPT
Universita degli Studi di Torino
Corso di Laurea Magistrale in Matematica
Simulation Models for Economics
The world is not just black and white
02 September 2015
Gabriele Roggero, mat. 718639
Supervisor: Prof. Pietro Terna
1
Contents
1 The Model, the agents and the world 4
1.1 NetLogo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.1 Data Production . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.2 Training of the Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.3 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 The Code 6
2.1 Breeds and variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 The Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 The Hunting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 The Neural Network and its training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5.1 Construction of Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5.2 Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6 The Optimal Hunting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Simulations and Conclusions 12
3.1 Simulation 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Simulation 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.3 Simulation 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2
Introduction
The aim of this work is to simulate a prey-predator model where the presence of an infectious disease
in the prey population is assumed. Such disease divides prey into susceptible and infected and will alter
their vital functions such the movement and the capability to gain energy from food. This decreased
movement capability allows the predators to find easier the prey to catch, these prey will be the ones
which have traveled less than the others. Like almost every dynamical system concerning a prey-predator
models, the aim of each species is to find as soon as possible food resources to stay alive.
The starting point for our work are notes by D’Urso, Ferrari, Roggero (2015), that studies the model
with analytical techniques. We will use instead an ABMs technique to model and simulate a deep change
respect the original notes. On the notes mentioned above, the disease a↵ects the predator population by
altering their attack rate and their conversion e�ciency1, conversely, in our work, the disease infects the
prey population and consequently, after predation, the disease passes on that predator who ate an ill prey.
We start our simulation with free interactions between the two species in a bounded world and we will
store velocity, total distance and age of each prey who will be attacked by predators. After the produc-
tion of this data, we will train the neural network developed in R (using the NNet library) to predict
the possible successes (the prey selected for the hunting is healthy) and the possible failures (the prey
selected for the hunting is sick).
We will do supervised learning, so production of appropriate input data and collection of related outcomes
is as important as sensitive. So briefly, our data is a matrix with nrows (where n is the number of prey
hunted) and three columns (velocity, total distance and age of prey hunted) representing the hunting’s
story and as outcomes a binary classification (true stands for ill, false stands for healthy) of predators’
health. So our questions could be: Is it always a good deal hunt prey that has more di�culty moving
around?
One of the things that we learned from this work is that, as the title suggests, the world is not just black
and white. A mechanical use of a neural network as a systematic method for hunting is not plausible. This
is not plausible because in the animal world the species must be preserved in every possible way and with
all the adaptations necessary. If the predators will attack only via what the neural network predicted,
they will no more hunt an ill prey because they are reasoning like a robot and the prey population will be
gradually destroyed involving accordingly the predators population. Finally we come to the conclusion
that reasoning in a mechanical way (i.e. like a robot) would not lead to any remarkable results, but that
a fair alternation of what is right and what is wrong is the way to find the equilibrium that we are looking
for!
1Coversion e�ciency: the fraction of prey energy assimilated by the predator and turned into new predators.
3
1 The Model, the agents and the world
1.1 NetLogo
As we mentioned above, we built a prey-predator model where the prey population is structured. The
expansion of the disease is neither vertical nor horizontal but every step, randomly, the disease attacks
some healthy prey. The predation occurs when a predator finds a prey in his attack-radius. If several
prey are in the attack-radius of the same predator then this one will attack the prey with the less total
distance, where for total distance we mean the distance traveled by a prey from the beginning of her life.
Let’s explain what happens when a prey and a predator become ill. An ill prey has a lower mobility
because has a slower velocity and hence she can do small movements while an ill predator has a decreased
conversion e�ciency. For instance by hunting the same prey (ill or healthy is indi↵erent) an ill predator
gains less energy then an healthy one. This decreased conversion e�ciency is related with the virulence
of the disease.
Prey at every step move, eat grass, that ramdomly regrows, reproduce themselves and eventually die.
In the same way, predators move, hunt prey, reproduce themselves and so on. The dynamic is really
interesting because predators will always attack prey which are moving less than the others (we will
explain later why this assumption is admissible in a prey-predator dynamics) but this prey could have
traveled few distance because it is just born or maybe it is simply infected.
The idea of using a neural network stems from our desire to simulate a kind of generational learning that
could simulate even better the techniques of hunting and selection of the most advanced predators.
Figure 1: A framework of our NetLogo interface
1.2 R
We have chosen R to simulate our neural network for two reasons: the first is that there exist the
NNet library, a dedicated neural network library and the second is that there exist the NetLogo-Rserve-
Extension extension that provides an easy and stable connection between the two ambients.
1.2.1 Data Production
The production of the data to train the neural network is been maybe the most di�cult aspect of our
work. Given the di�culty of the project, a correct or uncorrect data production could lead us to a
completely implausible scenario. We decided that three most significant components to train the network
are:
• velocity (vel): the velocity of a prey, i.e. the maximum distance she can do during a single tick of
NetLogo;
• total distance (total distance): the total distance that a prey has traveled since she was born;
4
• age (age): the age of a prey.
Let’s give an explanation of these choices. When a predator meets a prey (i.e. a prey is in his attack-
radius) all the informations it has are in fact velocity, total distance and age of that prey. In order to
gain the maximum advantage and the minimum cost of the predation, it’s reasonable that the predator
will attack the prey with the lower total distance. The age instead is useful to balance and improve the
learning of the net.
1.2.2 Training of the Net
After production of the data, we have to train the neural network to predict if, given a prey’s profile,
attack the choosen prey will be an optiman choice.
1.2.3 Prediction
After the training, we can start the ”hunting with cognition”. At every NetLogo’s tick when a predator
selects a potential prey, the network will predict the output of an hypothetical attack using the prey’s
profile (a vector of three components: velocity, total distance and age) so the predator will act accordingly.
5
2 The Code
The best way to understand our work is to present the most significant pieces of NetLogo and R code
and comment them in detail.
2.1 Breeds and variables
First of all, we start as usual in NetLogo, by declaring all the breeds and their own variables and global
variables:
Figure 2: Preying and predatory species and the variables
In the following table1 we can summarize the variables we used:
Prey
energy energy that every prey has: energy is
gained by eating (grass) and is lost by moving.
disease variable that specifies if a prey is infected or not.
total distance total distance that a prey do. It depends by
vel (velocity) of that prey.
vel velocity of the prey. It depends by
disease of that prey.
birth tick support variable to calculate the age of the prey.
age age of each prey.
Predators
energy energy that every predator has: energy is
gain by eating (prey) and lost by moving.
disease variable that specifies if a predator is infected
or not.
target vel velocity of those prey selected
aby predators.
target total distance distance of those prey selected by predators
done during their whole life.
target age distance of those prey selected by predators.
Global variables
dist distance between predator and selected prey.
coeff support variable for the detection of the disease.
list of vel list (vector) containing velocities of each
selected prey.
list of total distance list (vector) containing distances of each
selected prey.
list of ages list (vector) containing ages of each
selected prey.
OutputData list (vector) of 0 and 1 representing predator’s health
(additional infos will be gave, don’t worry!)
InputData prediction list (vector) of profile of each selected prey.
Predictions prediction of neural network about the attack
of selected prey.
aFor ”selected” we mean that prey that is selected between several prey to be hunted. We will call this prey as
potential prey in danger.
Table 1: Explanation of all variables
6
2.2 The Setup
The core procedures of our work are the setup and the go. Let’s start explaining the first one:
• Initializine the five vector OutputData, list of vel, list of total distance, list of ages,
InputData prediction as null vector;
• Set the world’s paths as green, the prey’s shape like rabbit and the predators’ shape like wolf;
• Create prey and predators setting their own starting energy;
• Set the prey’s variables and set the predators as healthy.
• Run the prey infection procedure which consist in set randomly, with a uniform distribution, the
disease equal to one.
Figure 3: The setup procedure
2.3 The Go
The go procedure is fundamental as well because in this procedure the learning process is formalized.
Given the amount of data produced at each iteration of the process, it has chosen as convention to train
the neural network in a range between 1�200 selected from the slider learning-threshold. We thus have
two section divided by the threshold: during the first half we accumulate data about interactions between
predators and prey, reached the threshold value we pass them to the neural network for the learning and
during the second half on forward predators will use the network’s prediction to hunt only healthy prey.
As it regards the ifelse control statement we refer to the chapter Simulations and Conclusions3. Before
and after the threshold the action’s structure is almost the same as we can see in figure4, but we have
to analyze it well. For the prey the structure remains the same: we will ask them to move, to eat, to
have a chance of getting sick if they are healthy or to heal if they are sick, maybe reproduce themselves,
to die in case of energy equal to zero or otherwise to increase their age. For the predators is slightly
di↵erent. Indeed before the threshold we will ask them to move, to hunt, possibly reproduce themselves
and eventually die in case of energy equal to zero. After reached the threshold, i.e. after that the decision
making process is ready, we will ask them to move, to hunt with cognition and so on.
7
Figure 4: The go procedure
After this brief excursion through the setup and the go, we go further into the procedures of our simu-
lation. Since the procedures move, eat, reproduce, death, increment age and NewGrass are important
but not so significant as to be explained here in detail, please consult the Netlogo reference code.
2.4 The Hunting
We explain now the procedure that governs the hunting, one of the most important of our work (figure 5).
As we mentioned above, there are two types of hunting, the pre-threshold hunting and the post-threshold
cognitive hunting. We can call the first type of hunting the wild hunting because predators will always
attack che prey with the minimun total distance. This is not reductive, is a simplification, but not
an extreme simplification because a predator can realize how far a prey has come (it may have followed
her or it can see her traces) and hunt the prey with less road traveled means that these two cases could
occur:
• the prey has traveled a little way because she is sick;
• the prey has traveled a little way because she is newly born, she is a puppy.
In both cases, this kind of predation is the most simple, but only in one case is the most optimal.
If it happens the first situation, i.e. the prey is sick, the energy of predator decreases according to the
formula:
8
energy = energy - energy * virulence-of-disease - dist
moreover his health changes from healthy to sick and his color become grey.
If it happens the second situation, i.e. the prey is healthy, the energy of predator increases according to
the formula:
energy = energy + predator-gain-from-food - dist
This procedure is important as well because within it we build the bases to the learning. Indeed at every
interaction between a predator and a prey, the prey’s profile is stored in three di↵erent lists (or vectors):
• list of vel contains the velocity of every prey that has been hunted;
• list of total distance contains the total distance of every prey that has been hunted;
• list of ages contains the age of every prey that has been hunted.
These three lists will build the matrix of input data for train the network. Finally we build the
OutputData, i.e. a list of zero and one depending on what we obtain from hunting: one if the the
prey was sick and zero if the prey was healthy.
For a detailed discussion, we note also that it is an healing is contemplated for the predators: their energy
must exceed an energy threshold (for instance 25).
Figure 5: The hunting procedure.
9
2.5 The Neural Network and its training
Finally we are arrived at the neural network! As we mentioned above, reached the threshold, the matrix
of the input data and the vector of the output date are sent to R to execute the training of the net.
2.5.1 Construction of Input and Output
The three vectors containing the prey’s profiles are already stored. We need to build a dataframe to work
with R and this is the way we took:
Figure 6: Construction of the Input matrix.
while the output vector is converted to a matrix to simplify its use:
Figure 7: Construction of the Output matrix.
2.5.2 Training
To achieve a result as accurate as possible (with the resources at our disposal), it was decided to train the
neural network tenfold and take only the one with the minimum error. We stored so our trained neural
network in the variable bestNet.
Figure 8: Training of the Net.
2.6 The Optimal Hunting
After all these required parts we are able to deal with the funniest section of our work: the smart
hunting. Thanks to the training of the Net, now when a predator selects a prey, realtime the prey’s
profile (i.e.:[vel, total distance, age]t) is processed by the bestNet and a prediction is produced. Such
prediction will be zero if from its profile the prey was classified as healthy, otherwise it will be one. Now
it’s easy to avoid being hunted a sick prey.
10
Figure 9: The hunting process conditioned by the Neural network.
11
3 Simulations and Conclusions
We will exploit this section to briefly explain for a not so expert user how to manage and launch the code
to make some experiment on his own. The steps are as follows:
1. open an R session and write the two commands that are reported in the NetLogo interface;
2. set the sliders of the variables as you prefer;
3. press the button Connecting with Rserve. If the connection is successful a positive message appears
on Command Center (similarly the adjacent monitor print an error message)
3. press the setup button and then the go one to start the simulation;
4. press the button Disconnecting from Rserve to stop the connection between the two environments.
After this digression, we return to our simulations. First of all a remark is needed: before implementing
the neural network, all simulations were really di�cult because the system tended to degenerate very
quickly and often leading to the extinction of the predators. Only some settings of parameters, very
laboriously found, have led to a kind of equilibrium2. This is obviously in agreement with what was
carried out analytically in the notes we mentioned in the introduction.
After implementing the neural network, we noticed how the evolution of the environment was much less
sudden, but still degenerative and again no more significant from our point of view. The way to fix this
problem is enclosed in the phrase we wrote in the Introduction: a fair alternation of what is right and
what is wrong is the way to find the equilibrium that we are looking for!
So we decided to add a new controll in the go procedure that verify if a density condition is satisfied.
Indeed the following condition for us if doesn’t represent the conservation of species at least prevents that
predators act against their instincts:
hunting with cognition () count prey
world-width ⇤ world-height � 4 ⇤ count predators
world-width ⇤ world-height
This formula can be read as:
The smart hunting is admissible if and only if the density of prey is at least four times the density of predators.
We decided as convention to impose this multiplier to protect the environment from extinctions not plau-
sible and unnatural.
We report below10 the salty part of the go procedure.
2For equilibrium we mean a not trivial equilibrium. What we are looking for is a kind of dynamic that permits the
coexistence of the two species.
12
Figure 10: The density control in the go procedure.
3.1 Simulation 1
The first simulation is characterized by a rather high virulence of the disease, and we set the training
threshold at 100 ticks. After running the dynamic till ticks 350 we have a situation not so di↵erent from
the beginning one. We can see from the plot as the oscillation of the population of prey is very strong
and that follows the phases of hunting of the predators.
Figure 11: Oscillations of the populations at ticks 350.
Running the simulation till ticks 441 we have the completely extintion of predators:
13
Figure 12: Extintion of predators at ticks 441.
14
15
3.2 Simulation 2
The second simulation is characterized by a rather low virulence of the disease, and we set the training
threshold at 100 ticks. This is a completely favorable situation where predators have a so low virulence
rate that their reproduction remains almost unchanged and we have the completely extinction of prey
even before starting the neural learning.
Figure 13: Extintion of populations nearby ticks 50.
16
Figu
re14:
Extintion
ofpop
ulation
snearby
ticks50.
17
3.3 Simulation 3
The third simulation is characterized by a really high virulence of the disease, and we set the training
threshold at 100 ticks. After running the dynamic till ticks 201 we have a situation quite interesting. We
can see that although there is an huge amount of prey, predators cannot grow in number because there
are too many infected prey and their hunting becomes impossible. From this simulation we can observe
that there is a breakpoint where passed this one the dynamic is predictable looking at the plot of the
populations.
Figure 15: Oscillations of the populations at ticks 201.
18
19
20
References
[1] D’Urso, E., Ferrari, G., Roggero, G., 2015. An eco-epidemiological model of diseased predators with
di↵erential catch rates. Notes.
[2] Oliveira, N.M., Hilker, F.M., 2010. Modelling disease introduction as biological control of invasive
predators to preserve endangered prey. Bulletin of Mathematical Biology 72, 444-468.
21