伪代码 - repeat-until 命令

伪代码 - repeat-until 命令

您好,我怎样才能重写重复直到命令,以便第 15 行中的“直到”更接近数字 15(与第 14 行不在同一列)

\documentclass{article} 
\usepackage{amsmath}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{algorithm}
\clearpage
\begin{algorithm}
\caption{2-opt algoritmus}
\begin{algorithmic}[1]
\Repeat
\For{$i = 1$ do $n - 1$}
\For{$j = i + 1$ do $n$}
\If{$j < n$}
\If{$c(x_i, x_{i+1}) + c(x_j, x_{i+1}) < c(x_j, x_{i+1}) + c(x_j, x_{j+1})$}
\State Swap \textit{$x_i$} a \textit{$x_j$}
\EndIf
\Else
\If{$c(x_i, x_1) + c(x_n, x_{i+1}) < c(x_i, x_{i+1}) + c(x_n, x_1)$}
\State Swap \textit{$x_i$} a \textit{$x_n$}
\textbf{until}
\EndIf
\EndIf
\EndFor
\EndFor
\State 
\textbf{Until}
\end{algorithmic}
\end{algorithm}
\end{document}

答案1

“重复直到”的格式是

\Repeat
  <body>
\Until{<condition>}

所以你的代码应该是

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

\begin{document}

\begin{algorithm}
\caption{2-opt algoritmus}
\begin{algorithmic}[1]
\Repeat
  \For{$i = 1$ do $n - 1$}
    \For{$j = i + 1$ do $n$}
      \If{$j < n$}
        \If{$c(x_i, x_{i+1}) + c(x_j, x_{i+1}) < c(x_j, x_{i+1}) + c(x_j, x_{j+1})$}
          \State Swap $x_i$ a $x_j$
        \EndIf
      \Else
        \If{$c(x_i, x_1) + c(x_n, x_{i+1}) < c(x_i, x_{i+1}) + c(x_n, x_1)$}
          \State Swap $x_i$ a $x_n$
        \EndIf
      \EndIf
    \EndFor
  \EndFor
\Until{you're tired}
\end{algorithmic}
\end{algorithm}

\end{document}

用适当的情况替换“你累了”。

在此处输入图片描述

答案2

另一个解决方案可能是

\usepackage{algorithmic}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{amssymb}
\begin{algorithm}[h]
\KwData{$\textit{SINR}_k$, $\Delta_i$ of each $k^\text{th}$ user}
\KwResult{Optimal ${D_{u}}$}
\text{Initialize m = 1, n = 1}, $\lambda \succcurlyeq 0$\\
\text{Set stopping criteria}, $\epsilon_1, \epsilon_2$\\

\Repeat{${\lambda}$ converged, i.e 
$||\nabla_{\lambd1}}||\leq\epsilon_2$}\\
{ Initialize ${D_{u}}$\\
\Repeat{${D_{u}}$ converged, i.e $||\nabla_{{D_{u}}^{n-1}}||\leq 
\epsilon_1$}{
\text{Run optimization problem in (\ref{optimization_finalv3}}) \\
\text{Update ${D_{u}}$ as in (\ref{gduk})}\\
\text{n = n+1} \\} 
\text{Update ${\lambda}$ as in (\ref{gdlam})} \\
 m = m+1  }
\textbf{Return} \textit{{${D_{u}}$}}
\caption{Optimization algorithm: SIC triangle}
\end{algorithm}

在此处输入图片描述

相关内容