Policy Improvement

INC 637 Artificial
Intelligence
Lecture 13
Reinforcement Learning (RL)
(continue)
Reinforcement Learning
Value Table
Action
State
3
2
 
 3
 
2
4
 
 22 
3
 
 4 
State Value Table
1 dimension
(V-table)
2
4
3
 7 66 9

 4 43  7

State  9 45 3
6
4
6

7
8
5
 :
:
:

6
4
 7
7 6
7 4
5 3
8 6
7 0
9 5
:
:
8 9
6
3  9 7
11 54 6

4
8 5
8 56 4

46 8 5
:
: :

 6 6 4
4
3
Action Value Table
2 dimension
(Q-table)
Bellman Equation for a Policy p
คำนวณหำ Value Function จำก policy π
Rt  rt 1   rt 2   2 rt  3   3 rt  4
 rt 1   rt 2   rt 3   2 rt  4

 rt 1   Rt 1
So:
V (s)  Ep Rt st  s
p
 Ep rt 1   V st 1  st  s
Or, without the expectation operator:
Vp (s)   p (s,a) PsasRsas   V p ( s)
a
s
Bellman Optimality Equation for V*
The value of a state under an optimal policy must equal
the expected return for the best action from that state:
p

V (s)  max Q (s,a)
aA(s)
 max Ert 1   V (st 1 ) s t  s, at  a

aA(s)
 max
aA(s)
a
a

P
R


V
(s)
 ss ss
s 
3 Methods to find V*
• Dynamic Programming
• Monte Carlo
• Temporal Difference
Dynamic Programming
ประกอบด้ วยขบวนกำรหลัก 2 ส่ วน
Policy Evaluation
Policy Improvement
Policy Evaluation
p
Bellman equation for V :
V (s)   p (s,a) PssRss   V ( s)
p
a
a
a
p
s 
— a system of S simultaneous linear equations
แต่ ละ state มีสมกำรของตัวเองซึ่งขื้นกับ value function ของ next state
เป็ นสมกำร linear หลำยตัวแปร
อย่ ำงไรก็ตำม ก็ยงั ยำกทีจ่ ะแก้ สมกำรถ้ ำมีหลำยๆ state
Iterative Policy Evaluation
V0  V1 
 Vk  Vk1 
