2023–24, dusted off in 2026
learned ML by building game-playing AIs instead of doing the usual tutorials.
i wanted to learn ML, but the usual starter projects weren't doing it for me. games were eye-catching enough that i actually wanted to know what was going on inside them, so i built two: a flappy bird agent with deep Q-learning, and a top-down racing car that learned a circuit with tabular Q-learning. then, while putting this portfolio together, i dusted them both off, re-ran them, and i fixed them. the fixed versions are playable, so you can race them.
the setup
a stripped-down flappy bird clone, and an agent that sees seven numbers every frame: how far the top and bottom of the next gap are from the bird, the same for the gap after it, how far away the next pipe is, its own vertical speed, and its height. it gets two moves, flap or don't. a small neural network scores both and the agent picks the better one.
that's deep Q-learning. nobody tells it anything about pipes. it just gets rewarded, gets punished, and crashes. a lot.
the part where it didn't quite learn
back in 2023 i trained it, watched it clear a few pipes, and optimistically wrote in the README that it had started to learn.
cut to this year. i'm adding it to this portfolio, i re-run that exact recipe on the same game, and it averages 0.15 pipes. so it hadn't really started learning yet. but it did leave a good trail of clues.
reading the code back, there were a few things worth fixing. (i didn't test them one at a time, so i can't tell you which one did the real damage. any of them could have.)
raw pixel values in the hundreds went straight into the network with a learning rate of 0.05, which i think saturated it. that would explain the stuck-on-one-action behaviour 2023 me put down to exploding gradients.
dying cost −1000 while a pipe paid +10, and with a discount of 0.9, a pipe forty frames away was worth about one and a half percent of its +10. next to the −1000, pipes barely registered.
a safety rule took over near the floor and ceiling, but memory recorded the move the agent had chosen, not the one that actually happened. so it was learning from moves it never made.
exploration switched off after 80 games. and the training target wasn't detached, so every update also nudged the thing it was aiming at.
dusting it off
same game, same family of algorithm, just a sturdier recipe.
inputs are scaled to roughly −1 to 1 and measured from the bird. rewards are +0.1 per frame alive, +1 per pipe, and −1 for a crash, with a discount of 0.99 so the next pipe actually stays in view. double DQN with a target network, Huber loss, and gradient clipping (which the 2023 README had already suggested as the next thing to try. good instinct, 2023 me). mini-batches from a replay buffer. and no safety override. it keeps itself alive now.
the change that made the biggest difference was the one i least expected: letting it see the gap after next. consecutive gaps can be far apart with very little time between them, so you have to start moving before you've even cleared the current pipe. without that, every version i tried plateaued.
also, DQN on this task swings between brilliant and hopeless from one evaluation to the next, and most of the later checkpoints in the run fell apart again. so i keep the best checkpoint instead of the last one, and the numbers below are on layouts that weren't used to pick it.
flappy bird, scoreboard
on 100 pipe layouts it never saw in training or selection, capped at 200 pipes, the fixed agent averages 85.9. the 2023 version averages 0.15. and a hand-written rule (flap when you drop near the bottom of the gap) averages 69.5.
so it beats a rule that fits in one sentence. i'll take it.
the car, round one
the racing car came a year later. a top-down pygame game on a trace of the Bahrain circuit, learning with a table of Q-values. when i dusted this one off and re-ran its state, reward, and settings on the rebuilt game, from 32 starting points round the lap it never trained from, it covered 0.40% of a lap on average.
the table had room for millions of states. it visited 13 of them. i think that's because each distance sensor was split into five bins 240 pixels wide, so being a car's length from a wall looked exactly the same as being in the middle of the road.
the reward paid for distance driven, not distance round the track, so driving in circles counted as progress. and the game itself had a couple of quirks: hitting a wall only halved your speed, and the car moved twice per frame whenever you weren't steering.
the car, round two
same fixes as flappy bird, plus one i had to build from scratch: a way to measure progress. i flood-fill the track image once, starting from the finish line, so every pixel of road knows how far round the lap it is. the reward is how much of that the car gained this frame. circles don't pay anymore.
the table becomes a small network. the sensors become seven distances that turn with the car, plus its speed. a crash ends the run. it trains from starting points all round the lap and gets judged on others it never saw.
and same instability as before. the score swung hard between checkpoints and several later ones collapsed, so again i keep the best one, not the last.
the car, scoreboard
from 32 held-out starting points, the fixed agent covers 90.6% of a lap on average and finishes 29 of them. from the start line it does a full lap in 18.05 seconds. the 2024 version covers 0.40%.
a short hand-written rule does better, though: steer toward the more open diagonal, slow down when the road ahead closes. it covers 93.8% and finishes 30. turns out sensors make this track easy if you already know how to drive. the agent had to work that out from reward alone, so i'm calling it a moral victory.
2026 · from scratch
a pendulum on a sliding cart, and a pile of learning algorithms racing to balance it. no RL libraries.
a block on a rail with a pendulum hanging off it. push the block left or right, get the pendulum upright, keep it there. the twist is that i wrote all of it myself: the physics, the reward, the training loop, and the graphs. no gymnasium, no RL libraries. a very confident thing to say right before deriving equations of motion by hand.
the setup
a 15 kg block on a 10 metre rail, a 2 metre arm, a 5 kg bob on the end, and a motor that pushes with up to 100 newtons. physics runs 240 times a second. the agent gets a say 30 times a second, and that number isn't a guess: an upright pendulum this long starts to fall in about half a second, and catching it takes ten-odd decisions in that time.
two modes. easy starts almost upright, so it's just balancing. normal starts hanging straight down, so it has to swing itself up first. that's where things got interesting (read: stalled).
the reward, round two
the first reward paid the running total of time spent upright, every step. so step 100 paid a hundred times what step 1 did, and an episode's score grew with the square of its length. oops.
the fixed one scores one step at a time: mostly how upright the arm is, minus a little for drifting toward the walls, minus a little for spinning. that last one is there because a pendulum whirling round and round is above the rail half the time, and spinning averages half the reward for zero skill.
none of it gets checked by staring at numbers. it gets checked by ordering: upright and still has to beat hanging, the middle has to beat the wall, still has to beat spinning. the first version rewarded spinning (a sign error) and failed that test straight away.
a very big spreadsheet
first real learner: tabular Q-learning. chop every number the agent sees into bins, keep a table of how good each push is from each bin, nudge the table after every step.
with eight angle bins, the whole region where balancing is even possible fit inside two of them. so to the agent, nearly upright and about to fall looked identical. thirty-six bins fixed that, and it solved easy mode.
also, on a fresh all-zero table, picking the best push always picked the first one: full left. it pushed left 80% of the time until i broke ties at random.
normal mode was another story. on 100,000 episodes of the swing-up, the best it ever managed was 12.0 seconds above the rail, and by the end it was averaging 2.1. one table has to cover a big, coarse swing and fine control at the top, and every extra bin multiplies the table.
monte carlo
next up, Monte Carlo control. instead of updating after every step, it plays a whole episode, then walks back through it and credits every push with what actually happened afterwards. no guessing about the future, it just waits and finds out.
it solved easy mode inside 1,000 episodes: every one of its last 100 held for the full 10.0 seconds. the Q-learning run i'm comparing it to was still averaging 5.7 at the end of 10,000. (they don't use the same bins or the same number of pushes yet, so call it a promising heat, not a fair race.)
on the swing-up it gets above the rail and holds it for up to 10.3 seconds, averaging 2.8 by the end. better, not solved.
the original plan was model-based RL: learn the physics, then plan through it. that's still the next stop.