Skip to content

thanhtulearncode/Snake

Repository files navigation

Snake RL

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.

Features

  • 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

Performance

Average score: ~43 | Best: 80+ | Success rate: 100%

Installation

  1. Clone and enter directory:
git clone <https://github.com/thanhtulearncode/Snake>
cd Snake
  1. Create virtual environment:
python -m venv venv
venv\Scripts\activate  # Windows
source venv/bin/activate  # Linux/Mac
  1. Install dependencies:
pip install -r requirements.txt

Usage

# 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 benchmark

Arguments:

  • --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)

Technical Details

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)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages