强化学习2——有模型强化学习MDP(搬砖马尔科夫,贝尔曼等式)

强化学习——马尔科夫系列

前言

参考了李宏毅《深度强化学习》,和开源大佬们的教程,根据自己的理解删删补补,打工人搬砖整理。

mark下下面的公式,对后面理解算法很重要~​🙂​
mark下下面的公式,对后面理解算法很重要~​🙂​
mark下下面的公式,对后面理解算法很重要~​🙂​

Bellman Equation: V ( s ) = R ( S ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s ) V ( s ′ ) V(s)=R(S)+ \gamma \sum_{s' \in S}P(s'|s)V(s') V(s)=R(S)+γsSP(ss)V(s)

Bellman Expectation Equation: v π ( s ) = ∑ a ∈ A π ( a ∣ s ) ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v π ( s ′ ) ) \mathrm{v}^{\pi}(\mathrm{s})=\sum_{\mathrm{a} \in \mathrm{A}} \pi(\mathrm{a} \mid \mathrm{s})\left(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}^{\pi}\left(\mathrm{s}^{\prime}\right)\right) vπ(s)=aAπ(as)(R(s,a)+γsSP(ss,a)vπ(s))

Bellman optimality Equation: v π ( s ) = max ⁡ a ∈ A q π ( s , a ) = max ⁡ a ∈ A ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v ( s ′ ) ) \mathrm{v}^{\pi}(\mathrm{s})=\max _{\mathrm{a} \in \mathcal{A}} \mathrm{q}^{\pi}(\mathrm{s}, \mathrm{a})=\max _{\mathrm{a} \in \mathcal{A}}(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}\left(\mathrm{s}^{\prime}\right)) vπ(s)=maxaAqπ(s,a)=maxaA(R(s,a)+γsSP(ss,a)v(s))

马尔科夫决策过程(MDP)

1 马尔科夫过程(MP)

1.1 马尔科夫性质

​ 如果一个状态的下一个状态只取决去它当前状态,而跟它当前状态之前的状态没有关系,则次状态转移称之为符合马尔科夫性质的

1.2 马尔科夫过程

在这里插入图片描述
上图就是马尔科夫链,P就是马尔科夫的状态转移矩阵。

1.3 马尔科夫的一个例子

在这里插入图片描述
如上图,是一个马尔科夫链的一个例子,我们可以在任一个状态进行T步的采样,这样就能得到一连串的轨迹。我们在上图中在状态S3开始进行了3次4步取样轨迹。

2 马尔科夫奖励过程(MRP)

马尔科夫奖励过程=马尔科夫链+奖励函数:

S S S是状态集合

P P P是状态转移概率 P ( S t + 1 = s ′ ∣ s t = s ) P\left(S_{t+1}=s^{\prime} \mid s_{t}=s\right) P(St+1=sst=s)

R R R是奖励函数 R ( s t = s ) = E [ r t ∣ s t = s ] R\left(s_{t}=s\right)=\mathbb{E}\left[r_{t} \mid s_{t}=s\right] R(st=s)=E[rtst=s]

γ ∈ [ 0 , 1 ] \gamma \in[0,1] γ[0,1]是折扣系数

如果状态有限则 R R R是一个向量

2.1 马尔科夫奖励过程的一个例子

在这里插入图片描述
如上图所示,奖励函数R就是一个向量,表示到达每个状态所获得的奖励

2.2 回报和状态价值函数

Horizon:每个时期的最大时间步数,如下面的就是T-t个时间步

回报:把奖励进行折扣后获得的收益 G t = R t + 1 + γ R t + 2 + γ 2 R t + 3 + γ 3 R t + 4 + … + γ T − t − 1 R T \mathrm{G}_{\mathrm{t}}=\mathrm{R}_{\mathrm{t}+1}+\gamma \mathrm{R}_{\mathrm{t}+2}+\gamma^{2} \mathrm{R}_{\mathrm{t}+3}+\gamma^{3} \mathrm{R}_{\mathrm{t}+4}+\ldots+\gamma^{\mathrm{T}-\mathrm{t}-1} \mathrm{R}_{\mathrm{T}} Gt=Rt+1+γRt+2+γ2Rt+3+γ3Rt+4++γTt1RT

