skip to content
[Jimjimu Notes]

Chapter 3: Optimal Policy and Bellman Optimality Equation

/ 9 min read

Table of Contents

Motivating examples

给定如下策略(令γ=0.9\gamma=0.9): alt text

贝尔曼公式:

vπ(s1)=1+γvπ(s2),vπ(s2)=+1+γvπ(s4),vπ(s3)=+1+γvπ(s4),vπ(s4)=+1+γvπ(s4).\begin{aligned} v_{\pi}(s_1) &= -1 + \gamma v_{\pi}(s_2), \\ v_{\pi}(s_2) &= +1 + \gamma v_{\pi}(s_4), \\ v_{\pi}(s_3) &= +1 + \gamma v_{\pi}(s_4), \\ v_{\pi}(s_4) &= +1 + \gamma v_{\pi}(s_4). \end{aligned}

解方程组得 State values: vπ(s4)=vπ(s3)=vπ(s2)=10,vπ(s1)=8v_{\pi}(s_4) = v_{\pi}(s_3) = v_{\pi}(s_2) = 10, v_{\pi}(s_1) = 8

有了state value之后我们可以计算action values:

qπ(s1,a1)=1+γvπ(s1)=6.2,qπ(s1,a2)=1+γvπ(s2)=8,qπ(s1,a3)=0+γvπ(s3)=9,qπ(s1,a4)=1+γvπ(s1)=6.2,qπ(s1,a5)=0+γvπ(s1)=7.2.\begin{aligned} q_{\pi}(s_1, a_1) &= -1 + \gamma v_{\pi}(s_1) = 6.2, \\ q_{\pi}(s_1, a_2) &= -1 + \gamma v_{\pi}(s_2) = 8, \\ q_{\pi}(s_1, {\color{blue}a_3}) &= 0 + \gamma v_{\pi}(s_3) = 9, \\ q_{\pi}(s_1, a_4) &= -1 + \gamma v_{\pi}(s_1) = 6.2, \\ q_{\pi}(s_1, a_5) &= 0 + \gamma v_{\pi}(s_1) = 7.2. \end{aligned}
  • 接下来的问题是 While the policy is not good, how can we improve it?\color{blue}\text{While the policy is not good, how can we improve it?}
  • Answer: We can improve the policy based on action values

目前的π(as1)\pi(a|s_1) 策略:

