All projects

03Machine learning

RL Snake

Watch four deep Q-learning checkpoints play Snake, from random moves to deliberate strategy, streamed live from a PyTorch inference API.

  • Python
  • PyTorch
  • DQN
  • FastAPI
Connecting to the model server…

Pick an agent

Same network, four points in training.

Warming upThe agents are loading on the model server. This page will unlock as soon as it responds.

Score
0
Moves
0
Status
Ready

Project Overview

This project trains Deep Q-Network (DQN) reinforcement learning agents to play Snake, using the same class of algorithm that underpinned DeepMind's Atari breakthroughs, and hosts four training-stage checkpoints so you can watch intelligence emerge in real time. Pick an agent above and press Play to watch it.

Untrained

0 episodes: pure random actions, dies almost immediately.

Beginner

~50 episodes: learns to avoid immediate walls and survive briefly.

Intermediate

~250 episodes: navigates toward food and avoids its own tail.

Advanced

~1,000 episodes: efficient navigation, high scores, deliberate strategy.

Deep Q-Learning

DQN is a model-free reinforcement learning algorithm that approximates the optimal action-value function Q(s, a) with a neural network. The agent receives a reward signal (+10 for food, −10 for dying, 0 otherwise) and adjusts its network weights to maximise cumulative reward over time; no hand-coded rules, just trial, error, and gradient descent.

State Space

An 11-dimensional boolean vector: danger ahead / left / right, current heading (4 booleans), and food direction relative to the head (4 booleans). Compact enough for a small fully-connected network to process in microseconds.

Neural Network

Three fully connected layers (11 → 256 → 256 → 4 outputs). The four output Q-values correspond to the four actions; the agent selects the highest-valued action at each step.

Experience Replay

Transitions (s, a, r, s′) are stored in a fixed-size replay buffer and sampled randomly during training, breaking temporal correlations and stabilising gradient updates.

Target Network

A periodically frozen copy of the main network provides stable Q-value targets, preventing the moving-target feedback loops that cause naïve Q-learning to diverge.

About This Project

Technologies Used

  • PyTorch: Deep learning framework used to define, train, and serialise the DQN neural network
  • Python: Core training loop, game environment, replay buffer, and epsilon-greedy scheduler
  • FastAPI: Lightweight Python backend exposing session-based REST endpoints (start / step / reset / health) for the frontend to drive the agent one move at a time
  • Next.js & React: Frontend application with real-time game state polling and canvas rendering
  • Canvas 2D API: Browser-side game board rendering, drawing grid lines, snake segments, and food each frame from the API response
  • Railway: Cloud hosting for the Python inference API, with a wake-up endpoint to handle cold-start latency

Key Features

  • Four training-level checkpoints for direct side-by-side behavioural comparison
  • Session-based API design: each game run is isolated, enabling multiple concurrent viewers
  • Live canvas rendering of the full 20×20 grid at 150 ms per tick
  • Live score and move count, updated with every API step
  • Automatic wake-up with live progress while the Railway host cold-starts
  • One step request in flight at a time, with play, pause, reset and 2× speed