q-lambda.tex 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. \documentclass{article}
  2. \usepackage[pdftex,active,tightpage]{preview}
  3. \setlength\PreviewBorder{2mm}
  4. \usepackage[utf8]{inputenc} % this is needed for umlauts
  5. \usepackage[ngerman]{babel} % this is needed for umlauts
  6. \usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
  7. \usepackage{amssymb,amsmath,amsfonts} % nice math rendering
  8. \usepackage{braket} % needed for \Set
  9. \usepackage{caption}
  10. \usepackage{algorithm}
  11. \usepackage{xcolor}
  12. \usepackage[noend]{algpseudocode}
  13. \usepackage{mathtools,bm}
  14. \DeclareMathOperator*{\argmax}{arg\,max}
  15. \DeclareCaptionFormat{myformat}{#3}
  16. \captionsetup[algorithm]{format=myformat}
  17. \begin{document}
  18. \begin{preview}
  19. \begin{algorithm}[H]
  20. \begin{algorithmic}
  21. \Require
  22. \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. \Statex $\lambda \in [0, 1]$: Trade-off between TD and MC
  29. \Procedure{QLearning}{$\mathcal{X}$, $A$, $R$, $T$, $\alpha$, $\gamma$, $\lambda$}
  30. \State Initialize $Q: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$ arbitrarily
  31. \State Initialize $e: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$ with 0. \Comment{eligibility trace}
  32. % \State Start in state $s \in \mathcal{X}$
  33. \While{$Q$ is not converged}
  34. \State Select $(s, a) \in \mathcal{X} \times \mathcal{A}$ arbitrarily
  35. \While{$s$ is not terminal}
  36. \State $r \gets R(s, a)$
  37. \State $s' \gets T(s, a)$ \Comment{Receive the new state}
  38. \State Calculate $\pi$ based on $Q$ (e.g. epsilon-greedy)
  39. \State {$\color{red} a^* \gets \argmax_{\tilde{a}} Q(s', \tilde{a})$}
  40. \State $a' \gets \pi(s')$
  41. \State $e(s, a) \gets e(s, a) + 1$
  42. \State $\delta \gets r + \gamma \cdot Q(s', {\color{red} a^*}) - Q(s, a)$
  43. \For{$(\tilde{s}, \tilde{a}) \in \mathcal{X} \times \mathcal{A}$}
  44. \State $Q(\tilde{s}, \tilde{a}) \gets Q(\tilde{s}, \tilde{a}) + \alpha \cdot \delta \cdot e(\tilde{s}, \tilde{a})$
  45. \State ${\color{red} e(\tilde{s}, \tilde{a}) \gets \begin{cases}\gamma \cdot \lambda \cdot e(\tilde{s}, \tilde{a})&\text{if } a' = a^*\\
  46. 0 &\text{otherwise}\end{cases}}$
  47. \EndFor
  48. \State $s \gets s'$
  49. \State $a \gets a'$
  50. \EndWhile
  51. \EndWhile
  52. \Return $Q$
  53. \EndProcedure
  54. \end{algorithmic}
  55. \caption{SARSA($\lambda$): Learn function $Q: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$}
  56. \label{alg:sarsa-lambda}
  57. \end{algorithm}
  58. \end{preview}
  59. \end{document}