状态价值函数:回报的期望 V t ( s ) = E [ G t ∣ s t = s ] = E [ R t + 1 + γ R t + 2 + γ 2 R t + 3 + … + γ T − t − 1 R T ∣ s t = s ] \begin{aligned} \mathrm{V}_{\mathrm{t}}(\mathrm{s}) &=\mathbb{E}\left[\mathrm{G}_{\mathrm{t}} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}\right] \\ &=\mathbb{E}\left[\mathrm{R}_{\mathrm{t}+1}+\gamma \mathrm{R}_{\mathrm{t}+2}+\gamma^{2} \mathrm{R}_{\mathrm{t}+3}+\ldots+\gamma^{\mathrm{T}-\mathrm{t}-1} \mathrm{R}_{\mathrm{T}} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}\right] \end{aligned} Vt(s)=E[Gtst=s]=E[Rt+1+γRt+2+γ2Rt+3++γTt1RTst=s],就是说从 s s s这个状态开始,你有可能获得多大的价值,也可以说当你进入这个状态后,你具有多大的价值

2.3 贝尔曼等式

V ( s ) = E [ G t ∣ s t = s ] = E [ R t + 1 + γ R t + 2 + γ 2 R t + 3 + … + γ T − t − 1 R T ∣ s t = s ] \begin{aligned} \mathrm{V}(\mathrm{s}) &=\mathbb{E}\left[\mathrm{G}_{\mathrm{t}} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}\right] \\ &=\mathbb{E}\left[\mathrm{R}_{\mathrm{t}+1}+\gamma \mathrm{R}_{\mathrm{t}+2}+\gamma^{2} \mathrm{R}_{\mathrm{t}+3}+\ldots+\gamma^{\mathrm{T}-\mathrm{t}-1} \mathrm{R}_{\mathrm{T}} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}\right] \end{aligned} V(s)=E[Gtst=s]=E[Rt+1+γRt+2+γ2Rt+3++γTt1RTst=s]

其中马尔科夫奖励过程的价值函数满足下列等式,这个等式称为贝尔曼等式
V ( s ) = R ( s ) ⏟ Immediate reward  + γ ∑ s ′ ∈ S P ( s ′ ∣ s ) V ( s ′ ) ⏟ Discounted sum of future reward  \mathrm{V}(\mathrm{s})=\underbrace{\mathrm{R}(\mathrm{s})}_{\text {Immediate reward }}+\underbrace{\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}\right) \mathrm{V}\left(\mathrm{s}^{\prime}\right)}_{\text {Discounted sum of future reward }} V(s)=Immediate reward  R(s)+Discounted sum of future reward  γsSP(ss)V(s)
​ 1. R(s)为当前可以立即获得的价值,就是奖励函数中的一个元素参考2.1图最下方。

​ 2. S可以看成未来的所有状态,V(s′) 代表的是未来某一个状态的价值

​ 3. 转移 P(s’|s) 是指从当前状态转移到未来状态的概率。

​ 在未来可能到达的状态奖励上进行一个折扣+加上当前可以立即获得的奖励=当前状态的价值,这就是贝尔曼等式。贝尔曼等式就是当前状态与未来状态的迭代关系,表示当前状态的值函数可以通过下个状态的值函数来计算

对贝尔曼等式的理解
在这里插入图片描述
如图是1.2中的例子,右边的图定义了一个状态转移矩阵,在1.2中有提及。

那么左图就是我们要举的一个例子,$V(s_{1})=R(s)+\gamma \sum_{s^{\prime} \in S} P\left(s^{\prime} \mid s\right) V\left(s^{\prime}\right)\=R(s_{1})+\gamma P\left(s_{1}\mid s\right) V\left(s_{1}\right)+\gamma P\left(s_{2}\mid s\right) V\left(s_{2}\right)+\gamma P\left(s_{3}\mid s\right) V\left(s_{3}\right)\ $

将概率为零状态转移的则全部消除,其实也不难计算和理解。

则全部的状态价值函数的矩阵表达式为:
[ V ( s 1 ) V ( s 2 ) ⋮ V ( s N ) ] = [ R ( s 1 ) R ( s 2 ) ⋮ R ( s N ) ] + γ [ P ( s 1 ∣ s 1 ) P ( s 2 ∣ s 1 ) … P ( s N ∣ s 1 ) P ( s 1 ∣ s 2 ) P ( s 2 ∣ s 2 ) … P ( s N ∣ s 2 ) ⋮ ⋮ ⋱ ⋮ P ( s 1 ∣ s N ) P ( s 2 ∣ s N ) … P ( s N ∣ s N ) ] [ V ( s 1 ) V ( s 2 ) ⋮ V ( s N ) ] \left[\begin{array}{c} V\left(s_{1}\right) \\ V\left(s_{2}\right) \\ \vdots \\ V\left(s_{N}\right) \end{array}\right]=\left[\begin{array}{c} R\left(s_{1}\right) \\ R\left(s_{2}\right) \\ \vdots \\ R\left(s_{N}\right) \end{array}\right]+\gamma\left[\begin{array}{cccc} P\left(s_{1} \mid s_{1}\right) & P\left(s_{2} \mid s_{1}\right) & \ldots & P\left(s_{N} \mid s_{1}\right) \\ P\left(s_{1} \mid s_{2}\right) & P\left(s_{2} \mid s_{2}\right) & \ldots & P\left(s_{N} \mid s_{2}\right) \\ \vdots & \vdots & \ddots & \vdots \\ P\left(s_{1} \mid s_{N}\right) & P\left(s_{2} \mid s_{N}\right) & \ldots & P\left(s_{N} \mid s_{N}\right) \end{array}\right]\left[\begin{array}{c} V\left(s_{1}\right) \\ V\left(s_{2}\right) \\ \vdots \\ V\left(s_{N}\right) \end{array}\right] V(s1)V(s2)V(sN)=R(s1)R(s2)R(sN)+γP(s1s1)P(s1s2)P(s1sN)P(s2s1)P(s2s2)P(s2sN)P(sNs1)P(sNs2)P(sNsN)V(s1)V(s2)V(sN)

