Skip to content
Ahmed Haroon
Solved Exercises for Reinforcement Learning

Finite Markov Decision Processes

Returns, value functions, Bellman equations, and optimal policies.

Chapter 3 formalizes reinforcement learning with finite Markov decision processes, returns, value functions, and Bellman equations.

Exercise 3.1

Example 1: Precision Farming

State: Current soil moisture level, weather forecast, crop growth stage, and water availability.

Action: Irrigate, fertilize, spray, or do nothing.

Reward: Increase in crop yield.

Example 2: Stock Portfolio Management

State: Current stock price, market sentiment, and available capital.

Action: Buy stock, sell stock, hold, reallocate into bonds.

Reward: Increase in portfolio value.

Example 3: Basketball Game Strategy

State: Current score difference, time left in game, player stamina levels, foul count.

Action: Attempt 2-point shot, attempt 3-point shot, pass, dribble, or defend.

Reward: Increase in score.

Exercise 3.2

  • Partial Observability: In many real world tasks, it is not possible to know the exact state of the environment. For example, in autonomous driving, the agent cannot observe the intentions of other drivers or hidden obstacles. MDPs assume full observability, which is often unrealistic.

  • Markov Property Violation: MDPs assume that the future depends only on the current state (Markov property). However, in many real world tasks, the future depends on the entire history of states and actions. For instance, in medical diagnosis, a patient's current symptoms alone may not be sufficient - the sequence of how symptoms developed over time is crucial for proper treatment decisions.

  • Non-Stationarity: MDPs assume that the environment is stationary, meaning the transition probabilities and reward function remain constant over time. However, in many real world tasks, the environment is non-stationary. For example, in financial markets, the underlying dynamics change due to economic conditions, regulatory changes, or market sentiment shifts.

Exercise 3.3

If you go too detailed, the problem becomes too complex.

If you go too high-level, you lose important details, necessary for the agent to make decisions.

Therefore, a good choice is where the agent's actions clearly affect the outcomes and the problem is not too complicated to learn from.

In short: It’s not a free choice — it’s about choosing the level that makes the learning and decision-making practical.

Exercise 3.4