V
p
a “sweep”
A sweep consists of applying a backup operation to each state.
ตือใช้ value function จำก อันก่ อนมำประมำณอันปัจจุบนั
A full policy evaluation backup:
Vk 1 (s)   p (s,a) PsasRsas   Vk (s)
a
s
Example
Terminal state
Terminal state
• Undiscounted episodic task
• Actions that would take agent off the grid
leave state unchanged
• Reward is –1 for every action until the
terminal state is reached
(0.25(-1+0.0) + 0.25(-1-1.0) + 0.25(-1-1.0) + 0.25(-1-1.0)
Policy Improvement
สำมำรถทำได้ จำก value function ทีค่ ำนวณได้ มำโดยปรับ
ให้ ไปเลือก action ทีม่ ี value function สู งสุ ด
p(s)  argmax Qp (s,a)
a
 argmax  Psas Rsas    V p (s)
a
s 
เรียกว่ ำเป็ น greedy actions
p
Then V  V
p
What if V p  V p ?
i.e., for all s S, V p(s)  max  PsasRsas   V p (s ) ?
a
s 
But this is the Bellman Optimality Equation.
So V p  V  and both p and p are optimal policies.
Iterative Policy Improvement
p0  V
p0
p1
 p1  V 
policy evaluation
p V p
*
*
policy improvement
“greedification”
กำร estimate ค่ ำ value function โดยใช้ ค่ำ estimate
ของมันเองเรียก Bootstrapping
*
Dynamic Programming
Monte Carlo Method
Dynamic Programming ต้ องรู้ world model
นั่นคือมี reward เท่ ำใด ทีไ่ หมบ้ ำง และ next state เป็ นอะไร
Monte Carlo จะไม่ ต้องรู้ world model มำก่ อน
คุณสมบัติของ Monte Carlo
• ใช้ ได้ กบั episodic tasks เท่ ำนั้น
• learn จำก ประสบกำรณ์ จริงโดยใช้ complete returned reward
Monte Carlo Policy Evaluation
• Goal: learn Vp(s)
• Given: some number of episodes under p
which contain s
• Idea: Average returns observed after visits to s
1
2
3
4
5
• Every-Visit MC: average returns for every time s is
visited in an episode
• First-visit MC: average returns only for first time s
is visited in an episode
• Both converge asymptotically
Backup Diagram of MC
• Entire episode included
• Only one choice at each
state (unlike DP)
s1
a1
s2
a2
• MC does not bootstrap
s3
a3
• Time required to
estimate one state does
not depend on the total
number of states
Series of
actions
Monte Carlo Policy Iteration
Q p k (s, p k 1 (s))  Qp k (s,arg max Qp k (s,a))
a
 max Q p k (s,a)
a
p
 Q k (s,p k (s))
p
 V k (s)
Policy Improvement
Example: Blackjack
• Object: Have your card sum be greater than
the dealers without exceeding 21.
• States (200 of them):
– current sum (12-21)
– dealer’s showing card (ace-10)
– do I have a useable ace?
• Reward: +1 for winning, 0 for a draw, -1 for
losing
• Actions: stick (stop receiving cards), hit
(receive another card)
• Policy: Stick if my sum is 20 or 21, else hit
เช่ นในสถำนะกำรหนึ่งไม่ มี usable ace และ เจ้ ำมือ show 8
เรำเริ่มต้ นที่ แต้ ม = 12 จั่วได้ เป็ น 14 และ 18 แล้ วดูผลว่ ำแพ้ หรื อชนะ
จะนำค่ ำผลลัพธืทไี่ ด้ มำ update state ที่ 12, 14, 18 นี้
ทำไปหลำยหมื่นครั้งจะได้ กรำฟของทุก state ดังรูป
Temporal Difference (TD)
Policy Evaluation (the prediction problem):
p
for a given policy p, compute the state-value function V
Recall:
Simple every - visit Monte Carlo method :
V(st )  V(st )   Rt  V (st )
target: the actual return after time t
The simplest TD method,
TD(0) :
V(st )  V(st )   rt 1   V (st1 )  V(st )
target: an estimate of the return
Simple Monte Carlo
V(st )  V(st )   Rt  V (st )
where Rt is the actual return following state
st .
st
T
T
T
TT
T
TT
T
TT
T
TT
T
Simplest TD Method
V(st )  V(st )   rt 1   V (st1 )  V(st )
st
rt 1
st 1
TT
T
T
T
TT
T
T
TT
T
TT
T
Dynamic Programming
V(st )  Ep rt 1   V(st )
st
rt 1
T
TT
TT
T
T
T
T
T
T
st 1
T
TD Bootstraps and Samples
• Bootstrapping: update involves an
estimate
– MC does not bootstrap
– DP bootstraps
– TD bootstraps
• Sampling: update does not involve
an expected value
– MC samples
– DP does not sample
– TD samples
Learning an Action-Value Function
p
Estimate Q for the current behavior policy
After every transition from a nonterminal state
p.
st , do this :
Qst , at   Qst , at    rt 1   Qst 1 ,at 1   Qst ,at 
If st 1 is terminal, then Q(st 1, at 1 )  0.
Sarsa: On-Policy TD
ทีเ่ ป็ น on-policy เพรำะต้ องทำ action ก่ อนเพื่อให้ ได้ next state
แล้ วค่ อยนำมำ update value table
Q-learning Off-Policy TD
One - step Q - learning :


Qst , at   Qst , at    rt 1   max Qst1 , a  Qst , at 
a
ทีเ่ ป็ น off-policy เพรำะไม่ ต้องทำตำม next state ทีจ่ ะเลือกเอำ
action ทีไ่ ด้ reward สู งสุ ด
Current state
  0.9
Action
ไปทำงขวำ
Next state