6

I am doing my master thesis and need to include an algorithm into my LaTeX code. I have been trying for hours now, but I still have several errors that always show up. It is my first time using the algorithm package, so help would be appreciated. Here is the code:

\documentclass{article}
\usepackage{algorithm,algorithmic,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Resource Allocation Algorithm }
\label{alg:algorithm_sum}
\begin{algorithmic}[1]
\State{Initialization of maximum number of iteration $I_{\text{max}}$, given $\mu^m$ and $\beta^m$ and obtain the intermediate solution for (opt. variables)} 
\If{ref. is satisfied} 
\State{Convergence =  true} 
\Return{opt. variables} 
\Else 
\State{update $\mu$ and $\beta$ according to ref. and $m=m+1$} 
\State{Convergence =  false} 
\EndIf
\Until{Convergence = true or $m=I_{\text{max}}$} 
\end{algorithmic}
\end{algorithm}
\end{document}
1
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Commented Jul 21, 2015 at 8:59

2 Answers 2

4

First of all you are loading both algorithmic and algpseudocode (variant of algorithmicx) which are not compatible.

The syntax you are using is the one of algpseudocode so don't load algorithmic. Moreover, you are using \text and forgot to load amsmath.

Finally, the \Until command has to be used to close a \Repeat statement which is missing in your code.

This is your modified MWE with \Repeat (move it to the right place in the algorithm):

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Resource Allocation Algorithm }
\label{alg:algorithm_sum}
\begin{algorithmic}[1]
\State{Initialization of maximum number of iteration $I_{\text{max}}$, given $\mu^m$ and $\beta^m$ and obtain the intermediate solution for (opt. variables)}
\Repeat
\If{ref. is satisfied}
\State{Convergence =  true}
\Return{opt. variables}
\Else
\State{update $\mu$ and $\beta$ according to ref. and $m=m+1$}
\State{Convergence =  false}
\EndIf
\Until{Convergence = true or $m=I_{\text{max}}$}
\end{algorithmic}
\end{algorithm}
\end{document} 

Output:

enter image description here

2
  • Thanks a lot for the feedback, I will be more careful, it is a strange new environment for me :) And sometimes the errors are weird. This solved my problem! Commented Jul 21, 2015 at 9:37
  • @redpixel2511 You're welcome. Remember that you can accept one of the answers (the one which helped you most) to mark your question as solved. See How do you accept an answer? Commented Jul 21, 2015 at 9:40
0

Another possibility is to leave algorithmic and remove algpseudocode to eliminate incompatibility. But change the syntax a bit as this:

\documentclass{article}
\usepackage{amsmath,algorithm,algorithmic}
\begin{document}

\begin{algorithm}
\caption{Resource Allocation Algorithm }
\label{alg:algorithm_sum}
\begin{algorithmic}[1]
\STATE{Initialization of maximum number of iteration $I_{\text{max}}$, given $\mu^m$ and $\beta^m$ and obtain the intermediate solution for (opt. variables)} 
\REPEAT
\IF{ref. is satisfied} 
\STATE{Convergence =  true} 
\RETURN{opt. variables} 
\ELSE 
\STATE{update $\mu$ and $\beta$ according to ref. and $m=m+1$} 
\STATE{Convergence =  false} 
\ENDIF
\UNTIL{Convergence = true or $m=I_{\text{max}}$} 
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

Note also that you may revise your algorithm, because until usually needs a repeat to close. You may change it as you wish.

3
  • 1
    until without a repeat doesn't look like pseodo-code... Commented Jul 21, 2015 at 9:19
  • Absolutely right! Commented Jul 21, 2015 at 9:19
  • Yes, thank you, I think I just forgot that line.. Commented Jul 21, 2015 at 9:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.