Introduction to Reinforcement LearningPart 1 · Foundations

Lecture 3MDPs and dynamic programming

States and consequences come back: Markov decision processes, returns and discounting, value functions and the Bellman equations, and the dynamic-programming algorithms that solve an MDP when the model is known: policy evaluation, policy iteration and value iteration.

By the end you should be able to write down an MDP, its Bellman equations, and run policy evaluation, policy iteration and value iteration by hand on a small example.

Key ideas, one per episode

  • Ep. 10States, actions, dynamics, rewards, and a discount.
  • Ep. 11Expected. Cumulative. Reward.
  • Ep. 12Turn the equals sign into an update, and sweep.
  • Ep. 13RL is an iteration of two problems: evaluation and control.
  • Ep. 14One sweep, then improve. And sweep where it matters.

Based on: Lecture 3: Markov Decision Processes and Dynamic Programming, DeepMind × UCL Reinforcement Learning Lecture Series (2021), by Diana Borsa. The slides follow that order of topics, in our own words, figures and examples; they are not official material of the original course.

The title slide of Lecture 3: MDPs and dynamic programming

57 slides, written to be read on your own, with worked examples, “try it” boxes, small simulations and check-yourself questions (answers at the end). PDF, 1.7 MB.

Watch

5 short episodes of ACML Explains for this lecture. Pick one from the list, and open the transcript to read along: each timestamp plays the episode from there. The narration is generated with a voice clone of Teddy, used with his permission.

Episode 10 · 5:02

From one state to many

A bandit has one state. Chess has more boards than you can count, and every move changes the next one. Welcome to Markov decision processes.

States, actions, dynamics, rewards, and a discount.

TranscriptEpisode 10

From one state to many

Welcome back. About three hundred million years have passed since the last episode. Roughly. Last module, Bit lived in a world with one state: a row of slot machines. Pull, get paid, pull again. Nothing he did changed the world. Real problems aren't like that. In chess, I take your queen, and on the next move you checkmate me. Today: many states, and consequences.

Markov

First, a name you'll hear a lot: Markov. And by the way, Markov is a surname. It's a person, not a thing. A Markov chain is a process with no memory. To predict what comes next, the present is enough. Knowing the whole history doesn't make the prediction any better. Is the weather Markov? Not really. Today's weather alone won't tell you that winter is coming. The season matters. Chess? Yes. Give me the board, and I don't care how you got there. Quick quiz. Let's find a victim. A card game where you only see your own hand. Markov? Not from your seat. Which cards were already played tells you a lot. The history matters. A Markov chain, you watch. Things happen to you, and you just observe. A Markov decision process, you act. Your choices change what happens next. That's the difference.

The MDP

So, formally, an MDP is a few things together. S: the set of all the states. A: the set of all the actions. p: the dynamics, the probability of landing in each next state, with each reward, after an action. And γ, the discount. We'll get to it in a minute. In chess, S is every legal board, and A is every legal move. Both are enormous. That problem will follow us all season. So an MDP is the rules of the game. The agent's job is to play it well.

Why probabilities?

Why probabilities? Picture a remote-controlled robot arm. You tell it: lift ten degrees. It lifts nine. The motor, the sensor, the control loop… infinite reasons. So the same action doesn't always land in the same place. Now, if I knew p perfectly, could I play perfectly? In principle, yes. Think two hundred moves ahead, in your head. Not feasible for big games. But in principle, p is everything. p is the dynamics. And we'll assume p never changes. If it does, the rules change under your feet. That's called concept drift. No concept drift in this course. Want concept drift? Do a PhD.

The can-collecting robot

An example. A robot that collects soda cans. Its battery is either high or low. Two states. With a high battery, it can search for cans, or wait for someone to bring one. With a low battery, it can also go back and recharge. And if it searches on low, it might run flat and need rescuing. Searching pays more than waiting. Rescues cost a lot. Those numbers are the reward, and we choose them. Now, a game. What if waiting paid more than searching? The best strategy is… wait. Forever. Is that what you want from a cleaning robot? Same lesson as before: define the reward properly. And notice: recharging now means more cleaning later. Today's choice changes tomorrow's options. That's what sequential means.

The return and the discount

So what is Bit after now? The return: the sum of the rewards from here on. Problem: if Bit gets one point every step, forever, the sum is infinity. Every policy that never stops looks the same. Also, honestly? We don't care that much about what happens in a thousand years. So we discount. Each step further away, multiply by γ, a number between zero and one. Now the sum is finite. Say every reward is at most 10. Then the return is at most 10 over one minus γ. With γ at 0.9: 100. γ near one: Bit looks far ahead. γ near zero: he looks only at the next step. γ equals zero: pure greed. Think of it like the horizon at sea. You know there's more out there, it just doesn't bother you. With γ at point nine, that horizon is about ten steps. At point nine nine, about a hundred.

Why discount?

Why else discount? Money now is worth more than money later: it earns interest. The future is uncertain, and our model of it is never perfect. People and animals prefer rewards sooner, too. There are other options, like the average reward per step. But the discounted return is the classic default. Average reward is handy for tasks that never end, like a server that runs forever. Does γ actually help your problem? Run it once with γ and once without. Congratulations, you just got a project idea.

What Bit will collect

One last thing. The return at time t isn't what Bit collected. It's what he will collect, from now on. It's in the future, so it's random. That's why next time we'll talk about its expected value. So, an MDP: states, actions, dynamics, rewards, and a discount. The whole sequential world in one tuple. Next: what does it mean for a policy to be the best? With me so far?

Interactive lab

Dynamic programming lab

Watch policy evaluation, value iteration and policy iteration sweep a small gridworld. Change the discount, make moves slippery, add walls, and see the values settle and the greedy arrows lock in.

Here the model is known: every move costs −1 and the two shaded corners end the episode, as in the 4×4 gridworld of Sutton and Barto (Example 4.1). Each sweep applies a Bellman equation once to every cell.

Simple math

One Bellman update per cell, every sweep

q(s, a) = Σ p(s′ | s, a) [ r + γ v(s′) ] is what action a is worth in cell s, given the current values. The three algorithms differ only in what they do with it.

Policy evaluation
v(s) ← Σₐ π(a | s) q(s, a)

Average over what the policy does. Here the policy is random: each of the four moves with probability ¼.

Value iteration
v(s) ← maxₐ q(s, a)

Take the best action instead of the average. The values converge to v*, and the greedy arrows to an optimal policy.

Policy iteration
Evaluate π, then π(s) ← argmaxₐ q(s, a)

Evaluate the current policy until its values settle, make it greedy, and repeat until the policy stops changing.

Sweeps
0
Largest change Δ
–
Arrows last changed
–
Improvements
–

Values start at 0. Press One sweep.

Values and greedy arrows

Darker cells are worse (further from an exit). The arrows are the greedy actions; ties show more than one.

How much the values still change

The largest change in any cell, per sweep, on a log scale. Dotted lines mark policy improvements.

Δ per sweep

Play

A game that goes with this lecture, and what to look for while you play.

Markov’s Maze

A maze where every step is a roll of the dice.

In this lecture: Every rune tile has its own transition probabilities: where you meant to go, the opposite way, left, right, or stay. Hover over a tile to read them. You are playing inside an MDP; plan for where you are likely to land, not only where you aim.

Each rune tile resolves your move with its own five probabilities: where you meant to go, the opposite way, left, right, or stay. Reach the goal seal without falling into traps or lava, while cursed knights patrol.