Skip to main content
Open In Colab In the Gridworld example of the policy iteration section, you saw that you may not need to reach the optimal state value function v∗(s)v_*(s) to obtain an optimal policy. The value function at iteration k=3k=3 already gives the same policy as a far more accurate value function at large kk. We could have stopped early, and taking the argument to the limit, we can question the need to start from a policy at all. Instead of letting the policy π\pi dictate which actions are selected, we select the actions that maximize the expected return: the immediate reward plus the discounted value of the successor state. We therefore do the improvement step in each iteration, computing the optimal value function without a policy. In this section we look at an algorithm called value iteration that does that. The algorithm has three steps.
  1. Set v0(s)=0v_0(s) = 0 for every state ss.
  2. For k=0,1,2,…k = 0, 1, 2, \dots, update every state from the values of the previous sweep, vk+1(s)=max⁡a(r(s,a)+γ∑s′p(s′∣s,a) vk(s′))v_{k+1}(s) = \max_a \big( r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v_k(s') \big), and stop when no value changes by more than a small threshold θ\theta.
  3. Return the greedy policy π∗(s)=arg⁡max⁡a(r(s,a)+γ∑s′p(s′∣s,a) v(s′))\pi_*(s) = \arg\max_a \big( r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v(s') \big).
The basic principle behind value iteration is the principle that underlies dynamic programming, called the principle of optimality, as applied to policies. According to this principle an optimal policy can be divided into two components.
  1. An optimal first action a∗a_*.
  2. An optimal policy from the successor state s′s^\prime.
More formally, for a discount factor γ>0\gamma > 0, a policy π(a∣s)\pi(a|s) achieves the optimal value from state ss, vπ(s)=v∗(s)v_\pi(s) = v_*(s), if and only if it selects only optimal actions at ss and achieves the optimal value, vπ(s′)=v∗(s′)v_\pi(s^\prime) = v_*(s^\prime), from every successor state s′s^\prime that it reaches from ss with positive probability. Effectively this principle allows us to decompose the problem into two sub-problems, one of them straightforward to determine, and to use the Bellman optimality equation, which provides the one-step backup induction at each iteration. v∗(s)=max⁡a(r(s,a)+γ∑s′∈Sp(s′∣s,a) v∗(s′))v_*(s) = \max_a \bigl( r(s, a) + \gamma \sum_{s^\prime \in \mathcal S} p(s^\prime \mid s, a)\, v_*(s^\prime) \bigr) As an example, if I want to move optimally towards a location in the room, I can make an optimal first step and at that point follow the optimal policy, which I was magically given, towards the desired final location. Think of making that optimal first step by walking backwards from the goal state. We start at the end of the problem, where we know the final rewards, and work backwards to all the states that connect to it in our look-ahead tree.
Output from cell 2 One-step look-ahead tree of the value iteration update. The agent takes the max over its actions; the environment averages over the next states. The “start from the end” intuition behind the equation is usually applied with no consideration of whether we are at the end or not. We just do the backup inductive step for each state. In value iteration with synchronous backups, we start at k=0k=0 from the value function v0(s)=0.0v_0(s)=0.0, and at each iteration k+1k+1, for all states s∈Ss \in \mathcal{S}, we update vk+1(s)v_{k+1}(s) from vk(s)v_k(s). For a discounted problem with 0≤γ<10 \le \gamma < 1 and bounded rewards, the update is a contraction, and the value function converges to v∗v_* from any starting point. With γ=1\gamma = 1 convergence needs further assumptions, for example an episodic problem in which every policy eventually reaches a terminal state; without them the values can grow without bound. The equation of value iteration is taken straight from the Bellman optimality equation, by turning the latter into an update rule called the Bellman update. vk+1(s)=max⁡a(r(s,a)+γ∑s′∈Sp(s′∣s,a) vk(s′))v_{k+1}(s) = \max_a \bigl( r(s, a) + \gamma \sum_{s^\prime \in \mathcal S} p(s^\prime \mid s, a)\, v_k(s^\prime) \bigr) Value iteration can be written in vector form, with the max taken separately in each state, as vk+1=max⁡a(ra+γPavk)\mathbf v_{k+1} = \max_a ( \mathbf r^a + \gamma P^a \mathbf v_k ) Notice that we are not building an explicit policy at every iteration, and the intermediate value functions may not correspond to a feasible policy.

Value Iteration in Gridworld Example

  • To solve the nonlinear equations for v∗(s)v_*(s) we use an iterative approach.
  • Steps:
    • Initialize estimates for the utilities of states with arbitrary values: v(s)←0v(s) \leftarrow 0 for all s∈Ss \in \mathcal S, except the two exits, whose values stay at +1+1 and −1-1
    • Next use the iteration step below repeatedly. Since we are taking max⁡\max we only need to consider states whose next states have a positive value.
vk+1(s)←r(s)+γmax⁡a∑s′p(s′∣s,a) vk(s′),v_{k+1}(s) \leftarrow r(s) + \gamma \max_a \sum_{s'} p(s' \mid s, a)\, v_k(s'), where r(s)=−0.04r(s) = -0.04 is the reward of every nonterminal state. The reward here depends only on the state, as in Artificial Intelligence: A Modern Approach, so it sits outside the max.
  • Apply this to the maze example, with γ=1\gamma = 1 for simplicity. A state sxys_{xy} is in column xx and row yy, counted from the bottom left; the +1+1 exit is s43s_{43}, the −1-1 exit is s42s_{42}, and s22s_{22} is the wall.
Output from cell 3 Initial values v0v_0. The shaded state s33s_{33} is the only one with a positive-valued next state, so it is the only state whose value rises in the first sweep.
  • For the remaining states, the value is equal to the immediate reward in the first iteration.

Value Iteration (k=1)

v1(s33)=r(s33)+γmax⁡a[∑s′p(s′∣s33,a) v0(s′)]v_1(s_{33}) = r(s_{33}) + \gamma \max_a \bigl[\sum_{s^{'}} p(s^\prime \mid s_{33},a)\, v_0(s^\prime) \bigr] v1(s33)=−0.04+max⁡a[∑s′p(s′∣s33,↑)v0(s′),∑s′p(s′∣s33,↓)v0(s′),∑s′p(s′∣s33,→)v0(s′),∑s′p(s′∣s33,←)v0(s′)]v_1(s_{33}) = -0.04 + \max_a \bigl[ \sum_{s'} p(s'| s_{33},\uparrow) v_0(s'), \sum_{s'} p(s'| s_{33},\downarrow)v_0(s'), \sum_{s'} p(s'| s_{33},\rightarrow) v_0(s'), \sum_{s'} p(s'| s_{33}, \leftarrow)v_0(s') \bigr] v1(s33)=−0.04+∑s′p(s′∣s33,→)v0(s′)v_1(s_{33}) = -0.04 + \sum_{s^{'}} p(s^{'}| s_{33},\rightarrow) v_0(s^\prime) v1(s33)=−0.04+p(s43∣s33,→)v0(s43)+p(s33∣s33,→)v0(s33)+p(s32∣s33,→)v0(s32)v_1(s_{33}) = -0.04 + p(s_{43}|s_{33},\rightarrow)v_0(s_{43})+p(s_{33}|s_{33},\rightarrow)v_0(s_{33})+p(s_{32}|s_{33},\rightarrow)v_0(s_{32}) v1(s33)=−0.04+0.8×1+0.1×0+0.1×0=0.76v_1(s_{33}) = -0.04 + 0.8 \times 1 + 0.1 \times 0 + 0.1 \times 0 = 0.76 Output from cell 4 Values v1v_1 after the first sweep. Shaded: the states with a positive-valued next state, which improve in the second sweep.

Value iteration (k=2k = 2)

The values v1v_1 of the first sweep feed the second. Every state that has no positive-valued next state got v1(s)=−0.04v_1(s) = -0.04, and those values now enter the sums: v2(s33)=−0.04+0.8 v1(s43)+0.1 v1(s33)+0.1 v1(s32)=−0.04+0.8×1+0.1×0.76+0.1×(−0.04)=0.832,\begin{aligned} v_2(s_{33}) &= -0.04 + 0.8\, v_1(s_{43}) + 0.1\, v_1(s_{33}) \\ &\quad + 0.1\, v_1(s_{32}) \\ &= -0.04 + 0.8 \times 1 + 0.1 \times 0.76 \\ &\quad + 0.1 \times (-0.04) = 0.832, \end{aligned} v2(s23)=−0.04+0.8 v1(s33)+0.2 v1(s23)=−0.04+0.8×0.76+0.2×(−0.04)=0.560,\begin{aligned} v_2(s_{23}) &= -0.04 + 0.8\, v_1(s_{33}) + 0.2\, v_1(s_{23}) \\ &= -0.04 + 0.8 \times 0.76 + 0.2 \times (-0.04) = 0.560, \end{aligned} where both right-angle moves from s23s_{23} hit the top edge or the wall and leave the agent in place, and v2(s32)=−0.04+0.8 v1(s33)+0.1 v1(s42)+0.1 v1(s32)=−0.04+0.8×0.76+0.1×(−1)+0.1×(−0.04)=0.464.\begin{aligned} v_2(s_{32}) &= -0.04 + 0.8\, v_1(s_{33}) + 0.1\, v_1(s_{42}) \\ &\quad + 0.1\, v_1(s_{32}) \\ &= -0.04 + 0.8 \times 0.76 + 0.1 \times (-1) \\ &\quad + 0.1 \times (-0.04) = 0.464. \end{aligned} The best action is right in s33s_{33} and s23s_{23} and up in s32s_{32}.

Value iteration (k=3k = 3)

Output from cell 5 Values v2v_2 after the second sweep. Shaded: the states that improve in the third sweep.
Notice how information propagates outward from terminal states and eventually all states have correct value estimates. s32s_{32} has a lower value than s23s_{23} because one of its right-angle moves lands on the −1-1 exit
After many iterations, the value estimates converge to the optimal value function v∗(s)v_*(s) and a policy π∗\pi_* can be derived from the value function using the following equation: π∗(s)=arg⁡max⁡a∑s′p(s′∣s,a) v∗(s′)\pi_*(s) = \arg \max_a \sum_{s'} p(s' \mid s, a)\, v_*(s') The reward r(s)r(s) is the same for every action, so it drops out of the arg⁡max⁡\arg\max. Output from cell 6 Optimal values v∗v_* and the optimal policy π∗\pi_* of the maze, after 200 sweeps. The values match Figure 17.3 of Artificial Intelligence: A Modern Approach.

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
The code counts the step cost when the agent arrives in a nonterminal state and gives the exits the value 0, with the ±1\pm 1 paid on arrival. Its values are therefore exactly 0.04 higher than those of the walkthrough above, and the policy is the same. Value iteration interleaves the Bellman expectation backup with a max over actions in a single update: vk+1(s)=max⁡a∑s′p(s′∣s,a) (r(s,a,s′)+γ vk(s′))v_{k+1}(s) = \max_a \sum_{s'} p(s' \mid s, a)\,\bigl(r(s, a, s') + \gamma\, v_k(s')\bigr) After convergence the optimal policy is recovered greedily as π∗(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_*(s')).
Output from cell 9 The loop above computes the action values of each state, keeps their maximum as v(s)v(s), and discards them. q-Value Iteration for a Cleaning Robot keeps the action values from sweep to sweep instead, which lets you read the policy off the table without the model.

Convergence

The rate of convergence depends mainly on the discount factor γ\gamma, and also on the maximum reward value. The policy that you get from coarse estimates is close to the optimal policy long before vv 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 vkv_k and the greedy policy that vkv_k implies.
Output from cell 11 The values change by less than 10−410^{-4} only after 22 sweeps, but the greedy policy stops changing at sweep 12, marked by the dashed line. The states next to the +1+1 exit settle first. Values then spread outward from the terminal states, one step per sweep, which is why state (0,2), the farthest from the +1+1 exit, rises last. Its value even falls for the first four sweeps: until the value of the exit reaches it, every move only adds the step cost.
Found an error on this page? Report an issue.