Flappy Bird AI
I started by creating a playable game with human input to play flappy bird. After that basic testing, I planned on
training a population of computer-controlled birds too learn to play using evolution. I did this without using ML libraries because I felt like it :P (and I’ve never used python ML libs before).
To start, I created a simple neural network for each birds “brain”, each with 4 input nodes (y position, y position offset from next pipe, y velocity, and distance to next pipe), 6 hidden nodes, and one output node that determines whether the bird should jump that frame (>0.5 jump, <=0.5 do nothing). They each have their own weights that determine
A bird has a fitness score that is affected by time survived, distance from center of next pipe gap, and a bonus for passing through a pipe (this score is how birds are sorted for parent selection). I used a population of 100 birds and a simple genetic algorithm for each generation where the top few birds create offspring based on their brain’s weights, plus a little mutation for randomness & minor improvement. This allows for future generations’ weights to improve over time.
One of the hardest parts to figure out was the fitness calculation. Initially, the birds had a relatively small reward for passing a pipe, so compared to the amount they got for just surviving, they didn’t actually learn to pass through the pipes and would instead just hover around the gap height without learning to thread the gap. I then made the bonus much larger which actually helped train better birds.
Finally, when I was trying to get this working in the browser using pygbag to compile it to WebAssembly, I got a blank gray screen with NO errors O_O. I spent over an hour trying any fix I could find, and finally came to the conclusion that numpy was crashing the web assembly, and so I had to rewrite the neural network into base python. ToT
Anyways, the results are pretty great! While I was training it once, I got a top score of ~9400 after 700 generations, but I forgot to screenshot it :(.
Try the project here (no download necessary!) Press left/right to speed up/slow down
Github Repo
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.