Policy iteration
Plan with a known model: evaluate a policy, make it greedy, repeat until optimal.
Animated explanation
The idea
If you know the rules of the world, the transition probabilities and rewards, you can plan without trial and error. Policy iteration is the most intuitive way.
Start with any policy. Evaluate it: how much return does it get from every state? Then improve it: in every state, switch to the action that looks best when you assume you'll follow the old policy afterwards. The policy improvement theorem guarantees the new policy is at least as good everywhere. Repeat. Since there are finitely many policies and each step improves, you reach the optimal policy, typically in very few iterations.
Generalised policy iteration
Evaluation and improvement don't need to be complete. Doing a few sweeps of evaluation before improving (modified policy iteration), or just one (value iteration), still converges. Almost every RL method, including actor-critic and Q-learning, is a form of this generalised policy iteration: estimate values, act more greedily with respect to them, repeat.
Cost
Each exact evaluation solves an linear system (). Policy iteration takes few iterations but each is expensive; value iteration takes many cheap ones. Both need the full model and enumerate states, which is why model-free and approximate methods exist.
The algorithm, step by step
- 1Initialise a policy π (any, e.g. random).
- 2Evaluate: compute (linear solve or sweeps to convergence).
- 3Improve: .
- 4If , stop: π is optimal. Otherwise set π ← π′ and go to step 2.
The maths
Evaluation
Improvement
Improvement theorem
Strengths and weaknesses
Strengths
- Converges in few iterations; exact optimal policy
- Each iteration is guaranteed to improve
- Clear conceptual basis for most RL algorithms
Weaknesses
- Needs a complete, known model
- Exact evaluation is expensive for large state spaces
- Enumerates all states
Reach for it when…
Check your understanding
Lambdian Lab
Policy iteration on the gridworld
Using the provided gridworld:
- q_value(V, s, a, gamma): expected reward + γ·V(next state) (terminals have value 0).
- policy_evaluation(policy, gamma): exact solve (as in the MDP lesson) or sweeps to 1e-10.
- policy_improvement(V, gamma): greedy policy (ties → lowest action index).
- policy_iteration(gamma): start from "always right"; return (policy, V, iterations).