📚 The CoCalc Library - books, templates and other resources
cocalc-examples / martinthoma-latex-examples / source-code / Pseudocode / q-learning / q-learning.tex
132930 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}15\DeclareMathOperator*{\argmax}{arg\,max}1617\begin{document}18\begin{preview}19\begin{algorithm}[H]20\begin{algorithmic}21\Require22\Statex Sates $\mathcal{X} = \{1, \dots, n_x\}$23\Statex Actions $\mathcal{A} = \{1, \dots, n_a\},\qquad A: \mathcal{X} \Rightarrow \mathcal{A}$24\Statex Reward function $R: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$25\Statex Black-box (probabilistic) transition function $T: \mathcal{X} \times \mathcal{A} \rightarrow \mathcal{X}$26\Statex Learning rate $\alpha \in [0, 1]$, typically $\alpha = 0.1$27\Statex Discounting factor $\gamma \in [0, 1]$28\Procedure{QLearning}{$\mathcal{X}$, $A$, $R$, $T$, $\alpha$, $\gamma$}29\State Initialize $Q: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$ arbitrarily30\While{$Q$ is not converged}31\State Start in state $s \in \mathcal{X}$32\While{$s$ is not terminal}33\State Calculate $\pi$ according to Q and exploration strategy (e.g. $\pi(x) \gets \argmax_{a} Q(x, a)$)34\State $a \gets \pi(s)$35\State $r \gets R(s, a)$ \Comment{Receive the reward}36\State $s' \gets T(s, a)$ \Comment{Receive the new state}37\State $Q(s', a) \gets (1 - \alpha) \cdot Q(s, a) + \alpha \cdot (r + \gamma \cdot \max_{a'} Q(s', a'))$38\State $s \gets s'$39\EndWhile40\EndWhile41\Return $Q$42\EndProcedure43\end{algorithmic}44\caption{$Q$-learning: Learn function $Q: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$}45\label{alg:q-learning}46\end{algorithm}47\end{preview}48\end{document}495051