The greedy step in code
greedify takes the model as arrays. P[s, a, s'] holds , so it has shape (nS, nA, nS), and R[s, a] holds the expected reward . The matrix product P @ v takes the dot product of every row with the value vector, so (P @ v)[s, a] is , the expected value of the next state. Adding R to times that gives the whole table 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 in and , and . 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 . In the wall stops the move, so action 1 there always stays and still receives . Neither action leaves , so once the robot gets there it stays forever. Staying earns per step, so . The discount factor is .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 both parts land on , so P[2, 1, 2] adds up to 1.
evaluate computes with iterative policy evaluation. The index P[idx, pi] picks, for each state , the row of the action that the policy takes there. That is the transition matrix of the policy, and R[idx, pi] is its reward vector . Here is deterministic: pi[s] is the single action taken in state . Each pass of the loop applies the Bellman expectation backup to every state at once, starting from . The backup is a contraction, so the loop stops when no state changes by more than tol.
Code
Code
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.Code
Code
Round 2
Under , state 1 is worth 8.66, and that value now reaches state 0:Code
Code
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 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.Code
Code
greedify compares in each round: the two action values of every state under the current value function. The greedy action has the outlined bar.
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).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.

