Skip to content
Back to all work

Case study: CS 499 Enhancement One, software design and engineering

Pirate treasure-hunt agent

I took the deep Q-learning notebook I wrote for CS 370 and rebuilt it as a Python package a team could maintain: eight modules, a command-line tool, 140 tests and a model loader that refuses tampered files. Then I measured every claim I made about it.

Faster training on the same laptop: 179.9 s down to 18.0 s
10x
Faster training on the same laptop: 179.9 s down to 18.0 s
Seeds where every cell takes a shortest route, the lowest at 49 of 50
9 of 10
Seeds where every cell takes a shortest route, the lowest at 49 of 50
Known security advisories in the dependencies
19 to 0
Known security advisories in the dependencies
My role
Solo: design, code, tests and CI
When
Built for CS 370 in October 2026, rebuilt in November
Stack
Python, Keras, TensorFlow, pytest, Ruff, GitHub Actions

01

Where it started

For CS 370 I trained a pirate to find treasure in an 8 by 8 maze using deep Q-learning. It worked, but it lived in one notebook. The model was a global shared between cells, settings were magic numbers, an early-stopping setting was declared and never read, and the trained model was never saved.

For my capstone I wrote a code review of that notebook first. It found twelve problems, numbered S1 to S12, and every change in the rebuild traces back to one of them. I kept the original in the repository, untouched, so anyone can compare the two.

AreaCS 370 notebookThe rebuilt package
StructureOne notebook, a global model and epsilon8 modules, no global state
MazesOne hard-coded 8 by 8 mazeMaze files and a solvable-maze generator, all validated
SettingsGlobals and magic numbersOne TOML file, every key checked for type and range
Early stoppingpatience = 10 declared, never readStops a stalled run after patience games
Trained modelNever savedSaved, and its SHA-256 checked before every load
TestsNone140, run by CI on every push
DependenciesKeras unpinnedPinned: 19 known advisories down to 0

02

Decisions that mattered

Each one came with a cost. Here is what I chose, and what I gave up for it.

  1. Winning is not the finish line. Shortest routes are.

    The original stopped training as soon as the pirate won a full window of games. When I ran ten seeds, that left anywhere from 33 to 50 of the 50 routes as short as they could be. So now training keeps going after every cell wins: it audits every route after every game, keeps a snapshot of the best version so far, and restores it at the end. Polishing can never make the result worse.

    The trade-off: some runs play more games. I made the polish a setting, and setting it to zero gives back the original rule exactly.

  2. Make the audit cheap enough to run every game

    The pirate only sees the walls and its own cell, so its policy is really a table: the same move from the same cell, every time. I ask the network for every cell's move in one batched call instead of one call per step, which made a full audit cheap enough to run after every training game. A test checks the fast audit against the slow, step-by-step one.

  3. Test the training rules without TensorFlow

    The trainer depends on a two-method interface, not on TensorFlow. That let me test every stopping rule exactly, with stand-in agents, in milliseconds.

    The trade-off: one more seam to keep honest. A full train, evaluate and play cycle on a tiny maze runs the real network in the same suite.

  4. Treat a saved model as untrusted code

    Loading a crafted Keras file can run code on your machine (CWE-502), and the Keras version the course used had published bypasses of its safe mode. So before Keras ever reads a file, four checks have to pass:

    1. Its SHA-256 matches the one recorded when it was trained.
    2. The archive holds only the three entries a real model has.
    3. Every layer class is on an allowlist, so no custom code layers.
    4. A private copy then loads with Keras safe mode on.

    The tests attack it on purpose: a smuggled code layer, a renamed old-format file, an extra archive entry and a single changed byte. Each one is refused.

03

What it measured

Every number here comes from scripts in the repository, and the raw output is saved beside them. The training speed was measured on one laptop with the original seed and maze.

Ten times faster, from the code alone

Seconds to train on the CS 370 maze, seed 1671.

CS 370 notebook6.43 ms per move
179.9 s
New code, old libraries0.88 ms per move
26.9 s
New code, current libraries0.9 ms per move
18.0 s

Running the new code on the course's old library versions shows the speedup comes from the code, not from the upgrade.

Polishing closes the gap on every seed

Of the 50 open cells, how many reach the treasure by a shortest route: when every cell first wins, and after polishing.

  • seed 3From 33 to 50
  • seed 1From 44 to 50
  • seed 2From 44 to 50
  • seed 8From 44 to 49
  • seed 1671From 47 to 50
  • seed 5From 49 to 50
  • seed 9From 49 to 50
  • seed 4Already shortest from every cell: 50
  • seed 6Already shortest from every cell: 50
  • seed 7Already shortest from every cell: 50

The real agent learning, game by game

Win rate over the last 32 games for the reference run. It wins from every cell at game 286, then spends 10 more games polishing routes.

0%25%50%75%100%050100150200250300Training gameEvery cell wins, game 286

From the reference run's log in the repository. A smaller version of this agent runs live on my home page.

  • 374

    Games before a run that could not learn stopped itself as stalled, instead of playing all 15,000.

  • 41 of 41

    Shortest routes on a randomly generated 42-cell maze, after 226 games of training.

  • 273x

    Faster replay memory inserts at 100,000 moves, from swapping a list for a bounded deque.

04

Check my work

The tests take about ten seconds. CI runs them on every push, along with linting, security rules, a spelling check and a dependency audit. A trained reference run is in the repository, so you can evaluate it without training anything.

# install and test
python -m pip install -r requirements-dev.txt
python -m pip install -e . --no-deps
python -m pytest

# audit the trained pirate from every cell, then watch one route
python -m pirate_agent evaluate --run pretrained/cs370-seed1671
python -m pirate_agent play --run pretrained/cs370-seed1671 --start 0,0