浏览代码

Add dynamic programming and label correction algorithm

Martin Thoma 9 年之前
父节点
当前提交
b9e2162ab8

+ 36 - 0
source-code/Pseudocode/dynamic-programming/Makefile

@@ -0,0 +1,36 @@
+SOURCE = dynamic-programming
+DELAY = 80
+DENSITY = 300
+WIDTH = 512
+
+make:
+	pdflatex $(SOURCE).tex -output-format=pdf
+	pdflatex $(SOURCE).tex -output-format=pdf
+	make clean
+
+clean:
+	rm -rf  $(TARGET) *.class *.html *.log *.aux *.data *.gnuplot
+
+gif:
+	pdfcrop $(SOURCE).pdf
+	convert -verbose -delay $(DELAY) -loop 0 -density $(DENSITY) $(SOURCE)-crop.pdf $(SOURCE).gif
+	make clean
+
+png:
+	make
+	make svg
+	inkscape $(SOURCE).svg -w $(WIDTH) --export-png=$(SOURCE).png
+
+transparentGif:
+	convert $(SOURCE).pdf -transparent white result.gif
+	make clean
+
+svg:
+	make
+	#inkscape $(SOURCE).pdf --export-plain-svg=$(SOURCE).svg
+	pdf2svg $(SOURCE).pdf $(SOURCE).svg
+	# Necessary, as pdf2svg does not always create valid svgs:
+	inkscape $(SOURCE).svg --export-plain-svg=$(SOURCE).svg
+	rsvg-convert -a -w $(WIDTH) -f svg $(SOURCE).svg -o $(SOURCE)2.svg
+	inkscape $(SOURCE)2.svg --export-plain-svg=$(SOURCE).svg
+	rm $(SOURCE)2.svg

+ 3 - 0
source-code/Pseudocode/dynamic-programming/README.md

@@ -0,0 +1,3 @@
+Compiled example
+----------------
+![Example](dynamic-programming.png)

二进制
source-code/Pseudocode/dynamic-programming/dynamic-programming.png


+ 44 - 0
source-code/Pseudocode/dynamic-programming/dynamic-programming.tex

