This project showcases an AI agent learning to play the classic Flappy Bird game using the NEAT (NeuroEvolution of Augmenting Topologies) algorithm. By simulating evolution, the AI refines its skills over generations, transforming from a random neural network to selecting a best performing bird that masterfully plays the game.
NEAT is a genetic algorithm that evolves neural networks by improving both their structure and weights over time. Unlike fixed architectures, NEAT starts with simple networks and makes them more complex only when needed, ensuring efficiency and encouraging innovation.
-
Inputs to the AI:
- Bird's vertical position.
- Distance to the next pipe (horizontal and vertical).
-
Output from the AI:
- One action: Jump or Don't Jump.
The fitness function rewards the AI for:
- Surviving longer in the game.
- Successfully passing through pipes. This encourages the AI to improve its gameplay strategy over time.
- Activation Function: Tanh
- This function ensures smooth gradients and handles negative values well, aiding the learning process.
- Population Size:
- Starts with a certain number of birds and eliminates the poor performing ones over generations.
- Max Generation:
- Maximum number of generations of genomes the AI is trained on.
- Start with a population of simple neural networks.
- Test each network in the Flappy Bird environment.
- Use the fitness function to rank networks based on their performance.
- Combine the best-performing networks and introduce random changes to maintain diversity.
- Over successive generations, watch the AI improve and master the game.
Clone the Repository:
git clone https://github.com/Laasyakshara25/Flappy-Bird-NEAT.git
cd flappy-bird.py
Install Dependencies:
pip install pygame neat-python
Run the game
python flappy_bird.py