Lecture 17: MDPs, Value Iteration and Policy Iteration
Stanford CS229 lecture 17 notes: MDPs, Value Iteration and Policy Iteration.
- cs229
- machine-learning
- course-notes
On this page
Value function#
For a policy , define the value function:
This is the expected total payoff if you start in state and follow .
Bellman equation#
The value of a state is its immediate reward plus the discounted expected value of the next state:
For a finite number of states, this is a linear system of equations in unknowns, so we can solve it directly.
Optimal value function#
It satisfies the Bellman optimality equation:
And the optimal policy picks the action that attains that max:
A nice fact: the same is optimal for every starting state.
Algorithm 1: Value Iteration#
- Initialize for all .
- Repeat until convergence: for every state,
It converges to . Then extract using the argmax formula.
You can do the update synchronously (compute all new values from old values, then replace) or asynchronously (update in place, one state at a time). Both converge.
Algorithm 2: Policy Iteration#
- Initialize randomly.
- Repeat until convergence:
- (a) Solve for (using the linear system).
- (b) For each state, update the policy greedily:
graph LR
A["Policy π"] -->|"evaluate"| B["Value V^π"]
B -->|"greedy improvement"| AWhich one is better?#
- Policy iteration usually converges in fewer iterations, but each one requires solving a linear system, which is expensive for large state spaces.
- Value iteration has cheap iterations, so it's often preferred for large MDPs.
- For small MDPs, policy iteration is often faster.
Learning the model from data#
So far, we assumed we knew . In practice, we don't. We estimate it from experience by counting:
(If we've never tried , we say it goes to a uniformly random state.)
The full loop:
- Take actions using the current policy to collect experience.
- Update the estimate of (and if unknown).
- Solve the MDP (value iteration) with the new model, to get a new .
- Repeat.
Exploration vs exploitation#
If you always follow the current best policy, you might never try some actions that would be better, since you have no data on them. So we need to explore. A simple way is -greedy: with probability take the best known action, and with probability take a random one.