2.4 计算MRP的价值函数

2.4.1 蒙特卡罗(Monte Carlo)法

 Algorithm  1  Monte Carlo simulation to calculate MRP value function  1 : i ← 0 , G t ← 0  2: while  i ≠ N  do   3: generate an episode, starting from state  s  and time  t  4: Using the generated episode, calculate return  g = ∑ i = t H − 1 γ i − t r i  5:  G t ← G t + g , i ← i + 1  6: end while   7:  V t ( s ) ← G t / N \begin{array}{l} \text { Algorithm } 1 \text { Monte Carlo simulation to calculate MRP value function } \\ \hline 1: i \leftarrow 0, G_{t} \leftarrow 0 \\ \text { 2: while } i \neq N \text { do } \\ \text { 3: generate an episode, starting from state } s \text { and time } t \\ \text { 4: Using the generated episode, calculate return } g=\sum_{i=t}^{H-1} \gamma^{i-t} r_{i} \\ \text { 5: } G_{t} \leftarrow G_{t}+g, i \leftarrow i+1 \\ \text { 6: end while } \\ \text { 7: } V_{t}(s) \leftarrow G_{t} / N \\ \hline \end{array}  Algorithm 1 Monte Carlo simulation to calculate MRP value function 1:i0,Gt0 2: while i=N do  3: generate an episode, starting from state s and time t 4: Using the generated episode, calculate return g=i=tH1γitri 5: GtGt+g,ii+1 6: end while  7: Vt(s)Gt/N

​ 人话举例:假设我们想要计算 V ( s 4 ) V(s_{4}) V(s4),我们从 s 4 s_{4} s4开始随机产生N条轨迹,每条轨迹的步长Horizon是确定的,我们将这N条轨迹计算出的奖励相加得到 G s 4 a l l = G 1 + . . . + G N G_{s_{4}all}=G_{1}+...+G_{N} Gs4all=G1+...+GN,则 V ( s 4 ) = G s 4 a l l / N V(s_{4})=G_{s_{4}all}/N V(s4)=Gs4all/N。如下图是一个具体的例子。
第一张图是MRP结构,第二张图是价值函数举例

在这里插入图片描述
在这里插入图片描述
综上,蒙特卡罗法就是通过采样来计算现状态的价值函数。

​ 此法的依据是回报的期望 V t ( s ) = E [ G t ∣ s t = s ] = E [ R t + 1 + γ R t + 2 + γ 2 R t + 3 + … + γ T − t − 1 R T ∣ s t = s ] \begin{aligned} \mathrm{V}_{\mathrm{t}}(\mathrm{s}) &=\mathbb{E}\left[\mathrm{G}_{\mathrm{t}} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}\right] \\ &=\mathbb{E}\left[\mathrm{R}_{\mathrm{t}+1}+\gamma \mathrm{R}_{\mathrm{t}+2}+\gamma^{2} \mathrm{R}_{\mathrm{t}+3}+\ldots+\gamma^{\mathrm{T}-\mathrm{t}-1} \mathrm{R}_{\mathrm{T}} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}\right] \end{aligned} Vt(s)=E[Gtst=s]=E[Rt+1+γRt+2+γ2Rt+3++γTt1RTst=s]

