Exact Solution (not recommended)
Suppose we have two states: , and a deterministic policy that always chooses action . The dynamics are:- From , action leads to with reward
- From , action leads to with reward
- Reward vector:
Iterative Policy Evaluation
Solving the linear system exactly needs the whole matrix and takes operations, so its cost grows quickly with the number of states. One sweep of the iterative method takes operations, and fewer when is sparse, as it is when each state has only a few possible next states. This method is based on the recognition that the Bellman operator is contractive. We can apply the Bellman expectation backup equations repeatedly in an iterative fashion and converge to the state-value function of the policy. We start at by initializing all state-value function (a vector) to . In each iteration we start with the state value function of the previous iteration and apply the Bellman expectation backup as prescribed by the one step lookahead tree below that is decorated relative to what we have seen in the Bellman expectation backup with the iteration information. This is called the synchronous backup formulation as we are updating all the elements of the value function vector at the same time.
A simple scalar contraction that illustrates the idea behind iterative policy evaluation is:where:
This will converge to .
- is a constant (analogous to reward),
- is the contraction factor (analogous to the discount factor in MDPs).
Code
Code
Policy evaluation on the 4x3 gridworld
The Introduction to MDP section sampled episodes of the 4x3 gridworld under a fixed policy and averaged their returns. From , the average of 10,000 returns was 0.749, an estimate of . Policy evaluation computes the exact values from the model instead. The gridworld, its transition model and the policy are the same as in that section. The intended move succeeds with probability 0.8, and with probability 0.1 each the agent moves at a right angle to it. Entering a state costs , entering an exit pays or and ends the episode, and . Every episode ends at an exit, so the values are finite even with . The exits are absorbing with value 0, which leaves one unknown value for each of the 9 other states. The code also evaluates the uniform random policy, which picks each of the four moves with probability 1/4.Code
Code
Exact solution
With the matrix of transition probabilities among the 9 nonterminal states and the vector of expected rewards, the Bellman expectation equation is a linear system of 9 equations, solved directly below.Code
Code
Iterative method
The same values come from sweeps of the Bellman expectation backup, , starting from and stopping when the largest change is below .Code
Code
Policy evaluation using Ray
In a synchronous sweep every new value depends only on , so the backups of different states are independent and can run in parallel. The code below runs each sweep as one Ray task per state. The current vector is shared with all the tasks of the sweep, and the sweep ends when all of them have returned. For 9 states the cost of scheduling the tasks is far larger than the arithmetic, so this is slower than the loop above. The same structure pays off when the state space is large and each backup is expensive. For more, see the Ray documentation and Ray RLlib.Code
Code
Found an error on this page? Report an issue.

