Definition · A discounted Markov decision processStates 1, …, n, a finite set of actions, a transition matrix P(· | s, a) for each action, a reward r(s, a), and a discount γ with 0 ≤ γ < 1.
A policy d assigns one action to each state. Its value v_d is the unique solution of v = r_d + γ P_d v, where P_d and r_d are the matrix and the reward vector the policy selects. The unique solution exists because I − γP_d is invertible for γ < 1.
The lab takes two transition matrices and two reward vectors, one pair per action, and validates each matrix as a chain before anything else. The discount is set in twentieths so that it stays a fraction: at γ = 18/20 every value on the page is a rational with a small denominator, which is what makes the comparison between two policies decidable rather than approximate. The page heads its two action columns simply “action 1” and “action 2”; which real-world choice each stands for is in the matrices and the rewards you typed.
Evaluation, improvement, and the round structure
A round does two things. It evaluates the current policy by solving v = r_d + γP_d v exactly — the lab reports how many elimination operations that took — and then it improves: for each state it computes q(s, a) = r(s, a) + γ Σ P(s′|s, a) v(s′) for every action, using the v it just solved for, and switches to the best. If no state switched, the policy is optimal and the loop stops.
two states, two actions, γ = 9/10
action 1 P = 1/2 1/2 r = (1, 3)
1/4 3/4
action 2 P = 3/4 1/4 r = (2, 1)
1/2 1/2
round 1 policy action 1 / action 1
solve v = r + γPv v = (670/31, 750/31)
q at state 1: 670/31 against 683/31 → switch
q at state 2: 750/31 against 670/31 → keep
1 state improved
round 2 policy action 2 / action 1
solve v = (265/11, 285/11)
q at state 1: 47/2 against 265/11 → keep
q at state 2: 285/11 against 47/2 → keep
nothing changed — stop
4 policies in total, 2 rounds, and 47/2 = 23.5 against 265/11 = 24.09…
Look at the last comparison. 265/11 and 47/2 differ by 13/22, which is about 0.591. Both routes to it are exact, so the decision is decided; a floating-point evaluation would also have got it right here, and on a problem with a near-tie it would be choosing between two roundings and reporting a policy. That is the case exactness is insurance against, and it is not a rare one at discounts close to 1, where the values of neighbouring policies crowd together.
Theorem · Policy iteration terminatesGreedy improvement produces a policy whose value is at least as large as the current one in every state. Since there are finitely many policies and the loop only stops when no state changes, the loop cannot run for ever, and the policy it stops at satisfies the optimality condition for every state.
Let d be the current policy with value v, and d′ the greedy policy. By construction r_{d′} + γP_{d′}v ≥ v entrywise, since the greedy choice is at least as good as the current action, whose value is exactly v.
Applying r_{d′} + γP_{d′}(·) repeatedly preserves that inequality, because P_{d′} has non-negative entries and γ > 0; the iterates converge to v_{d′}, so v_{d′} ≥ v. If the values are equal in every state, no state changed and the loop has stopped. Otherwise the value has strictly increased somewhere, so the policy is new. With finitely many policies the loop can only strictly increase finitely often.
□
That is worth contrasting with the other way of solving the same problem. Value iteration starts at zero and applies the improvement operator over and over; it converges, and there is no step at which anything tells you it has arrived. What people do instead is stop when the change between steps falls below a number they chose, which is a statement about the increments and not about the distance to the answer.
Value iteration, measured against the answer the solve already has
Because the exact value is on the same page, the lab can print what value iteration is actually short by rather than how much it moved last step. On the two-state example at γ = 9/10 the iterates start at (0, 0) and climb — 2.00000, 4.02500, 5.94875, 7.72569 — reaching 17.03023 at step twelve against an exact 265/11 = 24.09…. It is short by 7.06068 and 7.06080, and it approaches from underneath and never arrives.
value iteration on the best policy, state 1, γ = 9/10, exact value 265/11
step v₁ denominator of v₁
0 0.00000 1 digit
1 2.00000 1
4 7.72569 5
8 13.33076 10
12 17.03023 15
20 21.05149
40 23.72139
the exact solve 265/11 = 24.090909… 2 digits, at round 2
still short by: at 12 steps 7.06068 at 40 steps 0.36952
Two things in that table are worth naming. The shortfall shrinks by a factor of about γ per step, which is why raising the discount towards 1 slows it down sharply while policy iteration takes the same two or three rounds. And the denominators grow by one power of the discount's denominator every step, from one digit to fifteen by step twelve, while the exact answer has a two-digit denominator — the iteration is accumulating complexity as it approaches a number that was simple all along.
Example · Raising the discountAt γ = 9/20 the two-state problem still stops in 2 rounds, with value (1330/341, 1770/341), and value iteration is within 0.0003 after twelve steps. At γ = 19/20 it still stops in 2 rounds, with value (1030/21, 1070/21), and value iteration is 27.02 short after twelve.
The number of rounds is a property of how many policies there are and how quickly the greedy step finds the best one; the iteration's progress is a property of γ. Those are different mechanisms, and the slider makes the difference visible in a few seconds.
Example · Three states and a penalty for being emptyThe stock-control example has three states and rewards (4, 1, −3) under one action and (1, 0, −1) under the other. At γ = 9/10 policy iteration takes 3 rounds over 8 policies and settles on (1640/59, 1440/59, 1345/59).
The middle round is the instructive one: the greedy step swings the policy from all-hold to all-restock, values it at (10, 360/41, 310/41), and then swings the first state back. A method that stopped at the first improvement would have taken that middle policy and been wrong by between fifteen and eighteen in every state.