A reinforcement learning implementation of the classic Snake game using Deep Q-Network (DQN). The agent learns to play Snake through self-play using PyTorch.
- Dueling DQN Architecture: Advanced neural network with value and advantage streams
- Prioritized Experience Replay: Focuses learning on important experiences
- N-step Returns: Better value estimation through multi-step returns
- Optimized Environment: Fast game engine with collision detection and visual rendering
- Multiple Training Modes: Train, play, evaluate, and benchmark the agent
Average score: ~43 | Best: 80+ | Success rate: 100%
- Clone and enter directory:
git clone <https://github.com/thanhtulearncode/Snake>
cd Snake- Create virtual environment:
python -m venv venv
venv\Scripts\activate # Windows
source venv/bin/activate # Linux/Mac- Install dependencies:
pip install -r requirements.txt# Train agent
python main.py --mode train --episodes 5000
# Watch agent play
python main.py --mode play --games 5
# Evaluate performance
python main.py --mode eval --episodes 500
# Benchmark speed
python main.py --mode benchmarkArguments:
--mode: train, play, eval, or benchmark--episodes: number of episodes (default: 5000)--render: show game during training--games: number of games in play mode (default: 5)
Algorithm: Dueling DQN with n-step returns, prioritized replay, and double Q-learning
State: 24 features (danger detection, direction, food position, space analysis)
Actions: 3 (turn left, straight, turn right)
Rewards: Food consumption (+10 + length bonus), approaching food (+0.8), collisions (-10)