Skip to main content
Open In Colab The policy π\pi is evaluated when we have produced the state-value function vπ(s)v_\pi(s) for all states. In other words when we know the expected discounted returns that each state can offer us. Suppose we have two states: S={s1,s2}S = \{s_1, s_2\}, and a deterministic policy π\pi that always chooses action aa. The dynamics are:
  • From s1s_1, action leads to s2s_2 with reward 22
  • From s2s_2, action leads to s1s_1 with reward 00
Let γ=0.9\gamma = 0.9 and the state transition matrix under π\pi: Pπ=[0110]P^\pi = \begin{bmatrix} 0 & 1 \\ 1 & 0 \\ \end{bmatrix}
  • Reward vector:
rπ=[20]r^\pi = \begin{bmatrix} 2 \\ 0 \\ \end{bmatrix} From the Bellman expectation equation: vπ=rπ+γPπvπv^\pi = r^\pi + \gamma P^\pi v^\pi Rewriting: (I−γPπ)vπ=rπ(I - \gamma P^\pi) v^\pi = r^\pi Compute: I−γPπ=[1001]−0.9⋅[0110]=[1−0.9−0.91]I - \gamma P^\pi = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ \end{bmatrix} - 0.9 \cdot \begin{bmatrix} 0 & 1 \\ 1 & 0 \\ \end{bmatrix} = \begin{bmatrix} 1 & -0.9 \\ -0.9 & 1 \\ \end{bmatrix} Solve: [1−0.9−0.91][v1v2]=[20]\begin{bmatrix} 1 & -0.9 \\ -0.9 & 1 \\ \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \\ \end{bmatrix} = \begin{bmatrix} 2 \\ 0 \\ \end{bmatrix} Solving gives: v1=2+0.9v2,v2=0.9v1,v1=2+0.81v1,v1=20.19≈10.526,v2≈9.474.\begin{aligned} v_1 &= 2 + 0.9 v_2, \quad v_2 = 0.9 v_1, \\ v_1 &= 2 + 0.81 v_1, \\ v_1 &= \frac{2}{0.19} \approx 10.526, \quad v_2 \approx 9.474. \end{aligned}

Iterative Policy Evaluation

Solving the linear system exactly needs the whole matrix PπP_\pi and takes O(∣S∣3)O(|\mathcal{S}|^3) operations, so its cost grows quickly with the number of states. One sweep of the iterative method takes O(∣S∣2)O(|\mathcal{S}|^2) operations, and fewer when PπP_\pi 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 k=0k=0 by initializing all state-value function (a vector) to v0(s)=0v_0(s)=0. In each iteration kk we start with the state value function of the previous iteration vk(s)v_k(s) 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. policy-evaluation-tree Tree representation of the state-value function with one step look ahead across iterations. The Bellman expectation backup is given by, vk+1(s)=∑a∈Aπ(a∣s)(r(s,a)+γ∑s′∈Sp(s′∣s,a) vk(s′))v_{k+1}(s) = \sum_{a \in \mathcal A} \pi(a|s) \bigl( r(s, a) + \gamma \sum_{s^\prime \in \mathcal S} p(s^\prime \mid s, a)\, v_k(s^\prime) \bigr) and in vector form, vk+1=rπ+γPπvk\mathbf{v}_{k+1} = \mathbf{r}_\pi + \gamma P_\pi \mathbf{v}_k
A simple scalar contraction that illustrates the idea behind iterative policy evaluation is:xk+1=c+γxkx_{k+1} = c + \gamma x_kwhere:
  • cc is a constant (analogous to reward),
  • γ∈[0,1)\gamma \in [0, 1) is the contraction factor (analogous to the discount factor in MDPs).
This scalar recurrence mimics the vector version:vk+1=rπ+γPπvkv_{k+1} = r^\pi + \gamma P^\pi v_kbut in one dimension, where PπP^\pi is just the scalar 1. Solving for the fixed point x=c+γxx = c + \gamma x:x(1−γ)=c⇒x=c1−γx (1 - \gamma) = c \Rightarrow x = \frac{c}{1 - \gamma}Just like in policy evaluation, this converges because the operator T(x)=c+γxT(x) = c + \gamma x is a contraction mapping when γ<1\gamma < 1.
This will converge to x=21−0.9=20x = \frac{2}{1 - 0.9} = 20.
To implement the iterative approach to the MDP solution for the trivial two state problem, we start with initial guess: v0=[00]v_0 = \begin{bmatrix} 0 \\ 0 \end{bmatrix} Update iteratively: Iteration 1: v1=rπ+γPπv0=[20]+0.9⋅[00]=[20]v_1 = r^\pi + \gamma P^\pi v_0 = \begin{bmatrix} 2 \\ 0 \end{bmatrix} + 0.9 \cdot \begin{bmatrix} 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 2 \\ 0 \end{bmatrix} Iteration 2: v2=rπ+γPπv1=[20]+0.9⋅[02]=[21.8]v_2 = r^\pi + \gamma P^\pi v_1 = \begin{bmatrix} 2 \\ 0 \end{bmatrix} + 0.9 \cdot \begin{bmatrix} 0 \\ 2 \end{bmatrix} = \begin{bmatrix} 2 \\ 1.8 \end{bmatrix} Iteration 3: Pπv2=[1.82]⇒v3=[20]+0.9⋅[1.82]=[3.621.8]P^\pi v_2 = \begin{bmatrix} 1.8 \\ 2 \end{bmatrix} \Rightarrow v_3 = \begin{bmatrix} 2 \\ 0 \end{bmatrix} + 0.9 \cdot \begin{bmatrix} 1.8 \\ 2 \end{bmatrix} = \begin{bmatrix} 3.62 \\ 1.8 \end{bmatrix} Iteration 4: Pπv3=[1.83.62]⇒v4=[20]+0.9⋅[1.83.62]=[3.623.258]P^\pi v_3 = \begin{bmatrix} 1.8 \\ 3.62 \end{bmatrix} \Rightarrow v_4 = \begin{bmatrix} 2 \\ 0 \end{bmatrix} + 0.9 \cdot \begin{bmatrix} 1.8 \\ 3.62 \end{bmatrix} = \begin{bmatrix} 3.62 \\ 3.258 \end{bmatrix} Continuing this will converge to: vπ≈[10.5269.474]v^\pi \approx \begin{bmatrix} 10.526 \\ 9.474 \end{bmatrix} which matches the exact solution from the linear system.

