← All animations

🕹️ Reinforcement Learning

No one tells the agent the right answer. It just acts, gets a reward (or a penalty), and nudges its expectations. Repeat thousands of times and a table of guesses hardens into a confident policy: the agent has learned, by trial and error, which way to go. Below you can watch a grid-world agent feel its way from a random scramble to the optimal path — and then twist the knobs that decide whether it succeeds.

1. Learning a grid-world by trial and error (Q-learning)

The agent starts at S and wants the goal (+1), while pits (−1) end the episode badly and grey walls block the way. Each step costs a tiny −0.04 (so dawdling is punished). It keeps a Q-value for every (square, direction): its best guess of total future reward. Colour shows the value of each square; the arrow is the current best action. Train and watch the policy crystallise.

Episode 0 · last reward —

Left: brighter = higher learned value; arrow = greedy action. Right: reward per episode — it climbs and steadies once the agent reliably reaches the goal.

2. The knobs — explore vs exploit, discount & reward

The same algorithm, but now you set the dials, then press Auto-train. ε (epsilon) is the chance of a random move: too greedy (ε→0) and the agent gets stuck on the first half-decent route; too random (ε→1) and it never commits. γ (gamma) is how much it cares about future reward: low γ is short-sighted, high γ plans ahead. And the reward design shapes everything. Drag the goal or a pit to a new square and watch the policy relearn.

ε = 0.20
γ = 0.90 −0.04 / step Episode 0

Try ε=0 (watch it freeze on a poor path), then ε≈0.2 (it finds the optimum), then ε=1.0 (it never settles). Drag the goal — the value field flows toward its new home.

3. The big picture — the agent–environment–reward loop

Reinforcement learning is just one tight loop, run over and over:

The exploration–exploitation trade-off is the heart of it: exploit what you know works, but keep exploring in case something better is out there. This same loop — maximise long-run reward by trial and error — is what trains an agent to play Go, Atari and StarCraft from raw pixels, and what teaches a robot to walk: the grid here is tiny, but swap the squares for a game screen or a robot's joints and the idea is identical.