Machine Learning for Flappy Bird using Neural Network & Genetic Algorithm

Read the complete tutorial about how to implement a machine learning algorithm for the Flappy Bird video game here:

This video shows a creation of an artificial intelligence controller for the Flappy Bird game using neural networks and a genetic algorithm.

The program is written in HTML5 using Phaser framework (). There is also used Synaptic Neural Network library () to implement entire artificial neural network instead of making a new one from the scratch.

Download source code here:

—————————————————————–

According to Arthur Samuel, machine learning is the science of getting computers to act without being explicitly programmed. It is a fine tuning process of learning that incrementally improves an initial random system.

The form of machine learning implemented in this program uses a genetic algorithm to train artificial neural networks.

The birds are learning how to flap optimally in order to fly safely through barriers as long as possible.

The main concept is based on these 3 steps:
1. creating the initial population of 10 birds randomly
2. learning as the game is being played
3. applying natural evolution to form the next improved population

—————————————————————–

To play the game, each bird has its own neural network consisted of the next 3 layers:

1. an input layer with 2 neurons representing what a bird sees:
– horizontal distance to the closest gap
– height difference to the closest gap

2. a hidden layer with 6 neurons

3. an output layer with 1 neuron to perform flap if its value is greater than 0.5

—————————————————————–

To measure the quality of birds and select the best ones, for each bird is calculated its fitness function in this way:
– reward a bird by its total traveled distance
– penalize a bird by its current distance to the closest gap

When the entire population is dead, the fittest four birds are selected to breed a new population by using genetic algorithm operators: selection, crossover and mutation.

—————————————————————–

Visit us:

Like us:

Follow us:

Music:
“Bedtime Tune” by Jay Man

30 comments

  1. Michael C. says:

    I dont unterstand why you would want to penalize the distance to the next gap if you already accounted for the total distance, someome please explaib this

  2. DivineDigitalProductions says:

    So you generate a population of neural networks. what values of the best-fit networks are you passing over? is it the weight values for the synapses? Assuming all input, hidden and output neurons stay the same as you stated.

  3. Guy Smith says:

    For everyone complaining that Flappy Bird is a frustratingly hard game, keep in mind that a neural network with 9 neurons and 18 synapses can play the game flawlessly.

  4. Max Kölbl says:

    This is a very cute idea and it’s certainly a good project for a beginner, but I’d like to be honest: it’s completely overpowered. Genetic algorithms are useful when you want to make fundamental changes to your network, such as changing its topology, but for a small network with a fixed topology, you could have used a regular learning algorithm which traverses search space a lot more precisely like gradient descent. But actually you don’t need a neural network for flappy birds at all. The birds movement describes a quadratic function and you could just implement an algorithm to compute these.

    But I don’t want to ruin the fun! Maybe you like my ideas and try them out! If you do the quadratic functions thing, PLEASE upload another video!

  5. DayB89 says:

    Not the best way to train a NN, but fun to watch! Very well illustrated! Maybe you could try something similar but using reinforcement learning. You could even try more complex games 🙂

  6. Alan Zhu says:

    I don’t see anything wrong with the audio tho. Can you do this with CNN image processing? So the computer only sees the whole image instead of calculated distance.

  7. Tobi says:

    After experimenting with this, the results are very misleading. The “Brain” simply flaps when bird goes over a certain distance from nearest pipe. This could have been hard coded in without a neural network at all. In fact, a network with the structure [1, 1, 1] converges fastest, because you’re not doing unnecessary work. The network is simply multiplying the number, and then the hardcoded:

    IF (NN-Output > 0.5) THEN flap();

    does the actual work.

    This could be replaced with

    IF (birdDistanceFromPipe > bird.height + birdInitialFlapSpeed) THEN flap();

  8. Burris Tech Show says:

    Hello. I am very desperate for this, could you please help me design an AI learning program for the game Osu! ?? I could pay you for your time if necessary, I really just need code to work with and learn from through experimentation to get started with machine learning, and Osu! is the perfect game as there are only two goals: survive as long as possible and get the highest score possible

  9. Clash Royale Crown Championship Highlights says:

    Wow nice one but for learning this game should all levels should be same or different

Comments are closed.