Giorgos Voutouri
SID:10291029
Snake Game with Reinforcement Learning
This project is a demonstration of training a reinforcement learning agent to play the classic game "Snake". The agent learns to navigate the game board, avoiding obstacles and collecting rewards, using a deep Q-network algorithm.
Getting Started
Prerequisites
Python 3
Installation
- Clone the repository { git clone https://github.coventry.ac.uk/voutourisg/AI_agent_Snake_Game.git }
- Access the repository { cd AI_agent_Snake_Game }
- Install the dependencies { pip install torch numpy pygame matplotlib IPython }
- Start the game { python3 AI_Agent.py }
How to play
Use the arrow keys to control the snake's movement. The objective is to collect the red dots while avoiding collision with the wall or the snake's own body.
Reinforcement learning
The agent uses a deep Q-network algorithm to learn the optimal policy. The state of the game board is represented as an array of binary values, with 1 representing the snake's head, 2 representing the snake's body, and 3 representing the food. The neural network takes the state as input and outputs the Q-values for each possible action. The agent selects the action with the highest Q-value at each time step.
The agent receives a reward of +1 for collecting food and a reward of -1 for colliding with the wall or the snake's own body. The training is done through experience replay, where the agent stores its experiences in a buffer and randomly samples them to update the neural network.
Results
After training for 17,000 episodes, the agent was able to consistently achieve an overall score of 99 and an average score of 33.12. The following graph shows the learning progress over time:
References
Some parts from the below repository were used towards the code development of this project.
https://github.com/patrickloeber/snake-ai-pytorch