π(as1)={1a=a20aa2\pi(a|s_1) = \begin{cases} 1 & a = a_2 \\ 0 & a \neq a_2 \end{cases}

显然这个策略不够好,因为从s1s_1向右走(a2a_2)会进入forbidden area。

观察上面的action value,qπ(s1,a3)q_{\pi}(s_1,a_3)最大,那么我们可以改进策略:

π(as1)={1a=a30aa3\pi_{\star}(a|s_1) = \begin{cases} 1 & a = a_3 \\ 0 & a \neq a_3 \end{cases}

Optimal Policy

The state value could be used to evaluate if a policy is good or not: if

vπ1(s)vπ2(s)for all sSv_{\pi_1}(s) \geq v_{\pi_2}(s) \quad \text{for all } s \in \mathcal{S}

then π1\pi_1 is “better” than π2\pi_2.

The definition leads to many questions:

  • Does the optimal policy exist?
  • Is the optimal policy unique?
  • Is the optimal policy stochastic or deterministic?
  • How to obtain the optimal policy?

To answer these questions, we study the Bellman optimality equation.

Bellman optimality equation (BOE) 贝尔曼最优化公式

Element-wise

贝尔曼方程(element-wise form)

vπ(s)=aπ(as)[rp(rs,a)r+γsp(ss,a)vπ(s)],sSv_{\pi}(s) = \sum_{a} \pi(a|s) \left[ \sum_{r} p(r|s, a) r + \gamma \sum_{s'} p(s'|s, a) v_{\pi}(s') \right], \quad s \in \mathcal S

右侧加上取最大值,得到Bellman optimality equation (BOE)

v(s)=maxπaπ(as)(rp(rs,a)r+γsp(ss,a)v(s)),sS=maxπaπ(as)q(s,a),sS\begin{aligned} v(s) &= {\color{blue}\max_{\pi}} \sum_{a} {\color{blue}\pi(a|s)} \left( \sum_{r} p(r|s, a) r + \gamma \sum_{s'} p(s'|s, a) v(s') \right), \quad s \in \mathcal{S} \\ &= {\color{blue}\max_{\pi}} \sum_{a} {\color{blue}\pi(a|s)} q(s, a), \quad s \in \mathcal{S} \end{aligned}

Remarks:

  • p(rs,a),p(ss,a),r,γp(r|s,a), p(s'|s,a), r, \gamma are known.
  • v(s),v(s)v(s), v(s') are unknown and to be calculated.
  • Is π(s)\pi(s) known or unknown?
    • 贝尔曼公式π\pi是给定的,贝尔曼最优公式是不给定的,你需要去求解 arg maxπaπ(as)q(s,a),sS{\color{blue}\argmax_{\pi}} \sum_{a} {\color{blue}\pi(a|s)} q(s, a), \quad s \in \mathcal{S} 然后带回公式

Matrix-vector form

Bellman optimality equation (matrix-vector form):

v=maxπ(rπ+γPπv){\color{red}v = \max_{\pi}(r_{\pi} + \gamma P_{\pi} v)}

where the elements corresponding to ss or ss' are

[rπ]saπ(as)rp(rs,a)r,[Pπ]s,s=p(ss)aπ(as)sp(ss,a)\begin{aligned} [r_{\pi}]_s &\triangleq \sum_{a} \pi(a|s) \sum_{r} p(r|s, a) r, \\ [P_{\pi}]_{s,s'} &= p(s'|s) \triangleq \sum_{a} \pi(a|s) \sum_{s'} p(s'|s, a) \end{aligned}

Here maxπ\max_{\pi} is performed elementwise:

maxπ[]=[maxπ(s1)maxπ(sn)]\max_{\pi} \begin{bmatrix} * \\ \vdots \\ * \end{bmatrix} = \begin{bmatrix} \max_{\pi(s_1)} * \\ \vdots \\ \max_{\pi(s_n)} * \end{bmatrix}

注意这里max作用于一个向量的时候是逐元素的,上面向量中也把下标写成了maxπ(si)\max_{\pi(s_i)}来表示对每个s对应的action进行最大化

更准确的可以写成:

[maxπ(s1)Fs1(π)maxπ(s2)Fs2(π)maxπ(sn)Fsn(π)] \begin{bmatrix} \max_{\pi(\cdot|s_1)}F_{s_1}(\pi)\\ \max_{\pi(\cdot|s_2)}F_{s_2}(\pi)\\ \vdots\\ \max_{\pi(\cdot|s_n)}F_{s_n}(\pi) \end{bmatrix}

因为一个完整 policy π\pi 本身其实就是:

π={π(s1),π(s2),,π(sn)}\boxed{ \pi= \{ \pi(\cdot|s_1), \pi(\cdot|s_2), \dots, \pi(\cdot|s_n) \} }

也就是说,policy 是每个状态下 action 概率分布的集合。上面的向量中每个分量求出π(si)\pi(\cdot | s_i) 最终合起来变成完整的使action value最大化的π\pi

Maximization on the right-hand side of BOE

v(s)=maxπaπ(as)(rp(rs,a)r+γsp(ss,a)v(s)),sS=maxπaπ(as)q(s,a),sS\begin{aligned} v(s) &= {\color{blue}\max_{\pi}} \sum_{a} {\color{blue}\pi(a|s)} \left( \sum_{r} p(r|s, a) r + \gamma \sum_{s'} p(s'|s, a) v(s') \right), \quad s \in \mathcal{S} \\ &= {\color{blue}\max_{\pi}} \sum_{a} {\color{blue}\pi(a|s)} q(s, a), \quad s \in \mathcal{S} \end{aligned}

由于π\pi是可以改变的,那么我们应该固定q(s,a)q(s,a),所以上式:

=maxaA(s)q(s,a)= \color{red} \max_{a \in \mathcal A(s)} q(s,a)

where the optimality is achieved when

π(as)={1a=a0aa\pi(a|s) = \begin{cases} 1 & a = a^* \\ 0 & a \neq a^* \end{cases}

where a=argmaxaq(s,a)a^* = \arg\max_a q(s,a).

Solve the Bellman optimality equation

The BOE is v=maxπ(rπ+γPπv)v = \max_{\pi}(r_{\pi} + \gamma P_{\pi} v) , Let:

f(v):=maxπ(rπ+γPπv)f(v) := max_{\pi} (r_{\pi} + \gamma P_{\pi} v)

BOE becomes:

v=f(v)v=f(v)

因为这是向量形式,拆开:

v=[v(s1)v(s2)v(sn)]=[[f(v)]s1[f(v)]s2[f(v)]sn]v= \begin{bmatrix} v(s_1)\\ v(s_2)\\ \vdots\\ v(s_n) \end{bmatrix} = \begin{bmatrix} [f(v)]_{s_1}\\ [f(v)]_{s_2}\\ \vdots\\ [f(v)]_{s_n} \end{bmatrix}

where

[f(v)]s=maxπaπ(as)q(s,a),sS[f(v)]_s = \max_{\pi} \sum_{a} \pi(a|s) q(s,a), \quad s \in \mathcal{S}

Preliminaries: Contraction mapping Theorem

  • Fixed point: xXx \in X is a fixed point of f:XXf : X \to X if

    f(x)=xf(x) = x
  • Contraction mapping (or contractive function): ff is a contraction mapping if

    f(x1)f(x2)γx1x2\|f(x_1) - f(x_2)\| \leq \gamma \|x_1 - x_2\|

    where γ(0,1)\gamma \in (0, 1).

    • γ\gamma must be strictly less than 1 so that many limits such as γk0\gamma^k \to 0 as k0k \to 0 hold.
    • Here \|\cdot\| can be any vector norm.
alt text

这里这个Algorithm迭代法,画成函数图像比较像蛛网模型

Solve the Bellman optimality equation

Let’s come back to the Bellman optimality equation:

v=f(v)=maxπ(rπ+γPπv)v = f(v) = \max_{\pi}(r_{\pi} + \gamma P_{\pi} v)

Theorem (Contraction Property)

f(v)f(v) is a contraction mapping satisfying

f(v1)f(v2)γv1v2\|f(v_1) - f(v_2)\| \leq \gamma \|v_1 - v_2\|

where γ\gamma is the discount rate!

(证明没看qaq)

既然贝尔曼最优方程已经满足contraction property,那么直接用上contraction mapping theorem

Important: The algorithm in (1) is called the value iteration algorithm. We will analyze it in the next lecture! This lecture focuses more on the fundamental properties.

Policy optimality

Suppose vv^{\star} is the solution to the Bellman optimality equation. It satisfies

v=maxπ(rπ+γPπv)v^{\star} = \max_{\pi} (r_{\pi} + \gamma P_{\pi} v^{\star})

设:

π=argmaxπ(rπ+γPπv)\pi^{\star} = \arg \max_{\pi} (r_{\pi} + \gamma P_{\pi} v^{\star})

则:

v=rπ+γPπvv^{\star} = r_{\pi^{\star}} + \gamma P_{\pi^{\star}} v^{\star}

此时π\pi^{\star}是一个策略,并且v=vπv^{\star} = v_{\pi^{\star}}π\pi^{\star}所对应的state value

并且我们有结论,π\pi^{\star}就是最优策略,并且vv^{\star}就是最优策略所对应的state value

Theorem(Policy Optimality)

Suppose That vv^{\star} is the unique solution to v=maxπ(rπ+γ+Pπv)v = \max_{\pi} (r_{\pi} + \gamma + P_{\pi} v) , and vπv_{\pi} is the state value function satisfying vπ=rπ+γ+Pπvπv_{\pi} = r_{\pi} + \gamma + P_{\pi} v_{\pi} for any given policy π\pi , then:

vvπ,πv^{\star} \ge v_{\pi}, \quad \forall \pi

What does an optimal policy π\pi^\star look like?

π(s)=argmaxπaπ(as)(rp(rs,a)r+γsp(ss,a)v(s))q(s,a)\pi^\star(s) = \arg\max_{\pi} \sum_a \pi(a|s) \underbrace{\left( \sum_r p(r|s,a) r + \gamma \sum_{s'} p(s'|s,a) v^\star(s') \right)}_{q^\star(s,a)}

Analyzing optimal policies

What factors determine the optimal state value and optimal policy? It can be clearly seen from the BOE

v(s)=maxπaπ(as)(rp(rs,a)r+γsp(ss,a)v(s))v(s) = \max_{\pi} \sum_a \pi(a|s) \left( \sum_r {\color{red}p(r|s,a) r} + {\color{red}\gamma } \sum_{s'} {\color{red} p(s'|s,a) v(s')} \right)

这里红色的量是已知的(γ,r\gamma, r等等我们可以去设计),需要求解最优策略π\pi和对应状态量vv

that there are three factors:

  • System model: p(ss,a)p(s'|s,a), p(rs,a)p(r|s,a)
  • Reward design: rr
  • Discount rate: γ\gamma

We next show how rr and γ\gamma can affect the optimal policy.

alt text 这里有一个隐藏条件rotherstep=0r_{otherstep} = 0

主要的规律是:

  • γ\gamma大,策略远视,γ\gamma小,策略偏向即时reward
  • γ=0\gamma = 0 The optimal policy becomes extremely short-sighted
  • rr的变化会改变策略,改变所有r: rar+br \rightarrow ar+b不会改变最优策略
  • γ<1\gamma < 1也会约束策略不会走特别长的步数