2021-08-16 强化学习(第2版)-Reinforcement Learning 第四章 动态规划(DP)

第四章 动态规划 Dynamic Programming(DP)

讨论在马尔科夫假设和贝尔曼方程的基础上讨论使用动态规划(Dynamic Programming, DP)来求解强化学习的问题。

引入. 动态规划和强化学习问题的联系

动态规划的关键点有两个:一是问题的最优解可以由若干小问题的最优解构成,即通过寻找子问题的最优解来得到问题的最优解。第二是可以找到子问题状态之间的递推关系,通过较小的子问题状态递推出较大的子问题的状态。而强化学习的问题恰好是满足这两个条件的。

我们先看看强化学习的两个基本问题。

  • 第一个问题是预测,即给定强化学习的6个要素:状态集S, 动作集A, 模型状态转化概率矩阵P, 即时奖励R,衰减因子γ, 给定策略π, 求解该策略的状态价值函数v(π)
  • 第二个问题是控制,也就是求解最优的价值函数和策略。给定强化学习的5个要素:状态集S, 动作集A, 模型状态转化 概率矩阵P, 即时奖励R,衰减因子γ, 求解最优的状态价值函数v∗和最优策略π∗

那么如何找到动态规划和强化学习这两个问题的关系呢?
回忆在马尔科夫决策过程(MDP)中状态价值函数的贝尔曼方程:

  • The Bellman equations express the value of a state in terms of the value of its successor states. They are a consistency condition on the value of states.
    v π ( s ) = E π [ G t ∣ S t = s ] = E π [ R t + 1 + γ G t + 1 ∣ S t = s ] = ∑ a ∈ A ( s ) π ( a ∣ s ) ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ E π [ G t + 1 ∣ S t + 1 = s ′ ] ] = ∑ a ∈ A ( s ) π ( a ∣ s ) ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ v π ( s ′ ) ] \newcommand\Epi{\mathbb{E}_{\pi}} \begin{aligned} v_{\pi}(s) &= \Epi{}[G_t | S_t = s] \\ &= \Epi{}[R_{t+1} + \gamma G_{t+1} | S_t = s] \\ &= \sum_{a \in \mathcal{A}(s)} \pi(a|s) \sum_{s', r} p(s', r | s, a) \left[r + \gamma \Epi{}[G_{t+1} | S_{t+1} = s']\right] \\ &= \sum_{a \in \mathcal{A}(s)} \pi(a|s) \sum_{s', r} p(s', r | s, a) [r + \gamma v_{\pi}(s')] \end{aligned} vπ(s)=Eπ[GtSt=s]=Eπ[Rt+1+γGt+1St=s]=aA(s)π(as)s,rp(s,rs,a)[r+γEπ[Gt+1St+1=s]]=aA(s)π(as)s,rp(s,rs,a)[r+γvπ(s)]
    The value function v π v_\pi vπ is the unique solution to its Bellman equation.

从这个式子我们可以看出,我们可以定义出子问题求解每个状态的状态价值函数,同时这个式子又是一个递推的式子, 意味着利用它,我们可以使用上一个迭代周期内的状态价值来计算更新当前迭代周期某状态s的状态价值。可见,使用动态规划来求解强化学习问题是比较自然的。

The term Dynamic Programming (DP) refers to a collection of algorithms that can be used to compute optimal policies given perfect model of the environment as a Markov Decision Process (MDP). DP methods tend to be computationally expensive and we often don’t have a perfect model of the environment, so they aren’t used in practice. However, they provide useful theoretical basis for the rest of reinforcement learning.

Unless stated otherwise, will assume that the environment is a finite MDP. If the state or action space is continuous, then we will generally discretise it and apply finite MDP methods to the approximated problem.

The key idea of DP, and of reinforcement learning generally, is the use of value functions to organize and structure the search for good policies. We use DP and the Bellman equations to find optimal value functions.

一 . 策略评估(预测问题)Policy Evaluation (Prediction)