2.4.2 动态规划的迭代方法

 Algorithm  2  Iterative algorithm to calculate MRP value function  1 :  for all states  s ∈ S , V ′ ( s ) ← 0 , V ( s ) ← ∞  2: while  ∥ V − V ′ ∥ > ϵ  do   3:  V ← V ′  4: For all states  s ∈ S , V ′ ( s ) = R ( s ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s ) V ( s ′ )  5: end while   6: return  V ′ ( s )  for all  s ∈ S \begin{array}{l} \text { Algorithm } 2 \text { Iterative algorithm to calculate MRP value function } \\ \hline 1: \text { for all states } s \in S, V^{\prime}(s) \leftarrow 0, V(s) \leftarrow \infty \\ \text { 2: while }\left\|V-V^{\prime}\right\|>\epsilon \text { do } \\ \text { 3: } V \leftarrow V^{\prime} \\ \text { 4: For all states } s \in S, V^{\prime}(s)=R(s)+\gamma \sum_{s^{\prime} \in S} P\left(s^{\prime} \mid s\right) V\left(s^{\prime}\right) \\ \text { 5: end while } \\ \text { 6: return } V^{\prime}(s) \text { for all } s \in S \\ \hline \end{array}  Algorithm 2 Iterative algorithm to calculate MRP value function 1: for all states sS,V(s)0,V(s) 2: while VV>ϵ do  3: VV 4: For all states sS,V(s)=R(s)+γsSP(ss)V(s) 5: end while  6: return V(s) for all sS

​ 人话解释:动态规划的方法就是,一直去迭代它的 Bellman equation,让它最后收敛,最后我们就可以得到它的一个状态。

​ 综上,动态规划法就是通过迭代来计算现状态的价值函数。

​ 此法的依据是贝尔曼等式 V ( s ) = R ( s ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s ) V ( s ′ ) V(s)=R(s)+\gamma \sum_{s^{\prime}\in S} P\left(s^{\prime} \mid s\right) V\left(s^{\prime}\right) V(s)=R(s)+γsSP(ss)V(s)

3 马尔科夫决策过程(MDP)

进入正题!!!!!!!!!!!

马尔科夫决策过程=马尔科夫奖励过程+决策

S S S是状态集合

A A A是动作集合

P P P是**状态转移概率 ** P ( S t + 1 = s ′ ∣ s t = s , a t = a ) P\left(S_{t+1}=s^{\prime} \mid s_{t}=s,a_{t}=a\right) P(St+1=sst=sat=a)

R R R奖励函数 R ( s t = s , a t = a ) = E [ r t ∣ s t = s , a t = a ] R\left(s_{t}=s,a_{t}=a\right)=\mathbb{E}\left[r_{t} \mid s_{t}=s,a_{t}=a\right] R(st=sat=a)=E[rtst=sat=a]

γ ∈ [ 0 , 1 ] \gamma \in[0,1] γ[0,1]是折扣系数

如果状态有限则 R R R是一个向量

​ **PS:**其中P多了一个动作的条件,它表明未来的状态不仅是依赖于你当前的状态,也依赖于在当前状态 agent 采取的这个动作。

​ 其中R多了一个动作的条件,它表明当前可能得到的奖励不仅是依赖于你当前的状态,也依赖于在当前状态 agent 采取的这个动作。

3.1 策略

π ( a ∣ s ) = P ( a t = a ∣ s t = s ) \pi(\mathrm{a} \mid \mathrm{s})=\mathrm{P}\left(\mathrm{a}_{\mathrm{t}}=\mathrm{a} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}\right) π(as)=P(at=ast=s)

这个策略是,在当前状态下采取行动a的可能性。

当MDP得到一个策略 π \pi π,此时就能在MDP和MRP之间进行一个转换,如下图,再次将a去掉,得到MRP。
P π ( s ′ ∣ s ) = ∑ a ∈ A π ( a ∣ s ) P ( s ′ ∣ s , a ) R π ( s ) = ∑ a ∈ A π ( a ∣ s ) R ( s , a ) \begin{aligned} P^{\pi}\left(s^{\prime} \mid s\right) &=\sum_{a \in A} \pi(a \mid s) P\left(s^{\prime} \mid s, a\right) \\ R^{\pi}(s) &=\sum_{a \in A} \pi(a \mid s) R(s, a) \end{aligned} Pπ(ss)Rπ(s)=aAπ(as)P(ss,a)=aAπ(as)R(s,a)

3.2 MP/MRP和MDP之间的对比

状态转移
在这里插入图片描述
MP/MRP的状态转移概率是由整个环境一开始就确定好了的,直接按照概率转移即可

​ MDP的状态转移多了一层动作,即当你到了状态s后,你按一定概率会采取三种动作,当采取了一个动作之后到了黑色节点,这个时候到哪个状态依然具有不确定性。就是说在当前状态和未来状态转移过程中多了一层决策性

