🐥 A Neural Network + Genetic Algorithm which plays Flappy Bird
A genetic algorithm with a neural network (fr3fou/gone) built on top of fr3fou/flappy-go which plays flappy bird.
Here's a video on how it plays.
This project was greatly inspired by Daniel Shiffman and his series on Neuroevolution - Neuroevolution - The Nature of Code
- https://stackoverflow.com/questions/14011844/choosing-parents-to-crossover-in-genetic-algorithms
- https://www.youtube.com/watch?v=lu5ul7z4icQ&list=PLRqwX-V7Uu6Yd3975YwxrR0x40XGJ_KGO)
The core game is reused and built on top of using composition
type Game struct {
*flappy.Game
// extra fields only used from the AI
}
type Bird struct {
*flappy.Bird
// extra fields only used from the AI
}
500 birds in total are put in the game with the following architecutre of their brain
var BrainLayers = []gone.Layer{
{
Nodes: 5,
Activator: gone.Sigmoid(),
},
{
Nodes: 8,
Activator: gone.Sigmoid(),
},
{
Nodes: 2,
Activator: gone.Sigmoid(), // ideally should be softmax
},
}
There are 5 inputs, 4 of them described in the following image and the last 1 is the velocity of the bird
The 2 outputs determine whether the bird should jump or not
if output[0] > output[1] {
bird.Jump()
}
Their score is determined by how well they perform (how long they survive)
At the end of each generation, the best performing birds are to mate.
Using the crossoverOdds
and mutationRate
arguments to ai.New()
it is determined what % of the new population should be created using ONLY mutation or crossover AND mutation.
After a new population has been created, the process gets repeated.