algpseudocode 中的 for 循环

algpseudocode 中的 for 循环

我想改变我的伪代码

\documentclass{article}\usepackage{amsmath}
\usepackage[noend]{algpseudocode}
\usepackage{algorithm}

\makeatletter



\begin{document}
\begin{algorithm}
\caption{AIP}\label{AIPal}
\begin{algorithmic}[1]
\State \textit{$\sum^{N_agent}_{i=1}$$L_A$} $\gets$ position of \textit{$agent_i$}
\State \emph{loop}:
\State \textit{$i$}$\gets$ 1         
\State \textit{$i$ plus 1}
\If {\textit{distance(eNB,$Agent_i$)} $< \delta$} \textit{close $agent_i$}
\If {\textit{distance(eNB,$Agent_i$)}$>\Delta$} \textit{$agent_i$ power equals $\alpha$} 
\State \textbf{goto} loop
\EndIf                                 
\EndIf                                 
\end{algorithmic}
\end{algorithm}
\end{document}

到 for 循环,但我不知道如何将其转换为 \For 代码。有人能帮助我吗?

答案1

另一种选择:使用algorithm2ealgorithm环境。

\usepackage[linesnumbered,boxed]{algorithm2e}         % first image
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}  % second image

在此处输入图片描述

在此处输入图片描述

代码

\documentclass{article}
\usepackage{amsmath}
%\usepackage[noend]{algpseudocode}
\usepackage[linesnumbered,boxed]{algorithm2e}

\begin{document}
\begin{algorithm}
%\SetAlgoNoLine              %To remove vertical line, activate this \SetAlgoNoLine 
\caption{AIP}\label{AIPal}
%\begin{algorithmic}[1]
 \textit{$\sum^{N_{agent}}_{i=1}$$L_A$} $\gets$ position of \textit{$agent_i$}

Set \textit{$i$}=1 

\While{\textit{$i$} $ < {N_{agent}}$}{
\textit{$i$}= \textit{$i$} + 1

%\State \textit{$i$ plus 1}
\If {\textit{distance(eNB,$Agent_i$)} $< \delta$}{ 
\textit{close $agent_i$}
}
\If {\textit{distance(eNB,$Agent_i$)}$>\Delta$} {\textit{$agent_i$ power equals $\alpha$} 
}%\EndIf                                 
}%\EndIf                              
%\end{algorithmic}
\end{algorithm}
\end{document}

答案2

不确定这是否正是您所追求的……

在此处输入图片描述

\documentclass{article}
\usepackage[noend]{algpseudocode}
\usepackage{algorithm,amsmath}

\begin{document}
\begin{algorithm}
  \caption{AIP}\label{AIPal}
  \begin{algorithmic}[1]
    \State $\displaystyle \sum^{N_{\text{agent}}}_{i = 1} L_A \gets \text{position of agent}_i$
    \State $i \gets 1$
    \State \emph{loop}:
      \State $i \gets i + 1$
      \If {$\text{distance}(\text{eNB},\text{agent}_i) < \delta$}
        \State close $\text{agent}_i$
        \If {$\text{distance}(\text{eNB},\text{agent}_i) > \Delta$}
          \State $\text{agent}_i$ power equals $\alpha$
          \State \textbf{goto} \emph{loop}
        \EndIf
      \EndIf
  \end{algorithmic}
\end{algorithm}
\end{document}

由于algorithmic带有[1]可选参数的环境提供了编号行。使用这些作为循环标识符:

在此处输入图片描述

\documentclass{article}
\usepackage[noend]{algpseudocode}
\usepackage{algorithm,amsmath}

\begin{document}
\begin{algorithm}
  \caption{AIP}\label{AIPal}
  \begin{algorithmic}[1]
    \State $\displaystyle \sum^{N_{\text{agent}}}_{i = 1} L_A \gets \text{position of agent}_i$
    \State $i \gets 1$
    \State $i \gets i + 1$ \label{loop}
    \If {$\text{distance}(\text{eNB},\text{agent}_i) < \delta$}
      \State close $\text{agent}_i$
      \If {$\text{distance}(\text{eNB},\text{agent}_i) > \Delta$}
        \State $\text{agent}_i$ power equals $\alpha$
        \State \textbf{goto} \ref{loop}
      \EndIf
    \EndIf
  \end{algorithmic}
\end{algorithm}
\end{document}

相关内容