Skip to main content
Open In Colab In this section we start developing dynamic programming algorithms that solve a perfectly known MDP. Bellman Expectation Backup derived the equations that give the value function of a policy. The Bellman optimality equations are nonlinear and are solved iteratively; their solution is the optimal value function v∗v_* together with an optimal policy π∗\pi_*. Policy iteration starts from some policy π\pi and alternates two steps. The policy evaluation step, the prediction problem, computes vπ(s)v_\pi(s) for every state, for example by repeating the sweep of Policy Evaluation until it converges: vk+1(s)=∑aπ(a∣s)∑s′p(s′∣s,a)[r+γ vk(s′)].v_{k+1}(s) = \sum_a \pi(a \mid s) \sum_{s'} p(s' \mid s, a) \big[ r + \gamma\, v_k(s') \big]. The policy improvement step of Policy Improvement then makes the policy greedy with respect to the result: π′(s)=arg⁡max⁡a∑s′p(s′∣s,a)[r+γ vπ(s′)].\pi'(s) = \arg\max_a \sum_{s'} p(s' \mid s, a) \big[ r + \gamma\, v_\pi(s') \big]. The loop stops when the improvement step no longer changes the policy. A finite MDP has finitely many deterministic policies, and each improvement step gives a policy at least as good as the one before, so policy iteration reaches an optimal policy after a finite number of iterations.
Output from cell 2 Evaluation (blue) moves the values to vπv_\pi of the current policy; improvement (orange) makes the policy greedy with respect to the current values. Each step moves away from the other condition, but together they reach the point where both hold: v∗v_* and π∗\pi_*.

Gridworld policy iteration example

The grid world below is the example of Sutton and Barto (Sutton & Barto, 2018):
  • It is an undiscounted episodic MDP (γ=1\gamma = 1).
  • The nonterminal states are s1,…,s14s_1, \dots, s_{14}. There is one terminal state, drawn twice as the shaded corners.
  • The four actions move the agent up, down, left or right with probability 1. An action that would leave the grid leaves the state unchanged.
  • The reward is −1-1 on every transition until the terminal state is reached.
  • The starting policy is the uniform random policy, π(up∣s)=π(down∣s)=π(left∣s)=π(right∣s)=0.25\pi(\text{up} \mid s) = \pi(\text{down} \mid s) = \pi(\text{left} \mid s) = \pi(\text{right} \mid s) = 0.25.
Output from cell 3 The 4x4 gridworld. The shaded corners are the terminal state, and the reward is −1-1 on every transition.

Policy evaluation

Evaluating the random policy starts from v0=0v_0 = 0 and applies the sweep above with γ=1\gamma = 1. Each of the four actions has probability 1/41/4 and leads to a single next state, so the sweep averages the four neighbors: v1(s1)=−1+14[v0(sterm)+v0(s1)+v0(s2)+v0(s5)]=−1,v1(s9)=−1+14[v0(s5)+v0(s8)+v0(s10)+v0(s13)]=−1.\begin{aligned} v_1(s_1) &= -1 + \tfrac{1}{4} [ v_0(s_{\text{term}}) + v_0(s_1) + v_0(s_2) + v_0(s_5) ] \\ &= -1, \\ v_1(s_9) &= -1 + \tfrac{1}{4} [ v_0(s_5) + v_0(s_8) + v_0(s_{10}) + v_0(s_{13}) ] \\ &= -1. \end{aligned} In s1s_1 the move up leaves the grid, so s1s_1 appears as its own neighbor. Every nonterminal state gets v1(s)=−1v_1(s) = -1. The figure below shows vkv_k for more sweeps, each next to its greedy policy; at k=∞k = \infty the values have converged to vπv_\pi of the random policy.

Policy improvement

The improvement step picks, in each state, the move with the largest q(s,a)=−1+v(s′)q(s, a) = -1 + v(s'). With v1v_1, the four moves from s4s_4 are worth q(s4,↑)=−1+v1(sterm)=−1,q(s4,→)=−1+v1(s5)=−2,q(s4,←)=−1+v1(s4)=−2,q(s4,↓)=−1+v1(s8)=−2,\begin{aligned} q(s_4, \uparrow) &= -1 + v_1(s_{\text{term}}) = -1, \\ q(s_4, \rightarrow) &= -1 + v_1(s_5) = -2, \\ q(s_4, \leftarrow) &= -1 + v_1(s_4) = -2, \\ q(s_4, \downarrow) &= -1 + v_1(s_8) = -2, \end{aligned} so π′(s4)=↑\pi'(s_4) = \uparrow, while in s6s_6 all four moves give −2-2, so all four are greedy. In the figure, each vkv_k is paired with its greedy policy, with every tied move drawn. Output from cell 4 Iterative evaluation of the random policy: each vkv_k is followed by the greedy policy with respect to it. The greedy policy stops changing at k=3k = 3, long before the values converge, and every move it keeps is optimal: each one leads along a shortest path to the terminal state. Policy iteration evaluates the random policy to convergence (k=∞k = \infty) and improves it once, which already gives an optimal policy for this gridworld. A second round of evaluation and improvement leaves that policy unchanged, and the algorithm stops.

Policy iteration on the 4x3 gridworld

The code below runs policy iteration on a different MDP: the 4x3 grid world of Artificial Intelligence: A Modern Approach, Figure 17.2. Unlike the 4x4 example above, 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.
  • Terminal states: +1 at (3,0), -1 at (3,1)
  • Wall: (1,1)
  • Step cost: -0.04
  • Discount factor γ = 1.0
Output from cell 7 Key references: (Sutton & Barto, 2018)

References

  • Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link

Found an error on this page? Report an issue.