# **An Outsider’s Tour of Reinforcement Learning (Part7)**
[TOC]
## [A Model, You Know What I Mean?](http://www.argmin.net/2018/02/26/nominal/)
Model 的使用在RL中仍然備受爭議
:::info
- Model-free : 系統不知道外界環境資訊,只能在執行每一步動作後從環境中得到反饋然後去學習,只能按部就班,一步一步等待真實世界的反饋,再根據反饋採取下一步行動。
Policy gradient : 通過探討系統和改進策略來解決最優控制問題
- 許多研究人員認為,為我們要解決的任務尋找一個 policy ,往往會比建構一個符合系統動態的模型更容易
:::
:::warning
- 在連續控制問題中,我們通常是有 Model 的。在無模型的情況下,用反複試驗來製造自動駕駛汽車的想法是荒謬的。
- 對於多數的系統而言,建構模型雖然費力,但都是能做到的。而且通常粗糙的模型幾乎就足以讓我們規劃最佳的控制策略。
:::
### **System identification**
動態系統的估計在控制領域中叫做 System identification。System identification 是根據系統的輸入輸出時間函數來確定描述系統行為的模型。建立一個能模仿真實系統行為的模型,並用當前可測量的系統的輸入輸出來預測系統輸出的未來演變。




($z_t$ ; $v_t$) : the state of the system
$u_t$ : control action (propeller force)
$e_t$ : random disturbance
($z_{t+1}$ ; $v_{t+1}$) = f (($z_t$ ; $v_t$),$u_t$,$e_t$)
$\begin{bmatrix}
z_{t+1}\\
v_{t+1}
\end{bmatrix}$ = $\begin{bmatrix}
1&1\\
0&1
\end{bmatrix}$ $\begin{bmatrix}
z_t\\
v_t
\end{bmatrix}$ + $\begin{bmatrix}
0\\
1/m\end{bmatrix}$ ($u_t$ - g)
($z_{t+1}$ ; $v_{t+1}$) = A($z_t$ ; $v_t$) + B$u_t$ + $e_t$
**$x_t$ = ($z_t$ ; $v_t$)**

::: info
1. Inject a random probing sequence $u_t$ for control and then measure how the state responds
2. Solve the least-squares problem
:::
最小平方法是一種數學優化技術。它通過最小化誤差的平方和尋找數據的最佳函數匹配。
利用最小平方法可以簡便地求得未知的數據,並使得這些求得的數據與實際數據之間誤差的平方和為最小。


**Label the minimizers A^ and B^**

### [Comparing Policies](https://nbviewer.jupyter.org/url/argmin.net/code/lqr_policy_comparisons_original.ipynb)           [lqrpols](https://hackmd.io/nwi7hg4-RgiEQrQQxib3tA?view)
#### **System identification with least-squares (Nominal control)**
noise $e_t$ : zero-mean with covariance 10^-4^I
time horizon T : 10
input sequence : Gaussian white noise with unit variance
A = $\begin{bmatrix}1&1\\0&1\end{bmatrix}$     B = $\begin{bmatrix}0\\1\end{bmatrix}$
A^ = $\begin{bmatrix}1.00032885&0.999484822\\0.00021146575&0.999153277\end{bmatrix}$    B^ = $\begin{bmatrix}-0.00174996\\ 0.99998723\end{bmatrix}$
#### **Compared three model free strategies**
:::info
- Policy Gradient
- Random search
- Uniform sampling
:::

在大約500次的 rollouts 後所有的方法看起來都是好的,但 Nominal control 只 rollout 一次就得到了好的結果
Rather than plotting the best performance, I plot the median performance :

The error bars here are encompassing the max and min over all trials.
:::danger
- Policy gradient 的結果看起來更糟糕了
- 該如何改善 Policy gradient 的結果 ? 調整 random seed 會讓它變得更好嗎 ?
- 從模擬的結果可知 Model-free 方法得出的結果比 Model-based 還要差。如果我們擁有 Model 的話,那我們應該要使用它。
:::
### **MetaLearning**
RL中經常提出的另一個主張是,在一項任務中學到的 policy 可能會 generalize to 到其他任務。
當我們改變時間範圍時,仍能夠在 LQR 問題上實現高性能。 也就是說,如果我們使 horizon 的長度任意長,那麼 policy 是否仍能實現 high performance 呢?
:::success
- **查看無限時間範圍內的 cost**
Nominal control : 使用我們估計的數值求解 Ricatti equation , 並產生一個無限時間範圍的控制器
:::
Model free :

The error bars for Policy Gradient are all over the place here.
:::warning
- 在無限時間範圍內,控制器必須穩定,以確定軌跡不會 blow up
- 穩定的一個充分必要條件是矩陣 (A + BK) 的所有特徵值都小於1
closed-loop system
$x_{t+1}$ = ( A+BK ) $x_t$ + $e_t$
如果( A + BK )具有大於1的特徵值,則該對應的特徵向量將以指數方式快速放大
:::
closed-loop system :
$x_{t+1}$ = ( A+BK ) $x_t$ + $e_t$
如果( A + BK )具有大於1的特徵值,則該對應的特徵向量將以指數方式快速放大
接下來,我們可以繪製各種方法在有限範圍時找到穩定控制策略的情況
Nominal control 只用一次模擬就找到了

### **A Coarse Model Can’t Do Everything**
:::warning
- 即使理解線性系統估計的複雜性,非時變的情況仍然是一個挑戰。 在非線性的情況下,只會更難。
- 我們不完全理解 Nominal control 對於 LQR 的限制,我們不知道模型需要建構到什麼程度才能獲得令人滿意的控制性能
:::
參考資料
[A Model, You Know What I Mean?](http://www.argmin.net/2018/02/26/nominal/)
[Least squares](https://en.wikipedia.org/wiki/Least_squares)
[Introduction to System Identification](https://www.youtube.com/watch?v=u7hJ1aF-JrU)