Policy evaluation on the 4x3 gridworld

The Introduction to MDP section sampled episodes of the 4x3 gridworld under a fixed policy π\pi and averaged their returns. From s11s_{11}, the average of 10,000 returns was 0.749, an estimate of vπ(s11)v_\pi(s_{11}). 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 −0.04-0.04, entering an exit pays +1+1 or −1-1 and ends the episode, and γ=1\gamma = 1. Every episode ends at an exit, so the values are finite even with γ=1\gamma = 1. 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.

Exact solution

With the matrix PπP_\pi of transition probabilities among the 9 nonterminal states and the vector rπr_\pi of expected rewards, the Bellman expectation equation v=rπ+γPπv\mathbf v = \mathbf r_\pi + \gamma P_\pi \mathbf v is a linear system of 9 equations, solved directly below.
Output from cell 4 Exact values of the two policies. Left: the fixed policy π\pi, with its action in each state. Right: the uniform random policy. The exact value of s11s_{11} under π\pi is 0.745. The estimate of 0.749 from 10,000 sampled returns in the Introduction to MDP section was two standard errors above it. The values rise along the route toward the +1+1 exit, from 0.745 in s11s_{11} to 0.958 in s33s_{33}. The lowest value, 0.428, is in s41s_{41}, where the move west can slip north into the −1-1 exit and the way to +1+1 is longest. Under the uniform random policy every value is negative, from −0.275-0.275 in s33s_{33} to −1.547-1.547 in s11s_{11}. A random walk takes many steps, each costing 0.04, and often ends at the −1-1 exit. The same state has the value 0.745 under π\pi and −1.547-1.547 under the random policy: a value function always belongs to a policy. The low values do not make the random policy useless. They measure how much reward it collects, not what it is good for. A random policy reaches every state and tries every action in it, which is what an agent needs when it does not yet know the environment and has to learn from experience. The methods of the reinforcement learning chapter rely on this: an ϵ\epsilon-greedy policy mixes random actions into the greedy one, so that the agent keeps exploring while it improves.

Iterative method

The same values come from sweeps of the Bellman expectation backup, vk+1=rπ+γPπvk\mathbf v_{k+1} = \mathbf r_\pi + \gamma P_\pi \mathbf v_k, starting from v0=0\mathbf v_0 = \mathbf 0 and stopping when the largest change is below 10−810^{-8}.
Output from cell 6 Largest error of vk\mathbf v_k over the 9 states after each sweep, on a log scale. After 35 sweeps the largest change is below 10−810^{-8}, and the result matches the exact solution to 4×10−94 \times 10^{-9}. The first sweeps show how value spreads back from the exits. vk\mathbf v_k counts only the rewards of the first kk steps, and the +1+1 is at least five steps from s11s_{11}, so vk(s11)v_k(s_{11}) is −0.04-0.04, −0.08-0.08 and −0.12-0.12 after 1, 2 and 3 sweeps, and the error stays close to 1. Once kk reaches the length of the route, the +1+1 starts to count: v5(s11)=0.141v_5(s_{11}) = 0.141, v10(s11)=0.707v_{10}(s_{11}) = 0.707 and v20(s11)=0.745v_{20}(s_{11}) = 0.745. From then on the error falls by about the same factor in every sweep, roughly a half, which is the straight part of the plot.

Policy evaluation using Ray

In a synchronous sweep every new value vk+1(s)v_{k+1}(s) depends only on vk\mathbf v_k, 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 vk\mathbf v_k 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.
Ray reaches the same values in the same 35 sweeps. They agree with the loop’s result to 10−1610^{-16}, the rounding error of floating-point arithmetic, and give vπ(s11)=0.745v_\pi(s_{11}) = 0.745 again.
Found an error on this page? Report an issue.