Dynamic Programming

题外话:以前做题的时候老是碰到dp,不过队内有两位dp大师,我也不怎么输出dp题,没想到最近看书又碰到了dp。。。DP真是个需要智商的东西,很绝望啊。。。
UPD:自己之前以为值迭代和策略迭代相比会更加不稳定,但是后来问了一下why,被告诉其实两者都会产生震荡(不稳定)。

Policy Evaluation (Prediction)

police evalution(prediction problem):We consider how to compute the state-value function \(v_\pi\) for an arbitrary policy \(\pi\).

For our purposes, iterative solution methods are most suitable. The initial approximation, \(v_0\), is chosen arbitrarily (except that the terminal state, if any, must be given value 0), and each successive
approximation is obtained by using the Bellman equation for \(v_\pi\) (4.4) as an update rule:

iterative policy evaluation:在\(v_\pi\)的存在下,找到一个序列的\({v_k}\),使得k趋近于无穷的时候,\(v_k=v_\pi\)。
expected update: To each state s: it replaces the old value of \(s\) with a new value,obtained from the old values of the successor states of s, and the expected immediate rewards, along all the one-step transitions possible under the policy being evaluated.
All the updates done in DP algorithms are called expected updates because they are based on an expectation over all possible next states rather than on a sample next state.
过程伪代码:

注意更新价值函数的时候有两种思路:第一种就是开两个数组old/new,按照之前的式子那样迭代。第二种是in-place,新算出来的值立马代替old,这样每轮迭代,如果一些依赖的状态已经更新了,就用更新后的来计算。in-place的收敛速度更快,但是其中每一轮计算各s的顺序也有很大影响。其实我在这里想到的居然是01背包的空间优化(囧了)。。。

Policy Improvement

