Chapter 4 develops policy evaluation, policy iteration, and value iteration when a complete model of the environment is available.
Exercise 4.1
q π ( s , a ) = ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ v π ( s ′ ) ] q_\pi(s,a) = \sum_{s',r} p(s',r \mid s,a) \Big[\, r + \gamma v_\pi(s') \Big] q π ( s , a ) = s ′ , r ∑ p ( s ′ , r ∣ s , a ) [ r + γ v π ( s ′ ) ]
Since the transition probabilities are deterministic, we can simplify the equation to:
q π ( s , a ) = r + γ v π ( s ′ ) q_\pi(s,a) = r + \gamma v_\pi(s') q π ( s , a ) = r + γ v π ( s ′ )
q π ( 11 , d o w n ) = − 1 + 1 ( 0 ) = − 1 q_\pi(11, down) = -1 + 1(0) = -1 q π ( 11 , d o w n ) = − 1 + 1 ( 0 ) = − 1
q π ( 7 , d o w n ) = − 1 + 1 ( − 14 ) = − 15 q_\pi(7, down) = -1 + 1(-14) = -15 q π ( 7 , d o w n ) = − 1 + 1 ( − 14 ) = − 15
Exercise 4.2
Since the transition probabilities are deterministic and γ = 1 \gamma = 1 γ = 1 , we can write the state values as:
v π ( s ) = ∑ a π ( a ∣ s ) [ r + v π ( s ′ ) ] v_\pi(s) = \sum_{a} \pi(a \mid s) \Big[ r + v_\pi(s') \Big] v π ( s ) = a ∑ π ( a ∣ s ) [ r + v π ( s ′ ) ]
For the first scenario, where the up \text{up} up action at state 15 15 15 leads to state 13 13 13 , we have:
v π ( 15 ) = − 1 + 0.25 [ v π ( 12 ) + v π ( 13 ) + v π ( 14 ) + v π ( 15 ) ] v_\pi(15) = -1 + 0.25 \Big[ v_\pi(12) + v_\pi(13) + v_\pi(14) + v_\pi(15)\Big] v π ( 15 ) = − 1 + 0.25 [ v π ( 12 ) + v π ( 13 ) + v π ( 14 ) + v π ( 15 ) ]
v π ( 15 ) = − 1 + 0.25 ( v π ( 15 ) − 56 ) v_\pi(15) = -1 + 0.25 \Big(v_\pi(15) -56\Big) v π ( 15 ) = − 1 + 0.25 ( v π ( 15 ) − 56 )
0.75 ⋅ v π ( 15 ) = − 15 0.75 \cdot v_\pi(15) = -15 0.75 ⋅ v π ( 15 ) = − 15
v π ( 15 ) = − 20 v_\pi(15) = -20 v π ( 15 ) = − 20
For the second scenario, where the down \text{down} down action at state 13 13 13 also leads to state 15 15 15 , we have:
v π ( 15 ) = − 1 + 0.25 [ v π ( 12 ) + v π ( 13 ) + v π ( 14 ) + v π ( 15 ) ] v_\pi(15) = -1 + 0.25 \Big[ v_\pi(12) + v_\pi(13) + v_\pi(14) + v_\pi(15)\Big] v π ( 15 ) = − 1 + 0.25 [ v π ( 12 ) + v π ( 13 ) + v π ( 14 ) + v π ( 15 ) ]
v π ( 15 ) = − 1 + 0.25 [ v π ( 13 ) + v π ( 15 ) − 36 ] v_\pi(15) = -1 + 0.25 \Big[ v_\pi(13) + v_\pi(15) - 36 \Big] v π ( 15 ) = − 1 + 0.25 [ v π ( 13 ) + v π ( 15 ) − 36 ]
0.75 ⋅ v π ( 15 ) − 0.25 ⋅ v π ( 13 ) = − 10 (1) 0.75 \cdot v_\pi(15) - 0.25 \cdot v_\pi(13) = -10 \tag{1} 0.75 ⋅ v π ( 15 ) − 0.25 ⋅ v π ( 13 ) = − 10 ( 1 )
v π ( 13 ) = − 1 + 0.25 [ v π ( 9 ) + v π ( 12 ) + v π ( 14 ) + v π ( 15 ) ] v_\pi(13) = -1 + 0.25 \Big[ v_\pi(9) + v_\pi(12) + v_\pi(14) + v_\pi(15)\Big] v π ( 13 ) = − 1 + 0.25 [ v π ( 9 ) + v π ( 12 ) + v π ( 14 ) + v π ( 15 ) ]
v π ( 13 ) = − 1 + 0.25 [ v π ( 15 ) − 56 ] v_\pi(13) = -1 + 0.25 \Big[ v_\pi(15) -56 \Big] v π ( 13 ) = − 1 + 0.25 [ v π ( 15 ) − 56 ]
v π ( 13 ) − 0.25 ⋅ v π ( 15 ) = − 15 (2) v_\pi(13) - 0.25 \cdot v_\pi(15) = -15 \tag{2} v π ( 13 ) − 0.25 ⋅ v π ( 15 ) = − 15 ( 2 )
Solving the two equations simultaneously, we get:
v π ( 15 ) = − 20 v_\pi(15) = -20 v π ( 15 ) = − 20
Exercise 4.3
The analog of (4.3) is:
q π ( s , a ) = E π [ R t + 1 + γ q π ( S t + 1 , A t + 1 ) | S t = s , A t = a ] q_{\pi}(s,a) = \mathbb{E}_{\pi}\!\left[ R_{t+1} + \gamma \, q_{\pi}(S_{t+1},A_{t+1}) \;\middle|\; S_t = s,\, A_t = a \right] q π ( s , a ) = E π [ R t + 1 + γ q π ( S t + 1 , A t + 1 ) ∣ S t = s , A t = a ]
The analog of (4.4) is:
q π ( s , a ) = ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ ∑ a ′ π ( a ′ ∣ s ′ ) q π ( s ′ , a ′ ) ] q_{\pi}(s,a) = \sum_{s',r} p(s',r \mid s,a) \Big[ r + \gamma \sum_{a'} \pi(a' \mid s') \, q_{\pi}(s',a') \Big] q π ( s , a ) = s ′ , r ∑ p ( s ′ , r ∣ s , a ) [ r + γ a ′ ∑ π ( a ′ ∣ s ′ ) q π ( s ′ , a ′ ) ]
The analog of (4.5) is:
q k + 1 ( s , a ) = ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ ∑ a ′ π ( a ′ ∣ s ′ ) q k ( s ′ , a ′ ) ] q_{k+1}(s,a) = \sum_{s',r} p(s',r \mid s,a) \Big[ r + \gamma \sum_{a'} \pi(a' \mid s') \, q_{k}(s',a') \Big] q k + 1 ( s , a ) = s ′ , r ∑ p ( s ′ , r ∣ s , a ) [ r + γ a ′ ∑ π ( a ′ ∣ s ′ ) q k ( s ′ , a ′ ) ]
Exercise 4.4
To fix this bug, we should update the policy improvement algorithm to only update the policy if there's a strict improvement in the value function.
policy-stable ← true \text{policy-stable} \leftarrow \text{true} policy-stable ← true
For each s ∈ S \text{For each } s \in \mathcal{S} For each s ∈ S :
old-action ← π ( s ) \quad \text{old-action} \leftarrow \pi(s) old-action ← π ( s )
For each a ∈ A \quad \text{For each } a \in \mathcal{A} For each a ∈ A :
Q ( s , a ) ← ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ V ( s ′ ) ] \quad \quad Q(s, a) \leftarrow \sum_{s',r} p(s', r \mid s, a) \Big[ r + \gamma V(s') \Big] Q ( s , a ) ← ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γV ( s ′ ) ]
best-action ← arg max a Q ( s , a ) \quad \text{best-action} \leftarrow \argmax_a Q(s, a) best-action ← arg max a Q ( s , a )
if Q ( s , best-action ) > Q ( s , old-action ) : \quad \text{if } Q(s, \text{best-action}) > Q(s, \text{old-action}): if Q ( s , best-action ) > Q ( s , old-action ) :
π ( s ) ← best-action \quad \quad \pi(s) \leftarrow \text{best-action} π ( s ) ← best-action
policy-stable ← false \quad \quad \text{policy-stable} \leftarrow \text{false} policy-stable ← false
If policy-stable, then stop and return V ≈ v ∗ and π ≈ π ∗ ; else go to 2 \text{If policy-stable, then stop and return } V \approx v^* \text{ and } \pi \approx \pi^* \text{; else go to 2} If policy-stable, then stop and return V ≈ v ∗ and π ≈ π ∗ ; else go to 2
Exercise 4.5
1. Initialization
Q ( s , a ) ∈ R , π ( s ) ∈ A ( s ) , ∀ s ∈ S , a ∈ A ( s ) Q ( terminal , a ) = 0 Q(s,a) \in \mathbb{R}, \quad \pi(s) \in \mathcal{A}(s), \quad \forall s \in \mathcal{S}, a \in \mathcal{A}(s) \quad Q(\text{terminal}, a) = 0 Q ( s , a ) ∈ R , π ( s ) ∈ A ( s ) , ∀ s ∈ S , a ∈ A ( s ) Q ( terminal , a ) = 0
2. Policy Evaluation
Loop: \text{Loop:} Loop:
Δ ← 0 \quad \Delta \leftarrow 0 Δ ← 0
Loop for each s ∈ S , a ∈ A ( s ) : \quad \text{Loop for each } s \in \mathcal{S}, a \in \mathcal{A}(s): Loop for each s ∈ S , a ∈ A ( s ) :
q ← Q ( s , a ) \quad \quad q \leftarrow Q(s, a) q ← Q ( s , a )
Q ( s , a ) ← ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ Q ( s ′ , π ( s ′ ) ) ] \quad \quad Q(s,a) \leftarrow \sum_{s',r} p(s', r \mid s, a) \big[ r + \gamma Q(s', \pi(s')) \big] Q ( s , a ) ← ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ Q ( s ′ , π ( s ′ )) ]
Δ ← max ( Δ , ∣ Q ( s , a ) − q ∣ ) \quad \quad \Delta \leftarrow \max(\Delta, |Q(s,a) - q|) Δ ← max ( Δ , ∣ Q ( s , a ) − q ∣ )
until Δ < θ \text{until } \Delta < \theta until Δ < θ
3. Policy Improvement
policy-stable ← true \text{policy-stable} \leftarrow \text{true} policy-stable ← true
For each s ∈ S \text{For each } s \in \mathcal{S} For each s ∈ S :
old-action ← π ( s ) \quad \text{old-action} \leftarrow \pi(s) old-action ← π ( s )
π ( s ) ← arg max a Q ( s , a ) \quad \pi(s) \leftarrow \argmax_a Q(s, a) π ( s ) ← arg max a Q ( s , a )
if π ( s ) ≠ old-action : \quad \text{if } \pi(s) \neq \text{old-action}: if π ( s ) = old-action :
policy-stable ← false \quad \quad \text{policy-stable} \leftarrow \text{false} policy-stable ← false
If policy-stable, then stop and return Q ≈ q ∗ and π ≈ π ∗ ; else go to 2 \text{If policy-stable, then stop and return } Q \approx q^* \text{ and } \pi \approx \pi^* \text{; else go to 2} If policy-stable, then stop and return Q ≈ q ∗ and π ≈ π ∗ ; else go to 2
Exercise 4.6
3. Policy Improvement
We will replace the deterministic greedy update with an ϵ \epsilon ϵ -soft update.
π ( a ∣ s ) = { 1 − ϵ + ϵ ∣ A ( s ) ∣ , if a = arg max a q ( s , a ) ϵ ∣ A ( s ) ∣ , otherwise. \pi(a \mid s) =
\begin{cases}
1 - \epsilon + \dfrac{\epsilon}{|\mathcal{A}(s)|}, & \text{if } a = \arg\max_a q(s, a) \\
\dfrac{\epsilon}{|\mathcal{A}(s)|}, & \text{otherwise.}
\end{cases} π ( a ∣ s ) = ⎩ ⎨ ⎧ 1 − ϵ + ∣ A ( s ) ∣ ϵ , ∣ A ( s ) ∣ ϵ , if a = arg max a q ( s , a ) otherwise.
Also, we will now check if the policy is stable by comparing the distributions π \pi π and π ′ \pi' π ′ rather than just the argmax action.
2. Policy Evaluation
The Bellman expectation update must use π ( a ∣ s ) \pi(a \mid s) π ( a ∣ s ) instead of a single action.
V ( s ) ← ∑ a π ( a ∣ s ) ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ V ( s ′ ) ] V(s) \leftarrow \sum_a \pi(a \mid s) \sum_{s',r} p(s', r \mid s, a)\bigl[ r + \gamma V(s') \bigr] V ( s ) ← a ∑ π ( a ∣ s ) s ′ , r ∑ p ( s ′ , r ∣ s , a ) [ r + γV ( s ′ ) ]
3. Policy Improvement
We will initialize π \pi π as an ϵ \epsilon ϵ -soft policy (e.g. uniform over all actions) and set V ( s ) = 0 V(s) = 0 V ( s ) = 0 .
Exercise 4.7
Since one free car can move from location 1 to 2, the optimal policy is asymmetric: it often sends cars 1 → 2 1 \rightarrow 2 1 → 2 and only shifts 2 → 1 2 \rightarrow 1 2 → 1 when location 1 is short.
Moreover, a 4 4 4 parking surcharge creates sharp boundaries around 10 10 10 cars, so the policy actively avoids holding more than 10 10 10 cars at either site, which produces the visible "notches" in the contour plots.
The optimal transfer policy with free movement from location 1 to location 2 and parking surcharges.
The corresponding optimal value function.
Exercise 4.8
The coin is not fair and the odds are against the gambler. As the number of flips increases, the probability of winning decreases.
When the gambler's capital allows them to reach 100 100 100 exactly with one win (such as at 50 50 50 or 75 75 75 ), the best strategy is to bet everything - it gives the highest possible chance of winning quickly.
But when the capital is just above or below such points (such as at 51 51 51 ), betting everything becomes too risky: losing would drop them too close to 0 0 0 , where recovery is unlikely. In those cases, smaller bets slightly increase the overall probability of eventually reaching the goal.
Exercise 4.9
We can see from the plots that for p h = 0.25 p_h = 0.25 p h = 0.25 , the results are very similar to those for p h = 0.4 p_h = 0.4 p h = 0.4 . In both cases, the odds are against the gambler, leading to large, high-risk bets only when the gambler can reach the goal exactly on a win.
In contrast, when p h = 0.55 p_h = 0.55 p h = 0.55 , the odds are favorable. The gambler adopts smaller, safer bets, using the advantage to make steady progress toward the goal without taking unnecessary risks.
Optimal stakes under unfavorable and favorable coin biases.
The associated value functions for each coin bias.
Exercise 4.10
q k + 1 ( s , a ) = E [ R t + 1 + γ max a ′ q k ( S t + 1 , a ′ ) | S t = s , A t = a ] q_{k+1}(s, a) = \mathbb{E}\left[ R_{t+1} + \gamma \max_{a'} q_k(S_{t+1}, a') \,\middle|\, S_t = s, A_t = a \right] q k + 1 ( s , a ) = E [ R t + 1 + γ a ′ max q k ( S t + 1 , a ′ ) S t = s , A t = a ]
= ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ max a ′ q k ( s ′ , a ′ ) ] = \sum_{s', r} p(s', r \mid s, a) \left[ r + \gamma \max_{a'} q_k(s', a') \right] = s ′ , r ∑ p ( s ′ , r ∣ s , a ) [ r + γ a ′ max q k ( s ′ , a ′ ) ]