SnakeAI

A Snake game AI project featuring two implementations of Deep Q-Network (DQN) algorithms, built with PyTorch.
Python PyTorch AI

About

A Snake game AI project featuring two implementations of Deep Q-Network (DQN) algorithms. The project compares the original DQN (V1) with an enhanced version (V2) that includes improved hyperparameters, reward shaping, and architectural enhancements. Built with PyTorch.

References:

Current V2 model performs 72.5% better then the V1 model as compared in a 1000 games played.

Features

Project Structure

snake-ai/
├── ai_demo.py              # Watch your AI agents play (V1 vs V2)
├── dqn_agent_v1.py         # Original DQN implementation
├── dqn_agent_v2.py         # Enhanced DQN implementation  
├── game.py                 # Play the game yourself
├── headless_game.py        # Fast training without graphics
├── snake_engine.py         # Core game logic
├── base.py                 # Game settings
├── model/                  # Trained AI models
│   ├── dqn_agent_v1.pth                # DQN V1 model
│   ├── dqn_agent_v1_data.json          # DQN V1 training data
│   ├── dqn_agent_v2.pth                # DQN V2 model
│   └── dqn_agent_v2_data.json          # DQN V2 training data
└── pyproject.toml          # Dependencies

Installation

# Clone the repository
git clone https://github.com/seriva/snakeai.git
cd snakeai

# Install dependencies
uv sync

Quick Start

1. Play the Game Yourself

uv run game.py

2. Train the AI Agents

Train DQN V1 (Original):

uv run dqn_agent_v1.py

Train DQN V2 (Enhanced):

uv run dqn_agent_v2.py

3. Watch the AI Play

uv run ai_demo.py

Watch both agents play side-by-side for direct comparison!

AI Agents

DQN V1 (Original)

The classic Deep Q-Network approach that learns which actions are good in each game situation.

DQN V2 (Enhanced)

An improved version of the standard DQN with several enhancements:

What the AI Sees

The AI gets 16 pieces of information about the game:

  1. Danger Detection (8 features): Is there danger in each of 8 directions around the snake head?
  2. Current Direction (4 features): Which way is the snake currently moving?
  3. Food Location (4 features): Which direction is the food relative to the snake head?

Both agents use this same information to make decisions.

Training

How the AI Learns

Training Time