2

I have been trying to format the pseudocode below combining algorithmicx and algpseudocode commands as well as following another related post

algorithmicx or algpseudocode custom command indentation within a while loop or if statement1

The following pseudocode is what I am trying to reproduce.

Algorithm 1

\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage{algpseudocode}
\algnewcommand\algorithmicgenerate{\textbf{GENERATE}}
\algnewcommand\GENERATE[1]{\State\algorithmicgenerate\ #1}
\algnewcommand\algorithmicset{\textbf{SET}}
\algnewcommand\SET[1]{\State\algorithmicset\ #1}
\algloop{FOR}

\begin{document}

\begin{algorithm}[H]

\begin{algorithmic}[1]
 \GENERATE{$S_t^{(i)} \qquad ,i=1,...,I,t=1,...,M$}
 \SET{$C_t^0(S_t^{(i)})=0 \qquad
 ,i=1,...,I,t=1,...,M$}
 \SET{$C_{M+1}^n(S_{M+1}^{(i)})=0 \qquad ,i=1,...,I,n=0,1,...,N$}
 \SET{$\hat{\Phi}_t^{(n)}(S_t^{(i)})=0 \qquad, i=1,...,I,t=1,...,M,n=0,1,...,N$}
 \FOR{$\quad t=M,...,1$}
  \SET{$\iota_t^{(i)} = \arg\max_{a_t^{(i)}\in A}\{P(S_t^{(i)};a_t^{(i)})\} \qquad ,i=1,...,I$}
  \FOR{$\, n=\max(1,N-t),...,N$}

\end{algorithmic}
\end{algorithm}
\end{document}

Which compiles to Ouput

*As you can see Line 7 would not allow me to nest a second FOR loop indented at the same level as Line 6.

Are there any custom commands more suitable for what I am trying to achieve as i honestly find it a bit confusing?

Any help would be greatly appreciated.*

1 Answer 1

1

Well, the correct code for an for loop in your case would be:

  \For{$\, n=\max(1,N-t),...,N$}
    \SET{$\iota_t^{(i)} = \arg\max_{a_t^{(i)}\in A}\{P(S_t^{(i)};a_t^{(i)})\} \qquad ,i=1,...,I$}
  \EndFor

Please notice the lower letter in \For and \EndFor.

To get the For loop commands printed in capital letters use the following two commands:

\algrenewcommand\algorithmicfor{\textbf{FOR}} % <=======================
\algrenewcommand\algorithmicend{\textbf{END}} % <=======================

Please see the following complete MWE

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{algorithm}
\usepackage{algpseudocode}
\algnewcommand\algorithmicgenerate{\textbf{GENERATE}}
\algnewcommand\GENERATE[1]{\State\algorithmicgenerate\ #1}
\algnewcommand\algorithmicset{\textbf{SET}}
\algnewcommand\SET[1]{\State\algorithmicset\ #1}
\algrenewcommand\algorithmicfor{\textbf{FOR}} % <=======================
\algrenewcommand\algorithmicend{\textbf{END}} % <=======================


\begin{document}

\begin{algorithm}[H]
\begin{algorithmic}[1]
\GENERATE{$S_t^{(i)} \qquad ,i=1,...,I,t=1,...,M$}
\SET{$C_t^0(S_t^{(i)})=0 \qquad
 ,i=1,...,I,t=1,...,M$}
\SET{$C_{M+1}^n(S_{M+1}^{(i)})=0 \qquad ,i=1,...,I,n=0,1,...,N$}
\SET{$\hat{\Phi}_t^{(n)}(S_t^{(i)})=0 \qquad, i=1,...,I,t=1,...,M,n=0,1,...,N$}
\For{$\quad t=M,...,1$}
  \SET{$\iota_t^{(i)} = \arg\max_{a_t^{(i)}\in A}\{P(S_t^{(i)};a_t^{(i)})\} \qquad ,i=1,...,I$}
  \For{$\, n=\max(1,N-t),...,N$}
    \SET{$\iota_t^{(i)} = \arg\max_{a_t^{(i)}\in A}\{P(S_t^{(i)};a_t^{(i)})\} \qquad ,i=1,...,I$}
  \EndFor
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}

and its result:

resulting pdf

1
  • Thanks a lot, it helps!
    – user304663
    Commented Jul 20, 2019 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .