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 the state transition tensor Pss′a=p(s′∣s,a)\mathcal P^a_{ss'} = p(s' \mid s, a), so it has shape (nS, nA, nS). R[s, a] holds the reward matrix Rsa=r(s,a)=E[Rt+1∣St=s,At=a]\mathcal R^a_s = r(s, a) = \mathbb E[R_{t+1} \mid S_t = s, A_t = a], the reward expected after taking aa in ss, averaged over the next state. The array R is this matrix, not the random reward Rt+1R_{t+1}. 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

A robot moves along a row of three states, s0s_0, s1s_1 and s2s_2, with a wall to the right of s2s_2. It has two actions: 0 (stay) and 1 (move right). A move to the right fails with probability 0.2, and the robot then stays where it is. The table lists every transition and its reward. The discount factor is γ=0.9\gamma = 0.9. Neither action leaves s2s_2. A policy that stays in s2s_2 earns +1+1 per step forever, so under it v(s2)=1/(1−γ)=10v(s_2) = 1/(1 - \gamma) = 10. 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 stores the table in the arrays P and R. In this MDP the reward does not depend on the next state, so the average over the next state changes nothing and R[s, a] is the reward in the table. The arrays describe only the environment, so R holds one-step rewards, not values. The values come from evaluate, which adds the rewards up over time. 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.