Q-Learning
Learn the best move in each square of a grid from rewards alone.
Press Play, or slide through the episodes. The mouse learns from rewards alone which way to go from every square.
- cheese, +10
- cat, −10
- each step, −1
- best path so far
- tap a square: wall, cat, open
Q(F2, ↑) ← 9.96 + 0.50 × (10 − 9.96) = 9.98
Episode 15: the mouse took its best move ↑ from F2 to F1, reward 10: it found the cheese. It tries a random move with chance ε = 0.10.
F2: Q = ↑ 9.98, → 3.98, ↓ 1.86, ← 0.00; best move ↑
| A1 | −3.70 | −3.77 | −3.58 | −3.70 | ↓ |
| B1 | −3.67 | −3.66 | −3.37 | −3.66 | ↓ |
| C1 | −3.70 | −3.63 | −3.68 | −3.61 | ← |
| E1 | 0.00 | 0.00 | 0.00 | 0.00 | – |
| A2 | −3.75 | −3.70 | −3.75 | −3.70 | → |
| C2 | −3.56 | −3.37 | −3.38 | −3.37 | → |
| E2 | 0.00 | 0.00 | 0.00 | 0.00 | – |
| F2 | 9.98 | 3.98 | 1.86 | 0.00 | ↑ |
| A3 | −3.88 | −3.90 | −3.84 | −3.73 | ← |
| C3 | −3.09 | −3.02 | −3.23 | −3.02 | ← |
| D3 | −2.65 | −5.00 | −2.80 | −3.03 | ↑ |
| F3 | 7.85 | −0.50 | −0.87 | −5.00 | ↑ |
| A4 | −3.98 | −4.04 | −4.26 | −4.01 | ↑ |
| B4 | −3.93 | −3.49 | −4.11 | −3.84 | → |
| C4 | −3.11 | −2.07 | −3.55 | −3.44 | → |
| D4 | −2.45 | 0.25 | −2.72 | −2.94 | → |
| E4 | −8.75 | 3.01 | −1.76 | −1.75 | → |
| F4 | 5.60 | −1.43 | −1.43 | −1.91 | ↑ |
| A5 | −4.34 | −4.28 | −4.46 | −4.31 | → |
| B5 | −4.16 | −4.10 | −4.29 | −4.15 | → |
| D5 | −2.89 | −2.26 | −2.26 | −2.58 | → |
| E5 | −1.90 | −2.13 | −2.07 | −2.17 | ↑ |
| F5 | −1.81 | −1.85 | −1.85 | −1.98 | ↑ |
Try it in Python
The Lab's maze, α, γ, ε and training so far, replayed in Python with the Lab's own random numbers, so the Q-values match the Lab's. It prints the last update, the best path and a map of the best move in every square. NumPy keeps the Q-table in an array; Plain Python in lists.
What you are looking at
A mouse starts in the bottom-left square of a maze. It can move up, right, down or left. Finding the cheese is worth +10, meeting the cat costs 10, and every step costs 1, so a short safe path is best. Nobody tells the mouse the way: it only feels the rewards.
Each square is cut into four triangles, one for each move. A triangle turns green when that move looks good and orange when it looks bad. The arrow shows the best move from each square, and the dashed line is the path the mouse would take if it always picked its best move. The mazes are made up, and Refresh Data shows another one.
What each symbol means
- s, a
- A square (the state) and a move from it (the action).
- s′
- The square the move leads to.
- r
- The reward for the move: −1, +10 or −10.
- Q(s, a)
- What the mouse thinks the move is worth: the reward it expects from here on.
- α
- The learning rate: how far each update moves Q.
- γ
- How much later rewards count compared with rewards now.
- ε
- How often the mouse tries a random move instead of its best one.
- n
- How many episodes the mouse has played. An episode ends at the cheese, at the cat, or after 100 moves.
How the mouse learns
At first every Q is 0, so the mouse wanders. After each move it updates the value of the move it just made, using only what it saw: the reward, and the best value it already gives the square it landed on.
The part in brackets is the surprise: how much better or worse the move turned out than expected. Q moves a fraction α of the way towards it.
The first time the mouse finds the cheese, only the last move learns that it is good. Next time, the move before it sees a square worth something, and learns that too. Episode by episode, the good news spreads backwards from the cheese to the start, until the arrows lead all the way there.
Exploring, and the other settings
If the mouse always took its best known move, it might keep using the first path it found and never discover a shorter one. So with chance ε it tries a random move instead. With ε = 0 it never explores; with a large ε it wanders so much that its runs are long and messy.
A large α learns fast but forgets fast; a small α learns slowly but steadily. In this maze nothing is random except the mouse's own choices, so a large α works well. γ near 1 makes the mouse patient: the cheese still counts when it is many steps away. A small γ makes it short-sighted, and the good news from the cheese fades before it reaches the start.
Click a square to turn it into a wall, then a cat, then back. The mouse learns the new maze from scratch, with the same luck as before.
The model in short
- Act: pick the best known move, or with chance ε a random one.
- Observe the reward and the next square.
- Update Q for that move towards the reward plus γ times the best value of the next square.
- Repeat over many episodes, until the best moves lead to the goal.