C2: Markov Decision Processes

Most materials in this chapter are based on materials fromhttps://ai.berkeley.edu
An agent deciding how to cross a grid world
C2
Markov Decision Processes
COMP3270
Introduction to Artificial Intelligence
1

Chapter 2: Roadmap

Chapter 2: Roadmap
Model Uncertainty
States, actions, transitions, and rewards
Compare Decisions
Policies, returns, and values
Compute a Policy
Value iteration and policy iteration
2

Uncertain Outcomes

Uncertain Outcomes
Robot close to a dangerous edge

C1: one action fixes the next state

C2: one action, several possible states

Outcome probabilities guide decisions

3

A Grid World with Noisy Actions

A Grid World with Noisy Actions
1 2 3 4
3
2
1 ●Agent
xy

State (x, y)

Agent at (1, 1)

x increases right; y increases up

1 2 3 4
3
2
1 ●Agent
xy

Regular states

Available actions: North, East, South, West

1 2 3 4
3
2
1 ●Agent
xy

Wall at (2, 2)

A move into the wall leaves the agent in place

1 2 3 4
3 +1Exit
2 −1Exit
1 ●Agent
xy

Exit states

Only available action: Exit

Exit pays +1 or −1, then the episode ends

1 2 3 4
3 +1Exit
2 −1Exit
1 ●Agent
xy

Movement reward

Every compass action pays −0.1-0.1

Including blocked moves and moves into exit squares

1 2 3 4
3 +1Exit
2 −1Exit
1 ●Agent
xy

Noisy movement

Intended direction: 0.8

Each sideways direction: 0.1

The agent observes its actual square after every action
4

Deterministic Grid World

Deterministic Grid World
1 2 3 4
3 +1Exit
2 -1Exit
1 ●Agent
xy

Choose a move

Last reward: 0.0

Total reward: 0.0

5

Stochastic Grid World

Stochastic Grid World
1 2 3 4
3 +1Exit
2 -1Exit
1 ●Agent
xy

Click an available action

Intended direction: 0.8

Each sideways direction: 0.1

6

Task: North, Then North

North, Then North
TASK
1234
321
xy
+1Exit
−1Exit

Start at (1, 1)

Choose North twice

What is the probability of
ending at (1, 2)?

7

The Transition Model and the Markov Property

The Transition Model
and the Markov Property
1234
321
xy
+1Exit
0.8
−1Exit
0.1
0.1
A: (1, 1) → (2, 1) → (3, 1)
B: (3, 3) → (3, 2) → (3, 1)

Same state: (3, 1)
Same action: North

Different histories
Same next-state probabilities

T(s,a,s′)=P(s′∣s,a)T(s,a,s^{\prime})=P(s^{\prime}\mid s,a)

Current state, action, next state

The current state and action suffice
Earlier history adds nothing

P(st+1∣st,at,st−1,at−1,…)=P(st+1∣st,at)P(s_{t+1}\mid s_t,a_t,s_{t-1},a_{t-1},\ldots)=P(s_{t+1}\mid s_t,a_t)
8

Defining a Markov Decision Process

Defining a Markov Decision Process
s∈Ss\in S
The possible states
a∈A(s)a\in A(s)
The actions available in state ss
T(s,a,s′)T(s,a,s^{\prime})
The probability of each next state
R(s,a,s′)R(s,a,s^{\prime})
The reward for that transition
Objective
Maximise expected cumulative reward

Also specify the initial state or distribution and any terminal states

9

A Fixed Plan Cannot Respond to a Slip

A Fixed Plan Cannot Respond to a Slip
Plan1. North2. North3. East…
+1Exit−1Exit1234321xyAgent

Start at (1, 1)

Follow a fixed list of actions

First action: North

We intend to move up

Dashed arrow: intended direction

Chosen action: North

Actual move: East

The agent lands at (2, 1)

A sideways slip, probability 0.1

Next on the list: North

North now points into the wall

This move leaves the agent in place

The plan still says North

But we are in a different square

We need an action for
where we are now

10

An MDP Solution Is a Policy

An MDP Solution Is a Policy
Original comic: a robot holds a map with one action arrow for each square
1 2 3 4
3 → → → +1Exit
2 ↑ ↑ −1Exit
1 ↑ → ↑ ←
xy

A policy π\pi determines
what to do for each state

After the slip, we are at (2, 1)

π(2,1)=East\pi(2,1)=\mathrm{East}

Observe the state, then use its action

11

Which Policy Is Better?