​ **一个关键的点:**MDP对于MRP增加了一个决策过程, P ( s ′ ∣ s ) P(s'|s) P(ss)变成了 P ( s ′ ∣ s , a ) P(s'|s,a) P(ss,a) P ( s ′ ∣ s , a ) P(s'|s,a) P(ss,a)意义要搞清楚:就是说我要考虑在s状态时采取a动作的概率,之后再考虑执行了a动作之后我的状态又有多少概率变成 s ′ s^{\prime} s。也就是说不是采取了a动作,状态一定会变成 s ′ s^{\prime} s

3.3 MDP价值函数

状态价值函数 v π ( s ) = E π [ G t ∣ s t = s ] \mathrm{v}^{\pi}(s)=\mathbb{E}_{\pi}\left[G_{t} \mid s_{t}=s\right] vπ(s)=Eπ[Gtst=s],这个期望是基于策略的

动作价值函数,Q函数: q π ( s , a ) = E π [ G t ∣ s t = s , A t = a ] q^{\pi}(s, a)=\mathbb{E}_{\pi}\left[G_{t} \mid s_{t}=s, A_{t}=a\right] qπ(s,a)=Eπ[Gtst=s,At=a],在某一个状态采取一个行动,它有可能得到的回报的一个期望,这个期望是基于策略的,对于所有的动作价值函数组成的就是一个维度:状态数 X 动作数 的一个矩阵。3.10的Q-Table可以看出来。

对动作函数进行加和,就可以得到价值函数 v π ( s ) = ∑ a ∈ A π ( a ∣ s ) q π ( s , a ) − − − − − − − ( 1 ) \mathrm{v}^{\pi}(s)=\sum_{a \in A} \pi(a \mid s) q^{\pi}(s, a)-------(1) vπ(s)=aAπ(as)qπ(s,a)(1)

3.4 Q函数的贝尔曼等式

q ( s , a ) = E [ G t ∣ s t = s , a t = a ] = E [ R t + 1 + γ R t + 2 + γ 2 R t + 3 + … ∣ s t = s , a t = a ] = E [ R t + 1 ∣ s t = s , a t = a ] + γ E [ R t + 2 + γ R t + 3 + γ 2 R t + 4 + … ∣ s t = s , a t = a ] = R ( s , a ) + γ E [ G t + 1 ∣ s t = s , a t = a ] = R ( s , a ) + γ E [ V ( s t + 1 ) ∣ s t = s , a t = a ] = R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v ( s ′ ) − − − − − − ( 2 ) \begin{aligned} \mathrm{q}(\mathrm{s}, \mathrm{a}) &=\mathbb{E}\left[\mathrm{G}_{\mathrm{t}} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}, \mathrm{a}_{\mathrm{t}}=\mathrm{a}\right] \\ &=\mathbb{E}\left[\mathrm{R}_{\mathrm{t}+1}+\gamma \mathrm{R}_{\mathrm{t}+2}+\gamma^{2} \mathrm{R}_{\mathrm{t}+3}+\ldots \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}, \mathrm{a}_{\mathrm{t}}=\mathrm{a}\right] \\ &=\mathbb{E}\left[\mathrm{R}_{\mathrm{t}+1} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}, \mathrm{a}_{\mathrm{t}}=\mathrm{a}\right]+\gamma \mathbb{E}\left[\mathrm{R}_{\mathrm{t}+2}+\gamma \mathrm{R}_{\mathrm{t}+3}+\gamma^{2} \mathrm{R}_{\mathrm{t}+4}+\ldots \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}, \mathrm{a}_{\mathrm{t}}=\mathrm{a}\right] \\ &=\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \mathbb{E}\left[\mathrm{G}_{\mathrm{t}+1} \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}, \mathrm{a}_{\mathrm{t}}=\mathrm{a}\right] \\ &=\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \mathbb{E}\left[\mathrm{V}\left(\mathrm{s}_{\mathrm{t}+1}\right) \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}, \mathrm{a}_{\mathrm{t}}=\mathrm{a}\right] \\ &=\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}\left(\mathrm{s}^{\prime}\right)------(2) \end{aligned} q(s,a)=E[Gtst=s,at=a]=E[Rt+1+γRt+2+γ2Rt+3+st=s,at=a]=E[Rt+1st=s,at=a]+γE[Rt+2+γRt+3+γ2Rt+4+st=s,at=a]=R(s,a)+γE[Gt+1st=s,at=a]=R(s,a)+γE[V(st+1)st=s,at=a]=R(s,a)+γsSP(ss,a)v(s)(2)

3.5 贝尔曼期望等式(Bellman Expectation Equation)

v π ( s ) = E π [ R t + 1 + γ v π ( s t + 1 ) ∣ s t = s ] \mathrm{v}^{\pi}(\mathrm{s})=\mathrm{E}_{\pi}\left[\mathrm{R}_{\mathrm{t}+1}+\gamma \mathrm{v}^{\pi}\left(\mathrm{s}_{\mathrm{t}+1}\right) \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}\right] vπ(s)=Eπ[Rt+1+γvπ(st+1)st=s]

