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 |
|---|
| high | search | high | rsearch | α |
| high | search | low | rsearch | 1-α |
| low | search | low | rsearch | β |
| low | search | high | -3 | 1-β |
| high | wait | high | rwait | 1 |
| low | wait | low | rwait | 1 |
| low | recharge | high | 0 | 1 |
Exercise 3.5
For the continuing case, the equation 3.3 will be modified to:
s′∈S+∑r∈R∑p(s′,r∣s,a)=1,∀s∈S,a∈A(s),
where, S+=S∪{terminal}
Exercise 3.6
In the episodic case, if failure occurs after K steps from time t, then the return would be
Gt=−γ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 K1 is the first failure from time t, K2 is the second failure from time t, and so on, the return would be
Gt=−γK1−1−γK2−1−...−γKn−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=0
G4=R5+γG5=2+0.5(0)=2
G3=R4+γG4=3+0.5(2)=4
G2=R3+γG3=6+0.5(4)=8
G1=R2+γG2=2+0.5(8)=6
G0=R1+γG1=−1+0.5(6)=2
Exercise 3.9
G1=R2+γR3+γ2R4+...+γnRn+1
=7+0.9(7)+0.92(7)+...+0.9n(7)
This is a geometric series, so we can use the formula for the sum of a geometric series to get:
G1=1−γa=1−0.97=70
Now,
G0=R1+γG1=2+0.9(70)=65
Exercise 3.10
Let the sum of the geometric series be S.
S=1+γ+γ2+...
Multiplying both sides by γ, we get:
γS=γ+γ2+γ3+...
Subtracting the first equation from the second, we get:
S−γS=(1+γ+γ2+...)−(γ+γ2+γ3+...)
On the right-hand side, all terms cancel except the first 1, leaving us with:
S(1−γ)=1
S=1−γ1
Exercise 3.11
Eπ[Rt+1∣St=s]=a∑π(a∣s)s′,r∑r⋅p(s′,r∣s,a)
Exercise 3.12
vπ(s)=a∑π(a∣s)qπ(s,a)
Exercise 3.13
qπ(s,a)=s′,r∑p(s′,r∣s,a)[r+γvπ(s′)]
Exercise 3.14
vπ(s)=a∑π(a∣s)s′,r∑p(s′,r∣s,a)[r+γvπ(s′)]
Since in our case, the transition probabilities are deterministic, we can simplify the equation to:
vπ(s)=a∑π(a∣s)[r+γvπ(s′)]
=a∑41(0+γvπ(s′))
=0.9⋅41⋅(2.3+0.4−0.4+0.7)=0.675
Approximating it to the nearest tenth, we get ≈0.7, and hence this equation holds
Exercise 3.15
vπ(s)=Eπ[t=0∑∞γtRt+1]
Adding a constant c to each reward, we get:
vπ′(s)=Eπ[t=0∑∞γt(Rt+1+c)]
=Eπ[t=0∑∞γt(Rt+1)]+ct=0∑∞γt
=vπ(s)+1−γc
Therefore, adding a constant c 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=1−γc
Exercise 3.16
In an episodic task, adding a constant c 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=0∑T−1γt
=vπ(s)+c1−γ1−γT
Now, we can no longer conclude that the signs of the rewards are not important.
Example: Maze with reward −1 per step and 0 at the goal prefers the shorter path. However, adding a constant c=1 to all rewards, we now have a reward of 0 per step and +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+1∣St=s,At=a]
=s′,r∑p(s′,r∣s,a)[r+γvπ(s′)]
=s′,r∑p(s′,r∣s,a)[r+γa′∑π(a′∣s′)qπ(s′,a′)]
Exercise 3.18
We can write the equation in the following two forms:
- Expectation form:
vπ(s)=Ea∼π(⋅∣s)[qπ(s,a)]
- Expanded form:
vπ(s)=a∈A∑π(a∣s)qπ(s,a)
Exercise 3.19
We can write the equation in the following two forms:
- Expectation form:
qπ(s,a)=Eπ[Rt+1+γvπ(St+1)∣St=s,At=a]
- Expanded form:
qπ(s,a)=s′,r∑p(s′,r∣s,a)[r+γvπ(s′)]
Exercise 3.20
If the ball is on the green, we can use the putter and reach the hole in 1 stroke.
v∗(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 2 strokes.
v∗(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 3 strokes.
v∗(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]
If the ball is on the green, then our first action lands us in the hole.
q∗(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 2 strokes.
q∗(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 3 strokes.
q∗(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 4 strokes.
q∗(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 3 strokes.
q∗(s,putter)=−3
Exercise 3.22
If γ=0 then only the immediate reward is considered. The reward for taking the left action is +1, while the reward for taking the right action is 0. So the optimal policy is to take the left action.
For the other two cases, following πleft, our value function will be:
Vleft(s)=1+γ2+γ4+...=1−γ21
And following πright, our value function will be:
Vright(s)=2γ+2γ3+2γ5+...=1−γ22γ
For γ=0.9,
Vleft(s)=1−0.921=5.26
Vright(s)=1−0.922⋅0.9=9.47
Therefore, following πright is optimal.
For γ=0.5,
Vleft(s)=1−0.521=1.33
Vright(s)=1−0.522⋅0.5=1.33
Therefore, we can follow either action as both are optimal.
Exercise 3.23
Since
q∗(s,a)=s′∑p(s′∣s,a)[r(s,a,s′)+γa′maxq∗(s′,a′)]
Therefore,
q∗(high,search)=α[rsearch+γa′maxq∗(high,a′)]+(1−α)[rsearch+γa′maxq∗(low,a′)]
q∗(low,search)=β[rsearch+γa′maxq∗(high,a′)]+(1−β)[−3+γa′maxq∗(low,a′)]
q∗(high,wait)=rwait+γa′maxq∗(high,a′)
q∗(low,wait)=rwait+γa′maxq∗(low,a′)
q∗(low,recharge)=γa′maxq∗(high,a′)
Exercise 3.24
v∗(s)=E[r+γv∗(s′)]
Since we always move to A′ no matter what action we take.
v∗(s)=10+0.9(16)=24.400
Exercise 3.25
v∗(s)=amaxq∗(s,a)
Exercise 3.26
q∗(s,a)=s′,r∑p(s′,r∣s,a)[r+γv∗(s′)]
Exercise 3.27
π∗(s)=argamaxq∗(s,a)
Exercise 3.28
π∗(s)=argamaxs′,r∑p(s′,r∣s,a)[r+γv∗(s′)]
Exercise 3.29
Firstly, note that
p(s′∣s,a)=r∑p(s′,r∣s,a)
and
r(s,a)=r∑r⋅p(s′,r∣s,a)
For vπ,
vπ(s)=a∑π(a∣s)s′,r∑p(s′,r∣s,a)[r+γvπ(s′)]
=a∑π(a∣s)[s′,r∑r⋅p(s′,r∣s,a)+γs′,r∑p(s′,r∣s,a)⋅vπ(s′)]
=a∑π(a∣s)[s′,r∑r⋅p(s′,r∣s,a)+γs′∑(r∑p(s′,r∣s,a))⋅vπ(s′)]
=a∑π(a∣s)[r(s,a)+γs′∑p(s′∣s,a)⋅vπ(s′)]
For qπ,
qπ(s,a)=s′,r∑p(s′,r∣s,a)[r+γvπ(s′)]
=r(s,a)+γs′∑p(s′∣s,a)⋅vπ(s′)
=r(s,a)+γs′∑p(s′∣s,a)⋅a′∑π(a′∣s′)⋅qπ(s′,a′)
For v∗,
v∗(s)=amax[r(s,a)+γs′∑p(s′∣s,a)⋅v∗(s′)]
For q∗,
q∗(s,a)=r(s,a)+γs′∑p(s′∣s,a)⋅a′maxq∗(s′,a′)