Table of Contents
The project is a snake game with an ai that can play the game at a reasonable level. It is built with pygame and neat-python. The logic for the game is a fast implementation of the snake game with numba which can be found here.
The ai gets 8 inputs from the game. The inputs are the distance from the snake to the 4 walls, the distance as well the angle to the food and 4 rays.
The inputs are then fed to the neural network. The output of the neural network is the direction the snake should move in. The direction is chosen by taking the direction with the highest output value.
The fitness function is the score of the snake. It is calculated using the following rules:
- +1 if the snake gets closer to the food
- -1 if the snake gets further from the food
- +100 if the snake eats the food
- +0.1 for every frame the snake is alive
- stop the game if the snake dies
The neural network is trained using the neat algorithm. The neat algorithm is a genetic algorithm that evolves the neural network to play the game. The neat algorithm is implemented in the neat-python library. The neat algorithm is explained in the neat-python documentation.
The visualization of the neural network is done using graphviz (click on the "Graph" button in the top right corner of the visualization window). The visualization of the training is done using matplotlib (click on the "Plot" button in the top right corner of the visualization window). The training is done in the background and the visualization is updated in real time.
- Press "p" augment the speed of the game
- Press "o" reduce the speed of the game
- Clone the repo and the submodules
git clone --recurse-submodules https://github.com/Apoorva64/snake-ai-python-neat.git
- Install the requirements
pip install -r requirements.txt
- Add the lib/fast_snake/src, src, data folder to the python path
set PYTHONPATH=%PYTHONPATH%;lib/fast_snake/src;src;data
- Run the game
python src/snake_ai/ai_multiprocessing.py
You can change settings in the ai_multiprocessing.py file.
RUNS_PER_GAME_SIZE = 10
ENABLE_GUI = True
# GAME_SIZE_RANGE = range(5, 100, 10)
GAME_SIZE_RANGE = [40]
SNAKE_GAME_SIZE = (40, 40)
FOOD_TIMER_MAX = SNAKE_GAME_SIZE[0] * np.sqrt(2)
NUMBER_OF_RAYS = 4
ROTATING_DIRECTIONS = False
INCLUDE_LAST_DIRECTION = False
INCLUDE_SNAKE_LENGTH = False
INCLUDE_WALL_DISTANCE = True
TEMPORAL_LENGTH = 1
CORE_COUNT = multiprocessing.cpu_count() - 1
Be careful when changing settings the game will not be able to do visulization on the first run. You must stop the game and relauch it to get them.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Appadoo Apoorva Srinivas - Linkedin - [email protected]
Project Link: https://github.com/Apoorva64/snake-ai-python-neat