这是我对算法的 MWE,但它抛出了一个错误,我不知道如何获取输入和初始化。我希望它看起来像附图一样。
\documentclass[a4paper,10pt,twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs,lipsum}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Algorithm 2: Boosted $K$ Nearest Neighbor}
\begin{algorithmic}[1]
% \qinput{Input:} \\
% \State $S={s_i}={\left(x_i,y_i\right)$
\For{t = 1 to T} \do
\State $S_t \gets S_{t-1}$
\For{$s_q \in S_t$} \do
\State $N_q \gets$ k nearest neighbors
\State of $s_q$ using $D(s_q,s_i)$
\State label($s_q$)$=argmax\sum_{s_i \in N_q}D(s_q,s_i)$;
\If{label($s_q$)$\ne y_q$}
\For{$s_i \in N_i$} \do
\If{$y_i \ne y_q$}
\State $w_{i}^{t} \gets w_{i}^{t} - \lambda/d(x_q,x_i)$;
\Else
\State $w_{i}^{t} \gets w_{i}^{t} + \lambda/d(x_q,x_i)$;
\EndIf
\EndFor
\EndIf
\EndFor
\If{label($s_q$)$=y_q \forall_{s_q}$}
\State break
\EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
答案1
这是一个提供可行输出的选项。我删除了标题中的物理编号:
\documentclass[10pt,twocolumn]{article}
\usepackage{algorithm,algpseudocode}% http://ctan.org/pkg/{algorithms,algorithmx}
\algnewcommand{\Inputs}[1]{%
\State \textbf{Inputs:}
\Statex \hspace*{\algorithmicindent}\parbox[t]{.8\linewidth}{\raggedright #1}
}
\algnewcommand{\Initialize}[1]{%
\State \textbf{Initialize:}
\Statex \hspace*{\algorithmicindent}\parbox[t]{.8\linewidth}{\raggedright #1}
}
\begin{document}
\begin{algorithm}
\caption{Boosted $K$ Nearest Neighbour}
\begin{algorithmic}[1]
\Inputs{$S={s_i}=\left(x_i,y_i\right)$}
\Initialize{\strut$w_i^0 \gets 0$, $i=1,\ldots,n$ \\ $S_0 \gets S$}
\For{t = 1 to T}
\State $S_t \gets S_{t-1}$
\For{$s_q \in S_t$}
\State $N_q \gets$ k nearest neighbors
\State of $s_q$ using $D(s_q,s_i)$
\State label($s_q$)$=argmax\sum_{s_i \in N_q}D(s_q,s_i)$;
\If{label($s_q$)$\ne y_q$}
\For{$s_i \in N_i$}
\If{$y_i \ne y_q$}
\State $w_{i}^{t} \gets w_{i}^{t} - \lambda/d(x_q,x_i)$;
\Else
\State $w_{i}^{t} \gets w_{i}^{t} + \lambda/d(x_q,x_i)$;
\EndIf
\EndFor
\EndIf
\EndFor
\If{label($s_q$)$=y_q \forall_{s_q}$}
\State break
\EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
新定义的\Inputs
and(等价)\Initialize
接受一个参数,该参数将其内容设置为\Statex
缩进\algorithmicindent
(默认值),以与其他算法元素对齐。内容存储在t
op-aligned中\parbox
,该 op-aligned 可容纳 80% 的行。我相信您不需要更多,但如果您需要,也可以修复它以避免导致框过满。