Sequência de controle indefinida \State \For \While

Sequência de controle indefinida \State \For \While

Estou tentando escrever um pseudocódigo no MiKTeX. Eu usei \usepackage{algorithm} \usepackage{algorithmic}, mas recebo:

Sequência de controle indefinida' para cada estrutura (\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}

Responder1

Você está carregando algorithmic, mas usando a sintaxe do 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}

Não faz muito sentido usar equationdentro de um algoritmo, principalmente no argumento de \If. Observe que \WHILEe \ENDWHILEdeve ser \Whilee \EndWhile.

insira a descrição da imagem aqui

informação relacionada