首先,我们来看如何使用动态规划来求解强化学习的预测问题,即求解给定策略的状态价值函数的问题。这个问题的求解过程我们通常叫做策略评估(Policy Evaluation)。

  • 策略评估的基本思路是从任意一个状态价值函数开始,依据给定的策略,结合贝尔曼期望方程、状态转移概率和奖励同步迭代更新状态价值函数,直至其收敛,得到该策略下最终的状态价值函数。
  • 假设我们在第k轮迭代已经计算出了所有的状态的状态价值,那么在第k+1轮我们可以利用第k轮计算出的状态价值计算出第k+1轮的状态价值。
  • We can use the Bellman equation for the state-value function v π v_\pi vπ to construct an iterative updating procedure.

Iterative Policy Evaluation

  • Consider a sequence of approximate value functions v 0 , v 1 , v 2 , … v_0, v_1, v_2, \dots v0,v1,v2, each mapping S + \mathcal{S}^{+} S+ to R \mathbb{R} R. The initial approximation, v 0 v_0 v0, is chosen arbitrarily (except that the terminal state, if any, must be given value 0 0 0), and each successive approximation is obtained by using the Bellman equation for v π v_\pi vπ as an update rule:
    v k + 1 ≐ E π [ R t + 1 + γ v k ( S t + 1 ) ∣ S t = s ] = ∑ a π ( s ∣ a ) ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ v k ( s ′ ) ] \newcommand\Epi{\mathbb{E}_{\pi}} \begin{aligned} v_{k+1} &\doteq \Epi [R_{t+1} + \gamma v_{k}(S_{t+1}) | S_t = s] \\ &= \sum_a \pi(s|a) \sum_{s', r} p(s', r| s, a) \left[r + \gamma v_k(s')\right] \end{aligned} vk+1Eπ[Rt+1+γvk(St+1)St=s]=aπ(sa)s,rp(s,rs,a)[r+γvk(s)]
    Clearly, v k = v π v_k = v_\pi vk=vπ is a fixed point. The sequence { v k } \{v_k\} {vk} can be shown in general to converge to v π v_\pi vπ as k → ∞ k \to \infty k under the same conditions that guarantee the existence of v π v_\pi vπ. This algorithm is called iterative policy evaluation. This update rule is an instance of an expected update because it performs the updates by taking an expectation over all possible next states rather than by taking a sample next state.

和上一节的式子唯一的区别是由于我们的策略π已经给定,我们不再写出,对应加上了迭代轮数的下标。我们每一轮可以对计算得到的新的状态价值函数再次进行迭代,直至状态价值的值改变很小(收敛),那么我们就得出了预测问题的解,即给定策略的状态价值函数v(π)。

下面我们用一个具体的例子来说明策略评估的过程。

策略评估求解实例

这是一个经典的Grid World的例子。我们有一个4x4的16宫格。只有左上和右下的格子是终止格子。该位置的价值固定为0,个体如果到达了该2个格子,则停止移动,此后每轮奖励都是0。个体在16宫格其他格的每次移动,得到的即时奖励R都是-1。注意个体每次只能移动一个格子,且只能上下左右4种移动选择,不能斜着走, 如果在边界格往外走,则会直接移动回到之前的边界格。衰减因子我们定义为 γ=1。由于这里每次移动,下一格都是固定的,因此所有可行的的状态转化概率P=1。这里给定的策略是随机策略,即每个格子里有25%的概率向周围的4个格子移动。
在这里插入图片描述

首先我们初始化所有格子的状态价值为0,如上图k=0的时候。现在我们开始策略迭代了。由于终止格子的价值固定为0,我们可以不将其加入迭代过程。在k=1的时候,我们利用上面的贝尔曼方程先计算第二行第一个格子的价值:
v 1 ( 21 ) = 1 4 [ ( − 1 + 0 ) + ( − 1 + 0 ) + ( − 1 + 0 ) + ( − 1 + 0 ) ] = − 1 v_1^{(21)} = \frac{1}{4}[(-1+0) +(-1+0)+(-1+0)+(-1+0)] = -1 v1(21)=41[(1+0)+(1+0)+(1+0)+(1+0)]=1
第二行第二个格子的价值是: v 1 ( 22 ) = 1 4 [ ( − 1 + 0 ) + ( − 1 + 0 ) + ( − 1 + 0 ) + ( − 1 + 0 ) ] = − 1 v_1^{(22)} = \frac{1}{4}[(-1+0) +(-1+0)+(-1+0)+(-1+0)] = -1 v1(22)=41[(1+0)+(1+0)+(1+0)+(1+0)]=1
其他的格子都是类似的,第一轮的状态价值迭代的结果如上图k=1的时候。现在我们第一轮迭代完了。开始动态规划迭代第二轮了。还是看第二行第一个格子的价值: v 2 ( 21 ) = 1 4 [ ( − 1 + 0 ) + ( − 1 − 1 ) + ( − 1 − 1 ) + ( − 1 − 1 ) ] = − 1.75 v_2^{(21)} = \frac{1}{4}[(-1+0) +(-1-1)+(-1-1)+(-1-1)] = -1.75 v2(21)=41[(1+0)+(11)+(11)+(11)]=1.75
第二行第二个格子的价值是: v 2 ( 22 ) = 1 4 [ ( − 1 − 1 ) + ( − 1 − 1 ) + ( − 1 − 1 ) + ( − 1 − 1 ) ] = − 2 v_2^{(22)} = \frac{1}{4}[(-1-1) +(-1-1)+(-1-1)+(-1-1)] = -2 v2(22)=41[(11)+(11)+(11)+(11)]=2
最终得到的结果是上图k=2的时候。第三轮的迭代如下: v 3 ( 21 ) = 1 4 [ ( − 1 − 1.7 ) + ( − 1 − 2 ) + ( − 1 − 2 ) + ( − 1 + 0 ) ] = − 2.425 v_3^{(21)} = \frac{1}{4}[(-1-1.7) +(-1-2)+(-1-2)+(-1+0)] = -2.425 v3(21)=41[(11.7)+(12)+(12)+(1+0)]=2.425 v 3 ( 22 ) = 1 4 [ ( − 1 − 1.7 ) + ( − 1 − 1.7 ) + ( − 1 − 2 ) + ( − 1 − 2 ) ] = − 2.85 v_3^{(22)} = \frac{1}{4}[(-1-1.7) +(-1-1.7)+(-1-2)+(-1-2)] = -2.85 v3(22)=41[(11.7)+(11.7)+(12)+(12)]=2.85

最终得到的结果是上图k=3的时候。就这样一直迭代下去,直到每个格子的策略价值改变很小为止。这时我们就得到了所有格子的基于随机策略的状态价值。可以看到,动态规划的策略评估计算过程并不复杂,但是如果我们的问题是一个非常复杂的模型的话,这个计算量还是非常大的。

二. 策略改进 Policy Improvement

Policy Improvement Theorem

Let π \pi π, π ′ \pi' π be any pair of deterministic policies, such that
q π ( s , π ′ ( s ) ) ≥ v π ( s ) ∀ s ∈ S . q_\pi(s, \pi'(s)) \geq v_\pi(s) \quad \forall s \in \mathcal{S}. qπ(s,π(s))vπ(s)sS.
That is, π ′ \pi' π is as least as good as π \pi π. Then we have (shown below)
v π ′ ( s ) ≥ v π ( s ) ∀ s ∈ S v_{\pi'}(s) \geq v_\pi(s) \quad \forall s \in \mathcal{S} vπ(s)vπ(s)sS

so π ′ \pi' π gives at least as good (expected) return as π \pi π.

The argument below also shows that if q π ( s , π ′ ( s ) ) > v π ( s ) q_\pi(s, \pi'(s)) > v_\pi(s) qπ(s,π(s))>vπ(s) at any s s s, then there is at least one s s s for which v π ′ ( s ) > v π ( s ) v_{\pi'}(s) > v_\pi(s) vπ(s)>vπ(s).

proof:

v π ( s ) ≤ q π ( s , π ′ ( s ) ) = E [ R t + 1 + γ v π ( S t + 1 ) ∣ S t = s , A t = π ′ ( s ) ] = E π ′ [ R t + 1 + γ v π ( S t + 1 ) ∣ S t = s ] ≤ E π ′ [ R t + 1 + γ R t + 2 + γ 2 R t + 3 + … ∣ S t = s ] = v π ′ ( s ) \newcommand\Epi{\mathbb{E}_{\pi}} \newcommand\E{\mathbb{E}} \begin{aligned} v_\pi(s) & \leq q_\pi(s, \pi'(s)) \\ & = \E{}[R_{t+1} + \gamma v_\pi(S_{t+1}) | S_t=s, A_t=\pi'(s)] \\ & = \E{}_{\pi'} [R_{t+1} + \gamma v_\pi(S_{t+1}) | S_t=s] \\ & \leq \E{}_{\pi'} [R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots| S_t=s] \\ & = v_{\pi'}(s) \end{aligned} vπ(s)qπ(s,π(s))=E[Rt+1+γvπ(St+1)St=s,At=π(s)]=Eπ[Rt+1+γvπ(St+1)St=s]Eπ[Rt+1+γRt+2+γ2Rt+3+St=s]=vπ(s)

Policy Improvement Algorithm

Now consider a policy that is greedy with respect to q π ( s , a ) q_\pi(s, a) qπ(s,a). Define
π ′ ( s ) = arg max ⁡ a q π ( s , a ) = arg max ⁡ a E [ R t + 1 + γ v π ( S t + 1 ) ∣ S t = s , A t = a ] = arg max ⁡ a ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ v π ( s ′ ) ] . \newcommand\Epi{\mathbb{E}_{\pi}} \newcommand\E{\mathbb{E}} \begin{aligned} \pi'(s) &= \argmax_a q_\pi(s, a) \\ &= \argmax_a \E{} [R_{t+1} + \gamma v_\pi(S_{t+1}) | S_t=s, A_t=a] \\ &= \argmax_a \sum_{s', r} p(s', r|s, a)[ r + \gamma v_\pi(s')]. \end{aligned} π(s)=aargmaxqπ(s,a)=aargmaxE[Rt+1+γvπ(St+1)St=s,At=a]=aargmaxs,rp(s,rs,a)[r+γvπ(s)].
Now we can use v π v_\pi vπ to get π ′ ≥ π \pi' \geq \pi ππ, then use v π ′ v_{\pi'} vπ to get another policy. (In the above, ties are broken arbitrarily when the policy is deterministic. If the policy is stochastic, we accept any policy that assigns zero probability to sub-optimal actions.)

Note that by construction
q π ( s , π ′ ( s ) ) ≥ v π ( s ) q_\pi(s, \pi'(s)) \geq v_\pi(s) qπ(s,π(s))vπ(s)
therefore
v π ′ ≥ v π v_{\pi'} \geq v_\pi vπvπ
so we get from this process a monotonically increasing sequence of policies.Note also that if π ′ \pi' π is as good as π \pi π then v π ′ = v π v_{\pi'} = v_\pi vπ=vπ and ∀ s ∈ S \forall s \in \mathcal{S} sS
v π = max ⁡ a E [ R t + 1 + γ v π ′ ( S t + 1 ) ∣ S t = s , A t = a ] = max ⁡ a ∑ s ′ , r p ( s ′ , r ∣ s , a ) ( r + γ v π ′ ( s ′ ) ) \begin{aligned} \newcommand\Epi{\mathbb{E}_{\pi}} \newcommand\E{\mathbb{E}} v_\pi &= \max_a \E{}[R_{t+1} + \gamma v_{\pi'(S_{t+1})}| S_t=s, A_t=a]\\ &= \max_a \sum_{s', r} p(s', r|s, a)(r + \gamma v_{\pi'}(s')) \end{aligned} vπ=amaxE[Rt+1+γvπ(St+1)St=s,At=a]=amaxs,rp(s,rs,a)(r+γvπ(s))
which is the Bellman optimality condition for v ∗ v_* v, so both π \pi π and π ′ \pi' π are optimal. This means that policy improvement gives a strictly better policy unless the policy is already optimal.
The policy improvement theorem holds for stochastic policies too, but we don’t go into that here.

三. 策略迭代

上面我们讲了使用策略评估求解预测问题,现在我们再来看如何使用动态规划求解强化学习的第二个问题控制问题。一种可行的方法就是根据我们之前基于任意一个给定策略评估得到的状态价值来及时调整我们的动作策略,这个方法我们叫做策略迭代(Policy Iteration)。

  • 如何调整呢?最简单的方法就是贪婪法。考虑一种如下的贪婪策略:个体在某个状态下选择的行为是其能够到达后续所有可能的状态中状态价值最大的那个状态。还是以第三节的例子为例,如上面的图右边。当我们计算出最终的状态价值后,我们发现,第二行第一个格子周围的价值分别是0,-18,-20,此时我们用贪婪法,则我们调整行动策略为向状态价值为0的方向移动,而不是随机移动。也就是图中箭头向上。而此时第二行第二个格子周围的价值分别是-14,-14,-20, -20。那么我们整行动策略为向状态价值为-14的方向移动,也就是图中的向左向上。
    如果用一副图来表示策略迭代的过程的话,如下图:

在这里插入图片描述
在策略迭代过程中,我们循环进行两部分工作,第一步是使用当前策略π∗评估计算当前策略的最终状态价值v∗,第二步是根据状态价值v∗根据一定的方法(比如贪婪法)更新策略π∗,接着回到第一步,一直迭代下去,最终得到收敛的策略π∗和状态价值v∗。
在这里插入图片描述A finite MDP has only a finite number of policies (as long as they are deterministic, of course) so this process is guaranteed to converge.

四. 价值迭代

观察第三节的图发现,我们如果用贪婪法调整动作策略,那么当k=3的时候,我们就已经得到了最优的动作策略。而不用一直迭代到状态价值收敛才去调整策略。那么此时我们的策略迭代优化为价值迭代。

还是以第三节的例子为例,如上面的图右边。比如当k=2时,第二行第一个格子周围的价值分别是0,-2,-2,此时我们用贪婪法,则我们调整行动策略为向状态价值为0的方向移动,而不是随机移动。也就是图中箭头向上。而此时第二行第二个格子周围的价值分别是-1.7,-1.7,-2, -2。那么我们整行动策略为向状态价值为-1.7的方向移动,也就是图中的向左向上。

和上一节相比,我们没有等到状态价值收敛才调整策略,而是随着状态价值的迭代及时调整策略, 这样可以大大减少迭代次数。此时我们的状态价值的更新方法也和策略迭代不同。现在的贝尔曼方程迭代式子如下:
     v k + 1 = max ⁡ a ( r + γ ∑ s ′ , r p ( s ′ , r ∣ s , a ) v k ( s ′ ) ) v_{k+1}= \max_{a}(r + \gamma \sum_{s', r} p(s', r| s, a) v_k(s')) vk+1=amax(r+γs,rp(s,rs,a)vk(s))
 可见由于策略调整,我们现在价值每次更新倾向于贪婪法选择的最优策略对应的后续状态价值,这样收敛更快。
 
Policy iteration can be slow because each iteration involves running the entire policy evaluation until convergence.

It turns out that one can truncate the policy evaluation step of policy iteration in many ways without losing convergence guarantees. One special case of this is value iteration, where we truncate policy evaluation after only one update of each state. This algorithm converges to v ∗ v_* v under the same conditions that guarantee the existence of v ∗ v_* v.
在这里插入图片描述
Note the max ⁡ a \max_a maxa in the assignment of V ( s ) V(s) V(s), since we only one sweep of the state space and then choose the greedy policy.

It may be more efficient to interpose multiple policy evaluation steps in between policy improvement iterations, all of these algorithms converge to an optimal policy for discounted finite MDPs.

五. 异步动态规划 Asynchronous Dynamic Programming

在前几节我们讲的都是同步动态规划算法,即每轮迭代我会计算出所有的状态价值并保存起来,在下一轮中,我们使用这些保存起来的状态价值来计算新一轮的状态价值。

另一种动态规划求解是异步动态规划算法,在这些算法里,每一次迭代并不对所有状态的价值进行更新,而是依据一定的原则有选择性的更新部分状态的价值,这类算法有自己的一些独特优势,当然有额会有一些额外的代价。

常见的异步动态规划算法有三种:

  • 第一种是原位动态规划 (in-place dynamic programming), 此时我们不会另外保存一份上一轮计算出的状态价值。而是即时计算即时更新。这样可以减少保存的状态价值的数量,节约内存。代价是收敛速度可能稍慢。

  • 第二种是优先级动态规划 (prioritised sweeping):该算法对每一个状态进行优先级分级,优先级越高的状态其状态价值优先得到更新。通常使用贝尔曼误差来评估状态的优先级,贝尔曼误差即新状态价值与前次计算得到的状态价值差的绝对值。这样可以加快收敛速度,代价是需要维护一个优先级队列。

  • 第三种是实时动态规划 (real-time dynamic programming):实时动态规划直接使用个体与环境交互产生的实际经历来更新状态价值,对于那些个体实际经历过的状态进行价值更新。这样个体经常访问过的状态将得到较高频次的价值更新,而与个体关系不密切、个体较少访问到的状态其价值得到更新的机会就较少。收敛速度可能稍慢。

  • The DP methods that we have described so far all involve a full sweep of the state space on each iteration. This is potentially a very costly procedure.

Asynchronous DP algorithms update the values in-place and cover states in any order whatsoever. The values of some states may be updated several times before the values of others are updated once. To converge correctly, however, an asynchronous algorithm must continue to update the values of all the states: it can’t ignore any state after some point in the computation.

Asynchronous DPs give a great increase in flexibility, meaning that we can choose the updates we want to make (even stochastically) based on the interaction of the agent with the environment. This procedure might not reduce computation time in total if the algorithm is run to convergence, but it could allow for a better rate of progress for the agent.

六. 广义策略迭代 Generalised Policy Iteration

We use the term generalised policy iteration(GPI) to refer to the general idea of letting policy evaluation and policy improvement processes interact, independent of the granularity and other details of the two processes. Almost all reinforcement learning methods are well described as GPI, including the policy iteration algorithms we have discussed in this section. GPI works via the competing but complementary nature of the two processes. In some cases it can be guaranteed to converge.

七. 动态规划的效率 Efficiency of Dynamic Programming

If we ignore a few technical details, then the (worst case) time DP methods take to find an optimal policy is polynomial in the number of states and actions. Compare this to the searching the states directly, which is exponential.

八. 动态规划求解强化学习问题小结

动态规划是我们讲到的第一个系统求解强化学习预测和控制问题的方法。它的算法思路比较简单,主要就是利用贝尔曼方程来迭代更新状态价值,用贪婪法之类的方法迭代更新最优策略。

动态规划算法使用全宽度(full-width)的回溯机制来进行状态价值的更新,也就是说,无论是同步还是异步动态规划,在每一次回溯更新某一个状态的价值时,都要回溯到该状态的所有可能的后续状态,并利用贝尔曼方程更新该状态的价值。这种全宽度的价值更新方式对于状态数较少的强化学习问题还是比较有效的,但是当问题规模很大的时候,动态规划算法将会因贝尔曼维度灾难而无法使用。因此我们还需要寻找其他的针对复杂问题的强化学习问题求解方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值