λ
home posts uses projects

Flappy AI

Apr 26, 2020

golang neural-networks genetic-algorithm

A genetic algorithm with a neural network (fr3fou/gone) built on top of fr3fou/flappy-go which plays flappy bird.

How it works

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
}

AI Architecture

500 birds in total are put in the game with the following architecture 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 in the following diagram and the last 1 is the velocity of the bird. α,β,γ,δ\alpha, \beta, \gamma, \delta describe the position of the bird relative to the next pipe and world.

flappy-ai.svg

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 rate 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.

LλBS

All systems up and running

Commit 1eafc4a, deployed on Jul 24, 2024.