Skip to content

Anroshka/snake-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Snake AI with Deep Q-Learning ๐Ÿ

An advanced implementation of a Snake game AI that learns through Deep Q-Learning, featuring real-time visualization, multi-agent support, and performance optimizations.

๐Ÿ“š Table of Contents

  1. ๐Ÿš€ Key Features
  2. ๐Ÿ› ๏ธ Technical Specifications
  3. ๐Ÿ“Š Real-time Visualization
  4. ๐Ÿš€ Installation
  5. ๐Ÿ’ป Usage
  6. โš™๏ธ Performance Features
  7. ๐Ÿ”„ Automatic Saving
  8. ๐ŸŽฎ Controls
  9. ๐Ÿ“ˆ Training Graphs
  10. ๐ŸŽฏ Latest Improvements
  11. ๐Ÿš€ Next Steps
  12. ๐Ÿ“ Requirements
  13. ๐Ÿ”ง Configuration
  14. ๐Ÿ“š Project Structure
  15. ๐Ÿค Contributing
  16. ๐Ÿ“„ License
  17. ๐Ÿ›ก๏ธ Security
  18. ๐Ÿ“œ Code of Conduct

๐Ÿš€ Key Features

  • ๐Ÿง  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

๐Ÿ› ๏ธ Technical Specifications

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

๐Ÿ“Š Real-time Visualization

  • 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

๐Ÿš€ Installation

  1. Clone the repository
git clone https://github.com/Anroshka/snake-ai.git
cd snake-ai
  1. Install dependencies:
pip install -r requirements.txt

For CUDA support (recommended):

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

๐Ÿ’ป Usage

Start training:

python train_multi.py

You'll be prompted to enter the number of snakes (2-6 recommended) for training.

โš™๏ธ Performance Features

  • 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

๐Ÿ”„ Automatic Saving

  • Best model preservation
  • Checkpoints every 10 episodes
  • Training statistics graphs
  • Performance metrics tracking
  • Individual agent models saving

๐ŸŽฎ Controls

  • ESC: Exit training
  • Automatic gameplay during training
  • Visualization every 10 episodes
  • Training stats display
  • Interactive agent count selection

๐Ÿ“ˆ Training Graphs

  • Multi-agent learning progress
  • Loss function
  • Epsilon decay
  • Score distribution
  • Moving averages
  • Agent interaction patterns

๐ŸŽฏ Latest Improvements

  • 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

๐Ÿš€ Next Steps

  • 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

๐Ÿ“ Requirements

  • Python 3.10+
  • CUDA-capable GPU (recommended)
  • PyTorch 2.0+
  • Pygame 2.4.0
  • Matplotlib for visualization

๐Ÿ”ง Configuration

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

๐Ÿ“š Project Structure

  • game.py: Snake game environment with multi-agent support
  • model.py: DQN implementation with PER
  • train_multi.py: Multi-agent training loop and visualization
  • models/: Saved models and checkpoints

๐Ÿค Contributing

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.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ›ก๏ธ Security

For details about our security policy and how to report security vulnerabilities, please see our Security Policy.

๐Ÿ“œ Code of Conduct

This project and everyone participating in it is governed by our Code of Conduct. By participating, you are expected to uphold this code.

๐Ÿ™ Acknowledgments

  • Thanks to all contributors who have helped shape Snake AI
  • Special thanks to the PyTorch and Pygame communities
  • Inspired by various reinforcement learning implementations

๐Ÿ“ž Contact

For questions and support, please:

  1. Check existing Issues
  2. Create a new issue if needed
  3. Follow our Security Policy for reporting vulnerabilities

About

๐Ÿ A Snake game AI that learns to play through Deep Q-Learning. Built with PyTorch and Pygame, featuring CUDA acceleration and real-time visualization of the learning process.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages