Mastering the Flappy Bird Game with the NEAT Algorithm

Tejashvi Singh
3 min readMay 3, 2024

Introduction

NeuroEvolution of Augmenting Topologies (NEAT) is a genetic algorithm that evolves artificial neural networks. It was developed by Kenneth O. Stanley as a method to evolve increasingly complex neural networks from simple initial structures. NEAT is distinctive because it evolves not only the weights of the connections but also the structures of the networks themselves, optimizing both topology and parameters. This powerful approach can be applied to various challenges, including control problems, reinforcement learning tasks, and games like Flappy Bird.

Working of NEAT

The NEAT algorithm begins with a population of simple neural networks and then applies genetic algorithms to breed new networks. It uses three key techniques:

1. Crossover: A genetic operator used to combine the genetic information of two parents to generate new offspring.
2. Mutation: This can occur in several ways, such as by adding new neurons, adding new connections, or modifying the weights of existing connections.
3. Speciation: NEAT groups similar topologies into species based on genetic similarity. This prevents the fittest networks from dominating the breeding process too early and encourages diversity in the solutions.

Each generation of networks is tested, and the best-performing networks breed to produce the next generation, gradually evolving more capable neural networks through this process.

Architecture

NEAT’s architecture involves several key components:

Genomes: The encoding of a neural network as a collection of genes, each representing connections between neurons, along with innovation numbers to track their historical origins.
Species: Groups of similar genomes, which allow for focused competition and preservation of innovative structures.
Innovation numbers: Unique markers given to new genes (neurons or connections) to help track the appearance of new features in the population.

The evolution begins with networks composed of input and output nodes only, and complexity grows as needed through mutations that can add hidden nodes and connections.

Application to Flappy Bird

To train a Flappy Bird agent, I used the NEAT algorithm to evolve a population of neural networks, each representing a different strategy for playing the game. The input to the network included variables such as the bird’s height, the distance to the next pipe, and the vertical distance to the opening of the next pipe. The output was whether the bird should “flap” or not.

Step-by-Step Training Process:

1. Initialization: Generate an initial population of simple neural networks.
2. Evaluation: Each network plays the game, and its fitness is evaluated based on its score.
3. Selection: Select the best-performing networks as parents for the next generation.
4. Breeding: Apply crossover and mutation to create a new generation of networks.
5. Speciation: Group similar networks into species to preserve genetic diversity.
6. Repetition: Repeat the process for numerous generations until the performance ceases to improve significantly.

Visualizing Progress:

1. Initial Generation: Basic gameplay with many failures.
2. After Several Generations: Improvement in avoiding pipes.
3. Mid-Training: Consistently achieving higher scores.
4. Late Training: Mastering the game with near-perfect scores.

Conclusion

The NEAT algorithm is a robust tool for evolving neural networks to tackle various problems, including gaming. In the case of Flappy Bird, it demonstrated significant capabilities in learning and optimization, evolving simple networks into sophisticated strategies capable of high scores. NEAT’s ability to dynamically adjust both the topology and weights of the network makes it particularly powerful for such adaptive challenges.

By integrating NEAT, developers and researchers can explore new frontiers in artificial intelligence, pushing the limits of automated learning and decision-making systems.

The application of the NEAT algorithm to train a Flappy Bird game agent showcases how versatile and powerful evolutionary strategies are in the field of AI and gaming. The progression from simple strategies to sophisticated gameplay illustrates the capability of NEAT to not only enhance performance but also innovate continually.

--

--