Which Policy Is Better?

Policy A

1 2 3 4
3 → → → +1Exit
2 ↑ ↑ −1Exit
1 ↑ → ↑ ←
xy

Policy B

1 2 3 4
3 → → → +1Exit
2 ↑ ↑ −1Exit
1 → → ↑ ←
xy
Start: (1, 1). Move: −0.1. Exit: +1 or −1.
Intended direction: 0.8; each sideways direction: 0.1
TASK
12

Living Rewards and the Optimal Policy

Living Rewards and the Optimal Policy
1 2 3 4
3 → → → +1Exit
2 ↑ ← −1Exit
1 ↑ ← ← ↓
xy

Living reward −.01-.01

Reward received on every move

Intended direction: 0.8
Each sideways direction: 0.1
Exit rewards: +1 and −1

13

Racing Car

Racing Car
A robot racing car

The car wants to travel quickly

Actions: Slow or Fast

Temperature changes what happens next

14

Racing-Car State Diagram

Racing-Car State Diagram
CoolWarmOverheatedSlowT = 1R = +1FastT = 0.5R = +2T = 0.5R = +2T = 0.5R = +1T = 0.5R = +1T = 1R = −10
T(s,a,s′)T(s,a,s') : transition probability · R(s,a,s′)R(s,a,s') : reward
15

What Is the Optimal Action Here?

What Is the Optimal Action Here?
CoolSlowFastT = 1T = 0.5T = 0.5CoolCoolWarmSlowFastSlowFastSlowFastT = 1T = 0.5T = 0.5Cool…Cool…Warm…T = 1T = 0.5T = 0.5Cool…Cool…Warm…T = 0.5T = 0.5T = 1Cool…Warm…Overheated−10
Slow: reward +1 · Fast: reward +2; overheating: −10 · T: transition probability
16

Choose an Action, Average the Outcomes

Choose an Action, Average the Outcomes
Cool SlowFast T = 1T = 0.5T = 0.5 CoolCoolWarm ………

At a state: choose

Take the action with the
highest expected return

After an action: average

Weight each possible
outcome by its probability

Fast: equal weights, 0.5 and 0.5

