├── README.md ├── images ├── Agent-Environment.png ├── Dyna.png ├── GPI.PNG ├── Not_Tabular_Value_Function.png ├── Planning-Learning.png └── Tabular_Value_Function.png ├── rl_cheatsheet.pdf └── rl_cheatsheet.tex /README.md: -------------------------------------------------------------------------------- 1 | # Reinforcement-Learning-CheatSheet 2 | Cheatsheet of Reinforcement Learning (Based on Sutton-Barto Book - 2nd Edition) - http://www.incompleteideas.net/book/RLbook2020.pdf 3 | 4 | In square brackets there are indicated references to the equations or paragraphs in the book. 5 | 6 | Initially Inspired by: https://github.com/FrancescoSaverioZuppichini/Reinforcement-Learning-Cheat-Sheet but quite completely rewritten. 7 | 8 | A lot of phrases are extracted from Sutton-Barto Book and/or Coursera Specialization on Reinforcement Learning (https://www.coursera.org/specializations/reinforcement-learning). 9 | 10 | Work-In-Progress 11 | 12 | -------------------------------------------------------------------------------- /images/Agent-Environment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linker81/Reinforcement-Learning-CheatSheet/HEAD/images/Agent-Environment.png -------------------------------------------------------------------------------- /images/Dyna.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linker81/Reinforcement-Learning-CheatSheet/HEAD/images/Dyna.png -------------------------------------------------------------------------------- /images/GPI.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linker81/Reinforcement-Learning-CheatSheet/HEAD/images/GPI.PNG -------------------------------------------------------------------------------- /images/Not_Tabular_Value_Function.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linker81/Reinforcement-Learning-CheatSheet/HEAD/images/Not_Tabular_Value_Function.png -------------------------------------------------------------------------------- /images/Planning-Learning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linker81/Reinforcement-Learning-CheatSheet/HEAD/images/Planning-Learning.png -------------------------------------------------------------------------------- /images/Tabular_Value_Function.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linker81/Reinforcement-Learning-CheatSheet/HEAD/images/Tabular_Value_Function.png -------------------------------------------------------------------------------- /rl_cheatsheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linker81/Reinforcement-Learning-CheatSheet/HEAD/rl_cheatsheet.pdf -------------------------------------------------------------------------------- /rl_cheatsheet.tex: -------------------------------------------------------------------------------- 1 | \documentclass[10pt,landscape]{article} 2 | \usepackage{multicol} 3 | \usepackage{calc} 4 | \usepackage{ifthen} 5 | \usepackage[landscape]{geometry} 6 | \usepackage{hyperref} 7 | \usepackage{graphicx} 8 | \usepackage{amsmath,amssymb} 9 | \usepackage{algorithm2e} 10 | \usepackage{float} 11 | 12 | % To make this come out properly in landscape mode, do one of the following 13 | % 1. 14 | % pdflatex rl_cheatsheet.tex 15 | % 16 | % 2. 17 | % latex rl_cheatsheet.tex 18 | % dvips -P pdf -t landscape rl_cheatsheet.dvi 19 | % ps2pdf rl_cheatsheet.ps 20 | 21 | 22 | % If you're reading this, be prepared for confusion. Making this was 23 | % a learning experience for me, and it shows. Much of the placement 24 | % was hacked in; if you make it better, let me know... 25 | 26 | 27 | % 2008-04 28 | % Changed page margin code to use the geometry package. Also added code for 29 | % conditional page margins, depending on paper size. Thanks to Uwe Ziegenhagen 30 | % for the suggestions. 31 | 32 | % 2006-08 33 | % Made changes based on suggestions from Gene Cooperman. 34 | 35 | 36 | % To Do: 37 | % \listoffigures \listoftables 38 | % \setcounter{secnumdepth}{0} 39 | 40 | 41 | % This sets page margins to .5 inch if using letter paper, and to 1cm 42 | % if using A4 paper. (This probably isn't strictly necessary.) 43 | % If using another size paper, use default 1cm margins. 44 | \ifthenelse{\lengthtest { \paperwidth = 11in}} 45 | { \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} } 46 | {\ifthenelse{ \lengthtest{ \paperwidth = 297mm}} 47 | {\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} } 48 | {\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} } 49 | } 50 | 51 | % Turn off header and footer 52 | \pagestyle{empty} 53 | 54 | 55 | % Redefine section commands to use less space 56 | \makeatletter 57 | \renewcommand{\section}{\@startsection{section}{1}{0mm}% 58 | {-1ex plus -.5ex minus -.2ex}% 59 | {0.5ex plus .2ex}%x 60 | {\normalfont\large\bfseries}} 61 | \renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}% 62 | {-1explus -.5ex minus -.2ex}% 63 | {0.5ex plus .2ex}% 64 | {\normalfont\normalsize\bfseries}} 65 | \renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}% 66 | {-1ex plus -.5ex minus -.2ex}% 67 | {1ex plus .2ex}% 68 | {\normalfont\small\bfseries}} 69 | 70 | \newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}} 71 | 72 | \makeatother 73 | 74 | % Define BibTeX command 75 | \def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em 76 | T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}} 77 | 78 | % Don't print section numbers 79 | \setcounter{secnumdepth}{0} 80 | 81 | 82 | \setlength{\parindent}{0pt} 83 | \setlength{\parskip}{0pt plus 0.5ex} 84 | 85 | \DeclareMathOperator*{\argmax}{argmax} 86 | 87 | % ----------------------------------------------------------------------- 88 | 89 | \begin{document} 90 | 91 | \raggedright 92 | \footnotesize 93 | \begin{multicols}{3} 94 | 95 | % multicol parameters 96 | % These lengths are set only within the two main columns 97 | %\setlength{\columnseprule}{0.25pt} 98 | \setlength{\premulticols}{1pt} 99 | \setlength{\postmulticols}{0.1pt} 100 | \setlength{\multicolsep}{1pt} 101 | \setlength{\columnsep}{1pt} 102 | 103 | \begin{center} 104 | \Large{Reinforcement Learning Cheat Sheet} \\ 105 | \end{center} 106 | \subsection{Recap} 107 | $\mathbb{E}[X] \stackrel{.}{=} \sum_{x_i} x_i \cdot Pr\{ X = x_i \}$ 108 | $\mathbb{E}[X | Y = y_j] = \sum_{x_i} x_i \cdot Pr\{ X = x_i | Y = y_j\}$ 109 | $\mathbb{E}[X | Y = y_j] = \sum_{z_k} Pr\{Z=z_k | Y = y_j\} \cdot \mathbb{E}[X|Y = y_j, Z = z_k]$ 110 | 111 | \subsection{Agent-Environment Interface} 112 | \begin{center} 113 | \includegraphics[width=\linewidth]{./images/Agent-Environment.png} 114 | \end{center} 115 | The Agent at each step $t$ receives a representation of the environment's \emph{state} $S_t \in \mathcal{S}$ and it selects an action $A_t \in \mathcal{A}(s)$. One time step later, as a consequence of its action, the agent receives a \emph{reward}, $R_{t + 1} \in \mathcal{R} \subseteq \mathbb{R}$ and goes to the new state $S_{t+1}$. 116 | 117 | \subsection{Markov Decision Process} 118 | A finite \textbf{Markov Decision Process}, MDP, is defined by: \\ 119 | finite set of states: $s \in \mathcal{S}$, \\ 120 | finite set of actions: $a \in \mathcal{A}$ \\ 121 | dynamics: 122 | \begin{equation} 123 | p(s', r | s, a) \stackrel{.}{=} Pr \{S_{t} = s', R_{t} = r | S_{t-1} = s, A_{t-1} = a \} \quad [3.2] \numberthis 124 | \end{equation} 125 | state transition probabilities: 126 | \begin{align*} 127 | p(s' | s, a) & \stackrel{.}{=} Pr \{S_{t} = s' | S_{t-1} = s, A_{t-1} = a \} \\ 128 | & = \sum_{r \in \mathcal{R}} p(s', r | s, a) \quad [3.4] \numberthis 129 | \end{align*} 130 | expected reward for state-action: 131 | \begin{align*} 132 | r(s, a) & \stackrel{.}{=} \mathbb{E}[ R_{t} | S_{t-1} = s, A_{t-1} = a ] \\ 133 | & = \sum_{r \in \mathcal{R}} r \cdot \sum_{s' \in \mathcal{S}} p(s', r | s, a) \quad [3.5] \numberthis 134 | \end{align*} 135 | expected reward for state-action-next state: 136 | \begin{align*} 137 | r(s',s, a) & \stackrel{.}{=} \mathbb{E}[ R_{t} | S_{t-1} = s, A_{t-1} = a, S_{t} = s' ] \\ 138 | & = \sum_{r \in \mathcal{R}} r \cdot \frac{p(s', r | s, a)}{p(s' | s,a)} \quad [3.6] \numberthis 139 | \end{align*} 140 | 141 | The MDP and agent together thereby give rise to a sequence or \emph{trajectory} that begins like this: 142 | $S_0,A_0,R_1,S_1,A_1,R_2,S_2,A_2,R_3...$ 143 | 144 | \subsection{Return} 145 | In RL the goal of the agent is not the maximization of the immediate reward, but of the cumulative reward in the long run. The \emph{return} is a specific function of reward sequence. In the simplest case the return is the sum of rewards: 146 | \begin{equation} 147 | G_t \stackrel{.}{=} R_{t+1} + R_{t+2} + R_{t+3} + ... + R_{T} \numberthis \label{eq:return_1} \quad [3.7] 148 | \end{equation} 149 | where $T$ is a final time step. 150 | When there is a natural notion of final time step ($T$), the agent-environment interaction breaks naturally into sub-sequences (\emph{episodes}) and the next episode begins independently of how the previous one ended. Tasks with episodes are called \emph{episodic tasks}. Each episodes ends in a special state called \emph{terminal state} with different rewards for the different outcomes. $\mathcal{S}^{+}$ is the set of all states plus the terminal state. 151 | 152 | When the agent-environment interaction does not break naturally into episodes but goes on continually without limit, we call these \emph{continuing tasks}. 153 | The previous formulation of return (Eq. \ref{eq:return_1}) is problematic because $T=\infty$. 154 | 155 | It is introduced the \emph{total discounted return} expressed as the sum of rewards (opportunely discounted using the \emph{discount rate} $0 \le \gamma \le 1$): 156 | \begin{align*} 157 | G_t & \stackrel{.}{=} R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + ...\\ 158 | & = \sum_{k = 0}^\infty \gamma^k R_{t + k + 1} \numberthis \label{eq:discounted_return_1} \quad [3.8]\\ 159 | & = R_{t+1} + \gamma G_{t+1} \numberthis \label{eq:discounted_return_2} \quad [3.9] 160 | \end{align*} 161 | 162 | To unify the notation for episodic and continuing tasks we use: 163 | 164 | \begin{equation} 165 | G_t \stackrel{.}{=} \sum_{k = t+1}^T \gamma^{k-t-1} R_{k} \numberthis \label{eq:discounted_return_generic} \quad [3.11] 166 | \end{equation} 167 | 168 | including the possibility that $T=\infty$ or $\gamma = 1$ \emph{but not both}. 169 | \subsection{Policy} 170 | A \emph{policy} is a mapping from a state to probabilities of selecting each possible action: 171 | \begin{equation} 172 | \pi(a|s) 173 | \label{eq: policy} 174 | \end{equation} 175 | That is the probability of select an action $A_t = a$ if $S_t = s$. 176 | 177 | \subsection{Value Functions} 178 | \emph{State-Value function} describes \emph{how good} is to be in a specific state $s$ under a certain policy $\pi$. 179 | Informally, it is the expected return (expected cumulative discounted reward) when starting from $s$ and following $\pi$. For any policy $\pi$ and $\forall s \in \mathcal{S}$: 180 | \begin{align*} 181 | v_{\pi}(s) & \stackrel{.}{=} \mathbb{E}_{\pi}[G_t | S_t = s] \numberthis \label{eq:v_def} \quad [3.12] \\ 182 | & = \mathbb{E}_{\pi}[R_{t+1} + \gamma G_{t+1} | S_t = s] \numberthis \quad [by \ 3.9] \\ 183 | & = \sum_{a} \pi(a|s) \sum_{s' ,r} p(s',r | s, a) \Big[r + \gamma v_{\pi}(s')\Big] \numberthis \label{eq:v_bellman} \quad [3.14] 184 | \end{align*} 185 | The last one is the \textbf{Bellman equation for $v_\pi$}. 186 | 187 | \emph{Action-Value function (Q-Function)} describes \emph{how good} is to perform a given action $a$ in a given state $s$ under a certain policy $\pi$. 188 | Informally, is the expected return when starting from $s$, taking action $a$ and thereafter following $\pi$: 189 | \begin{align*} 190 | q_{\pi}(s,a) & \stackrel{.}{=} \mathbb{E}_{\pi}[G_t | S_t = s, A_t = a] \numberthis \label{eq:q_def}\quad [3.13] \\ 191 | & = \sum_{s',r} p(s',r|s,a) \Big[r + \gamma \sum_{a'} \pi(a'|s') q_{\pi}(a',s') \Big] \numberthis \label{eq:q_bellman}\quad [Ex \ 3.17] 192 | \end{align*} 193 | The last one is the \textbf{Bellman equation for $q_\pi$}. 194 | \vfill\null 195 | 196 | \subsubsection{Relation between Value Functions} 197 | \begin{align*} 198 | v_{\pi}(s) & = \sum_{a} \pi(a|s) \cdot q_{\pi}(s,a) \numberthis \label{eq:v_derived_1} \quad [Ex \ 3.12] \\ 199 | & = \mathbb{E}_{\pi}[ q_{\pi}(s,a) | S_t = s] \numberthis \label{eq:v_derived_2} \quad [Ex \ 3.18] 200 | \end{align*} 201 | \begin{align*} 202 | q_{\pi}(s,a) & = \sum_{s',r} p(s',r|s,a) \Big[r + \gamma v_{\pi}(s')\Big] \numberthis \label{eq:q_derived_1} \quad [Ex \ 3.13]\\ 203 | & = \mathbb{E} \Big[ R_{t+1} + \gamma v_{\pi}(s') | S_t = s, A_t = a \Big] \numberthis \label{eq:q_derived_2} \quad [Ex \ 3.19] 204 | \end{align*} 205 | 206 | \subsection{Optimal Value Functions} 207 | \begin{align*} 208 | v_*(s) & \stackrel{.}{=} \max\limits_{\pi} v_\pi(s) \numberthis \label{eq: value_optimal} \quad [3.15]\\ 209 | & = \max_a \mathbb{E} [R_{t+1} + \gamma v_{*}(S_{t+1}) | S_{t} = s, A_t = a ] \quad [3.18]\\ 210 | & = \max_a \sum_{s',r} p(s',r|s,a) \Big[r + \gamma v_{*}(s') \Big] \quad [3.19] 211 | \end{align*} 212 | 213 | \begin{align*} 214 | q_*(s,a) & \stackrel{.}{=} \max\limits_{\pi} q_\pi(s,a) \numberthis \label{eq: action_value_optimal} \quad [3.16]\\ 215 | & = \mathbb{E} [R_{t+1} + \gamma \max_{a'} q_{*}(S_{t+1}, a') | S_{t} = s, A_t = a ] \\ 216 | & = \sum_{s',r} p(s',r|s,a) \Big[r + \gamma \max_{a'} q_{*}(s',a')\Big] \quad [3.20] 217 | \end{align*} 218 | 219 | \begin{equation} 220 | v_*(s) = \max\limits_{a \in A(s)} q_{\pi_*}(s,a) 221 | \end{equation} 222 | Intuitively, the above equation express the fact that the value of a state under the optimal policy \textbf{must be equal} to the expected return from the best action from that state. 223 | 224 | \subsubsection{Relation between Optimal Value Functions} 225 | \begin{align*} 226 | v_{*}(s) & = \max_a \sum_{s',r} p(s',r|s,a) \Big[r + \gamma \sum_{a'} \pi(a'|s') q_{*}(s',a')\Big] \quad [Ex \ 3.25] \numberthis \label{eq:vstar_derived_1}\\ 227 | \end{align*} 228 | \begin{align*} 229 | q_{*}(s,a) & = \sum_{s',r} p(s',r|s,a) \Big[r + \gamma v_{*}(s') \Big] \quad [Ex \ 3.26] \numberthis \label{eq:qstar_derived_1}\\ 230 | \end{align*} 231 | \end{multicols} 232 | 233 | \newpage 234 | 235 | \begin{multicols}{3} 236 | 237 | \section{Dynamic Programming} 238 | Collection of algorithms that can be used to compute optimal policies given a perfect model of the environment as a MDP. 239 | 240 | \subsection{Policy Evaluation [Prediction]} 241 | If the environment's dynamic is completely known, the Eq. \ref{eq:v_bellman} is a system of $|\mathcal{S}|$ equations in $|\mathcal{S}|$ unknowns ($v_\pi(s), s \in \mathcal{S}$). \\ 242 | We also can use an iterative solution, $\forall s \in \mathcal{S}$: 243 | \begin{align*} 244 | v_{k+1}(s) & \stackrel{.}{=} \mathbb{E}_{\pi}[R_{t+1} + \gamma v_{k}(S_{t+1}) | S_t = s] \\ 245 | & = \sum_{a} \pi(a|s) \sum_{s' ,r} p(s',r | s, a) [r + \gamma v_{k}(s')] \numberthis \quad [4.5] 246 | \end{align*} 247 | We can compute new values $v_{k+1}(s)$ from old values $v_k(s)$ without change old values or update the values \emph{in-place}. 248 | 249 | \subsubsection{Iterative Policy Evaluation for estimating $V \sim v_{\pi}$ (in-place version)} 250 | \begin{algorithm}[H] 251 | Inputs: $\pi$ - the policy to be evaluated \\ 252 | Params: $\theta$ - a small positive threshold determining the accuracy of the estimation \\ 253 | Initialize V(s), for all $s \in \mathcal{S}^{+}$ arbitrarily, except V(terminal) = 0\\ 254 | $\Delta \leftarrow 0$ \\ 255 | \While{$\Delta \ge \theta$} 256 | { 257 | \ForEach{$s \in S$} { 258 | $v \leftarrow V(s)$\\ 259 | $V(s) \leftarrow \sum\limits_a \pi(a|s) \sum\limits_{s', r} p(s',r | s, a) \begin{bmatrix} 260 | r + \gamma V(s') 261 | \end{bmatrix}$ \\ 262 | $\Delta \leftarrow \max(\Delta, | v - V(s)|)$ 263 | } 264 | } 265 | \caption{Iterative Policy Evaluation - estimating $V \sim v_{\pi}$ - [§4.1]} 266 | \end{algorithm} 267 | 268 | The algorithm tests the quantity $\max_{s \in \mathcal{S}} |v_{k+1}(s) - v_k(s)|$ after each \emph{sweep} and stops when it is sufficiently small ($\Delta < \theta$). 269 | %\vfill\null 270 | %\columnbreak 271 | 272 | \subsection{Policy Iteration} 273 | Policy iteration consists of two simultaneous, interacting processes: one making the value function consistent with the current policy (\emph{policy evaluation}), and the other making the policy greedy with respect to the current value function (\emph{policy improvement}). 274 | \begin{algorithm}[H] 275 | %\SetAlgoLined 276 | 1. Initialization \\ 277 | Assign arbitrarily $V(s) \in \mathbb{R}$ and $\pi(s) \in \mathcal{A}(s)$ for all $s \in S$,\\ 278 | 2. Policy Evaluation \\ 279 | $\Delta \leftarrow 0$ \\ 280 | \While{$\Delta \ge \theta$} 281 | { 282 | \ForEach{$s \in S$} { 283 | $v \leftarrow V(s)$\\ 284 | $V(s) \leftarrow \sum\limits_a \pi(a|s) \sum\limits_{s', r} p(s',r | s, a) \begin{bmatrix} 285 | r + \gamma V(s') 286 | \end{bmatrix}$ \\ 287 | $\Delta \leftarrow \max(\Delta, | v - V(s)|)$ 288 | } 289 | } 290 | 3. Policy Improvement \\ 291 | \emph{policy-stable} $ \leftarrow $ \emph{true} \\ 292 | \ForEach{$s \in S$} { 293 | \emph{old-action} $\leftarrow \pi(s)$\\ 294 | $\pi(s) \leftarrow \argmax\limits_a\sum\limits_{s', r} p(s',r | s, a) \begin{bmatrix} 295 | r + \gamma V(s') 296 | \end{bmatrix}$ \\ 297 | \If{\emph{old-action} $\neq \pi(s)$} 298 | { 299 | \emph{policy-stable} $\leftarrow$ \emph{false} 300 | } 301 | } 302 | \uIf{\emph{policy-stable}} 303 | { 304 | return V $\approx v_*$ and $\pi \approx \pi_*$ 305 | } 306 | \Else 307 | { 308 | go to 2 309 | } 310 | 311 | \caption{Policy Iteration - estimating $\pi \sim \pi_*$ - deterministic policy - [§4.3]} 312 | \end{algorithm} 313 | 314 | \subsection{Value Iteration} 315 | Instead of waiting the convergence of $V(s)$ (policy evaluation loop) we can perform only one step of policy evaluation that, combined with policy improvement, lead to the following formulation: 316 | \begin{align*} 317 | v_{k+1}(s) & = \max_a \mathbb{E}_{\pi}[R_{t+1} + \gamma v_{k}(S_{t+1}) | S_t = s] \\ 318 | & = \max_a \sum_{s' ,r} p(s',r | s, a) [r + \gamma v_{k}(s')] \numberthis \quad [4.10] 319 | \end{align*} 320 | 321 | \begin{algorithm}[H] 322 | Params: $\theta$ - a small positive threshold determining the accuracy of the estimation \\ 323 | Initialize V(s), for all $s \in \mathcal{S}^{+}$ arbitrarily, except V(terminal) = 0\\ 324 | \SetKwInOut{Output}{output} 325 | $\Delta \leftarrow 0$ \\ 326 | \While{$\Delta \ge \theta$} 327 | { 328 | \ForEach{$s \in S$} { 329 | $v \leftarrow V(s)$\\ 330 | $V(s) \leftarrow \max\limits_a \sum\limits_{s', r} p(s',r | s, a) \begin{bmatrix} 331 | r + \gamma V(s') 332 | \end{bmatrix}$ \\ 333 | $\Delta \leftarrow \max(\Delta, | v - V(s)|)$ 334 | } 335 | } 336 | 337 | \Output{Deterministic policy $\pi \approx \pi_*$ such that} 338 | $\pi(s) = \argmax\limits_a \sum\limits_{s', r} p(s',r | s, a) \begin{bmatrix} 339 | r + \gamma V(s') 340 | \end{bmatrix}$ 341 | \caption{Value Iteration - estimating $\pi \sim \pi_*$ - [§4.4]} 342 | \end{algorithm} 343 | One \emph{sweep} is one update of each state.\\ 344 | In value iteration only a single iteration of policy evaluation is performed between each policy improvement. 345 | Value iteration combines, in each of its sweeps, one sweep of policy evaluation and one sweep of policy improvement.\\ Faster convergence is often achieved by interposing multiple policy evaluation sweeps between each policy improvement sweep. 346 | The entire class of \emph{truncated policy iteration} algorithms can be thought of as sequences of sweeps, some of which use policy evaluation updates and some of which use value iteration updates. 347 | 348 | \subsection{Generalized Policy Iteration} 349 | Generalized Policy Iteration is a way 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. \\ 350 | Almost all reinforcement learning methods are well described as GPI. That is, all have identifiable policies and value functions, with the policy always being improved with respect to the value function and the value function always being driven toward the value function for the policy, as suggested by the diagram below [§4.6]: 351 | \begin{center} 352 | \includegraphics[width=0.6\linewidth]{./images/GPI.png} 353 | \end{center} 354 | 355 | \end{multicols} 356 | 357 | \newpage 358 | 359 | \begin{multicols}{3} 360 | 361 | \section{Monte Carlo Methods} 362 | Monte Carlo (MC) methods require only experience from actual or simulated environment. 363 | 364 | \subsection{MC Prediction} 365 | \begin{algorithm}[H] 366 | Inputs: $\pi$ - the policy to be evaluated \\ 367 | Initialize: $V(s) \in \mathbb{R}$ for all $s \in \mathcal{S}$ \\ 368 | $\text{Return(s)} \leftarrow \text{an empty list}$ for all $s \in \mathcal{S}$ \\ 369 | \While{forever - for each episode} 370 | { 371 | Generate an episode following $\pi$: $S_0, A_0, R_1, S_1, A_1,..., S_{T-1}, A_{T-1}, R_T$\\ 372 | $G \leftarrow$ 0 \\ 373 | \ForEach{step of episode, $t=T-1, T-2, ..., 0$} 374 | { 375 | $G \leftarrow \gamma G + R_{t+1}$\\ 376 | \If{$S_t$ is not in the sequence $S_0, S_1,..., S_{t-1}$ (i.e. it is the first visit to $S_t$) } 377 | { 378 | Append $G$ to Return($S_t$)\\ 379 | $V(S_t) \leftarrow$ average(Return($S_t$)) 380 | } 381 | } 382 | } 383 | \caption{On-policy First-visit Monte Carlo prediction - estimating $V \sim v_{\pi}$ [§5.1]} 384 | \end{algorithm} 385 | The \emph{first-visit} is the first time a particular state has been observed.\\ 386 | The \textbf{first-visit MC} method estimates $v_{\pi}(s)$ as the average of the returns following first visits to $s$, whereas the \textbf{every-visit MC} method averages the returns following all visits to $s$. 387 | The every-visit MC Prediction is derived from first-visit version removing the ``if'' condition. \\ 388 | In other words we move backward from the step $T$ and compute the $G$ incrementally and associate the values of $G$ to the current state and perform the average. 389 | 390 | \subsection{MC Estimation of Action Values} 391 | To determine a policy, if a model is not available, the state value is not sufficient and we have to estimate the values of state–action pairs.\\ 392 | The MC methods are essentially the same as just presented for state values, but now we have state–action pairs.\\ 393 | The only complication is that many state–action pairs may never be visited. We need to estimate the value of all the actions from each state, not just the one we currently favor. 394 | We can specify that the episodes start in a state–action pair, and that every pair has a nonzero probability of being selected as the start (assumption of \emph{exploring starts}). \\ 395 | 396 | \subsection{MC Control} 397 | \begin{algorithm}[H] 398 | Initialise: 399 | $\pi(s) \in \mathcal{A}(s)$ arbitrarily, for all $s \in \mathcal{S}$ \\ 400 | $Q(s,a) \in \mathbb{R}$ (arbitrarily) for all $s \in \mathcal{S}, a \in \mathcal{A}(s)$ \\ 401 | $ Returns(s,a) \leftarrow \text{empty list}$ for all $s \in \mathcal{S}, a \in \mathcal{A}(s)$ \\ 402 | 403 | \While{forever}{ 404 | Choose $S_0 \in S$ and $A_0 \in A(S_0)$, randomly such that all pairs have probability $ > 0$ \\ 405 | Generate an episode from $S_0, A_0$ following $\pi: S_0, A_0, R_1,...,S_{T-1},A_{T-1},R_T$\\ 406 | $G \leftarrow 0$ \\ 407 | \ForEach{step of episode, $t=T-1, T-2, ..., 0$} 408 | { 409 | $G \leftarrow \gamma G + R_{t+1}$\\ 410 | \If{$S_t, A_t$ pair is not seen before, is not in the sequence $S_0, A_0, S_1, A_1..., S_{t-1}, A_{t-1}$ } 411 | { 412 | Append $G$ to Returns($S_t, A_t$)\\ 413 | $Q(S_t, A_t) \leftarrow$ average(Returns($S_t,A_t$))\\ 414 | $\pi(S_t) \leftarrow \argmax_a (Q(S_t, a))$ 415 | } 416 | } 417 | } 418 | \caption{First-visit Monte Carlo (Exploring Starts) - estimating $\pi \sim \pi_*$ [§5.3]} 419 | \end{algorithm} 420 | 421 | To remove the exploring starts assumption, we can use an $\epsilon-$soft policy. Most of times it selects the greedy policy but with probability $\epsilon$ it instead selects an action at random.\\ 422 | Other approaches are the \emph{off-policy} methods that learn about the optimal policy while behaving according to a different exploratory policy. 423 | The policy being learned about is called the \emph{target policy}, $\pi$, and the policy used to generate behavior is called the \emph{behavior policy}, $b$ (usually an exploratory policy, e.g. random policy).\\ 424 | In order to use episodes from $b$ to estimate values for $\pi$, we require that every action taken under $\pi$ is also taken, at least occasionally, under $b$. That is, we require that $\pi(a|s) > 0$ implies $b(a|s) > 0$. This is called the \emph{assumption of coverage}. 425 | 426 | \subsection{Off-policy Every-visit MC Prediction} 427 | \begin{algorithm}[H] 428 | Inputs: $\pi$ - the policy to be evaluated \\ 429 | Initialize: $V(s) \in \mathbb{R}$ for all $s \in \mathcal{S}$ \\ 430 | $\text{Return(s)} \leftarrow \text{an empty list}$ for all $s \in \mathcal{S}$ \\ 431 | \While{forever - for each episode} 432 | { 433 | Generate an episode following $b$: $S_0, A_0, R_1, S_1, A_1,..., S_{T-1}, A_{T-1}, R_T$\\ 434 | $G \leftarrow$ 0 \\ 435 | $W \leftarrow$ 1 \\ 436 | \ForEach{step of episode, $t=T-1, T-2, ..., 0$} 437 | { 438 | $G \leftarrow \gamma W G + R_{t+1}$\\ 439 | Append $G$ to Return($S_t$)\\ 440 | $V(S_t) \leftarrow$ average(Return($S_t$)) 441 | $W \leftarrow W \frac{\pi(A_t|S_t)}{b(A_t|S_t)}$ 442 | } 443 | } 444 | \caption{Off-policy Every-visit Monte Carlo prediction - estimating $V \sim v_{\pi}$ [Course2-Week2]} 445 | \end{algorithm} 446 | 447 | \subsection{Incremental Implementation} 448 | The average used to compute $V(S_t)$, can be performed incrementally: 449 | \begin{align*} 450 | V_n(S_t) = \frac{1}{n} \sum_{i=1}^{n} G_i(t) = V_{n-1}(S_t) + \frac{1}{n} (G_n(t) - V_{n-1}(S_t)) \label{eq:incremental_mc}\numberthis 451 | \end{align*} 452 | 453 | \subsection{Off-policy MC Control} 454 | The policy used to generate behavior, called the behavior policy, may in fact be unrelated to the policy that is evaluated and improved, called the target policy. An advantage of this separation is that the target policy may be deterministic (e.g., greedy), while the behavior policy can continue to sample all possible actions. 455 | \begin{algorithm}[H] 456 | Initialize:for all $s \in \mathcal{S}$, $a \in \mathcal{A}(s)$ \\ 457 | $Q(s, a) \in \mathbb{R}$ 458 | $C(s,a) \leftarrow 0$ 459 | $\pi(s) \leftarrow \argmax_a +Q(s,a)$ 460 | 461 | \While{forever - for each episode} 462 | { 463 | $b \leftarrow any soft policy$ 464 | Generate an episode following $b$: $S_0, A_0, R_1, S_1, A_1,..., S_{T-1}, A_{T-1}, R_T$\\ 465 | $G \leftarrow$ 0 \\ 466 | $W \leftarrow$ 1 \\ 467 | \ForEach{step of episode, $t=T-1, T-2, ..., 0$} 468 | { 469 | $G \leftarrow \gamma G + R_{t+1}$ \\ 470 | $C(S_t,A_t) \leftarrow C(S_t,A_t) + W$ \\ 471 | $Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \frac{W}{C(S_t,A_t)}[G - Q(S_t,A_t)]$ \\ 472 | $\pi(s) \leftarrow \argmax_a +Q(S_T,a)$ \\ 473 | \If{$A_t \neq \pi(S_t)$} 474 | { 475 | exit For Loop 476 | } 477 | $W \leftarrow W \frac{1}{b(A_t|S_t)}$ 478 | } 479 | } 480 | \caption{Off-policy MC Control - estimating $\pi \sim \pi_*$ [§5.7]} 481 | \end{algorithm} 482 | 483 | \end{multicols} 484 | 485 | \newpage 486 | 487 | \begin{multicols}{3} 488 | 489 | \section{Temporal-Difference Learning} 490 | 491 | \subsection{TD Prediction} 492 | Starting from Eq. \ref{eq:incremental_mc}, we can consider a generic update rule of $V(S_t)$ 493 | 494 | \begin{align*} 495 | V(S_t) \leftarrow V(S_t) + \alpha (G_t - V(S_t)) \label{eq:constant_alpha_mc}\numberthis \quad [6.1] 496 | \end{align*} 497 | $\alpha$ is a constant step-size and we call previous method \emph{constant-$\alpha$ MC}. 498 | MC has to wait the end of an episode to determine the increment to $V(S_t)$. \\ 499 | Differently from MC, TD updates the value at each step of the episode following the equation below: 500 | 501 | \begin{align*} 502 | V(S_t) \leftarrow V(S_t) + \alpha (R_{t+1} + \gamma V(S_{t+1}) - V(S_t)) \label{eq:td_0}\numberthis \quad [6.2] 503 | \end{align*} 504 | 505 | \begin{algorithm}[H] 506 | Inputs: $\pi$ - the policy to be evaluated \\ 507 | Params: step size $\alpha \in ]0,1]$ \\ 508 | Initialize: $V(s) \in \mathbb{R}$ for all $s \in \mathcal{S}^+$ except for V(terminal)=0 \\ 509 | 510 | \ForEach{episode} 511 | { 512 | Initialize $S$\\ 513 | \ForEach{step of episode - until $S$ is terminal} 514 | { 515 | $A \leftarrow$ action given by $\pi$ for S\\ 516 | Take action $A$, observe $R$, $S'$\\ 517 | $V(S) \leftarrow V(S) + \alpha (R + \gamma V(S') - V(S))$ \\ 518 | $S \leftarrow S'$ 519 | } 520 | } 521 | \caption{Tabular TD(0) - estimating $v_{\pi}$ [§6.1]} 522 | \end{algorithm} 523 | 524 | Recall that: 525 | \begin{align*} 526 | v_{\pi}(s) & \stackrel{.}{=} \mathbb{E}_{\pi}[G_t | S_t = s] \label{eq:v_pi_MC} \numberthis \quad [3.12 | 6.3]\\ 527 | & = \mathbb{E}_{\pi}[R_{t+1} + \gamma G_{t+1} | S_t = s] \label{eq:v_pi_MC2} \numberthis \quad [by \ 3.9] \\ 528 | & = \mathbb{E}_{\pi}[R_{t+1} + \gamma v_{\pi}(S_{t+1}) | S_t = s] \label{eq:v_pi_TD} \numberthis \quad [6.4] \\ 529 | \end{align*} 530 | MC methods use an estimate of Eq. \ref{eq:v_pi_MC} as a target. 531 | The MC target is an estimate because the expected value in Eq. \ref{eq:v_pi_MC} is not known; a sample return is used in place of the real expected return.\\ 532 | 533 | DP and TD methods use an estimate of Eq. \ref{eq:v_pi_TD} as a target.\\ 534 | The DP target is an estimate because $v_{\pi}(S_{t+1})$ is not known and the current estimate, $V(S_{t+1})$, is used instead. \\ 535 | The TD target is an estimate because it samples the expected values in Eq. \ref{eq:v_pi_TD} and it uses the current estimate $V$ instead of the true $v_{\pi}$.\\ 536 | 537 | TD methods update their estimates based in part on other estimates. They learn a guess from a guess, i.e. they \textbf{bootstrap}. 538 | TD and MC methods have an advantage over DP methods in that they do not require a model of the environment, of its reward and next-state probability distributions. 539 | 540 | The most obvious advantage of TD methods over MC methods is that they are naturally implemented in an online, fully incremental fashion. 541 | With MC methods one must wait until the end of an episode, because only then the return is known, whereas with TD methods one needs wait only one time step. 542 | In practice, TD methods have usually been found to converge faster than constant-$\alpha$ MC methods on stochastic tasks.\\ 543 | The error, available at time $t+1$, between $V(S_t)$ and the better estimate $R_{t+1} + \gamma V(S_{t+1})$ is called \emph{TD error}: 544 | \begin{equation*} 545 | \delta_t \stackrel{.}{=} R_{t+1} + \gamma V(S_{t+1}) - V(S_t) \numberthis \quad [6.5] 546 | \end{equation*} 547 | 548 | \subsection{Sarsa - On-policy TD Control} 549 | Sarsa (State-action-reward-state-action) is an on-policy TD control. 550 | Sarsa is sample-based version of policy iteration which uses Bellman equations for action values. 551 | The update rule: 552 | 553 | \begin{align*} 554 | Q(S_t, A_t) \leftarrow & \\ 555 | Q(S_t, A_t) + & \alpha \left[R_{t+1} + \gamma Q(S_{t+1}, A_{t+1}) - Q(S_t, A_t) \right] \numberthis \quad [6.7] 556 | \end{align*} 557 | 558 | \begin{algorithm}[H] 559 | Params: step size $\alpha \in ]0,1]$, small $\epsilon > 0$ \\ 560 | Initialize $Q(s,a)$ for all $s \in \mathcal{S}^+$ and $a \in \mathcal{A}(s)$, arbitrarily except that $Q(terminal-state, \cdot) = 0$\\ 561 | \ForEach{episode} 562 | { 563 | Initialize $S$\\ 564 | Choose $A$ from $S$ using policy derived from $Q$ (e.g. $\epsilon$-greedy)\\ 565 | \ForEach{step of episode - until $S$ is terminal} 566 | { 567 | Take action $A$, observe $R$, $S'$\\ 568 | Choose $A'$ from $S'$ using policy derived from $Q$ (e.g. $\epsilon$-greedy)\\ 569 | $Q(S, A) \leftarrow Q(S, A) + \alpha \left[R + \gamma Q(S', A') - Q(S, A) \right]$\\ 570 | $S \leftarrow S'$\\ 571 | $A \leftarrow A'$ 572 | } 573 | } 574 | \caption{Sarsa - On-policy TD Control - estimating $Q \sim q_*$ [§6.4]} 575 | \end{algorithm} 576 | 577 | \subsection{Q-Learning - Off-policy TD Control} 578 | Q-Learning is an off-policy TD control. 579 | Q-learning is a sample-based version of value iteration which iteratively applies the Bellman's optimality equation. 580 | The update rule: 581 | 582 | \begin{align*} 583 | Q(S_t, A_t) \leftarrow & \\ 584 | Q(S_t, A_t) + & \alpha \left[R_{t+1} + \gamma \max_a Q(S_{t+1}, a) - Q(S_t, A_t) \right] \numberthis \quad [6.8] 585 | \end{align*} 586 | 587 | \begin{algorithm}[H] 588 | Params: step size $\alpha \in ]0,1]$, small $\epsilon > 0$\\ 589 | Initialize $Q(s,a)$ for all $s \in \mathcal{S}^+$ and $a \in \mathcal{A}(s)$, arbitrarily except that $Q(terminal-state, \cdot) = 0$\\ 590 | \ForEach{episode} 591 | { 592 | Initialize $S$\\ 593 | \ForEach{step of episode - until $S$ is terminal} 594 | { 595 | Choose $A$ from $S$ using policy derived from $Q$ (e.g. $\epsilon$-greedy)\\ 596 | Take action $A$, observe $R$, $S'$\\ 597 | $Q(S, A) \leftarrow Q(S, A) + \alpha \left[R + \gamma \max_a Q(S', a) - Q(S, A) \right]$\\ 598 | $S \leftarrow S'$ 599 | } 600 | } 601 | \caption{Q-Learning - Off-policy TD Control - estimating $\pi \sim \pi_*$ [§6.5]} 602 | \end{algorithm} 603 | 604 | \subsection{Expected Sarsa} 605 | Similar to Q-Learning, the update rule of Expected Sarsa, takes the expected value instead of the maximum over the next state: 606 | \begin{align*} 607 | Q(S_t, A_t) \leftarrow & \\ 608 | Q(S_t, A_t) + \alpha [R_{t+1} & + \gamma \mathbb{E}_{\pi} [ Q(S_{t+1}, A_{t+1}) | S_{t+1} ] - Q(S_t, A_t) ] \\ 609 | Q(S_t, A_t) + \alpha [R_{t+1} & + \gamma \sum_a \pi(a| S_{t+1}) Q(S_{t+1}, a) - Q(S_t, A_t)] \numberthis \quad [6.9] 610 | \end{align*} 611 | 612 | The next action is sampled from $\pi$. However, the expectation over actions is computed independently of the action actually selected in the next state. 613 | In fact, it is not necessary that $\pi$ is equal to the behavior policy. This means that Expected Sarsa, like Q-learning, can be used to learn off-policy without importance sampling. \\ 614 | If the target policy is greedy with respect to its action value estimates we obtain the Q-Learning. Hence Q-Learning is a special case of Expected Sarsa. 615 | \end{multicols} 616 | 617 | \newpage 618 | \begin{multicols}{3} 619 | \section{\emph{n}-step Bootstrapping} 620 | 621 | \subsection{\emph{n}-step TD Prediction} 622 | 623 | MC methods updates the estimate of $v_{\pi}(S_t)$ for each state based on the entire sequence of observed rewards from that state until the end of the episode using: 624 | 625 | \begin{equation} 626 | G_t \stackrel{.}{=} R_{t+1} + \gamma R_{t+2} + ... + \gamma^{T-t-1}R_T \numberthis 627 | \end{equation} 628 | 629 | In the one-step TD, instead, the update, is based on just the one next reward, bootstrapping from the value of the state one step later as a proxy for the remaining rewards using \emph{one-step return}: 630 | 631 | \begin{equation} 632 | G_{t:t+1}\stackrel{.}{=} R_{t+1} + \gamma V_t(S_{t+1}) \numberthis 633 | \end{equation} 634 | 635 | In the n-step TD, it is used the \emph{n-step return}: 636 | 637 | \begin{equation} 638 | G_{t:t+n}\stackrel{.}{=} R_{t+1} + \gamma R_{t+2} + ... + \gamma^{n-1} R_{t+n} + \gamma^n V_{t+n-1}(S_{t+n}) \numberthis 639 | \end{equation} 640 | 641 | \end{multicols} 642 | 643 | \newpage 644 | 645 | \begin{multicols}{3} 646 | \section{Planning and Learning} 647 | Planning methods use simulated experience generated by a model, learning methods use real experience generated by the environment. 648 | Many ideas and algorithms can be transferred between planning and learning. 649 | 650 | \begin{algorithm}[H] 651 | Params: step size $\alpha \in ]0,1]$, small $\epsilon > 0$\\ 652 | Initialize $Q(s,a)$ for all $s \in \mathcal{S}^+$ and $a in \mathcal{A}(s)$, arbitrarily except that $Q(terminal-state, \cdot) = 0$\\ 653 | \ForEach{episode} 654 | { 655 | 1. Select a state $S \in \mathcal{S}$, and an action, $A \in \mathcal{A}(s)$, at random\\ 656 | 2. From a Sample Model obtain the sample reward and next state following: R, S' = model (S,A)\\ 657 | 3. Apply one-step tabular Q-Learning to $S, A, R, S'$:\\ 658 | $Q(S, A) \leftarrow Q(S, A) + \alpha \left[R + \gamma \max_a Q(S', a) - Q(S, A) \right]$\\ 659 | } 660 | \caption{Random-sample one-step tabular Q-planning [§8.1]} 661 | \end{algorithm} 662 | 663 | \subsection{Dyna} 664 | Within a planning agent, there are at least two roles for real experience: \\ 665 | (1) it can be used to improve the model (to match more accurately the real environment), \emph{model learning},\\ 666 | (2) it can be used to directly improve the value function and policy using the kinds reinforcement learning methods discussed before, \emph{direct RL} [§8.2].\\ 667 | \begin{center} 668 | \includegraphics[width=0.6\linewidth]{./images/Planning-Learning.png} 669 | \end{center} 670 | The experience can improve value functions and policies either directly or indirectly via the model (\emph{indirect RL}). 671 | The real experience obtained with the interaction with the environment can be used to improve directly the Policy/value function (direct RL) or indirectly through the model learning and the planning that use simulated experience (\emph{indirect RL}) [§8.2].\\ 672 | \begin{center} 673 | \includegraphics[width=0.8\linewidth]{./images/Dyna.png} 674 | \end{center} 675 | 676 | \begin{algorithm}[H] 677 | Initialize $Q(s,a)$ and $Model(S,A)$ for all $s \in \mathcal{S}^+$ and $a \in \mathcal{A}(s)$\\ 678 | \While{forever} 679 | { 680 | (a) $S \leftarrow$ current (nonterminal) state \\ 681 | (b) $A \leftarrow \epsilon$-greedy$(S, Q)$ \\ 682 | (c) Take action $A$; observe resultant reward, $R$, and state, $S'$ \\ 683 | (d) $Q(S, A) \leftarrow Q(S, A) + \alpha \left[R + \gamma \max_a Q(S', a) - Q(S, A) \right]$ \\ 684 | (e) $Model(S, A) \leftarrow R, S'$ (assuming deterministic environment) \\ 685 | (f) \ForEach{n times} 686 | { 687 | $S \leftarrow$ random previously observed state \\ 688 | $A \leftarrow$ random action previosuly taken in $S$\\ 689 | $R, S' \leftarrow Model(S, A)$ \\ 690 | $Q(S, A) \leftarrow Q(S, A) + \alpha \left[R + \gamma \max_a Q(S', a) - Q(S, A) \right]$ \\ 691 | } 692 | } 693 | \caption{Dyna-Q [§8.2]} 694 | \end{algorithm} 695 | 696 | (d) Direct reinforcement learning\\ 697 | (e) Model-learning\\ 698 | (f) Planning\\ 699 | 700 | \vfill\null 701 | \columnbreak 702 | 703 | If (e) and (f) were omitted, the remaining algorithm would be one-step tabular Q-learning.\\ 704 | 705 | The agent responds instantly to the latest sensory information and yet always planning in the background. Also the model-learning process is in background. As new information is gained, the model is updated to better match reality. As the model changes, the ongoing planning process will gradually compute a different way of behaving to match the new model.\\ 706 | 707 | Models may be incorrect for many reasons: environment is stochastic and only a limited number of samples have been observed, the model was learned using function approximation that has generalized imperfectly, the environment has changed and its new behavior has not yet been observed. 708 | When the model is incorrect, the planning process is likely to compute a suboptimal policy. In some cases, the suboptimal policy computed by planning quickly leads to the discovery and correction of the modeling error. This happens when the model is optimistic, predicting greater reward or better state transition than are actually possible. It is more difficult to correct a model when the environment becomes better than it was before.\\ 709 | In Figure below there are represented the relation among algorithms presented in the Course on Coursera [Course3-Week1].\\ 710 | \begin{center} 711 | \includegraphics[width=1\linewidth]{./images/Tabular_Value_Function.png} 712 | \end{center} 713 | \end{multicols} 714 | 715 | \newpage 716 | 717 | \begin{multicols}{3} 718 | 719 | \section{On-policy Prediction with Function Approximation} 720 | We can approximate value function not as a table but as a parametrized functional form: $\hat{v}(s,\mathbf{w}) \sim v_{\pi}(s)$ where $\mathbf{w} \in \mathbb{R}^d$ and the number of weights is much less than the number of states ($d << |\mathcal{S}|$). 721 | The value estimation can be framed as a Supervised Learning problem.\\ 722 | The Monte Carlo methods estimate the value function using samples of the return so the input is the state and the targets are the returns (pairs $(S_i, G_i)$). \\ 723 | For TD methods the targets are the one-step bootstrap return (pairs $(S_i, R_{i+1} + \gamma \hat{v}(S_{i+1}, \mathbf{w})$).\\ 724 | In RL setting, the data is temporally correlated and the full dataset is not fixed and available from the beginning. Moreover, due to the bootstrapping methods (TD, DP), the target labels change.\\ 725 | In tabular case the learned values at each state were decoupled - an update at one state affected no other. 726 | Now making one state's estimate more accurate means making others' less accurate. 727 | 728 | \begin{equation} 729 | \overline{VE}(\mathbf{w}) \stackrel{.}{=} \sum_{s \in \mathcal{S})} \mu(s) \left [ v_{\pi}(s) - \hat{v}(s, \mathbf{w}) \right ]^2 \label{eq:value_error} \numberthis \quad [9.1] 730 | \end{equation} 731 | where $\mu(s)$ is a state distribution ($\mu(s) \ge 0$ and $\sum_s \mu(s) = 1$) representing how much we care about the error in each state $s$. 732 | Usually to minimize the \ref{eq:value_error} it is used the Stochastic Gradient Descent (SGD): 733 | 734 | \begin{align*} 735 | \mathbf{w}_{t+1} & \stackrel{.}{=} \mathbf{w}_{t} - \frac{1}{2} \alpha \nabla \left [ v_{\pi}(S_t) - \hat{v}(S_t, \mathbf{w}_t) \right ]^2 \numberthis \quad [9.4]\\ 736 | & = \mathbf{w}_{t} - \alpha \left [ v_{\pi}(S_t) - \hat{v}(S_t, \mathbf{w}_t) \right ] \nabla \hat{v}(S_t, \mathbf{w}_t) \numberthis \quad [9.5] 737 | \end{align*} 738 | Usually we have only an approximation $U_t$ of $v_{\pi}(S_t)$ but, if $U_t$ is an unbiased estimation of $v_{\pi}(S_t)$, that is $\mathbb{E}[U_t|S_t=s] = v_{\pi}(S_t)$, for each $t$, then $\mathbf{w}_t$ is guaranteed to converge to a local optimum (under the stochastic approximation condition for decreasing $\alpha$). \\ 739 | For MC, $U_t \stackrel{.}{=} G_t$ and hence $U_t$ is an unbiased estimation of $v_{\pi}(S_t)$. 740 | 741 | \begin{algorithm}[H] 742 | Inputs: $\pi$ - the policy to be evaluated \\ 743 | a differentiable function $\hat{v} : \mathcal{S} \times \mathbb{R}^d \rightarrow \mathbb{R}$ \\ 744 | Parameters: step size $\alpha > 0$ \\ 745 | Initialize: $\mathbf{w} \in \mathbb{R}^d$ arbitrarily (e.g. $\mathbb{w} = 0$) \\ 746 | \While{forever - for each episode} 747 | { 748 | Generate an episode following $\pi$: $S_0, A_0, R_1, S_1, A_1,..., S_{T-1}, A_{T-1}, R_T$\\ 749 | \ForEach{step of episode, $t=0,1,..., T-1$} 750 | { 751 | $\mathbf{w} \leftarrow \mathbf{w} - \alpha \left [ G_t - \hat{v}(S_t, \mathbf{w}_t) \right ] \nabla \hat{v}(S_t, \mathbf{w})$ 752 | } 753 | } 754 | \caption{Gradient MC - Estimating $v \sim v_{\pi}$ [§9.3]} 755 | \end{algorithm} 756 | 757 | \rule{0.3\linewidth}{0.25pt} \\ 758 | \scriptsize 759 | \href{https://github.com/linker81/Reinforcement-Learning-CheatSheet}{https://github.com/linker81/Reinforcement-Learning-CheatSheet} 760 | \end{multicols} 761 | 762 | \end{document} 763 | --------------------------------------------------------------------------------