Skip to content
Lambdian

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 ∣S∣×∣S∣|S|\times|S| linear system (O(∣S∣3)O(|S|^3)). 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

  1. 1
    Initialise a policy π (any, e.g. random).
  2. 2
    Evaluate: compute VπV^\pi (linear solve or sweeps to convergence).
  3. 3
    Improve: π′(s)=arg⁡max⁡a∑s′P(s′∣s,a)[r+γVπ(s′)]\pi'(s) = \arg\max_a\sum_{s'}P(s'\mid s,a)[r + \gamma V^\pi(s')].
  4. 4
    If π′=π\pi' = \pi, stop: π is optimal. Otherwise set π ← π′ and go to step 2.

The maths

Evaluation

Vπ=Rπ+γPπVπV^\pi = R^\pi + \gamma P^\pi V^\pi

Improvement

π′(s)=arg⁡max⁡aQπ(s,a)\pi'(s) = \arg\max_a Q^\pi(s,a)

Improvement theorem

Qπ(s,π′(s))≥Vπ(s) ∀s ⇒ Vπ′≥VπQ^\pi(s,\pi'(s)) \ge V^\pi(s)\ \forall s \ \Rightarrow\ V^{\pi'} \ge V^\pi

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…

Small to medium MDPs with a known model: inventory and maintenance planning, board-game endgames, as a teaching and debugging baseline.

Check your understanding

Question 1

Q1Policy iteration stops when…

Question 2

Q2Why must policy iteration terminate?

Question 3

Q3What does policy iteration need that Q-learning does not?

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).
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.