A Python implementation of the classic Snake game with reinforcement learning agents (Q-Learning and Deep Q-Network) to train AI to play optimally.
This project implements:
- Snake Game Environment: A Pygame-based game environment with configurable grid sizes
- Q-Learning Agent: Tabular Q-learning for smaller state spaces
- Deep Q-Network (DQN): Neural network-based agent for larger state spaces
- Training Pipeline: Complete training loop with metrics and visualization
- Testing Suite: Comprehensive tests for all components
- Flexible Environment: Configurable grid sizes, reward functions, and game parameters
- Multiple RL Agents: Both Q-Learning and DQN implementations
- Real-time Visualization: Watch the agent learn and play
- Comprehensive Logging: Track training progress and performance metrics
- Extensive Testing: Unit tests for all components
- Python 3.8+
- Pygame
- PyTorch
- NumPy
- Matplotlib
- Seaborn
-
Clone the repository:
-
Install dependencies:
pip install -r requirements.txtpython main.py --mode train --agent dqn --episodes 1000python main.py --mode train --agent qlearning --episodes 500 --grid-size 10python main.py --mode train --agent dqn --episodes 2000 --grid-size 15python main.py --mode play --agent dqn --model-path models/dqn_model.pthpython -m pytest tests/ -vThe agents typically achieve:
- Q-Learning: 15-25 average score on 10x10 grid
- DQN: 30-50 average score on 15x15 grid
Training progress is logged and visualized with:
- Average score per episode
- Reward progression
- Learning curves
SnakeRL/
├── src/
│ ├── environment/
│ │ ├── __init__.py
│ │ ├── snake_env.py # Snake game environment
│ │ └── state_representation.py # State encoding utilities
│ ├── agents/
│ │ ├── __init__.py
│ │ ├── base_agent.py # Abstract base agent
│ │ ├── qlearning_agent.py # Q-Learning implementation
│ │ └── dqn_agent.py # Deep Q-Network implementation
│ ├── utils/
│ │ ├── __init__.py
│ │ ├── visualization.py # Plotting and visualization
│ │ └── logger.py # Logging utilities
│ └── models/
│ ├── __init__.py
│ └── dqn_model.py # Neural network architecture
├── tests/
│ ├── __init__.py
│ ├── test_environment.py # Environment tests
│ ├── test_agents.py # Agent tests
│ └── test_utils.py # Utility tests
├── main.py # Main training script
├── requirements.txt # Dependencies
├── README.md # This file
└── .gitignore # Git ignore file
The project includes comprehensive tests:
# Run all tests
python -m pytest tests/ -v
# Run specific test file
python -m pytest tests/test_environment.py -v
# Run with coverage
python -m pytest tests/ --cov=src --cov-report=html- Average Score: Mean score over last 100 episodes
- Best Score: Highest score achieved during training
- Success Rate: Percentage of episodes with score > 0
- Training Time: Time taken to complete training
Key parameters can be adjusted in the training script:
- Grid size (default: 10x10)
- Number of episodes (default: 1000)
- Learning rate (default: 0.1 for Q-Learning, 0.001 for DQN)
- Epsilon decay (default: 0.995)
- Reward values (food: +10, collision: -10, step: -0.1)