/SnakeGameAI

Using Deep Q Learning to play Snake game.

Primary LanguagePython

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: