What You'll Learn
- Explain the policy improvement theorem and why it guarantees progress
- Implement policy iteration from scratch
- Implement value iteration from scratch
- Compare policy iteration and value iteration on the same MDP
- Solve simple MDPs to find optimal policies using DP methods
We can now evaluate any policy, computing for any choice of . But our goal is not just to evaluate policies. We want to find the best policy.
The insight of this chapter is that once we have , we can construct a better policy. And by alternating between evaluation and improvement, we can climb all the way to the optimum. Try it below: run sweeps and watch the optimal values and policy emerge.
Value Iteration Visualization
Watch value iteration solve the GridWorld problem step by step.
The Core Insight
The process of constructing a new policy that is at least as good as the current policy, by acting greedily with respect to the current value function.
Suppose you know for all states. From any state , you can ask: “What if I took a different action just once, then followed afterward? Would I do better?”
The greedy action is the one that maximizes this one-step lookahead. The policy improvement theorem tells us something remarkable: if we switch to always taking the greedy action, we are guaranteed to do at least as well as before, and usually better.
This leads to two algorithms, both of which converge to the optimal policy and optimal value function :
- Evaluate the current policy completely
- Improve: make the policy greedy with respect to those values
- Repeat until the policy stops changing
- Very few rounds, but each needs a full evaluation
- Apply the Bellman optimality backup to all states
- Repeat until the values converge
- Extract the greedy policy at the end
- Cheap per sweep, but may need many sweeps
Both algorithms find the same optimal policy. They differ in how they organize computation. The best choice depends on the problem structure and computational constraints.
Chapter Overview
The Greedy Policy
The key operation in both algorithms is constructing a greedy policy from a value function: in every state, pick the action with the best one-step lookahead — immediate reward plus discounted value of the state you land in.
Given a value function , the greedy policy selects:
This picks the action that maximizes expected immediate reward plus discounted future value. This is the one-step lookahead from the Bellman optimality equation, applied as a decision rule.
The greedy policy answers: “Looking one step ahead and using my current value estimates for what comes after, which action looks best?”
It is called “greedy” because it always takes what looks best right now. Surprisingly, this myopic approach leads to globally optimal behavior when combined with accurate value estimates.
Why This Matters
Dynamic Programming gives us exact solutions to MDPs. When we have access to the complete model (all transitions and rewards), DP finds the true optimal policy.
But DP has limitations:
DP requires the full model. In most real-world problems, we do not know precisely. We might have a simulator, or we might only have real experience.
DP requires enumeration. We must loop over all states and all actions. This becomes impractical when the state space is large (millions of states) or continuous.
These limitations motivate model-free methods. When you don’t have the model, Monte Carlo methods estimate these values from experience — that is the very next step after this chapter.
Despite these limitations, DP is essential:
- It provides the theoretical foundation for understanding RL
- Many RL algorithms are approximate versions of DP methods
- For small MDPs, DP gives the ground truth to compare against
Key Questions We Will Answer
- Why does greedy improvement never make things worse?
- How many policy iterations are typically needed?
- When should you use policy iteration vs. value iteration?
- What is the relationship between these algorithms and Q-learning?
Check Your Understanding
Key Takeaways
- Greedy improvement never makes a policy worseThe policy improvement theorem: acting greedily with respect to the exact value function of the current policy produces a policy that is at least as good in every state.
- Policy iteration alternates evaluation and improvementEvaluate, greedify, repeat. It terminates in finitely many rounds, and only at an optimal policy.
- Value iteration folds improvement into the value updateThe max over actions in the Bellman optimality backup improves implicitly; the policy is extracted once at the end.
- Both algorithms reach the same optimumFor finite MDPs with a known model, they differ in computational path, not in the answer.
- DP needs the full model — RL removes that requirementEverything here assumes known transitions and rewards. Monte Carlo and TD methods estimate the same quantities from experience.