q π ( s , a ) = E π [ R t + 1 + γ q π ( s t + 1 ,   A t + 1 ) ∣ s t = s , A t = a ] \mathrm{q}^{\pi}(\mathrm{s}, \mathrm{a})=\mathrm{E}_{\pi}\left[\mathrm{R}_{\mathrm{t}+1}+\gamma \mathrm{q}^{\pi}\left(\mathrm{s}_{\mathrm{t}+1}, \mathrm{~A}_{\mathrm{t}+1}\right) \mid \mathrm{s}_{\mathrm{t}}=\mathrm{s}, \mathrm{A}_{\mathrm{t}}=\mathrm{a}\right] qπ(s,a)=Eπ[Rt+1+γqπ(st+1, At+1)st=s,At=a]

上面两个式子定义了当前状态跟未来状态之间的一个关联。

把(2)带入(1)中得:
v π ( s ) = ∑ a ∈ A π ( a ∣ s ) ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v π ( s ′ ) ) \mathrm{v}^{\pi}(\mathrm{s})=\sum_{\mathrm{a} \in \mathrm{A}} \pi(\mathrm{a} \mid \mathrm{s})\left(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}^{\pi}\left(\mathrm{s}^{\prime}\right)\right) vπ(s)=aAπ(as)(R(s,a)+γsSP(ss,a)vπ(s))

把(1)带入(2)中得:
q π ( s , a ) = R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) ∑ a ′ ∈ A π ( a ′ ∣ s ′ ) q π ( s ′ , a ′ ) \mathrm{q}^{\pi}(\mathrm{s}, \mathrm{a})=\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \sum_{\mathrm{a}^{\prime} \in \mathrm{A}} \pi\left(\mathrm{a}^{\prime} \mid \mathrm{s}^{\prime}\right) \mathrm{q}^{\pi}\left(\mathrm{s}^{\prime}, \mathrm{a}^{\prime}\right) qπ(s,a)=R(s,a)+γsSP(ss,a)aAπ(as)qπ(s,a)
这两个等式代表了当前时刻的值和未来时刻的值之间的一个关联。

3.6 备份(backup)图

在这里插入图片描述

每一个空心圆圈代表一个状态,每一个实心圆圈代表一个状态-动作对。

​ Backup 类似于 bootstrapping 之间这个迭代关系,就对于某一个状态,它的当前价值是跟它的未来价值线性相关的。
在这里插入图片描述

3.7 策略评估

预测你当前采取的这个策略最终会产生多少的价值。

方法:进行迭代评估

将下面第一个公式改写为第二个公式,然后用动态规划对第二个公式进行迭代,最后收敛到一个值,就是我们当前状态的价值。
v π ( s ) = ∑ a ∈ A π ( a ∣ s ) ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v π ( s ′ ) ) \mathrm{v}^{\pi}(\mathrm{s})=\sum_{\mathrm{a} \in \mathrm{A}} \pi(\mathrm{a} \mid \mathrm{s})\left(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}^{\pi}\left(\mathrm{s}^{\prime}\right)\right) vπ(s)=aAπ(as)(R(s,a)+γsSP(ss,a)vπ(s))

v k π ( s ) = ∑ a ∈ A π ( a ∣ s ) ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v k + 1 π ( s ′ ) ) \mathrm{v}_{k}^{\pi}(\mathrm{s})=\sum_{\mathrm{a} \in \mathrm{A}} \pi(\mathrm{a} \mid \mathrm{s})\left(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}_{k+1}^{\pi}\left(\mathrm{s}^{\prime}\right)\right) vkπ(s)=aAπ(as)(R(s,a)+γsSP(ss,a)vk+1π(s))

3.8 预测和控制

预测给定一个 policy,我们要确定它的 value function 是多少。
∘ \circ 输入: M D P < S , A , P , R , γ > \mathrm{MDP}<\mathrm{S}, \mathrm{A}, \mathrm{P}, \mathrm{R}, \gamma> MDP<S,A,P,R,γ> 和 policy π \pi π 或者 M R P < S , P π , R π , γ > \mathrm{MRP}<\mathrm{S}, \mathrm{P}^{\pi}, \mathrm{R}^{\pi}, \gamma> MRP<S,Pπ,Rπ,γ>
∘ \circ 输出: value function v π \mathrm{v}^{\pi} vπ
∘ \circ Prediction 是说给定一个 MDP 以及一个 policy π , \pi, π, 去计算它的 value function。就是对于每个状态, 它的价值函数是多少。

