Skip to content
Ahmed Haroon
Solved Exercises for Reinforcement Learning

Dynamic Programming

Policy evaluation, policy iteration, value iteration, and the gambler's problem.

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,rp(s,rs,a)[r+γvπ(s)]q_\pi(s,a) = \sum_{s',r} p(s',r \mid s,a) \Big[\, r + \gamma v_\pi(s') \Big]

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π(11,down)=1+1(0)=1q_\pi(11, down) = -1 + 1(0) = -1 qπ(7,down)=1+1(14)=15q_\pi(7, down) = -1 + 1(-14) = -15

Exercise 4.2

Since the transition probabilities are deterministic and γ=1\gamma = 1, we can write the state values as:

vπ(s)=aπ(as)[r+vπ(s)]v_\pi(s) = \sum_{a} \pi(a \mid s) \Big[ r + v_\pi(s') \Big]

For the first scenario, where the up\text{up} action at state 1515 leads to state 1313, 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π(15)56)v_\pi(15) = -1 + 0.25 \Big(v_\pi(15) -56\Big) 0.75vπ(15)=150.75 \cdot v_\pi(15) = -15 vπ(15)=20v_\pi(15) = -20

For the second scenario, where the down\text{down} action at state 1313 also leads to state 1515, 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π(13)+vπ(15)36]v_\pi(15) = -1 + 0.25 \Big[ v_\pi(13) + v_\pi(15) - 36 \Big] 0.75vπ(15)0.25vπ(13)=10(1)0.75 \cdot v_\pi(15) - 0.25 \cdot v_\pi(13) = -10 \tag{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π(15)56]v_\pi(13) = -1 + 0.25 \Big[ v_\pi(15) -56 \Big] vπ(13)0.25vπ(15)=15(2)v_\pi(13) - 0.25 \cdot v_\pi(15) = -15 \tag{2}

Solving the two equations simultaneously, we get:

vπ(15)=20v_\pi(15) = -20

Exercise 4.3

The analog of (4.3) is:

qπ(s,a)=Eπ ⁣[Rt+1+γqπ(St+1,At+1)  |  St=s,At=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]

The analog of (4.4) is:

qπ(s,a)=s,rp(s,rs,a)[r+γaπ(as)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]

The analog of (4.5) is:

qk+1(s,a)=s,rp(s,rs,a)[r+γaπ(as)qk(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]

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-stabletrue\text{policy-stable} \leftarrow \text{true}

For each sS\text{For each } s \in \mathcal{S}:

old-actionπ(s)\quad \text{old-action} \leftarrow \pi(s)

For each aA\quad \text{For each } a \in \mathcal{A}:

Q(s,a)s,rp(s,rs,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]

best-actionarg maxaQ(s,a)\quad \text{best-action} \leftarrow \argmax_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}):

π(s)best-action\quad \quad \pi(s) \leftarrow \text{best-action}

policy-stablefalse\quad \quad \text{policy-stable} \leftarrow \text{false}

If policy-stable, then stop and return Vv 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}

Exercise 4.5

1. Initialization

Q(s,a)R,π(s)A(s),sS,aA(s)Q(terminal,a)=0Q(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

2. Policy Evaluation

Loop:\text{Loop:}

Δ0\quad \Delta \leftarrow 0

Loop for each sS,aA(s):\quad \text{Loop for each } s \in \mathcal{S}, a \in \mathcal{A}(s):

qQ(s,a)\quad \quad q \leftarrow Q(s, a)

Q(s,a)s,rp(s,rs,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]

Δmax(Δ,Q(s,a)q)\quad \quad \Delta \leftarrow \max(\Delta, |Q(s,a) - q|)

until Δ<θ\text{until } \Delta < \theta

3. Policy Improvement

policy-stabletrue\text{policy-stable} \leftarrow \text{true}

For each sS\text{For each } s \in \mathcal{S}:

old-actionπ(s)\quad \text{old-action} \leftarrow \pi(s)

π(s)arg maxaQ(s,a)\quad \pi(s) \leftarrow \argmax_a Q(s, a)

if π(s)old-action:\quad \text{if } \pi(s) \neq \text{old-action}:

policy-stablefalse\quad \quad \text{policy-stable} \leftarrow \text{false}

If policy-stable, then stop and return Qq 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}

Exercise 4.6

3. Policy Improvement

We will replace the deterministic greedy update with an ϵ\epsilon-soft update.

π(as)={1ϵ+ϵA(s),if a=argmaxaq(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}

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 π(as)\pi(a \mid s) instead of a single action.

V(s)aπ(as)s,rp(s,rs,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]

3. Policy Improvement

We will initialize π\pi as an ϵ\epsilon-soft policy (e.g. uniform over all actions) and set V(s)=0V(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 121 \rightarrow 2 and only shifts 212 \rightarrow 1 when location 1 is short.

Moreover, a 44 parking surcharge creates sharp boundaries around 1010 cars, so the policy actively avoids holding more than 1010 cars at either site, which produces the visible "notches" in the contour plots.

Optimal policy for the modified Jack's car rental problem

The optimal transfer policy with free movement from location 1 to location 2 and parking surcharges.

Optimal value function for the modified Jack's car rental problem

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 100100 exactly with one win (such as at 5050 or 7575), 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 5151), betting everything becomes too risky: losing would drop them too close to 00, 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 ph=0.25p_h = 0.25, the results are very similar to those for ph=0.4p_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 ph=0.55p_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 policies for the gambler's problem at different probabilities of heads

Optimal stakes under unfavorable and favorable coin biases.

Value functions for the gambler's problem at different probabilities of heads

The associated value functions for each coin bias.

Exercise 4.10

qk+1(s,a)=E[Rt+1+γmaxaqk(St+1,a)|St=s,At=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] =s,rp(s,rs,a)[r+γmaxaqk(s,a)]= \sum_{s', r} p(s', r \mid s, a) \left[ r + \gamma \max_{a'} q_k(s', a') \right]