An AI implementation that learns to play Flappy Bird using NEAT (NeuroEvolution of Augmenting Topologies), a genetic algorithm that evolves neural networks.
This project uses the NEAT algorithm to train an AI to play Flappy Bird. The neural network evolves over generations, progressively getting better at navigating through pipes. Key features:
- Neural networks control bird actions
- Population-based training using genetic algorithms
- Fitness-based selection for evolution
- Visual display of training progress
- Python 3.6 or higher
- pygame
- neat-python
git clone https://github.com/kitan23/NEAT_flappy_bird.git
cd NEAT_flappy_bird# Create a virtual environment
python -m venv venv
# Activate the virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate# Install required packages
pip install -r requirements.txtTo run the AI training:
python flappy_bird.pyDuring execution:
- Watch as birds learn to navigate through pipes
- The terminal will display information about each generation
- Progress indicators show the current generation, score, and fitness
The behavior of the NEAT algorithm can be customized by modifying the config-feedforward.txt file:
pop_size: Controls the population size in each generationfitness_threshold: The fitness level at which training will stopnum_inputs: Number of inputs to the neural network (3 by default - bird's y position, distance to top pipe, distance to bottom pipe)num_outputs: Number of outputs from the neural network (1 by default - jump or don't jump)
Feel free to experiment with these settings to optimize training for your system.
- A population of birds (each controlled by its own neural network) is initialized
- Each bird attempts to navigate through the pipes
- Birds are assigned fitness scores based on how far they progress
- The best performers are selected to reproduce, creating the next generation
- Mutation and crossover introduce variation in the neural networks
- The process repeats until birds can successfully play the game
- Based on the NEAT-Python implementation by CodeBullet
- Flappy Bird game assets used under fair use
