Reinforcement Learning - Basics


RL_1Basics

1. 基本概念

这是一个强化学习的最基本示例:

env

  • Task

    机器人在一个 Grid of Cells 里,找到从起点走到终点的最优路径

  • Rules

    1. 可以自由移动(不能斜着走),但是不应该进入 Forbidden Cells,会因此受罚

    2. 机器人无法感知,Grid of Cells 对它而言是个黑箱

 

 

1.1 State

Status of the agent with respect to the environment

state

本案例中机器人的 Location 就是 State,9 个 Cells 就是 9 个 State: s1,s2,...,s9

State Space = The set of all States S={si}i=19

 

 

1.2 Action

actions

每一个 State 都有可以执行的 Action,本案例中有 5 种:上 (a1),下 (a3),左 (a4),右 (a2),静止 (a5)

不同的 State 可用的 Action 不一样,比如理论上位于最上方的 State s1,s2,s3 就不应该有 Action a1,否则会撞墙

但实际上可能仍然会保留 a1,并为了严谨性增加 “撞墙回弹的功能”

Action Space of State = The set of all Actions of a State A(si)={a}i=15

 

 

1.3 State-Transition

它描述的是这么一个过程:When taking an Action, the agent may move from one State to another

  • Deterministic

    在哪一个 State 做了哪一个 Action 会转到哪一个 State 是100%确定的,你在 s5 左转 (a2) 一定会进入 s6

    s5a2s6

    用 State-Transition Probability 的方式表示为

    p(s6|s5,a2)=1p(si|s5,a2)=0i6

    也可以用 Tabular Representation

    table

  • Stochastic

    很多情况下 State-Transition 是 Stochastic 的,即在同一 State 采取同一 Action 可能跳转到不同的新 State,比如:

    1. 上面的 Table 里 s1,s2,s3 的 Action Space 包含了 a1,但是采取后的效果相当于静止

    2. 墙若有弹性,那么在 s1 采取 a1 后撞墙,Agent 的状态有两种可能性:

      • 弹回到 s1 原地静止

      • 弹回到 s4

    假设 2. 中所述两种情况的概率是 50-50,那么用 Conditional Probability 可表示为

    p(s1|s1,a1)=0.5p(s4|s1,a1)=0.5p(si|s1,a1)=0i1,4

    不过 Stochastic 的 State-Transition 就没法写成 Table 了

  • General Form

     

 

 

1.4 Policy

这和 State-Transition 是两个东西!各自的 Conditional Probability 定义不一样!!!

policy

Policy π is a (probabilistic) mapping from State Space to Action Space

有点像是一种 “场”,在当前类型的案例里一般用箭头表示;无论 Agent 在哪里 / 哪个 State,Policy 都能告诉它下一步要往哪里走

所以无论 Agent 从哪里开始任务,policy 最终都应该能把它带到目的地,就像上图一样

  • Deterministic

    如上图,一个 State 能采取的 Action 是确定的,只有一个

    d policy

    s1 为例,与它相关的 Policy 用 Conditional Probability 可表示为

    π(a2|s1)=1π(ai|s1)=0i2
  • Stochastic

    一个 State 有概率采取不同的 Action

    s policy

    s1 为例,与它相关的 Policy 用 Conditional Probability 可表示为

    π(a2|s1)=1π(a3|s1)=0.5π(si|a1)=0i2,3

    也可以写成 Table,与 State-Transition 完全不一样,Policy 无论 Deterministic / Stochastic 都可以用 Tabular Representation

    policy table

 

 

1.5 Reward

在采取某个 Action 后得到的数值

  • Positive Reward = Encouragement to take such Action

  • Negative Reward = Punishment to take such Action

好吧,其实你也可以用 Positive Number 表示 punishment,但那样 Reward 就该改名叫 Cost 了

Reward 的 Absolute Values 不重要,重要的是 Relative Values