Transition Probabilities p(s',r | s,a):

Current State (s)Action (a)Next State (s')Reward (r)Probability
highsearchhighrsearchr_{search}α
highsearchlowrsearchr_{search}1-α
lowsearchlowrsearchr_{search}β
lowsearchhigh-31-β
highwaithighrwaitr_{wait}1
lowwaitlowrwaitr_{wait}1
lowrechargehigh01

Exercise 3.5

For the continuing case, the equation 3.3 will be modified to:

sS+rRp(s,rs,a)=1,sS,  aA(s),\sum_{s' \in \mathcal{S}^{+}} \sum_{r \in \mathcal{R}} p(s', r \mid s, a) = 1, \qquad \forall s \in \mathcal{S}, \; a \in \mathcal{A}(s),

where, S+=S{terminal}\mathcal{S}^{+} = \mathcal{S} \cup \{\text{terminal}\}

Exercise 3.6

In the episodic case, if failure occurs after KK steps from time tt, then the return would be

Gt=γK1G_t = -\gamma^{K-1}

However, in the continuing case, the return includes the discounted future rewards for all the future failures as well, not just the immediate failure.

Then if K1K_1 is the first failure from time tt, K2K_2 is the second failure from time tt, and so on, the return would be

Gt=γK11γK21...γKn1G_t = -\gamma^{K_1-1} - \gamma^{K_2-1} - ... - \gamma^{K_n-1}

Exercise 3.7

Since we are not discounting the reward, the agent has no incentive to take the shortest route to the goal. Whether it follows the optimal path or wanders randomly before eventually reaching the goal, it still receives the same reward of +1.

To address this, we should introduce reward discounting, so that rewards received sooner are valued more highly. This will encourage the agent to reach the goal as quickly as possible.

Exercise 3.8

G5=0G_5 = 0 G4=R5+γG5=2+0.5(0)=2G_4 = R_5 + \gamma G_5 = 2 + 0.5(0) = 2 G3=R4+γG4=3+0.5(2)=4G_3 = R_4 + \gamma G_4 = 3 + 0.5(2) = 4 G2=R3+γG3=6+0.5(4)=8G_2 = R_3 + \gamma G_3 = 6 + 0.5(4) = 8 G1=R2+γG2=2+0.5(8)=6G_1 = R_2 + \gamma G_2 = 2 + 0.5(8) = 6 G0=R1+γG1=1+0.5(6)=2G_0 = R_1 + \gamma G_1 = -1 + 0.5(6) = 2

Exercise 3.9

G1=R2+γR3+γ2R4+...+γnRn+1G_1 = R_2 + \gamma R_3 + \gamma^2 R_4 + ... + \gamma^n R_{n+1} =7+0.9(7)+0.92(7)+...+0.9n(7)= 7 + 0.9(7) + 0.9^2(7) + ... + 0.9^n(7)

This is a geometric series, so we can use the formula for the sum of a geometric series to get:

G1=a1γ=710.9=70G_1 = \frac{a}{1 - \gamma} = \frac{7}{1 - 0.9} = 70

Now,

G0=R1+γG1=2+0.9(70)=65G_0 = R_1 + \gamma G_1 = 2 + 0.9(70) = 65

Exercise 3.10

Let the sum of the geometric series be SS.

S=1+γ+γ2+...S = 1 + \gamma + \gamma^2 + ...

Multiplying both sides by γ\gamma, we get:

γS=γ+γ2+γ3+...\gamma S = \gamma + \gamma^2 + \gamma^3 + ...

Subtracting the first equation from the second, we get:

SγS=(1+γ+γ2+...)(γ+γ2+γ3+...)S - \gamma S = (1 + \gamma + \gamma^2 + ...) - (\gamma + \gamma^2 + \gamma^3 + ...)

On the right-hand side, all terms cancel except the first 1, leaving us with:

S(1γ)=1S(1 - \gamma) = 1 S=11γS = \frac{1}{1 - \gamma}

Exercise 3.11

Eπ ⁣[Rt+1St=s]=aπ(as)s,rrp(s,rs,a)\mathbb{E}_\pi\!\left[ R_{t+1} \mid S_t = s \right] = \sum_{a} \pi(a \mid s) \sum_{s',\,r} r \cdot p(s', r \mid s, a)

Exercise 3.12

vπ(s)=aπ(as)qπ(s,a)v_\pi(s) = \sum_{a} \pi(a \mid s) \, q_\pi(s,a)

Exercise 3.13

qπ(s,a)=s,rp(s,rs,a)[r+γvπ(s)]q_\pi(s,a) = \sum_{s',\,r} p(s', r \mid s,a) \left[ r + \gamma \, v_\pi(s') \right]

Exercise 3.14

vπ(s)=aπ(as)s,rp(s,rs,a)[r+γvπ(s)]v_\pi(s) = \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a) \left[ r + \gamma v_\pi(s') \right]

Since in our case, the transition probabilities are deterministic, we can simplify the equation to:

vπ(s)=aπ(as)[r+γvπ(s)]v_\pi(s) = \sum_a \pi(a \mid s) \left[ r + \gamma v_\pi(s') \right] =a14(0+γvπ(s))= \sum_{a} \tfrac{1}{4} \Big( 0 + \gamma \, v_\pi(s') \Big) =0.914(2.3+0.40.4+0.7)=0.675= 0.9 \cdot \frac{1}{4} \cdot (2.3 + 0.4 - 0.4 + 0.7) = 0.675

Approximating it to the nearest tenth, we get 0.7\approx 0.7, and hence this equation holds

Exercise 3.15

vπ(s)=Eπ ⁣[t=0γtRt+1]v_\pi(s) = \mathbb{E}_\pi \!\left[ \sum_{t=0}^\infty \gamma^t R_{t+1} \right]

Adding a constant cc to each reward, we get:

vπ(s)=Eπ ⁣[t=0γt(Rt+1+c)]v'_\pi(s) = \mathbb{E}_\pi \!\left[ \sum_{t=0}^\infty \gamma^t (R_{t+1} + c) \right] =Eπ ⁣[t=0γt(Rt+1)]+ct=0γt= \mathbb{E}_\pi \!\left[ \sum_{t=0}^\infty \gamma^t (R_{t+1}) \right] + c \sum_{t=0}^\infty \gamma^t =vπ(s)+c1γ= v_\pi(s) + \frac{c}{1 - \gamma}

Therefore, adding a constant cc to all rewards shifts every state value by the same amount, which does not change the relative ordering of the states. Thus, the signs of the rewards are not important, only the intervals between them are important.

vc=c1γv_c = \frac{c}{1 - \gamma}

Exercise 3.16

In an episodic task, adding a constant cc to all rewards does not shift the values of the states by the same constant. Instead, the shift is dependent on the length of the episode.

vπ(s)=vπ(s)+ct=0T1γtv'_\pi(s) = v_\pi(s) + c \sum_{t=0}^{T-1} \gamma^t =vπ(s)+c1γT1γ= v_\pi(s) + c \frac{1 - \gamma^T}{1 - \gamma}

Now, we can no longer conclude that the signs of the rewards are not important.

Example: Maze with reward 1-1 per step and 00 at the goal prefers the shorter path. However, adding a constant c=1c = 1 to all rewards, we now have a reward of 00 per step and +1+1 at the goal. Now, any agent that reaches the goal will get the same reward and we no longer encourage the agent to take the shorter path.

Exercise 3.17

qπ(s,a)=Eπ ⁣[Rt+1+γGt+1St=s,At=a]q_\pi(s,a) = \mathbb{E}_\pi\!\left[\,R_{t+1} + \gamma G_{t+1} \mid S_t=s, A_t=a\right] =s,rp(s,rs,a)[r+γvπ(s)]= \sum_{s',r} p(s',r \mid s,a) \Big[\, r + \gamma v_\pi(s') \Big] =s,rp(s,rs,a)[r+γaπ(as)qπ(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]

Exercise 3.18

We can write the equation in the following two forms:

  1. Expectation form:
vπ(s)=Eaπ(s) ⁣[qπ(s,a)]v_\pi(s) = \mathbb{E}_{a \sim \pi(\cdot \mid s)} \! \left[ q_\pi(s,a) \right]
  1. Expanded form:
vπ(s)=aAπ(as)qπ(s,a)v_\pi(s) = \sum_{a \in \mathcal{A}} \pi(a \mid s) \, q_\pi(s,a)

Exercise 3.19

We can write the equation in the following two forms:

  1. Expectation form:
qπ(s,a)=Eπ ⁣[Rt+1+γvπ(St+1)St=s,At=a]q_\pi(s,a) = \mathbb{E}_\pi\!\left[\,R_{t+1} + \gamma v_\pi(S_{t+1}) \mid S_t=s, A_t=a\,\right]
  1. Expanded form:
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]

Exercise 3.20

If the ball is on the green, we can use the putter and reach the hole in 11 stroke.

v(s)=1v^*(s) = -1

If it is a little further away from the green, we can use a driver to land on the green followed by a putter to reach the hole in a total of 22 strokes.

v(s)=2v^*(s) = -2

If it is even further away from the green, we can use the driver twice to land on the green followed by a putter to reach the hole in a total of 33 strokes.

v(s)=3v^*(s) = -3

Exercise 3.21

The first action should be to use a putter, followed by the optimal action from the new state. This can be written mathematically as:

q(s,putter)=E[Rt+1+γv(St+1)St=s,At=putter]q^*(s,putter) = \mathbb{E}[R_{t+1} + \gamma v^*(S_{t+1}) \mid S_t=s, A_t=putter]

If the ball is on the green, then our first action lands us in the hole.

q(s,putter)=1q^*(s,putter) = -1

If the ball is a little further away from the green, then our first action lands us on the green, followed by a putter to reach the hole in a total of 22 strokes.

q(s,putter)=2q^*(s,putter) = -2

If the ball is further away from the green, then our first action moves us closer to the green. This is followed by a driver to land us on the green, followed by a putter to reach the hole in a total of 33 strokes.

q(s,putter)=3q^*(s,putter) = -3

If the ball is even further away from the green, then our first action moves us closer to the green. This is followed by a driver used twice to land us on the green, followed by a putter to reach the hole in a total of 44 strokes.

q(s,putter)=4q^*(s,putter) = -4

If the ball is in the sand, then our first action keeps us in the sand. This is followed by using a driver and landing on the green, and then using a putter to reach the hole. A total of 33 strokes.

q(s,putter)=3q^*(s,putter) = -3

Exercise 3.22

If γ=0\gamma = 0 then only the immediate reward is considered. The reward for taking the left action is +1+1, while the reward for taking the right action is 00. So the optimal policy is to take the left action.

For the other two cases, following πleft\pi_{left}, our value function will be:

Vleft(s)=1+γ2+γ4+...=11γ2V_{left}(s) = 1 + \gamma^2 + \gamma^4 + ... = \frac{1}{1 - \gamma^2}

And following πright\pi_{right}, our value function will be:

Vright(s)=2γ+2γ3+2γ5+...=2γ1γ2V_{right}(s) = 2\gamma + 2\gamma^3 + 2\gamma^5 + ... = \frac{2\gamma}{1 - \gamma^2}

For γ=0.9\gamma = 0.9,

Vleft(s)=110.92=5.26V_{left}(s) = \frac{1}{1 - 0.9^2} = 5.26 Vright(s)=20.910.92=9.47V_{right}(s) = \frac{2\cdot 0.9}{1 - 0.9^2} = 9.47

Therefore, following πright\pi_{right} is optimal.

For γ=0.5\gamma = 0.5,

Vleft(s)=110.52=1.33V_{left}(s) = \frac{1}{1 - 0.5^2} = 1.33 Vright(s)=20.510.52=1.33V_{right}(s) = \frac{2\cdot 0.5}{1 - 0.5^2} = 1.33

Therefore, we can follow either action as both are optimal.

Exercise 3.23

Since

q(s,a)=sp(ss,a)[r(s,a,s)+γmaxaq(s,a)]q^{*}(s, a) = \sum_{s'} p(s' \mid s,a) \Big[\, r(s, a, s') + \gamma \, \max_{a'} q^{*}(s', a') \Big]

Therefore,

q(high,search)=α[rsearch+γmaxaq(high,a)]+(1α)[rsearch+γmaxaq(low,a)]q^*(\text{high}, \text{search}) = \alpha \left[ r_{\text{search}} + \gamma \max_{a'} q^*(\text{high}, a') \right] + (1-\alpha) \left[ r_{\text{search}} + \gamma \max_{a'} q^*(\text{low}, a') \right] q(low,search)=β[rsearch+γmaxaq(high,a)]+(1β)[3+γmaxaq(low,a)]q^*(\text{low}, \text{search}) = \beta \left[ r_{\text{search}} + \gamma \max_{a'} q^*(\text{high}, a') \right] + (1-\beta) \left[ -3 + \gamma \max_{a'} q^*(\text{low}, a') \right] q(high,wait)=rwait+γmaxaq(high,a)q^*(\text{high}, \text{wait}) = r_{\text{wait}} + \gamma \max_{a'} q^*(\text{high}, a') q(low,wait)=rwait+γmaxaq(low,a)q^*(\text{low}, \text{wait}) = r_{\text{wait}} + \gamma \max_{a'} q^*(\text{low}, a') q(low,recharge)=γmaxaq(high,a)q^*(\text{low}, \text{recharge}) = \gamma \max_{a'} q^*(\text{high}, a')

Exercise 3.24

v(s)=E[r+γv(s)]v^*(s) = \mathbb{E}[r + \gamma v^*(s')]

Since we always move to AA' no matter what action we take.

v(s)=10+0.9(16)=24.400v^*(s) = 10 + 0.9(16) = 24.400

Exercise 3.25

v(s)=maxaq(s,a)v^*(s) = \max_a q^*(s,a)

Exercise 3.26

q(s,a)=s,rp(s,rs,a)[r+γv(s)]q^*(s,a) = \sum_{s',r} p(s',r \mid s,a) \Big[\, r + \gamma v^*(s') \Big]

Exercise 3.27

π(s)=argmaxaq(s,a)\pi^*(s) = \arg\max_a q^*(s,a)

Exercise 3.28

π(s)=argmaxas,rp(s,rs,a)[r+γv(s)]\pi^*(s) = \arg\max_a \sum_{s',r} p(s',r \mid s,a) \Big[\, r + \gamma v^*(s') \Big]

Exercise 3.29

Firstly, note that

p(ss,a)=rp(s,rs,a)p(s' \mid s,a) = \sum_{r} p(s',r \mid s,a)

and

r(s,a)=rrp(s,rs,a)r(s, a) = \sum_{r} r \cdot p(s',r \mid s,a)

For vπv_{\pi},

vπ(s)=aπ(as)s,rp(s,rs,a)[r+γvπ(s)]v_{\pi}(s) = \sum_{a} \pi(a \mid s) \sum_{s',r} p(s',r \mid s,a) \Big[\, r + \gamma v_{\pi}(s') \Big] =aπ(as)[s,rrp(s,rs,a)+γs,rp(s,rs,a)vπ(s)]= \sum_{a} \pi(a \mid s) \Big[ \sum_{s',r} r \cdot p(s',r \mid s,a) + \gamma \sum_{s',r} p(s',r \mid s,a) \cdot v_{\pi}(s') \Big] =aπ(as)[s,rrp(s,rs,a)+γs(rp(s,rs,a))vπ(s)]= \sum_{a} \pi(a \mid s) \Big[ \sum_{s',r} r \cdot p(s',r \mid s,a) + \gamma \sum_{s'} \Big(\sum_{r} p(s',r \mid s,a)\Big) \cdot v_{\pi}(s') \Big] =aπ(as)[r(s,a)+γsp(ss,a)vπ(s)]= \sum_{a} \pi(a \mid s) \Big[ r(s, a) + \gamma \sum_{s'} p(s' \mid s,a) \cdot v_{\pi}(s') \Big]

For qπq_{\pi},

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] =r(s,a)+γsp(ss,a)vπ(s)= r(s, a) + \gamma \sum_{s'} p(s' \mid s,a) \cdot v_{\pi}(s') =r(s,a)+γsp(ss,a)aπ(as)qπ(s,a)= r(s, a) + \gamma \sum_{s'} p(s' \mid s,a) \cdot \sum_{a'} \pi(a' \mid s') \cdot q_{\pi}(s',a')

For vv^*,

v(s)=maxa[r(s,a)+γsp(ss,a)v(s)]v^*(s) = \max_a \Big[r(s, a) + \gamma \sum_{s'} p(s' \mid s,a) \cdot v^*(s') \Big]

For qq^*,

q(s,a)=r(s,a)+γsp(ss,a)maxaq(s,a)q^*(s,a) = r(s, a) + \gamma \sum_{s'} p(s' \mid s,a) \cdot \max_{a'} q^*(s', a')