Professional Documents
Culture Documents
Performance Analysisof Flappy Bird Playing Agent Using Neural Networkand Genetic Algorithm
Performance Analysisof Flappy Bird Playing Agent Using Neural Networkand Genetic Algorithm
net/publication/337187784
Performance Analysis of Flappy Bird Playing Agent Using Neural Network and
Genetic Algorithm
CITATIONS READS
4 3,085
3 authors, including:
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Selvakumar Kamalanathan on 16 January 2021.
Abstract. The aim of this paper is to develop and study an artificial intelligence
based game-playing agent using genetic algorithm and neural networks. We first
create an agent which learns how to optimally play the famous “Flappy Bird”
game by safely dodging all the barriers and flapping its way through them and
then study the effect of changing various parameters like number of neurons on
the hidden layer, gravity, speed, gap between trees has on the learning process.
The gameplay was divided into two level of difficulty to facilitate study on the
learning process. Phaser Framework was used to facilitate HTML5 program-
ming for introducing real-life factors like gravity, collision and Synaptic Neural
Network library was used to implement neural network so as to avoid creating a
neural network from scratch. Machine Learning Algorithm which we have
adopted in this project is based on the concept of Neuro-Evolution and this form
of machine learning uses algorithms which can evolve and mature over time
such as a genetic algorithm to train artificial neural networks.
1 Introduction
The main aim of this work is to develop a Genetic Algorithm based approach for
constructing an Artificial Intelligence based agent which can play the Flappy Bird game
and improve upon itself, thereby, ultimately beating any person.
Flappy Bird is an ever-engaging game developed by Vietnamese video game artist
and programmer Dong Nguyen, under his game development company dotGears [1].
The gameplay action in Flappy Bird can be viewed from a side-view camera angle and
the on-screen bird can flap to rise against the gravity which pulls it towards the ground.
The agent is supposed to learn when to flap so as to avoid collision. Passing through
each obstacle-gap leads to the increment in score while collision leads to instant death.
There are various factors which can affect the difficulty of the gameplay and ulti-
mately make it more challenging for the agent to learn and decide when to flap and
when not to flap. Our work divided the gameplay into two levels: Easy and Difficult. In
the Easy Level, the velocity of the obstacle coming towards the agent was kept at 200,
gravity at 2000 and gap between the upper obstacle and lower obstacle, that is, the gap
for the agent to flap through as 130. While in the Difficult Level, the gameplay was
made harder by tweaking the above parameters with the obstacle velocity increased to
twice the original value, gravity increased to 2500 so as to force the agent to flap more
and the gap was reduced by 10 units.
In the Easy Level, we observed that the agent apparently became immortal after a
certain number of generations which forced us to make the gameplay harder and study
the results. Though it is worth mentioning that the idea of immortality of the agent is an
idea assumed by the authors as they observed agents surviving for as long as 15 min
but the notion of their death at the 16th min cannot be denied. For simplicity, a score of
150+ made the agent fall under the category of immortal and optimal agent.
Achieving a score of more than 150 was observed to be very difficult and almost
far-fetched in the Difficult level and therefore, the study was conducted up to 30
generations.
2 Theory
2.1 Genetic Algorithm
Genetic Algorithms are adaptive heuristic search algorithms which support the organic
process ideas of selection and genetic science. Intrinsically they represent an intelligent
exploitation of a random search space accustomed to solve improvement issues and
optimization problems. Though irregular, these are by no means random, instead they
exploit historical knowledge and information to direct the search into regions of higher
and improved performance within the search space. The fundamental techniques are
designed to mimic and simulate processes which lead to evolution in nature, they
particularly follow the principles first laid down by Charles Darwin. Since in nature,
competition among living beings for scanty resources leads to the fittest being domi-
nating over the weaker ones.
Some important terms in Genetic Algorithm and our and their role in our work are
discussed below:
Search Space: Aset of individuals is maintained within a search space. Individuals are
like chromosomes and the entries in that vector represent genes. The fitness function
defines the competing ability of an individual. The main idea is to create a new
individual which is carry the best characteristics of the parents.
Selection: The most fit individual is chosen and passed on to the next generation. In
our implementation, the top 4 fit values pass on to the next generation without any
hinderance.
Cross Over: The portion of the string is exchanged from the individuals which are
selected due to highest level of fitness from the rest. The string is selected randomly for
this purpose. The idea behind this is to introduce randomness while maintain the
characteristics of the fittest individuals.
Performance Analysis of Flappy Bird Playing Agent Using Neural Network 255
Mutation: Our project has set the mutation to 0.2 to introduce randomness. Mutation
is done on a very limited scale to flip the values of bits. This ensures that the generation
is always changing and not stuck at some undesirable point.
3 Implementation
We reward a bird by adding its travelled distance and penalize it by subtracting its
current distance to the closest gap. Subtracting distance to the closest gap from the total
distance travelled also eliminates the significance of the barrier gap, i.e. the distance
between each pair of obstacles (Fig. 1).
Fig. 1. Fitness function is total distance travelled minus distance to the closest gap
4 Results
generation so that graphical comparison with other variants of hidden layer can be done
(Figs. 2 and 3).
In Easy Level, the velocity of trees was kept to 200, gravity was set to 2000 and gap
was set to 150. Effect of the number of neurons in the hidden layer were observed
(Tables 1, 2, 3 and 4).
40000
30000
Fitness Function
20000
10000
0
1 2 3 4 5 6 7 8 9 10 11
Generation
-10000
160
140
120
100
Fitness Function
80
60
40
20
0
1 2 3 4 5 6 7 8 9 10 11
Generation
Table 5. (continued)
One (1) Neuron in Hidden
Layer
Generation Fitness Score
8 170.95 0
9 256.27 0
10 140.0 0
11 206.65 0
12 258.07 0
13 202.92 0
14 95.45 0
15 157.68 0
16 259.87 0
17 288.04 0
18 562.28 0
19 585.81 0
20 563.07 0
21 555.40 0
22 556.35 0
23 3432.81 10
24 17887.81 57
25 4036.65 12
26 2208.92 6
27 2519.74 7
28 13230.75 42
29 21820.14 70
30 26118.90 84
Table 6. (continued)
Five (5) Hidden Layer
Generation Fitness Score
10 14167.36 45
11 27619.94 89
12 26435.12 85
13 38383.66 124
14 23044.10 74
15 21832.11 70
16 20903.82 67
17 29448.02 95
18 8960.55 28
19 42077.80 136
20 6466.81 20
21 3123.06 9
22 7716.37 24
23 8946.76 28
24 26723.05 28
25 7730.05 24
26 39620.36 128
27 5280.06 16
28 15092.64 48
29 9873.50 31
30 17835.71 47
Table 7. (continued)
Ten (10) Hidden Layer
Generation Fitness Score
13 18446.62 59
14 2518.85 7
15 15079.15 48
16 35022.63 113
17 988.86 2
18 1585.55 4
19 21519.06 69
20 31339.33 101
21 28586.50 92
22 20607.40 66
23 15088.47 48
24 6495.66 20
25 11408.49 36
26 31341.45 101
27 7716.02 24
28 3433.39 10
29 16313.71 52
30 18446.62 59
Table 8. (continued)
Twenty (20) Hidden Layer
Generation Fitness Score
17 8654.58 27
18 2859.48 8
19 4358.95 13
20 3745.56 11
21 10796.09 34
22 13887.94 44
23 3768.68 11
24 5620.93 17
25 6813.21 21
26 4086.47 12
27 8675.23 27
28 16634.58 53
29 15086.20 48
30 12023.61 38
45000
40000
35000
30000
Fitness Function
25000
20000
15000
10000
5000
0
1 6 11 16 21 26
Generation
One Neuron Five Neuron Ten Neuron Twenty Neuron
160
140
120
100
80
Score
60
40
20
0
1 6 11 16 21 26
Generation
One Neuron Five Neuron Ten Neuron Twenty Neuron
5 Conclusion
It is evident from the results achieved that five neurons in the hidden layer is the best
setting for both easy and difficult level of gameplay for the Flappy Bird game playing
agent. The agent with five neurons in the hidden layer was able to score 150 points in
the fourth (4th) generation itself while the second-best performer, agent with one
neuron in the hidden layer, could only achieve this fete in the seventh (7th) generation.
Moreover, it is worth mentioning that the results obtained for one neuron agent was
highly unpredictable rendering it unreliable. Even in the case of difficult level, where
we only studied thirty (30) generations, the only agent to cross 35000 points of fitness,
that too thrice, was the agent with five (5) neurons in the hidden layer.
Hence it can be safely concluded that in the quest to balance computation speed,
resources, time and performance, an agent with five (5) neurons in the hidden layer is
the most optimum choice.
This work can be further improved by being more meticulous with the changes
made to the parameters which were changed to increase the difficulty of the gameplay
and providing a more comprehensive study on the more difficult gameplay by studying
more than thirty (30) generations. The same Flappy Bird playing agent can also be
developed using other methodologies like Q-Learning and a comparative study can be
done.
References
1. Rhiannon, W.: What is Flappy Bird? The game taking the App Store by storm. The Daily
Telegraph. January 30 (2014)
Performance Analysis of Flappy Bird Playing Agent Using Neural Network 265