Markov decision processes: states, actions, rewards and the Bellman equations
An MDP on a grid world with a cliff, return and discount, the value function and the Bellman equation, value iteration and policy iteration written from scratch, how the discount, slipping and step cost change optimal behaviour, and a check by simulation.
Series AI basics Part 47 of 52
- Map of artificial intelligence: what is what and how it all fits together
- A brief history of artificial intelligence: from Turing to language models
- Data, model, training, inference: the basic terms on a single example
- How to read mathematical notation: symbols, indices, sums and quantifiers
- Sets and relations: the building blocks of definitions in AI
- Functions and mappings: why every AI model is a function
- Logic and proofs: the rules AI reasons by
- Graphs and trees: the structure that both search and neural networks stand on
- Linear algebra for machine learning: vectors, matrices and why AI mostly multiplies
- Derivatives, the gradient and the chain rule: how a model finds out which way to learn
- Gradient descent: how a model learns
- What probability is: from rolling dice to the Kolmogorov axioms
- Conditional probability and Bayes’ theorem: how to update your estimate when you learn something new
- Random variables and the cumulative distribution function: one function that tells you everything about a distribution
- Probability density and probability mass functions: why a continuous variable has zero probability at a point
- Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what
- Overview of probability distributions: Bernoulli, binomial, Poisson, uniform, normal and exponential
- Law of large numbers and central limit theorem: why averages behave so predictably
- Statistics and parameter estimation: maximum likelihood, MAP and confidence intervals
- Information theory: entropy, cross-entropy and KL divergence — where loss functions come from
- State-space search: BFS, DFS, UCS and A* with animations
- Games and adversaries: minimax, alpha-beta pruning and Monte Carlo tree search
- Constraint satisfaction: backtracking, constraint propagation and AC-3 on sudoku and a map of Czech regions
- Logic and knowledge systems: forward and backward chaining, resolution and expert systems
- Planning: STRIPS, the blocks world, heuristics from relaxation and planning in AI agents
- Local search and simulated annealing: a travelling salesman through the Czech regional capitals
- Genetic algorithms: population, crossover, mutation and selection on a backpack for a hike
- Evolution strategies and genetic programming: from the one-fifth rule to Kepler’s law
- Ant colony optimization: how a pheromone trail finds a short path
- Particle swarm optimisation: inertia, personal experience and the swarm’s experience
- Neuroevolution: evolving the weights and shape of neural networks, and the NEAT algorithm
- Fuzzy sets and membership functions: how to write down “somewhat warm”
- Fuzzy logic and probability: why “somewhat warm” does not mean “probably warm”
- Fuzzy logic: t-norms, implications and IF–THEN rules
- Fuzzy control: Mamdani and Sugeno from a fan to a pole on a cart
- Fuzzy clustering: the fuzzy c-means algorithm and points between clusters
- Neuro-fuzzy systems (ANFIS): fuzzy rules that learn from data
- Markov chains: the transition matrix, the stationary distribution and PageRank
- Hidden Markov models: the Viterbi algorithm and the dishonest casino
- Bayesian networks: why the grass is wet and what it means for the sprinkler
- The Kalman filter: how to estimate where a car is and how fast it goes from imprecise measurements
- Types of machine learning: supervised, unsupervised, semi-supervised and more
- Prompts, context and RAG: how to give a model the right information
- Variational autoencoders (VAE): new images from a continuous latent space
- Generative adversarial networks (GAN): the forger versus the detective
- Diffusion models: how pure noise becomes an image
- Markov decision processes: states, actions, rewards and the Bellman equations
- Q-learning and DQN: learning the value of actions, from a table to a neural network
- Policy gradient and PPO: learning the policy directly
- How an AI agent works: a language model in a loop with tools
- Programming with AI agents: the task, tests and review
- Evaluating and securing AI systems: how to measure and how to keep watch
Full series syllabus 52 of 83 topics published
- How a reinforcement learning task is described as an MDP.
- What the value of a state is and how the Bellman equation computes it.
- Why small details in the rewards and the discount completely change an agent’s behaviour.
Useful to know first: Markov chains: the transition matrix, the stationary distribution and PageRank, Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what, Types of machine learning: supervised, unsupervised, semi-supervised and more
So far the models in this series have learned from examples with the correct answer, or searched for structure in data. Reinforcement learning is a third way: an agent acts, the world gives it rewards and punishments for it, and the agent has to work out how to act so as to collect as many of them as possible. Before we get to learning, we need a language in which to describe such a task precisely. That language is the Markov decision process. In this part we build one on a small world with a cliff and compute the best behaviour exactly — assuming we know the rules of the world.
It follows on from the parts Markov chains, Expected value, variance and covariance and Types of machine learning.
1. Agent, environment, reward
Definition — Markov decision process (MDP)
Let
- S be a finite non-empty set (the set of states),
- A be a finite non-empty set (the set of actions),
- P : S × A × S → [0, 1], (s, a, s′) ↦ P(s′ | s, a), be a function such that ∑s′ ∈ S P(s′ | s, a) = 1 for all s ∈ S and a ∈ A (the transition probabilities — for every pair (s, a), P(· | s, a) is a distribution on S),
- R : S × A × S → ℝ be a function (the reward for the transition from s via action a to s′),
- γ ∈ [0, 1) be a number (the discount factor).
A Markov decision process (MDP) is the five-tuple
(S, A, P, R, γ).
How the process runs: at times t = 0, 1, 2, … the agent is in state st ∈ S, chooses an action at ∈ A, the world moves it to state st+1 and the agent receives the reward rt+1 = R(st, at, st+1). The states st and actions at are random variables and the Markov property holds: for every t ∈ ℕ0 and all s0, …, st−1, s, s′ ∈ S, a0, …, at−1, a ∈ A for which the condition has positive probability,
P(st+1 = s′ | st = s, at = a, st−1, at−1, …, s0, a0) = P(s′ | s, a).
The next state thus depends only on the current state and action, not on the history — just as in a Markov chain, except that the transition is also influenced by the agent’s choice. A state s ∈ S is terminal if and only if P(s | s, a) = 1 and R(s, a, s) = 0 for every a ∈ A: whoever enters it stays there and receives nothing more — this models the end of an episode (here the cliff and both goals).
A board game with a die: you know which square you are on (state), you pick a move (action), the die decides where you actually end up (transition), and some squares earn you points (reward). How you got to your square does not matter — all that counts is where you stand now.
Our world is a 6 × 5 grid. The agent starts on the left (S) and can move up, right, down or left. The whole bottom row is a cliff: whoever falls into it gets −10 and the game ends. Not far from the start there is a small goal worth +1, far to the right a big goal worth +10. Two walls close off the shorter route along the top. And the floor may be slippery: with probability p the agent slips, instead of in the intended direction, into one of the two perpendicular directions.
P(s′ | s, a) = (1 − p) · [s′ = step in direction a] + p/2 · [s′ = step perpendicular to the left] + p/2 · [s′ = step perpendicular to the right]
- P(s′ | s, a)
- the probability with which the agent ends up on square s′ after action a — the whole distribution, not just “where it is supposed to go”, because the value of a move will be computed as an average over all the places it may land
- (1 − p) · […]
- the intended move — it gets most of the probability; with p = 0 only this term remains and the world is deterministic
- p/2 · […]
- slipping sideways, equally to the left and to the right — this is what makes the squares next to the cliff dangerous even when we walk along it rather than into it
Example — a step to the right along the cliff
The agent stands on the square right of the start (a wall above it, the cliff below) and chooses “right”, p = 0.2:
- 0.8 — the next square to the right,
- 0.1 — the cliff (reward −10, the end),
- 0.1 — the square right of the start (slips up into the wall and stays where it is),
together 0.8 + 0.1 + 0.1 = 1 (colours as in the formula above: the intended step and slipping sideways). The expected immediate reward of this move is 0.8 · 0 + 0.1 · (−10) + 0.1 · 0 = −1: on average one in ten such steps ends in a fall.
2. Return, discount and policy
Definition — return and policy
Let (S, A, P, R, γ) be an MDP and r1, r2, … ∈ ℝ the rewards the agent successively receives. The return from time t ∈ ℕ0 is the discounted sum of future rewards
Gt = ∑k=0∞ γk rt+k+1 = rt+1 + γ rt+2 + γ² rt+3 + …
(the series converges, see the theorem below; after entering a terminal state all further rewards are 0).
A policy is a rule by which the agent chooses an action based on the current state:
- a deterministic policy is a function π : S → A; at time t the agent chooses at = π(st);
- a stochastic policy is a function π : A × S → [0, 1], (a, s) ↦ π(a | s), such that ∑a ∈ A π(a | s) = 1 for every s ∈ S; the agent chooses an action at random with P(at = a | st = s, history) = π(a | s).
A deterministic policy is a special case of a stochastic one with π(a | s) = 1 if and only if a = π(s). A fixed policy π turns the MDP into a Markov chain on S with transition probabilities pss′ = ∑a ∈ A π(a | s) P(s′ | s, a). The goal is a policy with the largest possible expected return (made precise in the definition of the value function below).
The discount γ says how much less a reward is worth tomorrow than today. With γ = 0.9 a reward ten steps away has weight 0.9¹⁰ ≈ 0.35, with γ = 0.5 only 0.001. A small γ makes the agent impatient, a γ close to 1 patient. A policy is then simply a cheat sheet: “on this square go right, on that one go up”.
Gt = rt+1 + γ · Gt+1
- Gt
- the return from now to the end — the number the agent wants to make as large as possible
- rt+1
- the reward for the very next step — the only one it gets without waiting, hence no discount
- γ
- the discount — everything that comes later is made cheaper by one factor of γ; this is how “later” turns into “less”
- Gt+1
- the return from the next step — the same quantity one step further on; the recursion from which the Bellman equation will grow in a moment
Theorem — with γ < 1 the return is always finite
Let (S, A, P, R, γ) be an MDP with γ ∈ [0, 1) and Rmax = maxs, s′ ∈ S, a ∈ A |R(s, a, s′)| (the maximum exists because S and A are finite). Then for every policy, every t ∈ ℕ0 and every run of the process, the series Gt converges absolutely and
|Gt| ≤ Rmax (1 + γ + γ² + …) = Rmax/1 − γ,
even if the episode never ended.
Proof. Every reward is rt+k+1 = R(st+k, at+k, st+k+1), so |rt+k+1| ≤ Rmax. The triangle inequality gives |Gt| ≤ ∑k ≥ 0 γk |rt+k+1| ≤ Rmax ∑k ≥ 0 γk, and a geometric series with ratio 0 ≤ γ < 1 sums to 1/1 − γ. ∎
Here Rmax = 10 and γ = 0.9, so |G| ≤ 10/0.1 = 100. Without a cost per step it is much less still: a nonzero reward comes only from entering a goal or the cliff, and at most once per episode, so |G| ≤ 10. That is why γ = 1 can be used only where the episode is certain to end.
Example — the return of the shortest path to the big goal
On a solid floor the shortest path to the +10 goal takes 6 steps. The first five steps give reward 0, the sixth (entering the goal) +10. With γ = 0.9:
G0 = 0 + 0.9 · 0 + 0.9² · 0 + 0.9³ · 0 + 0.9⁴ · 0 + 0.9⁵ · 10 = 0.59049 · 10 = 5.9049.
On a solid floor nothing random happens, so this is directly the value of the start — and value iteration in the section on policies does indeed give 5.90. The small +1 goal, even 3 steps away, would give only 0.9² · 1 = 0.81.
3. Value and the Bellman equations
Definition — value function and the Bellman optimality equation
Let (S, A, P, R, γ) be an MDP with γ ∈ [0, 1), π a policy, and let Eπ denote the expected value when behaving according to π. The value function of the policy π is the function Vπ : S → ℝ and the action value is the function Qπ : S × A → ℝ,
Vπ(s) = Eπ[Gt | st = s], Qπ(s, a) = Eπ[Gt | st = s, at = a]
(thanks to the Markov property they do not depend on t). Vπ(s) is thus the expected return when the agent starts in s and follows π; for Qπ(s, a) we choose the first action a ourselves and only then does the agent follow π. The optimal values are
V*(s) = maxπ Vπ(s), Q*(s, a) = maxπ Qπ(s, a) for all s ∈ S, a ∈ A,
and a policy π* is optimal if and only if Vπ*(s) = V*(s) for all s ∈ S. (For a finite MDP with γ < 1 the maxima exist and there is an optimal deterministic policy.)
The following holds (Richard Bellman, 1957):
- the Bellman equation for a deterministic policy π: for all s ∈ S Vπ(s) = ∑s′ ∈ S P(s′ | s, π(s)) [R(s, π(s), s′) + γ Vπ(s′)];
- the Bellman optimality equation: for all s ∈ S and a ∈ A Q*(s, a) = ∑s′ ∈ S P(s′ | s, a) [R(s, a, s′) + γ V*(s′)], V*(s) = maxa ∈ A Q*(s, a), and V* is the only function S → ℝ that satisfies these equations;
- every deterministic policy π* with π*(s) ∈ argmaxa ∈ A Q*(s, a) for all s ∈ S is optimal.
The value of a square = the most I can get out of it. And that can be computed recursively: every move gives me an immediate reward plus the (slightly discounted) value of the square the move takes me to — averaged over wherever the die may throw me. I pick the move where this comes out highest.
V*(s) = maxa ∑s′ P(s′ | s, a) [R(s, a, s′) + γ V*(s′)]
- V*(s)
- the best expected return from square s — what we are looking for; it appears on both sides, so the equation is a condition that all the values must satisfy at once
- maxa
- the agent’s choice — it picks the action itself, so it takes the best one; this is where the “optimality” equation differs from the equation for a fixed policy π, where π(s) stands in place of the maximum
- ∑s′ P(s′ | s, a) […]
- an average over where the world actually throws the agent — the agent has no say in that, hence an expected value, not a maximum
- R(s, a, s′)
- the immediate reward for this transition (+1, +10, −10 or the step cost)
- γ
- the discount — the future is one step further on, hence one factor of γ (just as in the formula Gt = rt+1 + γ Gt+1)
- V*(s′)
- the best return from the landing square — the whole future summed up in a single number, so there is no need to walk through all the long paths
Example — the Bellman equation at the start (slipping 20%, γ = 0.9)
The finished optimal values of the start’s neighbours: the square above the start V* = 3.325, the square to the right V* = 2.260, the start itself V* = 2.854; the cliff is terminal (value 0, reward −10). We plug them into the right-hand side for each action (colours as in the Bellman equation above):
- ↑: 0.8 · (0 + 0.9 · 3.325) + 0.1 · (0 + 0.9 · 2.260) + 0.1 · (0 + 0.9 · 2.854) = 2.854,
- →: 0.8 · (0 + 0.9 · 2.260) + 0.1 · (−10 + 0.9 · 0.000) + 0.1 · (0 + 0.9 · 3.325) = 0.926,
- ↓: 0.8 · (−10 + 0.9 · 0.000) + 0.1 · (0 + 0.9 · 2.854) + 0.1 · (0 + 0.9 · 2.260) = −7.540,
- ←: 0.8 · (0 + 0.9 · 2.854) + 0.1 · (0 + 0.9 · 3.325) + 0.1 · (−10 + 0.9 · 0.000) = 1.354,
The results are Q*(start, a). The largest (maxa) is ↑ with 2.854, and that is exactly V*(start) — the equation holds (the difference in the last digit is rounding). The action “up” is worth something even despite the 10% chance that the agent slips to the left, hits the edge and stays on the start: that is why V*(start) also appears on the right-hand side. The action “down” leads into the cliff 80% of the time and comes out deeply negative.
4. Value iteration
Definition — value iteration
The Bellman operator of the MDP (S, A, P, R, γ) is the mapping B that assigns to every function V : S → ℝ the function BV : S → ℝ,
(BV)(s) = maxa ∈ A ∑s′ ∈ S P(s′ | s, a) [R(s, a, s′) + γ V(s′)].
Value iteration is the algorithm:
- input: an MDP (S, A, P, R, γ) with γ ∈ [0, 1) and a tolerance ε > 0;
- output: approximate optimal values V : S → ℝ and a deterministic policy π : S → A.
- Set V0(s) = 0 for all s ∈ S and k = 0.
- Compute Vk+1 = BVk, i.e. Vk+1(s) = maxa ∑s′ P(s′ | s, a) [R(s, a, s′) + γ Vk(s′)] for all s ∈ S.
- If maxs ∈ S |Vk+1(s) − Vk(s)| < ε, continue with step 4; otherwise increase k by 1 and go back to step 2.
- Return V = Vk+1 and π(s) ∈ argmaxa ∑s′ P(s′ | s, a) [R(s, a, s′) + γ V(s′)] for every s ∈ S.
For γ < 1, B is a contraction in the maximum norm: for all U, V : S → ℝ, maxs |(BU)(s) − (BV)(s)| ≤ γ · maxs |U(s) − V(s)|. Since BV* = V* (the Bellman optimality equation), it follows that maxs |Vk(s) − V*(s)| ≤ γk · maxs |V0(s) − V*(s)|: the error falls at least as fast as γk and the algorithm terminates for every ε > 0. By the definition of an MDP, terminal states have Vk = 0 in every round, which is why the pseudocode below does not recompute them at all.
In the first round only the squares right next to the goals know their value. In the second the information moves one square further, in the third one more. Like news spreading from neighbour to neighbour — after a few rounds every square knows where it pays to go.
# value iteration
V = 0 in all states
repeat:
for each non-terminal state s:
for each action a:
Q[a] = ∑ over s′ ( P(s′ | s, a) · (R(s, a, s′) + γ · V(s′)) )
V_new(s) = max(Q)
change = max over s |V_new(s) − V(s)|
V = V_new
until change < 10⁻⁶
π(s) = the action with the largest Q[a] (computed from the final V)
Example — the first three rounds (slipping 20%, γ = 0.9)
We follow two squares in the middle row: A right to the left of the big +10 goal and B one square further, right to the right of the small +1 goal. Above B there is a wall, above A free space, below both an ordinary floor.
| round k | Vk(A) | best action in A | Vk(B) | best action in B |
|---|---|---|---|---|
| 1 | 8.000 | → | 0.800 | ← |
| 2 | 8.000 | → | 5.832 | → |
| 3 | 9.166 | → | 6.343 | → |
Round 1 (V0 = 0 everywhere): from A a step to the right leads into the goal with probability 0.8, i.e. 0.8 · 10 = 8.000. From B nothing distant is visible yet; the best is a step to the left towards the small goal: 0.8 · 1 = 0.800.
Round 2: from B going right leads into A, which already has value 8: 0.8 · 0.9 · 8 = 5.76, plus 0.1 · 0.9 · 0.8 = 0.072 for slipping up (a wall, so it stays in B with V1 = 0.8) and 0 for slipping down — in total 5.832 (colours as in the Bellman equation in the previous section; the new value Vk+1 has the colour of V*). The best action in B has flipped from ← to →: the information about the big goal has travelled one square further.
maxs |Vk+1(s) − Vk(s)| ≤ γk · maxs |V1(s) − V0(s)|
- max |Vk+1 − Vk|
- the largest change in round k + 1 — what the computation watches and what decides when it stops; in the chart below the green (γ = 0.9) and blue (γ = 0.99) line
- γk
- every round shrinks the difference between two estimates at least γ-fold (contraction); after k rounds, therefore, γk-fold — hence the slow convergence for γ close to 1
- max |V1 − V0|
- the size of the first change; with V0 = 0 it is the largest expected reward for a single step, here at most 10 — hence the dashed bound 10 · γk in the chart
With γ = 0.9 the values settle (change below 10⁻⁶) after 50 iterations, with γ = 0.99 after 77. The error falls faster than the bound γk guarantees — the bound always holds, but for a particular world it tends to be pessimistic. The closer γ is to one, the further into the future the information has to spread and the longer the computation takes.
5. What the discount, slipping and step cost change
Now for the most interesting part. The same world, just different settings — and the optimal policy looks completely different.
On a solid floor the shortest path to the big goal runs right along the cliff: 6 steps and a start value of 5.90. When the floor is slippery, the cliff edge is dangerous — every step there means a 10% chance of falling. The optimal policy therefore goes around the walls over the top: 10 steps instead of 6, but only 0.7% of episodes fall into the cliff. If we used the risky policy on the slippery floor, the agent would fall in 30% of cases and the value of the start would drop to 0.64 instead of 2.85.
With γ = 0.5 the big goal is so far away that it does not pay off — +10 ten steps away is worth only 10 · 0.5¹⁰ ≈ 0.01 today. The agent goes carefully over the top to the small goal. When, on the other hand, every step costs −1, taking risks pays off: the agent goes to the big goal along the cliff edge and falls in 27% of episodes. The long safe path would cost it more than the average loss from falling.
| γ | slipping | step | path leads | V(start) | simulation | fall | steps |
|---|---|---|---|---|---|---|---|
| 0.9 | 0% | 0 | to +10 | 5.90 | 5.90 | 0.0% | 6.0 |
| 0.9 | 20% | 0 | to +10 | 2.85 | 2.84 | 0.7% | 13.1 |
| 0.5 | 20% | 0 | to +1 | 0.01 | 0.01 | 0.0% | 9.5 |
| 0.9 | 20% | −1 | to +10 | −2.86 | −2.90 | 26.9% | 6.0 |
| 0.5 | 0% | −1 | to +1 | −1.25 | −1.25 | 0.0% | 3.0 |
The “simulation” column is a check: the average return from 10,000 episodes following the policy found, with random slipping. The average return differs from the computed value of the start by a few hundredths at most — the Bellman equation really does compute the expected return.
A lesson for real applications: an agent’s behaviour is determined not only by “what the goal is”, but also by details such as the discount and the cost per step. Whoever designs the rewards designs the behaviour — including the behaviour they did not want.
6. Policy iteration
Definition — policy iteration
Policy iteration (Ronald Howard, 1960) is the algorithm:
- input: an MDP (S, A, P, R, γ) with γ ∈ [0, 1) and an initial deterministic policy π0 : S → A;
- output: an optimal deterministic policy π and its value function Vπ = V*.
- Set i = 0.
- Evaluation: compute Vπi : S → ℝ as the (unique) solution of the system of |S| linear equations V(s) = ∑s′ ∈ S P(s′ | s, πi(s)) [R(s, πi(s), s′) + γ V(s′)], s ∈ S.
- Improvement: for every s ∈ S choose πi+1(s) ∈ argmaxa ∈ A Qπi(s, a), where Qπi(s, a) = ∑s′ P(s′ | s, a) [R(s, a, s′) + γ Vπi(s′)]; if πi(s) is among the maximizers, keep πi+1(s) = πi(s).
- If πi+1 = πi (i.e. πi+1(s) = πi(s) for all s ∈ S), stop and return π = πi; otherwise increase i by 1 and go back to step 2.
That the returned policy is optimal and that the algorithm always terminates is stated by the policy improvement theorem below.
I take some cheat sheet, honestly work out how well I do with it from every square, and then I fix the cheat sheet wherever a different move would work out better. I repeat. The cheat sheet gets better with every round, and after a few rounds it no longer changes.
# policy iteration
π = “up everywhere”
repeat:
# evaluation: V^π by solving the system (here by repeated substitution)
V = 0
repeat until V changes by no more than 10⁻⁹:
V(s) = ∑ over s′ ( P(s′ | s, π(s)) · (R + γ · V(s′)) ) for each s
# improvement: in every state the best action according to V^π
π_new(s) = argmax over a ∑ over s′ ( P(s′ | s, a) · (R + γ · V(s′)) )
changed = number of states where π_new(s) ≠ π(s)
π = π_new
until changed = 0
Theorem — policy improvement
Let (S, A, P, R, γ) be an MDP with γ ∈ [0, 1), π : S → A a deterministic policy and π′ : S → A a deterministic policy such that π′(s) ∈ argmaxa ∈ A Qπ(s, a) for all s ∈ S. Then
- Vπ′(s) ≥ Vπ(s) for all s ∈ S;
- if Qπ(s, π(s)) = maxa ∈ A Qπ(s, a) for all s ∈ S (the improvement has nothing to change, π′ = π), then Vπ satisfies the Bellman optimality equation, Vπ = V* and π is optimal;
- otherwise there exists s ∈ S with Vπ′(s) > Vπ(s).
Proof sketch. By the choice of π′, Vπ(s) = Qπ(s, π(s)) ≤ Qπ(s, π′(s)) = E[r + γ Vπ(s′)]. We substitute the same inequality into Vπ(s′), then again… After unrolling, what remains is the expected return of the policy that plays π′ in all steps, i.e. Vπ′(s). ∎
There are finitely many deterministic policies — here 4 actions on 19 non-terminal squares, i.e. 419 ≈ 2.7 · 1011. None can repeat (every round either improves the value or ends), so policy iteration always terminates. In practice surprisingly few rounds are needed: here 6.
On the slippery floor with γ = 0.9, starting with the policy “up everywhere”:
| round | V(start) of the policy | evaluation steps | actions changed |
|---|---|---|---|
| 1 | 0.03 | 66 | 7 |
| 2 | 0.11 | 137 | 7 |
| 3 | 0.16 | 141 | 8 |
| 4 | 0.49 | 151 | 3 |
| 5 | 2.85 | 42 | 1 |
| 6 | 2.85 | 77 | 0 |
After 6 rounds the policy is the same as the one from value iteration. There are few rounds, but each contains a whole evaluation (here tens to hundreds of sweeps through the world). Which of the two methods is faster depends on the task; in practice they are combined.
7. When we do not know the rules of the world
Everything above assumes that we know P and R — that we can “look into the rules”. That is planning (dynamic programming). In real reinforcement learning the agent is thrown into a world whose rules it does not know: it has to try things, collect experience and estimate values from it. But the Bellman equation remains the heart of almost all methods — only instead of the exact average over P a sample from a single real step is used. That is Q-learning, the topic of the next part.
The same framework describes a robot learning to walk, inventory control in a warehouse, a recommender system, and a language model trained from human preferences in the part on RLHF — there the state is the text so far, the action the next token and the reward the rating of the answer.
8. Summary
- MDP = states, actions, random transitions, rewards and a discount; the next state depends only on the current state and action.
- The value of a state is the expected discounted return; the optimal values satisfy the Bellman equation.
- Value iteration repeats the Bellman equation; the error falls at least as fast as γk (here 50 iterations for γ = 0.9).
- Slipping moves the path away from the cliff (falls 0.7% instead of 30%), a small γ leads to the small nearby goal, a step cost to taking risks.
- Policy iteration alternates evaluation and improvement; here 6 rounds.
- Without knowing the rules of the world, the values have to be learned from experience — that is reinforcement learning.
Want to get your hands on it? Have an AI agent program this cliff world for you, with γ, slipping and the step cost adjustable interactively — and watch when the policy “flips” from one path to another.
Do you see it differently? Write to support@coden-app.com.