An advanced implementation of a Snake game AI that learns through Deep Q-Learning, featuring real-time visualization, multi-agent support, and performance optimizations.
- ๐ Key Features
- ๐ ๏ธ Technical Specifications
- ๐ Real-time Visualization
- ๐ Installation
- ๐ป Usage
- โ๏ธ Performance Features
- ๐ Automatic Saving
- ๐ฎ Controls
- ๐ Training Graphs
- ๐ฏ Latest Improvements
- ๐ Next Steps
- ๐ Requirements
- ๐ง Configuration
- ๐ Project Structure
- ๐ค Contributing
- ๐ License
- ๐ก๏ธ Security
- ๐ Code of Conduct
- ๐ง Deep Q-Network with Priority Experience Replay (PER)
- ๐ฎ Real-time game visualization with informative HUD
- ๐ Multi-agent support (2-6 snakes training simultaneously)
- ๐ Live training statistics and performance graphs
- ๐ Automatic checkpointing and model saving
- โก CUDA-accelerated training with AMP
- ๐ Adaptive learning parameters
- ๐ฏ Enhanced exploration/exploitation balance
- ๐ค Interactive agent count configuration
| Feature | Description |
|---|---|
| Framework | PyTorch with CUDA support |
| Visualization | Pygame, Matplotlib |
| Neural Network | Input Layer: 17 neurons (enhanced state) |
| Hidden Layer: 512 neurons | |
| Output Layer: 4 actions (movement directions) | |
| Training Parameters | Learning Rate: 0.0005 |
| Gamma: 0.99 | |
| Initial Epsilon: 1.0 | |
| Epsilon Decay: 0.997 | |
| Memory Size: 100,000 | |
| Batch Size: 64 |
- Individual scores for each snake
- Best score achieved
- Rolling average (last 100 games)
- Exploration rate (Epsilon)
- Samples collected
- Training performance (FPS)
- Loss function evolution
- Score distribution histogram
- Multi-agent interaction visualization
- Clone the repository
git clone https://github.com/Anroshka/snake-ai.git
cd snake-ai- Install dependencies:
pip install -r requirements.txtFor CUDA support (recommended):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121Start training:
python train_multi.pyYou'll be prompted to enter the number of snakes (2-6 recommended) for training.
- Multi-agent learning environment
- Multi-threaded processing
- Automatic device selection (GPU/CPU)
- Optimized rendering with FPS control
- Gradient clipping for stability
- Automatic Mixed Precision (AMP)
- Priority Experience Replay
- Efficient memory management
- Advanced reward system
- Best model preservation
- Checkpoints every 10 episodes
- Training statistics graphs
- Performance metrics tracking
- Individual agent models saving
- ESC: Exit training
- Automatic gameplay during training
- Visualization every 10 episodes
- Training stats display
- Interactive agent count selection
- Multi-agent learning progress
- Loss function
- Epsilon decay
- Score distribution
- Moving averages
- Agent interaction patterns
- Added multi-agent support (2-6 snakes)
- Enhanced visualization with semi-transparent HUD
- Stabilized FPS for better visualization
- Minimum samples threshold for training start
- Improved error handling and stability
- Better memory management
- Adaptive learning parameters
- Interactive configuration system
- Add competitive and cooperative training modes
- Implement agent specialization
- Optimize multi-agent interactions
- Develop adaptive learning rate scheduling
- Explore different state representations
- Add agent personality traits
- Python 3.10+
- CUDA-capable GPU (recommended)
- PyTorch 2.0+
- Pygame 2.4.0
- Matplotlib for visualization
All training parameters can be adjusted in train_multi.py:
- Number of agents (2-6)
- Episode count
- Memory size
- Batch size
- Learning rate
- Epsilon decay
- Visualization frequency
- Reward system parameters
game.py: Snake game environment with multi-agent supportmodel.py: DQN implementation with PERtrain_multi.py: Multi-agent training loop and visualizationmodels/: Saved models and checkpoints
We welcome contributions to Snake AI! Please read our Contributing Guidelines for details on how to submit pull requests, report issues, and contribute to the project.
Before contributing, please read our Code of Conduct.
This project is licensed under the MIT License - see the LICENSE file for details.
For details about our security policy and how to report security vulnerabilities, please see our Security Policy.
This project and everyone participating in it is governed by our Code of Conduct. By participating, you are expected to uphold this code.
- Thanks to all contributors who have helped shape Snake AI
- Special thanks to the PyTorch and Pygame communities
- Inspired by various reinforcement learning implementations
For questions and support, please:
- Check existing Issues
- Create a new issue if needed
- Follow our Security Policy for reporting vulnerabilities