Tiny Project 2: Snake Game AI
Github: https://github.com/Kiiiiii123/SnakeGameAI.
Introduction
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 π.
SnakeGameAI
ββ README.md
ββ agent.py
ββ arial.ttf
ββ game_env.py
ββ helper.py
ββ model
β ββ model.pth
ββ model.py
ββ requirements.txt
ββ snake_game.py
Environment
-
Observation Space
[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οΌ