Example 4.1 Consider the 4×4 gridworld shown below.

The nonterminal states are S={1,2,...,14}. There are four actions possible in each state, A={up,down,right,left}, which deterministically cause the corresponding state transitions, except that actions that would take the agent off the grid in fact leave the state unchanged. Thus, for instance, p(6,.1∣5,right)=1, p(7,.1∣7,right)=1, and p(10,r∣5,right)=0 for all r∈R. This is an undiscounted, episodic task. The reward is −1 on all transitions until the terminal state is reached. The terminal state is shaded in the figure (although it is shown in two places, it is formally one state). The expected reward function is thus r(s,a,s0)=−1 for all states s, s′ and actions a. Suppose the agent follows the equiprobable random policy (all actions equally likely). The left side of Figure 4.1 shows the sequence of value functions vk computed by iterative policy evaluation. The final estimate is in fact vπ, which in this case gives for each state the negation of the expected number of steps from that state until termination.

Exercise 4.1 In Example 4.1, if π is the equiprobable random policy, what is qπ(11,down)? What is qπ(7,down)?
Here, we can use the result of exercise 3.13 for calculation. The result of exercise 3.13 is :
qπ(s,a)=s′∑{Ps,s′a⋅[Rs,s′a+γ⋅vπ(s′)]}
where Ps,s′a=p(s′∣s,a) and Rs,s′a=Eπ(r∣s,a,s′).
So,
qπ(11,down)=P11,7down⋅[R11,7down+γ⋅vπ(7)]+P11,10down⋅[R11,10down+γ⋅vπ(10)]+P11,11down⋅[R11,11down+γ⋅vπ(11)]+P11,terminaldown⋅[R11,terminaldown+γ⋅vπ(terminal)]=0+0+0+p(terminal∣11,down)⋅{s′∑[r⋅p(r∣11,down,s′)]+γ⋅vπ(terminal)}=1⋅{[(−1)⋅p(−1∣11,down,7)+(−1)⋅p(−1∣11,down,10)+(−1)⋅p(−1∣11,down,11)+0⋅p(0∣11,down,terminal)]+γ⋅0}=1⋅{[(−1)⋅0+(−1)⋅0+(−1)⋅0+0⋅1]+γ⋅0}=0
qπ(7,down)=P7,3down⋅[R7,3down+γ⋅vπ(3)]+P7,6down⋅[R7,6down+γ⋅vπ(6)]+P7,11down⋅[R7,11down+γ⋅vπ(11)]+P7,7down⋅[R7,7down+γ⋅vπ(7)]=0+0+p(7∣11,down)⋅{s′∑[r⋅p(r∣7,down,s′)]+γ⋅vπ(11)}+0=1⋅{[(−1)⋅p(−1∣7,down,7)+(−1)⋅p(−1∣7,down,3)+(−1)⋅p(−1∣7,down,6)+(−1)⋅p(−1∣7,down,11)]+γ⋅(−14)}=1⋅{[(−1)⋅0+(−1)⋅0+(−1)⋅0+(−1)⋅1]−γ⋅14}=−1−γ⋅14