- Set for every state .
- For , update every state from the values of the previous sweep, , and stop when no value changes by more than a small threshold .
- Return the greedy policy .
- An optimal first action .
- An optimal policy from the successor state .
Code
Code
Value Iteration in Gridworld Example
- To solve the nonlinear equations for we use an iterative approach.
-
Steps:
- Initialize estimates for the utilities of states with arbitrary values: for all , except the two exits, whose values stay at and
- Next use the iteration step below repeatedly. Since we are taking we only need to consider states whose next states have a positive value.
- Apply this to the maze example, with for simplicity. A state is in column and row , counted from the bottom left; the exit is , the exit is , and is the wall.
- For the remaining states, the value is equal to the immediate reward in the first iteration.
Value Iteration (k=1)
Value iteration ()
The values of the first sweep feed the second. Every state that has no positive-valued next state got , and those values now enter the sums: where both right-angle moves from hit the top edge or the wall and leave the agent in place, and The best action is right in and and up in .Value iteration ()
Notice how information propagates outward from terminal states and eventually all states have correct value estimates. has a lower value than because one of its right-angle moves lands on the exit
Value iteration on the 4x3 gridworld
The code below runs value iteration on the 4x3 grid world of Artificial Intelligence: A Modern Approach, Figure 17.2. Its moves are noisy: the intended move succeeds with probability 0.8, and with probability 0.1 each the agent moves at a right angle to it. The last section of Policy Iteration solves the same MDP with policy iteration, so you can compare the two algorithms side by side.- Terminal states: +1 at (3,0), -1 at (3,1)
- Wall: (1,1)
- Step cost: -0.04
- Discount factor γ = 1.0
Code
Code
Convergence
The rate of convergence depends mainly on the discount factor , and also on the maximum reward value. The policy that you get from coarse estimates is close to the optimal policy long before has converged. So after a reasonable number of iterations, you could extract the policy greedily and use it to guide the agent’s actions. The code below runs value iteration on the same 4x3 gridworld with synchronous sweeps: every state is updated from the values of the previous sweep. After each sweep it records and the greedy policy that implies.Code
Code
Found an error on this page? Report an issue.

