This repository contains a Deep Q-Learning agent that learns how to play the classic Snake game. The project involves training an agent using reinforcement learning to maximize its score by optimizing the game's strategy.
Here’s a preview of the agent playing the Snake game:
After training the model over 200 games, the following results were obtained:
The chart above shows the agent's score over time, along with the average score per game.
The goal of this project is to apply deep reinforcement learning to teach an agent to play Snake. The agent is trained using a deep Q-network (DQN) that learns to optimize game performance over time by balancing exploration and exploitation.
- Deep Q-Learning: The model uses a neural network to approximate Q-values for each action and update them based on rewards.
- Exploration vs. Exploitation: The agent explores different strategies during training while gradually exploiting known strategies that yield better results.
- Reward Structure: The reward function encourages the agent to eat food while penalizing collisions with walls or itself.
- Configurable environment for training the agent.
- Dynamic adjustment of reward values and gamma (discount factor) for better learning.
- Visualization of the training process and game performance.
- Ability to save and load trained models for later use.
- Clone this repository:
git clone https://github.com/yourusername/snake-dqn.git
cd snake-dqn- Install the required dependencies:
pip install -r requirements.txtTo train and watch what the agent do:
python main.pyYou can modify hyperparameters such as the number of episodes, gamma, and learning rate in the utils/constant.py file.
This project is licensed under the MIT License. See the LICENSE file for details.