控制:在没有 policy 的前提下,我们要确定最优的 value function 以及对应的决策方案。
∘ \circ 输入: M D P < S , A , P , R , γ > \mathrm{MDP}<\mathrm{S}, \mathrm{A}, \mathrm{P}, \mathrm{R}, \gamma> MDP<S,A,P,R,γ>
∘ \circ 输出:最佳价值函数(optimal value function) v ∗ \mathrm{v}^{*} v 和最佳策略(optimal policy) π ∗ . \pi^{*} . π.
∘ \circ Control 就是说我们去寻找一个最佳的策略, 然后同时输出它的最佳价值函数以及最佳策略。

两者都可以通过动态规划来解决。

3.9 动态规划

​ 动态规划应用于 MDP 的规划问题(planning)而不是学习问题(learning),我们必须对环境完全已知的,状态转移概率和对应的 reward 都必须要知道。

3.10 MDP预测(策略估值)

这里可以参看3.7的动态规划,对下式进行迭代计算,给定初始值 v 1 π \mathrm{v}_{1}^{\pi} v1π, v 2 π \mathrm{v}_{2}^{\pi} v2π, 一直计算,直到收敛: v 1 π → . . . → v π \mathrm{v}_{1}^{\pi}\rightarrow...\rightarrow\mathrm{v}^{\pi} v1π...vπ
v k π ( s ) = ∑ a ∈ A π ( a ∣ s ) ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v k + 1 π ( s ′ ) ) \mathrm{v}_{k}^{\pi}(\mathrm{s})=\sum_{\mathrm{a} \in \mathrm{A}} \pi(\mathrm{a} \mid \mathrm{s})\left(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}_{k+1}^{\pi}\left(\mathrm{s}^{\prime}\right)\right) vkπ(s)=aAπ(as)(R(s,a)+γsSP(ss,a)vk+1π(s))
例子:

在这里插入图片描述

  1. 此游戏从某一个状态开始,然后到达终点状态。终止状态就是左上角跟右上角

  2. 策略就是随机的,上下左右都是0.5概率。在边缘状态的时候,比如说在第四号状态的时候,它往左走的话,它是依然存在第四号状态。

  3. 奖励函数就是说你每走一步,就会得到 -1 的奖励,所以需要尽快地到达终止状态

  4. 状态之间的转移也是确定的。比如从第六号状态往上走,它就会直接到达第二号状 P ( s 2 ∣ s 6 ) = P ( s 2 ∣ s 6 , a u p ) = 1 \mathrm{P}\left(\mathrm{s}_{2} \mid \mathrm{s}_{6}\right)=\mathrm{P}\left(\mathrm{s}_{2} \mid \mathrm{s}_{6}, \mathrm{a}_{up}\right)=1 P(s2s6)=P(s2s6,aup)=1很多时候有些环境是 probabilistic 的话,就是说 agent 在第六号状态,它选择往上走的时候,有可能地板是滑的,然后它可能滑到第三号状态或者第一号状态,这就是有概率的一个转移。但这里把这个环境进行了简化,从六号往上走,它就到了二号。

3.11 MDP控制(最优策略)

思路:

  1. 在所有的策略 π \pi π中,搜索一个策略使得每个状态的价值最大:

v ∗ (   s ) = max ⁡ π v π ( s ) \mathrm{v}^{*}(\mathrm{~s})=\max _{\pi} \mathrm{v}^{\pi}(\mathrm{s}) v( s)=πmaxvπ(s)

在这种极大化情况下, 我们就得到了最佳的的策略就(optimal policy),如下式所示:
π ∗ (   s ) = arg ⁡ max ⁡ π v π ( s ) \pi^{*}(\mathrm{~s})=\underset{\pi}{\arg \max } \mathrm{v}^{\pi}(\mathrm{s}) π( s)=πargmaxvπ(s)

  1. 当取得最佳的价值函数过后,根据3.4的(2)我们可以得到Q函数,我们可以通过对这个 Q 函数进行极大化,然后得到最佳策略。(当所有东西都收敛过后,因为 Q 函数是关于状态跟动作的一个函数,所以在某一个状态采取一个动作,可以使得这个 Q 函数最大化,那么这个动作就应该是最佳的动作。)

