A reinforcement learning project where an AI agent learns to master the classic Snake game using Deep Q-Learning (DQN) and a simple feedforward neural network.
- ✨ Overview
- ⚙️ Installation
- 🚀 Usage
- 📁 Project Structure
- 🤖 Agent Details
- 🧠 Model Details
- 🎯 Training Process
- 💾 Checkpointing & Fine-Tuning
- 📋 Logging
- 🖼️ Screenshots
- 🤝 Contributing
- 📝 License
The AI agent is trained using Q-Learning with function approximation via a neural network. It learns from its environmental interactions, predicting the best actions to take based on the current state to maximize cumulative reward (i.e., eat more food and survive longer!).
Make sure you have Python 3.7+ installed.
Install the required dependencies:
pip install -r requirements.txtTo begin training the agent:
python agent.py- Training progress and events are logged to
agent.log. - Models are saved periodically to the
./model/directory.
.
├── agent.py # Contains the Agent class and training logic
├── model.py # Defines the neural network and QTrainer
├── game.py # Implements the Snake game mechanics (not included here)
├── helper.py # Helper functions like plotting (not included here)
├── agent.log # Log file for training progress
├── model/ # Saved models (checkpoints)
└── requirements.txt # Python dependencies
The Agent class handles:
- Initializing the neural network and trainer
- Extracting game state features (11 inputs)
- Choosing actions via an exploration/exploitation policy
- Storing short- and long-term memories
- Training the model based on rewards received
The model is a simple feedforward neural network:
- Input Layer: 11 neurons representing game state
- Hidden Layer: 256 neurons with ReLU activation
- Output Layer: 3 neurons (straight, left, right)
Implemented in the Linear_QNet class inside model.py.
The agent trains by repeatedly playing the game and improving based on its mistakes:
- Short-term memory: Train immediately on the latest step
- Long-term memory: Sample batches from past experiences and train
Training is managed by the train() function in agent.py.
Models are checkpointed periodically and when new high scores are achieved:
def save(self, file_name='model.pth', n_games=-1):
...
torch.save(self.state_dict(), file_name)You can resume training or fine-tune from an existing model:
def load(self, file_name='model90.pth'):
...
self.load_state_dict(torch.load(file_name))💡 This improves training efficiency and resilience to interruptions.
Training events, scores, model saves, and errors are logged to agent.log.
Use the log file to track progress or debug unexpected behavior.
The AI agent in action learning to survive longer and eat more food:
Visualizing the agent’s improving performance:
Contributions are welcome! To contribute:
- Fork the repo
- Create your feature branch:
git checkout -b feature/my-feature - Commit your changes:
git commit -m "Add my feature" - Push to the branch:
git push origin feature/my-feature - Open a Pull Request 🎉
This project is licensed under the MIT License. See the LICENSE file for more information.
Built with ❤️ by an RL enthusiast. Enjoy watching your AI learn to slither like a pro! 🐍