@@ -0,0 +1,44 @@
+\documentclass{article}
+\usepackage[pdftex,active,tightpage]{preview}
+\setlength\PreviewBorder{2mm}
+
+\usepackage[utf8]{inputenc} % this is needed for umlauts
+\usepackage[ngerman]{babel} % this is needed for umlauts
+\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
+\usepackage{amssymb,amsmath,amsfonts} % nice math rendering
+\usepackage{braket} % needed for \Set
+\usepackage{caption}
+\usepackage{algorithm}
+\usepackage[noend]{algpseudocode}
+
+\DeclareCaptionFormat{myformat}{#3}
+\captionsetup[algorithm]{format=myformat}
+
+\begin{document}
+\begin{preview}
+    \begin{algorithm}[H]
+        \begin{algorithmic}
+        \Require
+        \Statex Sates $\mathcal{X} = \{1, \dots, n_x\}$
+        \Statex Actions $\mathcal{A} = \{1, \dots, n_a\},\qquad A: \mathcal{X} \Rightarrow \mathcal{A}$
+        \Statex Cost function $g: \mathcal{X} \times \mathcal{A} \rightarrow \mathbb{R}$
+        \Statex Horizon $N$
+        \Procedure{DynamicProgramming}{$\mathcal{X}$, $A$, $g$, $N$}
+            \State $J_N(x) \gets g_N(x) \quad \forall x \in \mathcal{X}$
+            \For{$k = N - 1$ to $0$}
+                \For{$x \in \mathcal{X}$}
+                    \For{$a \in A(x)$}
+                        \State $Q(x, a) \gets g_k(x, a) + \alpha \sum_{j=1}^{n_x} f_{xj}(a) \cdot J_{k+1}(j)$
+                    \EndFor
+
+                    \State $J_k (x) \gets \min_a (Q_k(x,a))$
+                    \State $\pi_k(x) \gets \arg \min_a (Q_k(x, a))$
+                \EndFor
+            \EndFor
+        \EndProcedure
+        \end{algorithmic}
+    \caption{Dynamic Programming}
+    \label{alg:dynamic-programming}
+    \end{algorithm}
+\end{preview}
+\end{document}

+ 36 - 0
source-code/Pseudocode/label-correction/Makefile

@@ -0,0 +1,36 @@
+SOURCE = label-correction
+DELAY = 80
+DENSITY = 300
+WIDTH = 512
+
+make:
+	pdflatex $(SOURCE).tex -output-format=pdf
+	pdflatex $(SOURCE).tex -output-format=pdf
+	make clean
+
+clean:
+	rm -rf  $(TARGET) *.class *.html *.log *.aux *.data *.gnuplot
+
+gif:
+	pdfcrop $(SOURCE).pdf
+	convert -verbose -delay $(DELAY) -loop 0 -density $(DENSITY) $(SOURCE)-crop.pdf $(SOURCE).gif
+	make clean
+
+png:
+	make
+	make svg
+	inkscape $(SOURCE).svg -w $(WIDTH) --export-png=$(SOURCE).png
+
+transparentGif:
+	convert $(SOURCE).pdf -transparent white result.gif
+	make clean
+
+svg:
+	make
+	#inkscape $(SOURCE).pdf --export-plain-svg=$(SOURCE).svg
+	pdf2svg $(SOURCE).pdf $(SOURCE).svg
+	# Necessary, as pdf2svg does not always create valid svgs:
+	inkscape $(SOURCE).svg --export-plain-svg=$(SOURCE).svg
+	rsvg-convert -a -w $(WIDTH) -f svg $(SOURCE).svg -o $(SOURCE)2.svg
+	inkscape $(SOURCE)2.svg --export-plain-svg=$(SOURCE).svg
+	rm $(SOURCE)2.svg

+ 3 - 0
source-code/Pseudocode/label-correction/README.md

@@ -0,0 +1,3 @@
+Compiled example
+----------------
+![Example](label-correction.png)

二进制
source-code/Pseudocode/label-correction/label-correction.png


+ 51 - 0
source-code/Pseudocode/label-correction/label-correction.tex

@@ -0,0 +1,51 @@
+\documentclass{article}
+\usepackage[pdftex,active,tightpage]{preview}
+\setlength\PreviewBorder{2mm}
+
+\usepackage[utf8]{inputenc} % this is needed for umlauts
+\usepackage[ngerman]{babel} % this is needed for umlauts
+\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
+\usepackage{amssymb,amsmath,amsfonts} % nice math rendering
+\usepackage{braket} % needed for \Set
+\usepackage{caption}
+\usepackage{algorithm}
+\usepackage[noend]{algpseudocode}
+
+\DeclareCaptionFormat{myformat}{#3}
+\captionsetup[algorithm]{format=myformat}
+
+\begin{document}
+\begin{preview}
+    \begin{algorithm}[H]
+        \begin{algorithmic}
+        \Require
+        \Statex Graph $G = (V, E)$
+        \Statex Starting node $s \in V$
+        \Statex End node $t \in V$
+        \Procedure{LabelCorrection}{$G$, $s$, $t$}
+            \State $d_s \gets 0$
+            \State $d_i \gets \infty \quad \forall i \neq s$
+            \State $u \gets \infty$
+            \State $K \gets \{s\}$ \Comment{Choose some datastructure here}
+            \While{$K$ is not empty}
+                \State $v \gets K.pop()$
+                \For{child $c$ of $v$}
+                    \If{$d_v + g_{vc} < \min(d_c, u)$}
+                        \State $d_v \gets d_v + g_{vc}$
+                        \State $c.parent \gets v$
+                        \If{$c \neq t$ and $c \notin K$}
+                            \State $K.insert(c)$
+                        \EndIf
+                        \If{$c = t$}
+                            \State $u \gets d_v + g_{vt}$
+                        \EndIf
+                    \EndIf
+                \EndFor
+            \EndWhile
+        \EndProcedure
+        \end{algorithmic}
+    \caption{Label correction algorithm}
+    \label{alg:label-correction-algorithm}
+    \end{algorithm}
+\end{preview}
+\end{document}