Forward Forecasting

← Portfolio
👾

PAC-MAN — DQN AI Player

From original 1982 Atari assembly source to a faithful Python port — then teaching a neural network to play it from scratch using Deep Q-Learning over 10 300 episodes and 18 hours of single-CPU training.

Training at a Glance
10 300
Episodes trained
18 h
CPU wall time
10.7 M
Gradient steps
79 %
Best maze clear
5 110
Peak single game
100 %
Success rate (ep 6 300)
Gameplay Screenshots
Ready screen
Ready screen
Gameplay start
Gameplay start
Frightened ghosts
Frightened ghosts
Death animation
Death animation
Game over
Game over
Training Results
Score progression
Score progression across 10 300 episodes
Dots eaten
Maze completion — dots eaten per checkpoint
Solve time
Steps to reach 1 200-point milestone
Success rate
Success vs. failure rate by checkpoint
Policy evolution
Policy evolution — maze snapshots every 500 episodes showing the agent learning to clear more dots
Checkpoint Evaluation (60 games each)
Checkpoint Mean score Dots eaten Maze % Success rate
ep 10085276 / 24431 %5 %
ep 2001 358115 / 24447 %72 %
ep 3001 642139 / 24457 %85 %
ep 1 3002 472191 / 24478 %100 %
ep 2 3002 383187 / 24477 %100 %
ep 3 3002 433187 / 24477 %100 %
ep 4 3002 395186 / 24476 %100 %
ep 5 3002 454189 / 24477 %98 %
ep 6 300 ★2 585193 / 24479 %100 %
ep 7 3002 429189 / 24477 %100 %
ep 8 3002 213176 / 24472 %93 %
ep 9 3002 007158 / 24465 %83 %
ep 10 3002 400184 / 24475 %100 %

★ Peak at ep 6 300. The dip at ep 8 300–9 300 reflects natural DQN oscillation before recovery. Success = scoring ≥ 1 200 points per episode.

Training Runs
RunEpisodesWall timeBest scoreNotes
Run 130021 min2 160ε decayed 1.0 → 0.05; buffer filled
Run 25 000478 min4 310Resumed from ep 300; ε = 0.05 fixed
Run 35 000590 min5 110Resumed from ep 5 300; ε = 0.05 fixed
Total10 300~18.1 h5 110Single CPU, no GPU
Neural Network Architecture
Input (81 features)
  │  Pac-Man pos · direction · ghost positions + states + distances
  │  fright timer · dots remaining · 7×7 local maze window
  │
Linear(81 → 256) → ReLU
  │
Linear(256 → 256) → ReLU
  │
Linear(256 → 4)  →  Q-values: [ UP  DOWN  LEFT  RIGHT ]

145 000 trainable parameters
Optimiser: Adam (lr = 1e-4)   Loss: Huber (SmoothL1)
Replay buffer: 100 000 transitions   Target sync: every 1 000 steps
Why Pac-Man is Hard for an AI

Astronomical state space

~240 walkable cells × 4 ghost positions × AI states × 2²⁴⁴ dot configurations. The agent must generalise across a state space it can never fully explore.

Sparse & delayed rewards

The level-clear bonus (+100) arrives hundreds of steps after the navigation choices that made it possible — the classic credit-assignment problem.

Deceptive reward landscape

The same tile is worth +1 (safe dot) or −50 (fatal collision) depending on ghost positions and whether a power pellet was recently eaten.

Non-stationary targets

Unlike supervised learning with fixed labels, DQN trains against Bellman targets computed from the same network it is updating — inherently unstable without experience replay and a frozen target network.

No memory

The network receives a single snapshot per step. It cannot remember a ghost's trajectory two steps ago and must re-derive all context from the current frame.

CPU scale

10 million gradient cycles on a single CPU takes ~18 hours. The same training would complete in ~30 minutes on a modern GPU. Every behaviour was discovered from random weight initialisation through pure trial and error.

Reward Shaping
EventReward
Eating a dot+1.0
Eating a power pellet+5.0
Eating a ghost (chain: ×1 / ×2 / ×3 / ×4)+20 / +40 / +80 / +160
Dying−50.0
Clearing a level+100.0
Per step (discourages idling)−0.05