if else 循环并重复直到循环不适用于算法包

if else 循环并重复直到循环不适用于算法包

代码:

\begin{algorithm}[!t]
\begin{algorithmic}[1]
\caption{Adaptive Random Search}\label{algo_ARS}
\State define the probability of local search as $P_s$ and global search as 1 - $P_s$
    \Repeat
    \State generate a random number r(0,1)
        \If{r \leq $P_s$} 
            \State local search is selected
        \Else
             \State global search is selected 
    \Until{$X_{ARS}$ is accepted}
\end{algorithmic}
\end{algorithm}

输出: 在此处输入图片描述

我编写的伪代码不起作用。我无法获得直到部分作为输出。

答案1

我不知道您具体使用了哪些包,但您可能缺少一个\EndIf关闭嵌套 if 语句的命令:

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

\begin{document}
\begin{algorithm}[!t]
\begin{algorithmic}[1]
\caption{Adaptive Random Search}\label{algo_ARS}
\State define the probability of local search as $P_s$ and global search as $1 - P_s$
    \Repeat
        \State generate a random number $r(0,1)$
        \If{$r \leq P_s$} 
            \State local search is selected
        \Else
             \State global search is selected
        \EndIf
    \Until{$X_\mathrm{ARS}$ is accepted}
\end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

相关内容