每个 \STATE \FOR \IF 的未定义控制序列错误

每个 \STATE \FOR \IF 的未定义控制序列错误
\begin{algorithm}
    \caption{SSBPSO}\label{SSBPSO_algo}
    \begin{algorithmic}
    \STATE \textbf{INITIALIZATION: } Set N as the number of particles in the swarm, M as the number of active stocks in the particle.
    \STATE \textbf{Initialize :} $\hat{Y_i}$ = $-\infty$  
    \FOR{i = 1, 2, 3,...N}
        \STATE Randomly generate weights for all the stocks in $X_i$ and normalize the sum of the total weight as 1.
        \STATE Randomly generate Boolean $True$ for M number of assets in particle $X_i$.
        \FOR{j = 1, 2 , 3,...M}
            \STATE Normalize the sum of the weights of the active assets to 1.
        \ENDFOR
        \STATE Calculate $f(X_i)$, i.e. Sharpe Ratio, for particle using (\ref{2}).
        \STATE $Y_i$ = $f(X_i)$ 
        \STATE pBest = $X_i$
        \IF{$Y_i$ $>$ $\hat{Y_i}$}
            \STATE $\hat{Y_i}$ = $Y_i$
            \STATE gBest = $X_i$
        \ENDIF
    \ENDFOR
    \STATE \textbf{Set: } $c_1$, $c_2$, $c_3$, and $c_4$. 
    
    \FOR{k = 1, 2, 3,...predefined number of iterations}
        \FOR{i = 1, 2, 3,...N }
            \STATE Calculate Velocity $V_i$ using (\ref{SSBPSO})
            \STATE Update particle $X_i$ with $V_i$ using (\ref{SBPSO_P}).
            \IF{$f(X_i)$ $>$ $Y_i$}
                \STATE Set $Y_i$ = $f(X_i)$.
                \STATE Update pBest = $X_i$
            \ENDIF
            
        \ENDFOR 
        \STATE Update gBest after each iteration.
    \ENDFOR
    
\end{algorithmic}
\end{algorithm}

答案1

你需要\usepackage{algorithmic}

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}

\begin{algorithm}

\caption{SSBPSO}\label{SSBPSO_algo}

\begin{algorithmic}
  \STATE \textbf{INITIALIZATION:} Set $N$ as the number of particles in the swarm,
         $M$ as the number of active stocks in the particle.
  \STATE \textbf{Initialize:} $\hat{Y}_i=-\infty$  
  \FOR{$i = 1, 2, 3,\dots,N$}
    \STATE Randomly generate weights for all the stocks in $X_i$ and
           normalize the sum of the total weight as 1.
    \STATE Randomly generate Boolean $\mathit{True}$ for $M$ number of assets in particle $X_i$.
    \FOR{$j = 1, 2 , 3,\dots,M$}
      \STATE Normalize the sum of the weights of the active assets to 1.
    \ENDFOR
    \STATE Calculate $f(X_i)$, i.e. Sharpe Ratio, for particle using (\ref{2}).
    \STATE $Y_i = f(X_i)$ 
    \STATE $\mathit{pBest} = X_i$
    \IF{$Y_i>\hat{Y}_i$}
      \STATE $\hat{Y}_i = Y_i$
      \STATE $\mathit{gBest} = X_i$
    \ENDIF
  \ENDFOR
  \STATE \textbf{Set:} $c_1$, $c_2$, $c_3$, and $c_4$. 
  \FOR{$k = 1, 2, 3,\dots,$ predefined number of iterations}
    \FOR{$i = 1, 2, 3,\dots,N$}
      \STATE Calculate Velocity $V_i$ using (\ref{SSBPSO})
      \STATE Update particle $X_i$ with $V_i$ using (\ref{SBPSO_P}).
      \IF{$f(X_i)>Y_i$}
        \STATE Set $Y_i = f(X_i)$.
        \STATE Update $\mathit{pBest} = X_i$
      \ENDIF
    \ENDFOR 
    \STATE Update $\mathit{gBest}$ after each iteration.
  \ENDFOR
\end{algorithmic}

\end{algorithm}

\end{document}

请注意我所做的更改:在到期时使用数学模式,将公式保持在一起,而\hat{Y}_i不是\hat{Y_i}放错重音。

在此处输入图片描述

相关内容