T(s,a,s′)T(s,a,s') is the transition probability
Choose the best action; average over chance outcomes
17

Q-State

Q-State
The agent has committed to the action but has not done it yet
Cool a = Slowa = Fast T(s, a, s′)= 1T(s, a, s′)= 0.5T(s, a, s′)= 0.5 CoolCoolWarm s′…s′…s′…(s, Slow)(s, Fast)s

ss is the current state
aa is the chosen action

(s,a)(s,a) is a Q-state

Original robot comic: the robot has chosen a move but its outcome is still uncertain

s′s' is the next state

T(s,a,s′)T(s,a,s')

Probability of reaching s′s' after taking aa in ss

18

A Corridor MDP

A Corridor MDP

T(s,a,s′)T(s,a,s'): probability of next state s′s' after action aa in current state ss

+10 Exita
 b
 c
 d
+1 Exite
East and West
0.8: move to the neighbour
Failed Movement
0.2: stay in the same square
Movement Reward
0 for every move
Exit at a or e
+10 or +1, then the episode ends
19

Where Can We Land?

Where Can We Land?

Choose East from c

abcde+10 Exit+1 Exit
Transition Probability
T(c,East,d)T(c,\mathrm{East},d)
T(c,East,c)T(c,\mathrm{East},c)
Any other state
TASK
20

Probability and Reward

Probability and Reward
+10 Exita
 b
 c
 d
+1 Exite

At a, compare East with Exit

State ss Action aa Next State s′s' Probability
T(s,a,s′)T(s,a,s')
Reward
R(s,a,s′)R(s,a,s')
a East
a East
a Exit
TASK
21

From d, West or East?

From d, West or East?
+10 Exita
 b
 c
 d
+1 Exite

West toward +10

Three successful moves, then Exit

East toward +1

One successful move, then Exit

A larger reward later, or a smaller reward sooner?

How much should waiting reduce a reward’s value?

Same model: 0.8 move, 0.2 stay · Movement reward 0
22

Comparing Reward Sequences

Comparing Reward Sequences
Original comic: a robot compares jewels along two possible routes

More reward?

[1,2,2]or[2,3,4][1,2,2]\quad\text{or}\quad[2,3,4]

Earlier reward?

[0,0,1]or[1,0,0][0,0,1]\quad\text{or}\quad[1,0,0]

How should we compare whole reward sequences?

23

Discounting

Discounting

Prefer rewards now to the same rewards later

Original gem comic: Now
11

Now

Original gem comic: One step later
γ\gamma

One step later

Original gem comic: Two steps later
γ2\gamma^2

Two steps later

Each extra step multiplies the reward’s value by γ\gamma

0<γ<10<\gamma<1
24

Which State Sequence Is Better?

Which State Sequence Is Better?
As₀R₁ = +1s₁R₂ = +2s₂R₃ = +3s₃EndBs₀R₁ = +3s₁R₂ = +2s₂R₃ = +1s₃End

Return from the start: G0=R1+γR2+γ2R3G_0=R_1+\gamma R_2+\gamma^2R_3

Without Discounting (γ=1\gamma=1) With γ=0.5\gamma=0.5
A
B
TASK
Calculate both returns for γ = 1 and γ = 0.5
25

Discounted Return

Discounted Return
Example
11
22
33
Reward
Rt+1R_{t+1}
Rt+2R_{t+2}
Rt+3R_{t+3}
Weight
11
γ\gamma
γ2\gamma^2
Gt=Rt+1+γRt+2+γ2Rt+3+⋯G_t=R_{t+1}+\gamma R_{t+2}+\gamma^2R_{t+3}+\cdots

A smaller γ\gamma gives less weight to later rewards

0≤γ≤10\leq\gamma\leq1 for finite reward sequences
26

Discounting and the Preferred Route

Discounting and the Preferred Route
+10 Exita
 b
 c
 d
+1 Exite

Start at d · Movement reward 0 · 0.8 move, 0.2 stay

Discount West to +10 East to +1 Preferred
0.1 0.005 0.082 East
0.9 6.769 0.878 West

Expected discounted returns include failed-move delays and a separate Exit action

27

Infinite Reward Sequences

Infinite Reward Sequences

At γ=0.9\gamma=0.9: Fast when Cool, Slow when Warm

CoolWarmOverheatedSlowT = 1R = +1FastT = 0.5R = +2T = 0.5R = +2T = 0.5R = +1T = 0.5R = +1T = 1R = −10
A racing car driving around an infinity-shaped track

The optimal policy keeps driving without overheating

Slow earns +1; Fast from Cool earns +2

The agent earns +1 on every move, forever

No discount

1+1+1+⋯→∞1+1+1+\cdots\to\infty

With γ=0.5\gamma=0.5

1+12+14+⋯=21+\frac12+\frac14+\cdots=2

An endless reward sequence can have a finite discounted return

Every reward has magnitude at most RmaxR_{\max}; 0≤γ<10\leq\gamma<1

|Gt|≤∑k=0∞γk|Rt+k+1|≤Rmax(1+γ+γ2+⋯)=Rmax1−γ|G_t|\leq\sum_{k=0}^{\infty}\gamma^k|R_{t+k+1}|\leq R_{\max}(1+\gamma+\gamma^2+\cdots)=\frac{R_{\max}}{1-\gamma}
28

State Values and Action Values

State Values and Action Values
Cool SlowFast T = 1T = 0.5T = 0.5 CoolCoolWarm ………

V*(s)V^*(s)

Best expected return
starting in ss

Q*(s,a)Q^*(s,a)

Take aa in ss, then act optimally

Includes the reward from the first action

V*(s)=max⁡aQ*(s,a)V^*(s)=\max_a Q^*(s,a)
ss is the current state; aa is the first action
29

A Backup Combines Reward and Future Value

A Backup Combines Reward and Future Value
SlowT = 1Cool Cool Fast T = 0.5R = +2T = 0.5R = +2CoolWarmV* = 15.5V* = 14.5

Choose Fast at Cool

Reward from This Action

22

Average Future Value

0.5(15.5)+0.5(14.5)=150.5(15.5)+0.5(14.5)=15
2+0.9(15)=15.52+0.9(15)=15.5
Q*(Cool,Fast)=0.5[2+0.9V*(Cool)]+0.5[2+0.9V*(Warm)]Q^*(\mathrm{Cool},\mathrm{Fast})=0.5[2+0.9V^*(\mathrm{Cool})]+0.5[2+0.9V^*(\mathrm{Warm})]
Discount 0.9 · Supplied optimal continuation values
30

The Bellman Optimality Equations

The Bellman Optimality Equations
Q*(s,a)=∑s′T(s,a,s′)[R(s,a,s′)+γV*(s′)]Q^*(s,a)=\sum_{s^{\prime}}T(s,a,s^{\prime})\bigl[R(s,a,s^{\prime})+\gamma V^*(s^{\prime})\bigr]
V*(s)=max⁡aQ*(s,a)V^*(s)=\max_a Q^*(s,a)
π*(s)∈arg max⁡aQ*(s,a)\pi^*(s)\in\operatorname*{arg\,max}_a Q^*(s,a)
Average over outcomes, then maximise over actions
31

Bellman in Gridworld

Bellman in Gridworld
1 2 3 4
3 0.31 0.51 0.72 +11.00
2 0.15 0.36 -1-1.00
1 0.01 0.01 0.15 -0.09
xy

Choose East at (3, 3)

Next State Probability Value
(4, 3) 0.80 1.00
(3, 3) 0.10 0.72
(3, 2) 0.10 0.36
Q*((3,3),East)≈0.72Q^*((3,3),\mathrm{East})\approx 0.72

Each term: probability × (transition reward + discounted next-state value)

Movement reward −0.1 · Discount 0.9 · Initial Classic configuration
32

Value Iteration

Value Iteration

Vk(s)V_k(s) is the best expected return with kk actions remaining

V0(s)=0V_0(s)=0
Vk+1(s)=max⁡a∈A(s)∑s′T(s,a,s′)[R(s,a,s′)+γVk(s′)]V_{k+1}(s)=\max_{a\in A(s)}\sum_{s^{\prime}}T(s,a,s^{\prime})[R(s,a,s^{\prime})+\gamma V_k(s^{\prime})]

One action now, then kk actions remain; Exit also counts as an action

33

Value Iteration in Gridworld

Value Iteration in Gridworld
Vk+1(s)=max⁡a∑s′T(s,a,s′)[R(s,a,s′)+γVk(s′)]V_{k+1}(s)=\max_a\sum_{s^{\prime}}T(s,a,s^{\prime})[R(s,a,s^{\prime})+\gamma V_k(s^{\prime})]
1 2 3 4
3 0.00 0.00 0.72 +11.00
2 0.00 0.00 -1-1.00
1 0.00 0.00 0.00 0.00
xy
V0(s)=0V_0(s)=0

One action: Exit gives +1 or −1

Two actions: move, then Exit

V2((3,3))=0.8[0+0.9(1)]+0.2[0+0.9(0)]=0.72V_2((3,3))=0.8[0+0.9(1)]+0.2[0+0.9(0)]=0.72

Every update reads the previous sweep

Movement reward 0.00 · Discount 0.90 · Two-action snapshot
34

Q-Value Iteration

Q-Value Iteration

Qk(s,a)Q_k(s,a) is the best expected return with kk actions remaining

Take action aa first, then act optimally

Q0(s,a)=0Q_0(s,a)=0
Qk+1(s,a)=∑s′T(s,a,s′)[R(s,a,s′)+γmax⁡a′Qk(s′,a′)]Q_{k+1}(s,a)=\sum_{s^{\prime}}T(s,a,s^{\prime})[R(s,a,s^{\prime})+\gamma\max_{a^{\prime}}Q_k(s^{\prime},a^{\prime})]
Vk(s)=max⁡aQk(s,a)V_k(s)=\max_a Q_k(s,a)
35

Q-Value Iteration in Gridworld

Q-Value Iteration in Gridworld
1 2 3 4
3 0.00 0.00 0.72 +11.00
2 0.00 0.00 -1-1.00
1 0.00 0.00 0.00 0.00
xy

Two actions remaining at (3, 3)

First Action Q2Q_2
North 0.09
East 0.72
South 0.09
West 0.00
V2((3,3))=max⁡aQ2((3,3),a)=0.72V_2((3,3))=\max_a Q_2((3,3),a)=0.72

Each row keeps one action value; the maximum gives the state value

Movement reward 0.00 · Discount 0.90 · Two-action snapshot
36

Convergence

Convergence
Vk→V*,Qk→Q*V_k\longrightarrow V^*,\qquad Q_k\longrightarrow Q^*

Stop when the largest change is below a tolerance

max⁡s|Vk+1(s)−Vk(s)|<ε\max_s|V_{k+1}(s)-V_k(s)|<\varepsilon
max⁡s,a|Qk+1(s,a)−Qk(s,a)|<ε\max_{s,a}|Q_{k+1}(s,a)-Q_k(s,a)|<\varepsilon
Finite states and actions, bounded rewards, 0≤γ<10\leq\gamma<1
37

Quiz: Value Iteration

Quiz: Value Iteration
Vk+1(s)=max⁡a∑s′T(s,a,s′)[R(s,a,s′)+γVk(s′)]V_{k+1}(s)=\max_a\sum_{s^{\prime}}T(s,a,s^{\prime})[R(s,a,s^{\prime})+\gamma V_k(s^{\prime})]
CoolWarmOverheated
T=1,R=+1T=1,\ R=+1
T=0.5,R=+2T=0.5,\ R=+2
T=0.5,R=+2T=0.5,\ R=+2
T=0.5,R=+1T=0.5,\ R=+1
T=0.5,R=+1T=0.5,\ R=+1
T=1,R=−10T=1,\ R=-10
SlowFast
Round Cool Warm Overheated
V0V_0 00 00 00
V1V_1
V2V_2
γ=1\gamma=1 · Find V1V_1 and V2V_2
TASK
38

Evaluate a Fixed Policy

Evaluate a Fixed Policy

Given this policy, how do we determine its values?

1 2 3 4
3 →0.31 →0.39 ↑0.45 Exit1.00
2 →0.00 →-0.72 Exit-1.00
1 →-0.31 →-0.39 →-0.45 →-0.47
xy
Vk+1π(s)=∑s′T(s,π(s),s′)[R(s,π(s),s′)+γVkπ(s′)]V^\pi_{k+1}(s)=\sum_{s^{\prime}}T(s,\pi(s),s^{\prime})[R(s,\pi(s),s^{\prime})+\gamma V^\pi_k(s^{\prime})]
Movement reward 0.00 · Discount 0.90 · 80% intended direction · Exit rewards +1 / −1
39

Choose Actions from Values

Choose Actions from Values

Given these optimal values V*V^*, which actions should we choose?

1 2 3 4
3 →0.64 →0.74 →0.85 Exit1.00
2 ↑0.57 ↑0.57 Exit-1.00
1 ↑0.49 ←0.43 ↑0.48 ←0.28
xy
π*(s)∈arg max⁡a∑s′T(s,a,s′)[R(s,a,s′)+γV*(s′)]\pi^*(s)\in\operatorname*{arg\,max}_{a}\sum_{s^{\prime}}T(s,a,s^{\prime})[R(s,a,s^{\prime})+\gamma V^*(s^{\prime})]
Movement reward 0.00 · Discount 0.90 · 80% intended direction · Exit rewards +1 / −1
40

Choose a Machine

Choose a Machine

Which machine would you choose for the next 100 plays?

Blue slot machine

Blue: $1 every play

Red slot machine

Red: $2 with probability 0.75

$0 with probability 0.25

41

The Machines as an MDP

The Machines as an MDP
WLBlue: T = 1, R = +1Blue: T = 1, R = +1T = 0.75, R = +2T = 0.75, R = +2T = 0.25, R = 0T = 0.25, R = 0

Always Blue or always Red, starting from either state

V100πBlue(s)=100(1)=100V^{\pi_{\mathrm{Blue}}}_{100}(s)=100(1)=100
V100πRed(s)=100[0.75(2)+0.25(0)]=150V^{\pi_{\mathrm{Red}}}_{100}(s)=100[0.75(2)+0.25(0)]=150

V100*(W)=V100*(L)=150V^*_{100}(W)=V^*_{100}(L)=150 → Choose Red

100 plays remain · γ=1\gamma=1 · Compare two fixed policies
42

What If the Transition Probabilities Are Unknown?

What If the Transition Probabilities Are Unknown?

We can no longer solve this MDP
from the given information alone

We must act, observe rewards,
and learn the missing probabilities

In reinforcement learning, transitions, rewards, or both may be unknown
43

Learn by Playing

Learn by Playing

Choose a machine on each of 10 turns

Blue machine

Blue: $1 every play

New Red machine

Red: $2 or $0, odds unknown

Illustrative 10-play record

Machine Observed rewards Sample average
Blue 1, 1, 1, 1 $1.00
Red 0, 0, 0, 0, 0, 2 $0.33

Each action earns a reward and may teach us something

44

Main Ideas in Reinforcement Learning

Main Ideas in Reinforcement Learning

We needed to act to learn about the machines

Idea What It Means
Exploration Try uncertain actions to gain information
Exploitation Use the best action according to current estimates
Regret Expected reward lost compared with choosing the best action
Sampling Repeat observations because outcomes vary by chance

Learning can be harder than solving a known MDP

45