Changing r={+1,1} to r={+2,0} will not make any difference

  • Deterministic

    一个 Action 始终只会对应一个 Reward 数值

    reward table

    可以写成如上 Table 并作如下设定

    • rbound=1 Agent 撞墙

    • rforbid=1 Agent 进入禁区

    • rtarget=+1 Agent 抵达终点

    • r=0 For all other cases

    (s1,a1) 的 (State, Action) 组合为例子,Reward 用 Conditional Probability 表示为

    p(r=1|s1,a1)=1p(r1|s1,a1)=0
  • Stochastic

    现实中一个 Action 对应的 Reward 数值并不会完全固定

    比如努力学习 (这是 Action),考试分数一定会提高(这是 Reward),但提高多少(这是 Reward 的数值)会取决于其它因素

    这种没法写成 Table

  • [ Reward Hacking ]

    如果奖励机制设计得很烂,训练出来的 Agent 可能会通过执行 Penalized Action 来最大化 Reward 收益

    比如在本案例中,Agent 可能会故意跨入 Forbidden Cell 抄近道抵达目的地

 

 

1.6 Episode & Trajectory

  • Trajectory

    Trajectory 是一条 State-Action-Reward Chain,如下两个例子

    trajectory

    对应的两条 Trajectory 为

    Policy 1:s1r=0a2s2r=0a3s5r=0a3s8r=+1a2s9Policy 2:s1r=0a3s4r=1a3s7r=0a2s8r=+1a2s9
  • Episode

    有 Terminal State 的 Finite Trajectory 是一个 Episode

    上面的两个 Trajectory 就是两个 Episode

    • Task with Terminal State Episodic Task

    • Task with no Terminal State Continuing Task

    Infinite Trajectory 见 1.7 Return - Discounted Return

 

 

1.7 Return

The sum of all Rewards along the Trajectory,用于评估一个 Policy 的好坏程度

1.6 的两个示例为基础,两个 Policy 的 Return 为

Policy 1:Return=0+0+0+1=1Policy 2:Return=01+0+1=0

Policy 1 的 Return 更大,显然它更好

 

1.7.1 Discounted Return

有时 Policy 在 Agent 抵达目的地后仍然会继续运行,类似于 “动态静止”

infinite trajectory

如上图,此时这条 Trajectory 从定义上而言,是 Infinite

s1r=0a2s2r=0a3s5r=0a3s8r=+1a2s9r=+1a5s9r=+1a5s9r=+1a2

那么 Trajectory 的 Return 就 diverge

Return=0+0+0+1+1+1+1+=

不止这条,其实从任何起点出发的 Trajectory 的 Return 最后都会因为抵达终点而发散,若 Return 的大小都是无穷,便无法比较 Policy 之优劣,这个衡量标准就失去了意义

所以需要引入一个 Discount Rate γ(0,1)

Discounted Return=0+0γ+0γ2+1γ3+1γ4+1γ5+1γ6+=γ311γ

无论 Intuitively 还是 Mathematically,引入这个值一定能让 Return 收敛

  • γ0 Emphasis on Near Future

  • γ1 Emphasis on Far Future

上面的描述是从 “计算尚未开始” 的角度解释

若从 “计算已经结束” 的角度解释:

  • Near Future = 古早历史记录

  • Far Future = 近期历史记录

 

1.7.2 Bootstrapping

Consider the following example

bootstrapping

Assume the Return obtained from State si is denoted as vi, then

v1=r1+r2γ+r3γ2+r4γ3+r1γ5+=r1+γv2v2=r2+r3γ+r4γ2+r1γ3+r2γ5+=r2+γv3v3=r3+r4γ+r1γ2+r2γ3+r3γ5+=r3+γv4v4=r4+r1γ+r2γ2+r3γ3+r4γ5+=r4+γv1

Bootstrapping = Returns rely on each other

类似于 “一直脚踩在另一只脚上,那就能走到无限高处”,概念荒唐但公式本身并不荒唐且可解

v1=r1+γv2v2=r2+γv3v3=r3+γv4v4=r4+γv1

够明显了,非齐次线性方程组,四个未知量四个公式,4 个 Return 求出来很容易

 

 

 

2. Markov Decision Process

2.1 Definition

Markov Decision Process (MDP) 是一个 Tuple

S,A,P,R,γSFinite State SpaceAFinite Action SpacePState-Transition Probability MatrixRReward FunctionγDiscount Rate

