📚 The CoCalc Library - books, templates and other resources
cocalc-examples / martinthoma-latex-examples / source-code / Pseudocode / sarsa-lambda / sarsa-lambda.tex
132937 viewsLicense: 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[noend]{algpseudocode}1213\DeclareCaptionFormat{myformat}{#3}14\captionsetup[algorithm]{format=myformat}1516\begin{document}17\begin{preview}18\begin{algorithm}[H]19\begin{algorithmic}20\Require21\Statex Sates $\mathcal{X} = \{1, \dots, n_x\}$22\Statex Actions $\mathcal{A} = \{1, \dots, n_a\},\qquad A: \mathcal{X} \Rightarrow \mathcal{A}$23\Statex Reward function $R: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$24\Statex Black-box (probabilistic) transition function $T: \mathcal{X} \times \mathcal{A} \rightarrow \mathcal{X}$25\Statex Learning rate $\alpha \in [0, 1]$, typically $\alpha = 0.1$26\Statex Discounting factor $\gamma \in [0, 1]$27\Statex $\lambda \in [0, 1]$: Trade-off between TD and MC28\Procedure{QLearning}{$\mathcal{X}$, $A$, $R$, $T$, $\alpha$, $\gamma$, $\lambda$}29\State Initialize $Q: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$ arbitrarily30\State Initialize $e: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$ with 0. \Comment{eligibility trace}31% \State Start in state $s \in \mathcal{X}$32\While{$Q$ is not converged}33\State Select $(s, a) \in \mathcal{X} \times \mathcal{A}$ arbitrarily34\While{$s$ is not terminal}35\State $r \gets R(s, a)$36\State $s' \gets T(s, a)$ \Comment{Receive the new state}37\State Calculate $\pi$ based on $Q$ (e.g. epsilon-greedy)38\State $a' \gets \pi(s')$39\State $e(s, a) \gets e(s, a) + 1$40\State $\delta \gets r + \gamma \cdot Q(s', a') - Q(s, a)$41\For{$(\tilde{s}, \tilde{a}) \in \mathcal{X} \times \mathcal{A}$}42\State $Q(\tilde{s}, \tilde{a}) \gets Q(\tilde{s}, \tilde{a}) + \alpha \cdot \delta \cdot e(\tilde{s}, \tilde{a})$43\State $e(\tilde{s}, \tilde{a}) \gets \gamma \cdot \lambda \cdot e(\tilde{s}, \tilde{a})$44\EndFor45\State $s \gets s'$46\State $a \gets a'$47\EndWhile48\EndWhile49\Return $Q$50\EndProcedure51\end{algorithmic}52\caption{SARSA($\lambda$): Learn function $Q: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$}53\label{alg:sarsa-lambda}54\end{algorithm}55\end{preview}56\end{document}5758