Skip to main content
Open In Colab In the policy improvement step you are given the value function of a policy and act greedily with respect to it: π′=greedy(vπ)\pi^\prime = \mathtt{greedy}(v_\pi) The policy improvement theorem guarantees that the greedy policy is at least as good as the one you started from: vπ′(s)≥vπ(s)v_{\pi^\prime}(s) \ge v_\pi(s) in every state. Repeating evaluation and improvement therefore converges to an optimal policy. Bellman Optimality shows why the policy where improvement stops is optimal. The step itself is one pass: given vπv_\pi, compute qπ(s,a)=r(s,a)+γ∑s′p(s′∣s,a) vπ(s′)q_\pi(s, a) = r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v_\pi(s') for every action and take the argmax.

The greedy step in code

greedify takes the model as arrays. P[s, a, s'] holds p(s′∣s,a)p(s' \mid s, a), so it has shape (nS, nA, nS), and R[s, a] holds the expected reward r(s,a)r(s, a). The matrix product P @ v takes the dot product of every row p(⋅∣s,a)p(\cdot \mid s, a) with the value vector, so (P @ v)[s, a] is ∑s′p(s′∣s,a) v(s′)\sum_{s'} p(s' \mid s, a)\, v(s'), the expected value of the next state. Adding R to γ\gamma times that gives the whole table qπ(s,a)q_\pi(s, a) in one line, and argmax along the action axis picks the greedy action in every state.

Example environment

The MDP has three states in a row and two actions. Action 0 stays put. Staying has reward r(s,stay)=0r(s, \text{stay}) = 0 in s0s_0 and s1s_1, and r(s2,stay)=+1r(s_2, \text{stay}) = +1. Action 1 tries to move one state to the right: it succeeds with probability 0.8, the agent slips and stays with probability 0.2, and every attempt has reward r(s,right)=−0.1r(s, \text{right}) = -0.1. In s2s_2 the wall stops the move, so action 1 there always stays and still receives r=−0.1r = -0.1. Neither action leaves s2s_2, so once the robot gets there it stays forever. Staying earns +1+1 per step, so v(s2)=1/(1−γ)=10v(s_2) = 1/(1 - \gamma) = 10. The discount factor is γ=0.9\gamma = 0.9. Output from cell 3 The three-state MDP. Blue arrows are action 0 (stay); orange arrows are action 1 (move right), labeled with their probabilities. The code below builds this MDP as the two arrays that greedify expects. For action 0, P[s, 0, s] = 1. For action 1, probability 0.8 goes to the state on the right, min(s + 1, 2), and 0.2 stays in s. In s2s_2 both parts land on s2s_2, so P[2, 1, 2] adds up to 1. evaluate computes vπv_\pi with iterative policy evaluation. The index P[idx, pi] picks, for each state ss, the row of the action that the policy takes there. That is the transition matrix PπP_\pi of the policy, and R[idx, pi] is its reward vector rπr_\pi. Here π\pi is deterministic: pi[s] is the single action taken in state ss. Each pass of the loop applies the Bellman expectation backup vk+1=rπ+γPπvkv_{k+1} = r_\pi + \gamma P_\pi v_k to every state at once, starting from v0=0v_0 = 0. The backup is a contraction, so the loop stops when no state changes by more than tol.

Round 1

Start from the policy that always stays, evaluate it, and improve it greedily. Then evaluate the new policy to check that it is better.
Under vπv_\pi, state 1 has a neighbor worth 10, so moving right is worth qπ(s1,right)=−0.1+0.9 (0.8⋅10+0.2⋅0)=7.1q_\pi(s_1, \text{right}) = -0.1 + 0.9\,(0.8 \cdot 10 + 0.2 \cdot 0) = 7.1 against 0 for staying, and state 1 switches. State 0 keeps staying, because under vπv_\pi its only neighbor is worth nothing and moving right only adds the reward r=−0.1r = -0.1.

Round 2

Under vπ′v_{\pi'}, state 1 is worth 8.66, and that value now reaches state 0:

Round 3 and optimality

A third round finds nothing to change. The policy is now greedy with respect to its own values, so another improvement step returns the same policy. The code also computes v∗v_* by repeating the backup with a max over actions until the values stop changing. The two agree, so the stable policy is optimal. Bellman Optimality explains why a policy that is greedy with respect to its own values is optimal.
The figure shows what greedify compares in each round: the two action values of every state under the current value function. The greedy action has the outlined bar. Output from cell 8 Action values in each round, computed from the values of the current policy. Round 1 switches s1s_1 to right, round 2 switches s0s_0, and round 3 changes nothing. Each improvement raises the value of the state it changes and lowers no other value, as the policy improvement theorem promises. The value of state 1 rises from 0 to 8.66 in round 1, and the value of state 0 rises from 0 to 7.48 in round 2. Output from cell 9 The policy and the value of each state after every round. Darker cells have higher values. The full policy iteration algorithm alternates policy evaluation with this step until the policy is stable; see Policy Iteration for the outer loop and a gridworld demo. A stable policy is greedy with respect to its own values, and Bellman Optimality Backup shows that such a policy is optimal.
All the policies in this section are deterministic. A policy is stored as one action per state, pi[s], and greedify returns one action per state, because argmax breaks ties by taking the first action. Nothing is lost by this choice. The policy improvement theorem also holds for a stochastic greedy policy that splits its probability among tied actions, and a finite MDP always has a deterministic optimal policy (Sutton & Barto, 2018).
Key references: (Mansour & Singh, 2013; Ahmed et al., 2018; Schulman et al., 2015; Huang et al., 2022)

References

  • Ahmed, Z., Le Roux, N., Norouzi, M., Schuurmans, D. (2018). Understanding the impact of entropy on policy optimization. arXiv [cs.LG]. Link
  • Huang, S., Kanervisto, A., Raffin, A., Wang, W., Ontañón, S., et al. (2022). A2C is a special case of PPO. arXiv [cs.LG]. Link
  • Mansour, Y., Singh, S. (2013). On the Complexity of Policy Iteration. arXiv [cs.AI]. Link
  • Schulman, J., Levine, S., Moritz, P., Jordan, M., Abbeel, P. (2015). Trust Region Policy Optimization. arXiv [cs.LG]. Link
  • Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link

Found an error on this page? Report an issue.