Skip to content

A snake game ai that can play the game at a reasonable level

Notifications You must be signed in to change notification settings

Apoorva64/snake-ai-python-neat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Logo

Snake ai

This is a snake ai that can play the game at a reasonable level

View Demo
Table of Contents
  1. About The Project
  2. Getting Started
  3. How does it work
  4. Usage
  5. Contributing
  6. License
  7. Contact

About The Project

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.

(back to top)

Built With

(back to top)

How does it work

Ai inputs

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.

Ai Outputs

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.

Fitness function

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

Training

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.

Visualization

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.

Additional controls

  • Press "p" augment the speed of the game
  • Press "o" reduce the speed of the game

(back to top)

Getting Started

Installation

  1. Clone the repo and the submodules
    git clone --recurse-submodules https://github.com/Apoorva64/snake-ai-python-neat.git
  2. Install the requirements
     pip install -r requirements.txt
  3. Add the lib/fast_snake/src, src, data folder to the python path
    set PYTHONPATH=%PYTHONPATH%;lib/fast_snake/src;src;data
  4. Run the game
    python src/snake_ai/ai_multiprocessing.py

(back to top)

Usage

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.

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Appadoo Apoorva Srinivas - Linkedin - [email protected]

Project Link: https://github.com/Apoorva64/snake-ai-python-neat

(back to top)

About

A snake game ai that can play the game at a reasonable level

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages