This project implements a reinforcement learning algorithm to train an AI agent to play the game Flappy Bird. The AI agent is trained using the NEAT (NeuroEvolution of Augmenting Topologies) algorithm.
The game environment is implemented using the Pygame library. The game consists of a bird that can flap its wings to avoid colliding with pipes that appear on the screen. The goal of the game is to navigate the bird through the gaps between the pipes for as long as possible.
Two reinforcement learning algorithms are implemented for training the AI agent:
-
NEAT Algorithm (NeuroEvolution of Augmenting Topologies): This algorithm evolves neural networks to control the bird's actions (flap or not flap) based on the current game state. NEAT is a genetic algorithm that evolves neural network architectures and weights over multiple generations.
-
Q-Learning: An attempt has been made to implement Q-learning, but it is still a work in progress and has not been successfully implemented yet.
- The
eval_genfunction evaluates the fitness of each AI agent (bird) in a generation by simulating the Flappy Bird game and calculating a fitness score based on the performance of each agent. - The
run_neatfunction trains AI agents using the NEAT algorithm for a specified number of generations. - The
replay_genomefunction allows replaying a trained AI agent (genome) without further training.
-
Ensure you have Python installed on your system along with the required dependencies listed in
requirements.txt. -
Run the script
main.pywith appropriate command-line arguments:
python gameNEAT.py [-h] [--config CONFIG] [--color COLOR] [--nGens NGENS] [--checkpoint CHECKPOINT] [--replay REPLAY]--config: Path to the NEAT configuration file (default:config-ff.txt).--color: Color of the bird (randomorred) (default:random).--nGens: Number of generations to run training (default:100).--checkpoint: Checkpoint path to restore training from (default:None).--replay: Path to the genome file to replay (default:None).
- To close the window, press 'esc'.
python gameNEAT.py --replay good_bird.pickle- The Q-learning algorithm is still a work in progress and has not been successfully implemented yet. Further work is needed to debug and refine the Q-learning implementation.
- Additional optimizations and improvements may be made to enhance the performance and effectiveness of the NEAT algorithm.
- Add a speed argument to vary the speed of the game
Feel free to contribute to the project and experiment with different reinforcement learning algorithms to train the Flappy Bird AI agent!