我该如何纠正这个错误,我尝试了很多次

我该如何纠正这个错误,我尝试了很多次

在此处输入图片描述

\documentclass[a4paper,11pt]{article}
\usepackage{algorithm} 
\usepackage{commath}
\usepackage{algpseudocode} 
\begin{document} 




\begin{algorithm}
    \caption{Game Theory Controller}
    {\textbf{Input:} Network $G$, Demands $D$, Restricted routes $P^0$, $P^1$ and lightpaths $L^0$, $L^1$}
    \begin{algorithmic}[1]
    \State $sol.unserverd\leftarrow 0$, $sol.L\leftarrow 0$
        \For  {\textbf{all} demand $d\in D$} 
            \If  {\abs{L^1(d)}\ \textgreater{0}}
            \State  Select lightpath $l\in L^1 (d)$ such that it can be allocated with the lowest slice index network $G$ \EndIf
                \if {$l$ exits} {$sol.L\leftarrow l$} and allocate $l$ in $G$ 
                \EndIf
                \else increment sol.unserved
                
                \EndFor
                    \For  {\textbf{all} demand $d\in D$} 
                        \If  {\abs{L^1(d)}\ = {0}}
                            \If  {\abs{P^1(d)}\ \textgreater{0}}
                            \State select route $p\in P^1$ and lightpatgh $l\in(d,p)$\textbackslash $L^0(d)$ such that $l$ can be allocated with the lowest slice index in network $G$ (first target) and $p$ is shortest (second target) 
                            \if {$l$ exits} {$sol.L\leftarrow l$} and allocate $l$ in $G$ \else increment sol.unserved
                            
                            
                                
                            \else
    
        \State select route $p\in P^1(d)$ and lightpath $l\in L (d,p)$ \textbackslash $L^0(d)$ such that $l$ can be allocated with the lowest slice index in network $G$ (first target) and $p$ is shortest (second target) 
            \EndIf
        \if $l$ exists {$sol.L(D)\leftarrow l$} and allocate $l$ in \else increment sol.unserved
 
    \if {sol.unserved=0 }   
    \State  sol.objective \leftarrow maximum allocated slive index network $G$
    \else
        \State sol.objective \leftarrow {\abs{S}\ + {10}} best sol.unsered
    \end{algorithmic} 
\end{algorithm} 
{\textbf{output: RSA}}
\end{document}

答案1

AlgpseudoCode 要求 \If 和 \For 语句以 \EndIf / \EndFor 结尾。

此外,还存在错误,因为如果我没记错的话, \leftarrow 需要数学模式, \abs 也是如此。

\documentclass[a4paper,11pt]{article}
\usepackage{algorithm} 
\usepackage{commath}
\usepackage{algpseudocode} 
\begin{document} 




\begin{algorithm}
    \caption{Game Theory Controller}
    {\textbf{Input:} Network $G$, Demands $D$, Restricted routes $P^0$, $P^1$ and lightpaths $L^0$, $L^1$}
    \begin{algorithmic}[1]
    \State $sol.unserverd\leftarrow 0$, $sol.L\leftarrow 0$
    \For {\textbf{all} demand $d\in D$}
        \If  {$\abs{L^1(d)}>{0}$}
            \State  Select lightpath $l\in L^1 (d)$ such that it can be allocated with the lowest slice index network $G$
            \If {$l$ exits} {$sol.L\leftarrow l$} and allocate $l$ in $G$ 
            \Else{} increment sol.unserved
            \EndIf
        \EndIf
    \EndFor
    \For {\textbf{all} demand $d \in D$}
        \If {$\abs{L^1(d)} = 0$}
            \If{$\abs{\P^1(d)} > 0$}
                \State Select route $p \in P^1(d)$ and lightpath $l\in L(d, p)\setminus L^0(d)$ such that $l$ can be allocated with the lowest slice index in network $G$ (first target) and $p$ is shortest (second target) 
                \If {$l$ exists} {$sol.L(D)\leftarrow l$} and allocate $l$ in \Else{} increment sol.unserved
                \EndIf
            \EndIf
        \EndIf
    \EndFor
    \If {sol.unserved=0 }   
        \State  $sol.objective \leftarrow$ maximum allocated slive index network $G$
    \Else
        \State $sol.objective \leftarrow \abs{S}\ + 10* best sol.unsered$
    \EndIf
    \end{algorithmic} 
\end{algorithm} 
{\textbf{output: RSA}}
\end{document}

要删除 \EndIf / \EndFor 的输出,请在加载 algpseudocode 后使用 \algnotext{EndIf} 和 \algnotext{EndFor}。

相关内容