where

  • State-Transition Probability Matrix

    正如其名,只是把所有的概率全写在一起

    P=p(St+1|St,At)where {St+1SStSAtA

    and it holds that st+1Sp(st+1|st,at)=1 for any (s,a)

  • Reward Function

    可以写成 Probability Distribution

    Rp(Rt+1|St,At)where {Rt+1RStSAtA

    也可以写成 Expectation

    R=E[Rt+1|St,At]where {Rt+1RStSAtA

    and it holds that rt+1Rp(rt+1|st,at)=1 for any (s,a)

    此处之所以用 Rt+1/rt+1 是因为倾向于理解为离开状态 St/st 才能获得 Reward

    如果阁下有不同的想法,比如 “进入 State 时获得 Reward”,事先定义一下,效果是一样的

 

 

2.2 其他特征

  • Policy

    π(A|S)= Probability of chooing Action A in State S

    此处写成一个 Matrix Form

    And it holds that atAp(at|st)=1 for any sS

  • Markov Property

    鬼话:The Memoryless property of a stochastic process

    人话:新决策的概率无关于决策的历史记录,MDP无记忆

    p(st+1|st,at)=p(st+1|st,at,st1,at1,...,...,s0,a0)p(rt+1|st,at)=p(rt+1|st,at,st1,at1,...,...,s0,a0)

    从数学表达式可以看出,你可以提供很多的过去决策供参考,但是全部木大

    下一个 State / Reward 只会参考现在 的 State & Action!

 

 

2.3 Markov Process

Markov Decision Process becomes Markov Process once the Policy is given

比如 1. 中的 Grid of Cells,左图里描述了采取的 Policy,右侧更加抽象的图就是对应的 Markov Process

markov process

 

 

 

3. Bellman Expectation Equation

一般的称谓是 Bellman Equation

 

3.1 State Value 状态价值

“告诉我们从哪个 State 出发最合适”

State Value vπ(s) is the Expected Return Gt the Agent can get starting at State s

Trajectory:StAtRt+1,St+1At+1Rt+2,St+2At+2Rt+3,Discounted Return:Gt=Rt+1+Rt+2γ+Rt+3γ2+State Value:vπ(s)=E[Gt|St=s]

关于 State Value vπ(s)

  1. Gt 有 Expectation 在数学上合理

    此处的 Gt,Rt+1,Rt+2,Rt+3 均为 Random Variable

    不如说是因为 Rt+1,Rt+2,Rt+3 是 Random Variable,所以作为结果的 Gt 也是 Random Variable

  2. Gt 有 Expectation 在直觉上合理

    以某个 State 为起点寻路,能抵达终点的 Trajectory 可能会有很多条,那么也可能会有多个 Discounted Return

    如果 State-Transition 是完全 Deterministic 的,那么此时以某一个 State 为起点的 Trajectory 必定只有一条,因此有

    vπ=Gt
  3. State Value 依赖 Policy

    Policy 变了 Trajectory 也会变,这很合理

    所以 State Value 的表达方式改成 v(π,s) 会更合适

[ Example ]

在 State-Transition 上,左图的 Policy π1,Deterministic 的;右图的 Policy π2,Stochastic 的

d v.s. s

各自以 s1 为起点的 State Value 为

Policy π1:vπ1(s1)=1(1+1γ+1γ2+)+0Policy π2:vπ2(s1)=0.5(1+1γ+1γ2+)+0.5(0+1γ+1γ2+)

最终结果

Policy π1:vπ1(s1)=1+γ1γPolicy π2:vπ2(s1)=0.5+γ1γ

 

3.2 Elementwise Form

Bellman Expectation Equation 用于计算 State Value

Consider the following Random Trajectory

StAtRt+1,St+1At+1Rt+2,St+2At+2Rt+3,

其 Discounted Return 为

Gt=Rt+1+Rt+2γ+Rt+3γ2+=Rt+1+γGt+1

那么 State Value 公式可作如下处理

vπ(st)=E[Gt|St=st]=E[Rt+1+γGt+1|St=st]=E[Rt+1|St=st]E{Immediate Reward}+γE[Gt+1|St=st]E{Future Reward}

分开计算

  • "Immediate Reward" Term

    E[Rt+1|St=st]=at{π(at|st)E[Rt+1|St=st,At=at]}=at{π(at|st)rt+1[p(rt+1|st,at)rt+1]}
  • "Future Reward"Term

    第二步的 Expectation 部分通过 Markov Property 化简

    E[Gt+1|St=st]=st+1{E[Gt+1|St+1=st+1,St=st]p(st+1|st)}=st+1{E[Gt+1|St+1=st+1]at[π(a|st)p(st+1|st,at)]}=st+1{vπ(st+1)at[π(a|st)p(st+1|st,at)]}

两部分合并可得

vπ(st)=E[Rt+1|St=st]+γE[Gt+1|St=st]=at{π(at|st)rt+1[p(rt+1|st,at)rt+1]}+γst+1{vπ(st+1)at[π(a|st)p(st+1|st,at)]}=at{π(at|st)rt+1[p(rt+1|st,at)rt+1]}+γatst+1{π(a|st)p(st+1|st,at)vπ(st+1)}=at{π(at|st)rt+1[p(rt+1|st,at)rt+1]}+at{π(a|st)γst+1[p(st+1|st,at)vπ(st+1)]}Bellman Equation:vπ(st)=at{π(at|st)(rt+1[p(rt+1|st,at)rt+1]+γst+1[p(st+1|st,at)vπ(st+1)])}

where

  • vπ(st),vπ(st+1) State Values to be computed

    with Bootstrapping in 1.7.2

  • π(at|st) given Policy

  • p(rt+1|st,at),p(st+1|st,at) Environment Models

    1. 当前语境下,这些 Model 是 known 的

    2. 若 Model unknown,那就是 Model-Free RL 的内容了

确认 Policy 和 Environment Models (以及 γ)的数值后,State Values 就很好求了

略微偷懒一点的写法(公式项的包含关系还是上面的那个更详细)

Bellman Equation:vπ(st)=atπ(at|st)[rt+1p(rt+1|st,at)rt+1+γst+1p(st+1|st,at)vπ(st+1)]stS

 

 

3.3 Matrix-Vector Form

3.3.1 Derivation

Elementwise Form 的 BEE 与 State 数量挂钩,即有多少个 State 就会有多少个这样的公式,把它们联立会非常 nice

先对 Elementwise Form 进行修改,退回到 3.2 的 “两部分合并”,第三行

vπ(st)=at{π(at|st)(rt+1[p(rt+1|st,at)rt+1]+γst+1[p(st+1|st,at)vπ(st+1)])}=at{π(at|st)rt+1[p(rt+1|st,at)rt+1]}+γatst+1{π(at|st)p(st+1|st,at)vπ(st+1)}=at{π(at|st)rt+1[p(rt+1|st,at)rt+1]rπ(st)}+γst+1{at[π(at|st)p(st+1|st,at)]pπ(st+1|st)vπ(st+1)}=rπ(st)+γst+1{pπ(st+1|st)vπ(st+1)}

where

  • rπ(st) Expectation of Immediate Reward

  • pπ(st+1|st) 从 State st 转到 State st+1 的概率

更换一下 Notation,使 si=st,sj=st+1,那么 BEE for si

vπ(si)=rπ(si)+γsj{pπ(sj|si)vπ(sj)}

然后写出所有 State 对应的上式,联立后得到 Matrix-Vector Form

Bellman Equation:vπ=rπ+γPπvπ

where

State Values:vπ=[vπ(s1)vπ(s2)vπ(sn)]RnExpected Immediate Rewards:rπ=[rπ(s1)rπ(s2)rπ(sn)]RnState-Transition Matrix:PπRn with [Pπ]ij=pπ(sj|si)

 

3.3.2 Example

Assume n=4,then

[vπ(s1)vπ(s2)vπ(s3)vπ(s4)]vπ=[rπ(s1)rπ(s2)rπ(s3)rπ(s4)]rπ+γ[pπ(s1|s1)pπ(s2|s1)pπ(s3|s1)pπ(s4|s1)pπ(s1|s2)pπ(s2|s2)pπ(s3|s2)pπ(s4|s2)pπ(s1|s3)pπ(s2|s3)pπ(s3|s3)pπ(s4|s3)pπ(s1|s4)pπ(s2|s4)pπ(s3|s4)pπ(s4|s4)]Pπ[vπ(s1)vπ(s2)vπ(s3)vπ(s4)]vπ

如果 Policy 和 Reward 如下

ex

那么 Matrix-Vector Form 变成

[vπ(s1)vπ(s2)vπ(s3)vπ(s4)]=[0.5(0)+0.5(1)111]+γ[00.50.50000100010001][vπ(s1)vπ(s2)vπ(s3)vπ(s4)]

 

3.3.3 How to Solve
  • Closed-Form Solution

    好消息是它很简洁很方便,坏消息是涉及 Matrix Inversion,所以大概是用不上了

    vπ=(IγPπ)1rπ
  • Iterative Solution

    坏消息是证明起来很烦,好消息是好理解也很好用

    vk+1=rπ+γPπvk

    你可以设定 k=0,然后 Guess 一个 v0 代入上式,得到 v1

    不断接替代入就能获得一长串结果 {v0,v1,v2,...},最终会发现

    vkvπ=(IγPπ)1rπas k

    [ Tips ]

    1. 这里用的下标 Notation 是 k 不是 t它和 State 没有关系

    2. 这是一种类似于级数逼近的方法,不用真的算到 k

      只需设置一个收敛阈值 ϵ,然后迭代到 vkvπ<ϵ 即可

 

 

3.4 Action Value 动作价值

“告诉我们采取 State 中的哪个 Action 最合适”

3.4.1 Definition

Action Value qπ(s,a) is the Expected Return Gt the Agent can get starting at State s and taking Action a

Trajectory:StAtRt+1,St+1At+1Rt+2,St+2At+2Rt+3,Discounted Return:Gt=Rt+1+Rt+2γ+Rt+3γ2+Action Value:qπ(s,a)=E[Gt|St=s,At=a]

Action Value 也称 Q-Value

 

3.4.2 与 State Value 的关系
  • "Action State"

    Action Value 与 3.1 中定义的 State Value 只差了一个 “选择某个 Action” 的过程

    以下是 State Value 的定义与公式,以供对比:

    State Value vπ(s) is the Expected Return Gt the Agent can get starting at State s

    vπ(s)=E[Gt|St=s]

    所以 Action Value 与 State Value 的关系式为

    vπ(s)=E[Gt|St=s]=a{π(a|s)E[Gt|St=s,At=a]}vπ(s)=a[π(a|s)qπ(s,a)]
  • "State Action"

    3.2 的 Elementwise BEE 联立可发现

    vπ(st)=at{π(at|st)(rt+1[p(rt+1|st,at)rt+1]+γst+1[p(st+1|st,at)vπ(st+1)])qπ(st,at)}

    也就是 qπ(st,at)vπ(st+1) 存在联系

    qπ(st,at)=rt+1[p(rt+1|st,at)rt+1]+γst+1[p(st+1|st,at)vπ(st+1)]
  • Summary

    AV to SV:vπ(st)=at[π(at|st)qπ(st,at)]SV to AV:qπ(st,at)=rt+1[p(rt+1|st,at)rt+1]+γst+1[p(st+1|st,at)vπ(st+1)]

 

 

 

4. Bellman Optimality Equation

Policy π is Optimal if vπ(s)>vπ(s) for all States s & all other Policies π

 

4.1 Definition

Bellman Optimality Equation (BOE):

  1. 假设我们不知道 Policy π(at|st)

  2. 相比 BEE 前面多了一个最大化的语句

以下是两种 BOE 公式

  • Elementwise Form

    vπ(st)=maxπat{π(at|st)(rt+1[p(rt+1|st,at)rt+1]+γst+1[p(st+1|st,at)vπ(st+1)])}=maxπat[π(at|st)qπ(st,at)]

    where

    • p(rt+1|st,at),p(st+1|st,at) Known

    • vπ(st),vπ(st+1) Unknown, to be Computed

    • π(at|st) Unknown, to be Computed

  • Matrix-Vector Form

    此处的 maxπ 会 Elementwise 施加在矩阵 (rπ+γPπvπ) 的每一项上

    vπ=maxπ[rπ+γPπvπ]

 

 

4.2 解法前置理论

4.2.1 Elementwise Form 解

以有 Action Value 的版本为基础

vπ(st)=maxπat[π(at|st)qπ(st,at)]

问题可以简化为

Find optimal coefficients c1,c2,c3 given

maxc1,c2,c3c1q1+c2q2+c3q3

为了让 ci 能对应上 π 的特征,设定 c1+c2+c3=1c1,c2,c30

在不失普适性的前提下,若假定 q3>q1,q2,那么 Optimal Coefficient 应当为

c1=0c2=0c3=1

把最大的 Weight 给最大的 Term,就能最大化它们之和

c1q1+c2q2+c3q3=q31=q3(c1+c2+c3)=c1q3+c2q3+c3q3c1q1+c2q2+c3q3

所以回到 BOE 上,可以得出结论

vπ(st)=maxatqπ(st,at)at=argmaxatqπ(st,at)π(at|st)={1a=a0aa

 

4.2.2 Matrix-Vector Form 解

以该形式为基础,要求 πvπ 两个变量

vπ=maxπ[rπ+γPπvπ]

类似于是在解如下类型的问题

Find x,aR that satisfies

x=maxa(2x1a2)

首先可以确定 a20,所以在公式的 Maximum 处必有 a=0,所以就变成了

x=2x1x=1

说实话我写这一部分的时候都在怀疑是否真的需要写下来,但是为了防止脑子 GaGa 了,还是留个记录吧(恼

 

4.2.3 Contraction Mapping Theorem
  • Fixed Point

    xX is a fixed point of f:XX if

    x=f(x)
  • Contraction Mapping

    f is a contraction mapping if

    f(x1)f(x2)γx1x2

    where γ(0,1)

    一个直观的解释是

    contract map

  • Contraction Mapping Theorem

    1. Definition

      For any contraction mapping f,there exists a unique fixed point x such that x=f(x)

    2. Algorithm

      Define a sequence {xk} where xk+1=f(xk),从任意起点 Initial Guess x0 开始不断代入上式,得到 {x0,x1,...,x},然后...

      xkxask

      Convergence Rate is Exponentially Fast given any Initial Guess,非常好用

    [ Example ]

    Given Contraction Mapping f(x)=0.5x where xR

    则迭代公式为

    xk+1=0.5xk

    若从 x0=10 开始,则得到的数列 {xk}

    x0=10,x1=5,x2=2.5,...,x=x=0

    最后得到 x=0 满足 Fixed Point 的结果(虽然实际上可以一眼出答案...)

 

 

4.3 Solution

BOE 的 Matrix-Vector Form 是关于 vπ 的函数,所以可以有

f(vπ)=maxπ[rπ+γPπvπ]

那么 BOE 就可以写成下面这个贼简洁的公式

vπ=f(vπ)

[ 重要结论 ]:BOE 的 f(vπ) 是一个 Contraction Mapping

具体证明就不说了,但是有个很 Intuitive 的证据,即 γ<0;总之,可以直接套用 4.2.3 Contraction Mapping Theorem 的结论

  1. Existence

    一定存在一个 Unique Fixed Point vπ 满足

    vπ=f(vπ)=maxπ[rπ+γPπvπ]
  2. Algorithm

    4.2.3 中的相同,could be solved iteratively by

    vk+1=f(vk)=maxπ[rπ+γPπvk]vkvask

    Convergence Rate is determined by γ

 

 

4.4 Policy Optimality & 总结

  • State Value

    Assume v is the Unique Solution to BOE

    v=maxπ[rπ+γPπv]

    Assume vπ is the State Value that satisfies BEE for all Policies π

    vπ=rπ+γPπvπ

    Then

    vvππ
  • Policy

    π(at|st)=argmaxπ[rπ+γPπv]=argmaxπat{π(at|st)(rt+1[p(rt+1|st,at)rt+1]+γst+1[p(st+1|st,at)v(st+1)])}=argmaxπat[π(at|st)q(st,at)]

    For any sS, the Deterministic Greedy Policy

    π(at|st)={1at=a0ata

    is an Optimal Policy solving the BOE

    所以其实可以这么表述

    v=vπ
  • Action Value

    a=argmaxπq(st,at)=argmaxa(rt+1[p(rt+1|st,at)rt+1]+γst+1[p(st+1|st,at)v(st+1)])