https://github.com/h-sinha/rl-snake-game
Deep Q-Learning applied to snake game
https://github.com/h-sinha/rl-snake-game
deep-learning pygame qlearning snake-game
Last synced: about 13 hours ago
JSON representation
Deep Q-Learning applied to snake game
- Host: GitHub
- URL: https://github.com/h-sinha/rl-snake-game
- Owner: h-sinha
- Created: 2019-12-06T05:57:30.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-25T01:02:59.000Z (over 2 years ago)
- Last Synced: 2025-04-05T16:31:32.255Z (6 months ago)
- Topics: deep-learning, pygame, qlearning, snake-game
- Language: Python
- Homepage:
- Size: 2.71 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RL-Snake-Game
This is an implementation in Keras and Pygame of deep Q-learning applied to the classic Snake game. This approach gives the system parameters related to its state, and a reward based on its actions. Initially the Bot has no information about the rules of game and what it needs to do. The goal for the agent is to figure it out and maximize the reward.# Requirements
Refer to [requirements.txt](../master/requirements.txt).# Instructions
* Set game speed in line 16 of game.py.
* The agent uses pretrained model [weights.hdf5](../master/weights.hdf5). For training the model from scratch comment line 78 in DQN.py and set epsilon in line 17 of game.py to 80.
* Start the game using
```
python game.py
```# Implementation Details
### State
Tuple with 13 values. Each value except direction is 0/1. Direction can take integral values from 0 to 4.
| index | Description |
|-------|:-----------------------------:|
| 0 |danger above player |
| 1 |danger below player |
| 2 |danger to the left of player |
| 3 |danger to the right of player |
| 4 |player moving up |
| 5 |player moving down |
| 6 |player moving left |
| 7 |player moving right |
| 8 |food on player's left |
| 9 |food on player's right |
| 10 |food below player |
| 11 |food above player |
| 12 |player's direction |### Reward
|Action | Reward |
|--------------|:------:|
|Food eaten | 10 |
|Game over | -10 |
|Anything else | 0 |### DL model
* 1 input layer with 13 neurons.
* 2 hidden layers each with 120 neurons.
* 1 output layer with 5 neurons. 5 output neurons as there are 5 possible actions - No change, move up, move down, move left, move right.Initially the player performs random moves for exploration. Later the action to be taken is decided using the deep learning network.
# Results
![]()
1st game
![]()
201st game
* After 100 games the agent consistently scores 15+ points.
* After 150 games the agent scores 25+ points.
* After 175 games the agent scores 35+ points