Snake Game with reinforcement Q-learning and neural networks!
A simple python program using keras to train a neural network to play the snake game.
First ensure you have pygame, keras and tensorflow (or other backend that you like =)) to run the program. I recommend using conda virtual environments.
To play the game controlling the snake with the arrow keys run
python play.py
For train/watch the neural network, run
python ai_play.py
In config.py you will encounter several parameters that can be adjusted as you like. A quick summary of each of the parameters:
Determine the width of the screen in pixels. Has not to do with the grid_size. Always ensure that this number is divisible by GRID_SIZE_X.
Determine the height of the screen in pixels. Has not to do with the grid size. Always ensure that this number is divisible by GRID_SIZE_Y.
The amount of squares in the grid on the horizontal component.
The amount of squares in the grid on the vertical component.
The color of the background in RGB.
The framerate that the game will run. Also determine the velocity of the training. FRAME_RATE = 0 will let your computer run as fast as possible. A good frame rate to watch is 15.
The learning rate for the q-function.
The probability of the snake do a random decision (for training purpose).
After each training, the EXPLORATION_COEFFICIENT will decay by this amount until it is zero (this value can be zero).
The learning rate of the 'Adam' algorithm of the neural network.
How many times the neural network should be trained against each memories.
How many memories should be collected before each training. For example, with 1, the neural network is trained each frame. Put 0 for no training.
#####DEATH_REWARD: Reward after death.
Reward after the snake is able to eat a fruit.
The snake receive a reward depending on it distance from the fruit given by the formula reward = FOOD_DISTANCE_REWARD_MULTIPLICATION_FACTOR/distance + FOOD_DISTANCE_REWARD_LINEAR_FACTOR.
The weights of the neural network will be loaded from the filename in this parameter. You can leave it empty for a empty neural network.
The weights of the neural network will be saved with this filename in this parameter. You can leave it empty for not saving.