The greedy step in code
greedify takes the model as arrays. P[s, a, s'] holds the state transition tensor , so it has shape (nS, nA, nS). R[s, a] holds the reward matrix , the reward expected after taking in , averaged over the next state. The array R is this matrix, not the random 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
A robot moves along a row of three states, , and , with a wall to the right of . 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 .
Neither action leaves . A policy that stays in earns per step forever, so under it .
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 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.

