In this tiny project, we will first re-engrave the game Snake with pygame, then turn the game into a reinforcement learning environment with a few minor code changes. In the core section, we will train an RL agent with Pytorch to play the game well β the snake can eat quite a bit of food while the game speed is very fast π.
[danger straight, danger right, danger left,
direction left, direction right,
direction up, direction down,
food left, food right,
food up, food down
]
Take the following figure for example:
Action Space
[1, 0, 0] -> go straight
[0, 1, 0] -> turn right
[0, 0, 1] -> turn left
Reward Function
eat food: +10
game over: -10
else: 0
Agent
Deep Q Learning
Model
Bellman Equation
Q Update Rule Simplified
Loss Function
Experiments
After training for about 100 games, we can get a pretty good agentοΌ