π ∗ ( a ∣ s ) = { 1 ,  if  a = arg ⁡ max ⁡ a ∈ A q ∗ ( s , a ) 0 ,  otherwise  \pi^{*}(a \mid s)=\left\{\begin{array}{ll} 1, & \text { if } a=\arg \max _{a \in A} q^{*}(s, a) \\ 0, & \text { otherwise } \end{array}\right. π(as)={1,0, if a=argmaxaAq(s,a) otherwise 

对于一个事先定好的 MDP 过程,当 agent 去采取最佳策略的时候

最佳策略一般是确定的,不随时间变化,但是不一定是惟一的,因为多种动作可能会取得相同的这个价值。

搜索最佳策略policy iteration 和 value iteration

参考公式:3.4的(2)->由v求得q

1 policy iteration

初始化一个 π \pi π

第一个步骤 policy evaluation:通过当前的 π \pi π来估算一个 V V V

第二个步骤是 policy improvement:估算出 V V V可以进一步推算出它的 Q Q Q 函数。在 Q Q Q 函数上做一个贪心的搜索(即极大化 arg max 操作)来得到一个更好的或者不坏的策略 π \pi π。将得到的新的策略取代旧的策略,返回到第一步,一直迭代到收敛。
在这里插入图片描述

在这里插入图片描述
**PS:**当改进停止后即策略收敛到最佳,最大此时的动作价值函数等于此时的状态价值函数:
在这里插入图片描述

贝尔曼最优等式(Bellman optimality equation)

因此有贝尔曼最优等式Bellman optimality equation:意义就是最佳策略下的一个状态的价值必须等于在这个状态下采取最好动作得到的回报的期望。

v π ( s ) = max ⁡ a ∈ A q π ( s , a ) \mathrm{v}^{\pi}(\mathrm{s})=\max _{\mathrm{a} \in \mathcal{A}} \mathrm{q}^{\pi}(\mathrm{s}, \mathrm{a}) vπ(s)=maxaAqπ(s,a)

前提:只有当整个状态已经收敛过后,得到一个最佳的 policy 的时候才满足贝尔曼最优等式,就是说上面这个式子中 π \pi π就是一个最优策略。

2 value iteration

我们从另一个角度思考问题,动态规划的方法将优化问题分成两个部分:

  • 我第一步执行的是最优的 action;
  • 我之后后继的状态每一步都按照最优的 policy 去做,那么我最后的结果就是最优的

所以将贝尔曼最优等式Bellman optimality equation作为一个更新准则:这个等式只有当整个 MDP 已经到达最佳的状态时才满足,但是只要我们一直迭代Bellman Optimality Equation,到了最后,它就能能逐渐趋向于最佳的策略,这是 value iteration 算法的精髓。value iteration就不停地迭代下面这个Bellman Optimality Backup。

v π ( s ) = max ⁡ a ∈ A q π ( s , a ) = max ⁡ a ∈ A ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v ( s ′ ) ) \mathrm{v}^{\pi}(\mathrm{s})=\max _{\mathrm{a} \in \mathcal{A}} \mathrm{q}^{\pi}(\mathrm{s}, \mathrm{a})=\max _{\mathrm{a} \in \mathcal{A}}(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}\left(\mathrm{s}^{\prime}\right)) vπ(s)=maxaAqπ(s,a)=maxaA(R(s,a)+γsSP(ss,a)v(s))

在这里插入图片描述
上图算法解析:

  1. 首先初始化
  2. 对每个状态进行Bellman Optimality Backup迭代,最后得到最优的策略 v k + 1 ( s ′ ) \mathrm v_{k+1}(s^{\prime}) vk+1(s)
  3. 然后根据V重构出Q,再求出最优策略,这和1 policy iteration的第二个步骤一样了。

4 MDP控制和预测问题总结

在这里插入图片描述
Bellman Equation: V ( s ) = R ( S ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s ) V ( s ′ ) V(s)=R(S)+ \gamma \sum_{s' \in S}P(s'|s)V(s') V(s)=R(S)+γsSP(ss)V(s)

Bellman Expectation Equation: v π ( s ) = ∑ a ∈ A π ( a ∣ s ) ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v π ( s ′ ) ) \mathrm{v}^{\pi}(\mathrm{s})=\sum_{\mathrm{a} \in \mathrm{A}} \pi(\mathrm{a} \mid \mathrm{s})\left(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}^{\pi}\left(\mathrm{s}^{\prime}\right)\right) vπ(s)=aAπ(as)(R(s,a)+γsSP(ss,a)vπ(s))

Bellman optimality Equation: v π ( s ) = max ⁡ a ∈ A q π ( s , a ) = max ⁡ a ∈ A ( R ( s , a ) + γ ∑ s ′ ∈ S P ( s ′ ∣ s , a ) v ( s ′ ) ) \mathrm{v}^{\pi}(\mathrm{s})=\max _{\mathrm{a} \in \mathcal{A}} \mathrm{q}^{\pi}(\mathrm{s}, \mathrm{a})=\max _{\mathrm{a} \in \mathcal{A}}(\mathrm{R}(\mathrm{s}, \mathrm{a})+\gamma \sum_{\mathrm{s}^{\prime} \in \mathrm{S}} \mathrm{P}\left(\mathrm{s}^{\prime} \mid \mathrm{s}, \mathrm{a}\right) \mathrm{v}\left(\mathrm{s}^{\prime}\right)) vπ(s)=maxaAqπ(s,a)=maxaA(R(s,a)+γsSP(ss,a)v(s))

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值