17

I am trying to write a pseudo code in MiKTeX. I have used \usepackage{algorithm} \usepackage{algorithmic} but I get:

Undefined control sequence' for every structure (\State, \While, \For, \If).

\begin{algorithm}
\caption{PSO}
\label{pseudoPSO}
\begin{algorithmic}[1]
\State Initialize a population of particles with random values positions and velocities from \textit{D} dimensions in the search space
\WHILE{Termination condition not reached} 
\For{ Each particle \textit{i}}
    \State Adapt velocity of the particle using Equation \ref{eq:1}
    \State Update the position of the particle using Equation \ref{eq:2}
    \State Evaluate the fitness {$f(\overrightarrow{X}_i)$}
    \If{\begin{equation}f(\overrightarrow{X}_i)<f(\overrightarrow{P}_i)\end    {equation} 
        \begin{equation}\overrightarrow{P}_i \gets \overrightarrow{X}_i\end{equation}
    \EndIf
    \If{\begin{equation}f(\overrightarrow{X}_i)<f(\overrightarrow{P}_g)\end{equation} 
        \begin{equation}\overrightarrow{P}_g \gets \overrightarrow{X}_i\end{equation}
    \EndIf
\EndFor
\ENDWHILE
\end{algorithmic}
\end{algorithm}
0

1 Answer 1

19

You're loading algorithmic, but using the syntax of algpseudocode.

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{PSO}
\label{pseudoPSO}
\begin{algorithmic}[1]
\State Initialize a population of particles with random values positions
       and velocities from \textit{D} dimensions in the search space
\While{Termination condition not reached}
\For{Each particle $i$}
    \State Adapt velocity of the particle using Equation \ref{eq:1}
    \State Update the position of the particle using Equation \ref{eq:2}
    \State Evaluate the fitness {$f(\overrightarrow{X}_i)$}
    \If{$f(\overrightarrow{X}_i)<f(\overrightarrow{P}_i)$}
       \State $\overrightarrow{P}_i \gets \overrightarrow{X}_i$
    \EndIf
    \If{$f(\overrightarrow{X}_i)<f(\overrightarrow{P}_g)$}
       \State $\overrightarrow{P}_g \gets \overrightarrow{X}_i$
    \EndIf
\EndFor
\EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}

It doesn't make much sense using equation inside an algorithm, especially in the argument of \If. Note that \WHILE and \ENDWHILE should be \While and \EndWhile.

enter image description here

6
  • Works perfectly now, thank you. I used equation when trying to get rid of the errors. Commented Mar 22, 2014 at 16:11
  • The above code is generating \caption outside float. \caption, at the 5th line.
    – Swaroop
    Commented Jul 2, 2016 at 5:57
  • @GuruSwaroop I tried it again and it runs without problems.
    – egreg
    Commented Jul 2, 2016 at 7:15
  • @erreg Can you help me with this issue : tex.stackexchange.com/questions/317611/…
    – Swaroop
    Commented Jul 2, 2016 at 7:23
  • @GuruSwaroop I get no error with that code either.
    – egreg
    Commented Jul 2, 2016 at 7:24

You must log in to answer this question.

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