📚 The CoCalc Library - books, templates and other resources
License: OTHER
\documentclass{article}1\usepackage[pdftex,active,tightpage]{preview}2\setlength\PreviewBorder{2mm}34\usepackage[utf8]{inputenc} % this is needed for umlauts5\usepackage[ngerman]{babel} % this is needed for umlauts6\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf7\usepackage{amssymb,amsmath,amsfonts} % nice math rendering8\usepackage{braket} % needed for \Set9\usepackage{caption}10\usepackage{algorithm}11\usepackage{xcolor}12\usepackage[noend]{algpseudocode}13\usepackage{mathtools,bm}14\DeclareMathOperator*{\argmax}{arg\,max}1516\DeclareCaptionFormat{myformat}{#3}17\captionsetup[algorithm]{format=myformat}1819\begin{document}20\begin{preview}21\begin{algorithm}[H]22\begin{algorithmic}23\Require24\Statex Sates $\mathcal{X} = \{1, \dots, n_x\}$25\Statex Actions $\mathcal{A} = \{1, \dots, n_a\},\qquad A: \mathcal{X} \Rightarrow \mathcal{A}$26\Statex Reward function $R: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$27\Statex Black-box (probabilistic) transition function $T: \mathcal{X} \times \mathcal{A} \rightarrow \mathcal{X}$28\Statex Learning rate $\alpha \in [0, 1]$, typically $\alpha = 0.1$29\Statex Discounting factor $\gamma \in [0, 1]$30\Statex $\lambda \in [0, 1]$: Trade-off between TD and MC31\Procedure{QLearning}{$\mathcal{X}$, $A$, $R$, $T$, $\alpha$, $\gamma$, $\lambda$}32\State Initialize $Q: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$ arbitrarily33\State Initialize $e: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$ with 0. \Comment{eligibility trace}34% \State Start in state $s \in \mathcal{X}$35\While{$Q$ is not converged}36\State Select $(s, a) \in \mathcal{X} \times \mathcal{A}$ arbitrarily37\While{$s$ is not terminal}38\State $r \gets R(s, a)$39\State $s' \gets T(s, a)$ \Comment{Receive the new state}40\State Calculate $\pi$ based on $Q$ (e.g. epsilon-greedy)41\State {$\color{red} a^* \gets \argmax_{\tilde{a}} Q(s', \tilde{a})$}42\State $a' \gets \pi(s')$43\State $e(s, a) \gets e(s, a) + 1$44\State $\delta \gets r + \gamma \cdot Q(s', {\color{red} a^*}) - Q(s, a)$45\For{$(\tilde{s}, \tilde{a}) \in \mathcal{X} \times \mathcal{A}$}46\State $Q(\tilde{s}, \tilde{a}) \gets Q(\tilde{s}, \tilde{a}) + \alpha \cdot \delta \cdot e(\tilde{s}, \tilde{a})$47\State ${\color{red} e(\tilde{s}, \tilde{a}) \gets \begin{cases}\gamma \cdot \lambda \cdot e(\tilde{s}, \tilde{a})&\text{if } a' = a^*\\480 &\text{otherwise}\end{cases}}$49\EndFor50\State $s \gets s'$51\State $a \gets a'$52\EndWhile53\EndWhile54\Return $Q$55\EndProcedure56\end{algorithmic}57\caption{Q($\lambda$): Learn function $Q: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$}58\label{alg:q-lambda}59\end{algorithm}60\end{preview}61\end{document}626364