We all have played the famous Pong game at least once before. This project integrates the game with AI, where a neural network learns to play Pong by playing against itself multiple times. Instead of a single neural network, numerous networks are trained in each generation. These networks compete and earn a score (called fitness), and the best ones merge to create better networks, similar to natural selection.
NEAT (NeuroEvolution of Augmenting Topologies) is a genetic algorithm designed to evolve neural networks. It uses evolutionary principles such as mutation, crossover, and selection to optimize neural networks. NEAT is effective in evolving both the weights and the structures of the networks, enabling complex and efficient network topologies to be discovered over generations. Read NEAT documentation for further understanding.
At the start of training, a neural network is created based on given configurations, forming a population of networks as the first generation. Each generation calculates the fitness value (number of times the ball hits the paddle) for each genome (neural network) by playing against all other genomes. The best genomes merge (crossover) to create new genomes for the next generation. This process continues until the fitness threshold or the maximum number of generations is reached. The best genome is saved using the Python pickle module.
After training, the best genome is stored, and a human player can test their performance against this AI. This allows users to see how well the AI has learned to play Pong and have fun playing with it.
To install the necessary packages, use the requirements.txt file:
pip install -r requirements.txt
-
Configuration: Modify the settings in the config.txt file as needed. This file is based on the NEAT documentation and customized for this game. For more details on configuration, check the NEAT documentation about the configuration file.
-
Main Actions: There are two primary actions you can perform:
- Train the AI: Add the
train
argument while running themain.py
. If you'd like to train the model from a specific checkpoint, you can just add the checkpoint as the second argument. Use the commands below:
python main.py train
python main.py train neat-checkpoint-9
- Test the AI: Just like before, Add the
test
argument while running themain.py
.
python main.py test
- Python
- Pygame
- NEAT
- Pickle (built-in Python module)
I am open to contributions, feel free to play with the code and make it better ;).
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to reach out via the following platforms: