The main goal of this project is to develop an AI bot which can learn to play the popular snake game. In order to compare the learning result with human performance, this project consists of two modes, namely, manual mode and ai mode. More instruction can be found below.
I adopt the idea of network input data and some training technics from here. According to this article, I used 11 boolean variables as the input. I rebuilt the model by using just 3 (input, hidden, output) fully connected layers. After some refinements, the model can converge after about 50 games.
To run this project, you need the following packages
- Pytorch
- Pygame
- numpy
- matplotlib
- other packages - you will find out by running the code :P
- If you want to run manual play mode:
python snake.py
- To train the model, go to Agent.py and uncomment line 299, then do:
python Agent.py
# You can tune the paramenters in Agent.py and network.py.
# You can also change the setting of the game wrapper in snake_ai.py. I set *self.FPS=40* to speed up the training.
- To play game based on the pre-trained model, go to Agent.py and uncomment line 300, then do:
python Agent.py
# This will call the model weight you save in './model' folder
To start:
python run.py
- Choose your option on game intro page.
- If you chose manual mode, you will see a white snake. Use w,a,s,d or up, down, left, right keys to control.
- If you chose AI mode, you will have to choose to train bot or play bot
- If you chose train, game will start to train and save the result in ./model. You can press
esc
to quit the game
- If you chose play, game will pick the weight saves in ./model and play the game. Press
esc
to quit.
Might implement a restart function in the future.