This repository contains an advanced implementation of a Snake game AI agent trained using Reinforcement Learning with PyTorch. The project showcases how to develop an intelligent agent that learns to master the classic Snake game by optimizing its strategy to achieve high scores.
- Snake Game Environment: A robust and customizable Snake game environment built with Pygame, designed for AI interaction.
- Deep Q-Network (DQN): Implements a Deep Q-Network, a powerful reinforcement learning algorithm, to enable the AI agent to learn optimal policies through experience.
- PyTorch Implementation: Leverages the flexibility and power of PyTorch for efficient neural network model definition, training, and inference.
- Training and Evaluation: Comprehensive scripts for training the AI agent from scratch, fine-tuning existing models, and evaluating its performance against human-like play.
- Visual Play: Provides an option to visualize the AI playing the game in real-time, offering insights into its learned behavior.
- Automatic Model Saving: The agent automatically saves its best-performing model, ensuring progress is preserved.
- Pre-trained Model Loading: Ability to load previously trained models to continue training or for immediate deployment and evaluation.
Ensure you have the following installed on your system:
- Python 3.8 or higher
pip(Python package installer)
Follow these steps to set up the project locally:
-
Clone the repository:
git clone https://github.com/AneKazek/RLSnake.git cd RLSnake -
Create a virtual environment (recommended for dependency management):
python -m venv rlsnake_env # On Windows: .\rlsnake_env\Scripts\activate # On macOS/Linux: source rlsnake_env/bin/activate
-
Install required dependencies:
pip install -r requirements.txt
To train the AI agent and watch it learn:
python notebooks/reinforcement_learning_pytorch_pygame/agent.pyTo play the game manually and test your own skills:
python notebooks/reinforcement_learning_pytorch_pygame/snake_game_human.pyRLSnake/
├── .gitignore
├── LICENCE.txt
├── README.md
├── requirements.txt
├── model/ # Directory to store trained models
│ └── model.pth # Saved PyTorch model
├── reports/ # Directory for generated reports and plots
│ └── Figure_1.png # Example training plot
└── notebooks/
└── reinforcement_learning_pytorch_pygame/
├── agent.py # Main script for training the AI agent
├── game.py # Snake game environment logic
├── model.py # Deep Q-Network model definition
├── helper.py # Utility functions (e.g., plotting)
├── snake_game_human.py # Script to play the game manually
├── arial.ttf # Font file for Pygame
└── LICENSE # License for the original notebook
An example of the training progress, showing the scores achieved over games:
This project is licensed under the MIT License - see the LICENCE.txt file for details.
- This project is inspired by various Reinforcement Learning tutorials and implementations of the Snake game.
- Special thanks to the PyTorch and Pygame communities for their excellent libraries.