Our reason for computing the value function for a policy is to help find better policies.Suppose we have determined the value function vπ for an arbitrary deterministic policy \(\pi\). For some state s we would like to know whether or not we should change the policy to deterministically choose an action \(a \neq \pi(s)\).
根据状态行为对的价值函数,
如果我们没有按照当前的policy选择一个action a,而是自己选择了一个a,并且得到了一个\(q_\pi(s,a)\)是大于原来的\(v_\pi(s)\)的话,也就意味着当前这一步贪心的选择一个比原来好的a哪怕之后还是按照之前的policy进行操作选择action也会比原来的策略好,所以就可以生成一个新的策略了。
policy improvement theorem:对于两个确定性策略\(\pi\)和\(\pi’\),假设对于所有的状态s来说都满足,\(q_\pi(s,\pi'(s))\geq v_\pi(s)\),那也就意味着都满足\(v_\pi'(s)\geq v_\pi(s)\)。
证明过程:

policy improvement:
根据上面在一个状态下选择一个较优的方式,我们可以拓展到在每个状态下在所有action中选择最优策略,规则如下:\(\pi'(s) = argmax_a q_\pi(s, a) = argmax_a \sum_{s’,r} p(s’, r | s, a) (r + \gamma v_\pi(s’))\).The greedy policy takes the action that looks best in the short term–after one step of lookahead–according to \(v_\pi\).
对于随机性的policy,上面的理论也是适用的,当有若干action的都可以达到最优的时候, each maximizing action can be given a portion of the probability of being selected in the new greedy policy. Any apportioning scheme is allowed as long as all submaximal actions are given zero probability.Any apportionment of probability among these actions is permitted.

Policy Iteration

We can thus obtain a sequence of monotonically improving policies and value functions:

E代表a policy evaluation,I代表了a policy improvement。Each policy is guaranteed to be a strict improvement over the previous one (unless it is already optimal). Because a finite MDP has only a finite number of policies, this process must converge to an optimal policy and optimal value function in a finite number of iterations.
伪代码:

Value Iteration

evolution可能很慢,In fact, the policy evaluation step of policy iteration can be truncated in several ways
without losing the convergence guarantees of policy iteration.
One important special case is when policy evaluation is stopped after just one sweep (one update of each state).This algorithm is called value iteration.

伪代码:

Because the max operation in (4.10) is the only difference between these updates, this just means that the max operation is added to some sweeps of policy evaluation. All of these algorithms converge to an optimal policy for discounted finite MDPs.

Asynchronous Dynamic Programming

If the state set is very large, then even a single sweep can be prohibitively expensive.
Asynchronous DP algorithms are in-place iterative DP algorithms that are not organized in terms of systematic sweeps of the state set. These algorithms update the values of states in any order whatsoever, using whatever values of other states happen to be available.
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 DP algorithms allow great flexibility in selecting states to update.
Of course, avoiding sweeps does not necessarily mean that we can get away with less computation. It just means that an algorithm does not need to get locked into any hopelessly long sweep before it can make progress improving a policy.
We can try to take advantage of this flexibility by selecting the states to which we apply updates so
as to improve the algorithm’s rate of progress. We can try to order the updates to let value information propagate from state to state in an efficient way.

Generalized Policy Iteration

generalized policy iteration (GPI):the general idea of letting policy evaluation and policy improvement processes interact, independent of the granularity and other details of the two processes.所以也就是说之前的三种迭代dp的方法都可以认为是GPI的。
所有的RL方法都可以被建模为GPI。It is easy to see that if both the evaluation process and the improvement
process stabilize, that is, no longer produce changes, then the value function and policy must be optimal.

两个过程都达到稳定,也说明找到了最优策略。
The evaluation and improvement processes in GPI can be viewed as both competing and cooperating.Making the policy greedy with respect to the value function typically makes the value function incorrect for the changed policy, and making the value function consistent with the policy typically causes that policy no longer to be greedy.

Efficiency of Dynamic Programming

DP is sometimes thought to be of limited applicability because of the curse of dimensionality, the fact that the number of states often grows exponentially with the number of state variables. Large state sets do create difficulties, but these are inherent difficulties of the problem, not of DP as a solution method. In fact, DP is comparatively better suited to handling large state spaces than competing methods such as direct search and linear programming.
In practice, DP methods can be used with today’s computers to solve MDPs with millions of states.In practice, these methods usually converge much faster than their theoretical worst-case run times, particularly if they are started with good initial value functions or policies.

Finite Markov Decision Processes Learning


题外话:中秋节跟着几位大爷呼呼social,基本上每个夜晚都是在看电影吃烤肉酒吧中度过,然后就导致代码bug没有fix(不过真的也不想fix,明明就不是我写的为啥要我改呢。。我是真的讨厌改别人代码里的bug)。。。就看了一篇过几天reading group要讲的论文(回来可能还得做个ppt啥的),赶快看看Finite Markov Dexision Processes这一章节吧。。。没看课程视频直接看了书。。。所以明天听完别人讲的后看看视频再做补充好了。。。

The Agent-Environment Interface


我们一般认为MDP形成了如下的一个序列:\(S_0,A_0,R_1,S_1,A_1,R_2….\),这里面我们认为\(A_t\)的时刻得到的奖励为\(R_{t+1}\)。。。

the probability of each possible value for \(S_t\) and \(R_t\) depends only on the immediately preceding state and action, \(S_{t−1}\) and \(A_{t−1}\).
This is best viewed a restriction not on the decision process but on the state.
Markov property:The state must include information about all aspects of the past agent-environment interaction that make a difference for the future.
我们可以通过上式推出来另外三个式子。。。
state-transition probabilities:

the expected rewards for state{action pairs :

the expected rewards for state{action{next-state:

agent-environment boundary

The general rule we follow is that anything that cannot be changed arbitrarily by the agent is considered to be outside of it and thus part of its environment.
We always consider the reward computation to be external to the agent because it defines the task facing the agent and thus must be beyond its ability to change arbitrarily.
The agent-environment boundary represents the limit of the agent’s absolute control, not of its knowledge.

MDP framework

one signal to represent the choices made by the agent (the actions), one signal to represent the basis on which the choices are made (the states)做出选择的基础, and one signal to define the agent’s goal (the rewards).

Goals and Rewards

reward hypothesis:That all of what we mean by goals and purposes can be well thought of as the maximization of the expected value of the cumulative sum of a received scalar signal (called reward).
The reward signal is your way of communicating to the robot what you want it to achieve, not how you want it achieved.

Returns and Episodes

The return is the sum of the rewards:\(G_t=R_{t+1}+R_{t+2}+…+R_T\).
Each episode ends in a special state called the terminal state, followed by a reset to a standard starting state or to a sample from a standard distribution of starting states.
the next episode begins independently of how the previous one ended.Tasks with episodes of this kind are called episodic tasks.
continuing tasks:goes on continually without limit
discounted return:

递推:\(G_t=R_{t+1}+\gamma G_{t+1}\)

Unified Notation for Episodic and Continuing Tasks

absorbing state:

\(G _ { t } \doteq \sum _ { k = t + 1 } ^ { T } \gamma ^ { k – t – 1 } R _ { k }\),其中T=inf or r=1,但不能同时满足,为了适应于两种不同task的规定。

Policies and Value Functions

value functions:functions of states (or of state–action pairs) that estimate how good it is for the agent to be in a given state (or how good it is to perform a given action in a given state).
a policy \(\pi(a|s)\) is a mapping from states to probabilities of selecting each possible action.\(A_t=a \)if \(S_t=s\)
The value function of a state \(s\) under a policy \(\pi\), denoted \(v _ { \pi } ( s ) \) is the expected return when starting in \(s\) and following \(\pi\) thereafter.
We call the function \(v_\pi\) the state-value function for policy \(\pi\):
\(v _ { \pi } ( s ) \doteq \mathbb { E } _ { \pi } \left[ G _ { t } | S _ { t } = s \right] = \mathbb { E } _ { \pi } \left[ \sum _ { k = 0 } ^ { \infty } \gamma ^ { k } R _ { t + k + 1 } | S _ { t } = s \right] \),for all \(s \in \mathcal { S}\)
\(q_{\pi}\) is the action-value function for policy \(\pi\):
\(q _ { \pi } ( s , a ) \doteq \mathbb { E } _ { \pi } \left[ G _ { t } | S _ { t } = s , A _ { t } = a \right] = \mathbb { E } _ { \pi } \left[ \sum _ { k = 0 } ^ { \infty } \gamma ^ { k } R _ { t + k + 1 } | S _ { t } = s , A _ { t } = a \right]\)
Bellman Equation for \(v_{\pi}\):

It expresses a relationship between the value of a state and the values of its successor states.
It states that the value of the start state must equal the (discounted) value of the expected next state, plus the reward expected along the way.

Optimal Policies and Optimal Value Functions

A policy \(\pi\) is defined to be better than or equal to a policy \(\pi^{‘}\) if its expected return is greater than or equal to that of \(\pi^{‘}\) for all states.
In other words, \(\pi \geq \pi ^ { \prime }\) if and only if \(v _ { \pi } ( s ) \geq v _ { \pi ^ { \prime } } ( s )\) ,for all \(s \in {S}\)。
optimal state-value function:
\(v_{*} ( s ) \doteq \max _ { \pi } v _ { \pi } ( s )\)
\(q_{*}\):optimal action-value function:

Bellman optimality equation

Intuitively, the Bellman optimality equation expresses the fact that the value of a state under an optimal policy must equal the expected return for the best action from that state.

Backup diagram:

The Bellman optimality equation for \(q_{*}\) is:

Backup diagram:

两种search(已知state求最优action):
\(v_*\)需要one-step ahead search,就是要知道所有可行的action及其能达到的state。
\(q_*\)不需要,因为记得就是state-action对,相当于cache。

计蒜之道2018初赛做题记录

最近又开始了计蒜之道,目前进行到了前两场,两场都打了:第一场是自己打,第二场帮考试中的队友打的。第一场题目不算太坑一个小时做完前三个不想写hard就可以晋级了。。。第二场题目有些坑点实在难以注意到结果半个小时写完前两个easy就晋级了。。。写了一整场的medium。。。想到博客已经n年没有更新了,最近考试周也过去了,就把题目补补(如果会的话),写到这里算了,毕竟月底蓝桥杯还是要咸鱼冲锋一下的。。剩下的几场因为没有号了,可能就随便打打了。。。

第一场

A. 百度无人车

二分答案没啥好说的。。

B. 百度科学家(简单)

这道题我比赛的时候写了两个版本简单和中等,困难当时也看出来要写可持久化线段树优化建图,但是老年人写不动就算了。
简单版本直接枚举的起点然后bfs的。。。不贴代码了。

C. 百度科学家(中等)

发现加的总边数不会超过1e5就很好办了。先Tarjan缩点成dag后重新建图,然后因为权值非负贪心选择出度为0的点中价值最小的即可。

D. 百度科学家(困难)

待补。

第二场

A. 淘宝的推荐系统

直接dp[i]表示到当前元素为止价值为i的合法序列长度,然后就是O(nd)的dp了。。。

B. 阿里巴巴的手机代理商(简单)

没有update不贴代码了。。

C. 阿里巴巴的手机代理商(中等)

这个题细节贼鸡儿多。。。就说一个地方吧,因为我是错在这里了,就是你update需要先删除然后断掉子树,然后插入在连接。具体看代码吧,至于为什么,考虑你把一个老的单词换成一个新的单词且老的是新的前缀的情况,所以这个顺序不能乱。

D. 阿里巴巴的手机代理商(困难)

待补。。写不动了啊。。