There exist three dependencies:
- numpy
- pyqt5
- Python3.6+
To install dependencies, run pip3 install -r requirements.txt
- Clone the repo or download it in someway
git clone https://github.com/Chrispresso/SnakeAI.git
- The two places you will ever really need to change stuff unless you feel like going crazy are
settings.py
, which control the hyperparameters of the Neural Network and Genetic Algorithm andsnake_app.py
, which is the graphics and GA. - Pick some things you would like to test with under
settings.py
. If you change theboard_size
drastically you will probably want to changeSQUARE_SIZE
undersnake_app.py
. The current settings when you first clone are the settings I used for training a snake to solve 10x10 and the 50x50 grids. Play around with this stuff if you want, you can always create another population in a new command window that use different settings. - Head over to
snake_app.py
and adjustshow=True, fps=200
if you would like.show
controls whether or not to display the snakes learning. FPS in the case of show is capped at your monitor refresh. Definitely faster to train withshow=False, fps=1000
. - Go to the area of
# Next generation
and you can print out the fitness if you would like. This is also where you cansave
the snake. Well you can save anywhere I guess, but this is where I saved the best snake from each generation. This can be done withsave_snake('path/to/population/folder', 'snake_name (i.e. best_snake_gen0)', snake, self.settings)
. This saves the snake, the constructor params that were used to create the snake and thesettings.py
file used for hyperparameters. If you load the same snake you saved, the snake will play exactly how it did before. The apple locations are based off an initiallyapple seed
. So if you load the same snake without modifying the contructor, then the snake will replay what it did. Very helpful for me since I trained without visualizations and needed to go back and record stuff for the video. - Run it! However you like, you can run it and get some snakes generating!
Let's say you have a 50 generations of snakes saved and you want to create a new population with the last 10 generations. You could start a new instance of snake_app.py
and modify for _ in range(self.settings['num_parents']):
portion to generate 10 less snakes. Then you can load your 10 best snakes and insert them into the population. This is where you can choose to either modify the constructor of your snake to have a different apple_seed
or allow the snake to run it's previous course. The choice is up to you and totally dependent on your goals!
If the ability to load snakes is something you want to have an easier time with let me know and I can work on that.