# Training an agent to play Flappy Bird

My goal is to train a model to play an endless game such as Flappy Bird. I've seen demo videos where the author explains that they used a neural network and genetic algorithm to train the net.

I know how I can use genetic algorithm to find optimal solutions for cases where the input are limited, such as in this example: https://youtu.be/bGz7mv2vD6g

My question: how would I model the agent that would be playing the game so that it's know what to do based on its input at each frame? The input (in the case of Flappy Bird) would be the bird's vertical position, the next pipe's x position, the y position of the center of the opening on the next pipe's, and maybe the velocity at which the pipe is moving towards the bird. I can reason through how to push those inputs through the neural network, then determine how well the bird performed based on the output of the network (jump or don't jump).

Where I'm stuck now is modeling the bird's genes in this context.

Thanks!

• How's flappy bird doing after a year of training? :)
– Jim
Aug 1 '18 at 20:14
• You know... it not only mastered Flappy Bird, but also Angry Birds, Duck Hunt, and NBA Jam - when playing as Larry. Piece of cake. Aug 2 '18 at 3:58
• Did you follow the answer below? Or did it require other magic?
– Jim
Aug 3 '18 at 12:39

I'm not entirely sure what you mean with 'modeling the bird's genes'. The way I would go with this is use this javascript library which incoperates genetic algorithms and neural networks for you, so you don't have to program 'genes' yourself. It has a lot of mutation methods of which the basis comes from the NEAT algorithm. Then you could use this library for a flappy bird impementation in Javascript.'

So for the genes, take a look at the NEAT paper on how to modify those of a neural network. A genome consists of nodes and connection genes. Take a look at the mutation methods described above as an example on how to mutate the genome.

On the input/output:

What I would use as inputs:

• Distance to next pipe
• Y position of center
• Y position of bird

These inputs all have a limit, which makes it nice to work with. I think the speed is not very relevant as you'll always aim for the gap, regardless of speed.

The fitness function could look something like this (pseudocode):

function fitness(genome):
while(alive):
var action = genome.activate([dist, y_pipe, y_bird]);
flappygame.compute(action);

return score;


So basically, let each genome play until it dies - then return the score of the flappy bird game as a fitness.