qq_52797084 2024-04-11 16:51 采纳率: 30.8%
浏览 105

用latex写论文中的伪代码,想要在伪代码中显示end while但不显示end if应该怎么改?

用latex写论文中的伪代码,想要在伪代码中显示end while但不显示end if应该怎么改?


\documentclass[11pt,twoside,a4paper]{article}

\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\usepackage{amsmath}
\usepackage[top=1cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\begin{document}

%% 写算法伪代码或者流程的前期准备
\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm

\begin{algorithm}[h]
  \caption{The Incentive Mechanism TACT} % 名称
  \begin{algorithmic}[1]
    \Require
      $T$,
      $N$,
      $K$,
      $\Delta$,
      $\Delta_{min}$,
      $a$,
      $b$,
      $c$,
      $d$,
      $\xi$,
      $\Phi$
    \Ensure
       $\Psi$,\{{$\beta^{t*}|t \in [1,T]$}\},\{{$p^{t*}|t \in [1,T]$}\,,\{{$\delta^{t*}|t \in [1,T]$}\}
       \State{\bfseries Initialization}:
       \State $\psi_{i}^{t}=0,\beta^{t}=0,p_{i}^{t}=0,\delta_{i}^{t}=0,\forall i \in \mathcal{N},t \in [1,T]$ ;
       \State{\bfseries Initial exploration phase}:
       \State Recruit all social-aware workers when t=1,i.e.,
       \For{each worker $i\in \mathcal{N}$,$t=1$} $\psi_{{i}}^{{1}}$=1,set $q_{{i,k}}^{{1}}=1$;
       \EndFor
       \State Determine the salary for recruited workers:\ $p_{{i}}^{{1*}} = p_{\max}$;\\
       Requester:pay $\beta^{{1*}}=argmin_{\beta^{{1}}}$ $(U_{p}^{t} \geq  0$) to the platform;\\
       Workers:perform the task;upload and share answers;
       \For {each worker $i\in \mathcal{N}$} update $n_{i}^{1}, \tilde q_{i}^{1}, \phi_{i}^{1}, \hat{q}_{i}^{1}$;
       \EndFor
      \State{\bfseries Learning of quality and the THS game}:
      \While{each period $t<T$}
      \State Sort the workers according to the CUCB-based quality 
      \Statex  \hspace{0.6cm}value $\hat {q}_{i}^{t}:\hat {q}_{w1}^{t}\geq \hat {q}_{w2}^{t}\geq...\geq \hat {q}_{wN}^{t}$; t=t+1;
        \State Select the top $K$ workers as winners, denoted as $\mathcal{W}^t$;
        \For{each worker $w_i \in \mathcal{W}^t$} $\psi_{wi}^{t}=1$;
        \EndFor
        \State Carry out the THS game: determine the optimal strategy 
        \Statex \hspace{0.6cm}group $\langle \beta^{t*},p^{t*},\delta^{t*}\rangle$ for each party according to Eqs. (18),
        \Statex \hspace{0.5cm} (19),and (25);
        
        \State Workers: perform the task; upload and share answers;
        \For{each worker $i \in \mathcal{N}$} update $n_{i}^{t},\tilde{q}_{i}^{t},\phi_{i}^{t},\hat {q}_{i}^{t}$;
        \EndFor
        \EndWhile
        \State{\bfseries Return} $\mathcal{W}^t,\Psi,\langle \beta^{t*},p^{t*},\delta^{t*}\rangle,\forall t \in [1,T]  $.
        
  
  \end{algorithmic}
\end{algorithm}

\end{document}
  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-11 16:51
    关注

    引自免费微信小程序:皆我百晓生

    为了在 LaTeX 的伪代码(使用 algorithmicxalgpseudocode 包)中只显示 end while 而不显示 end if,你需要对伪代码的结构进行微调。在这个例子中,我们可以将所有 if 语句嵌套在循环内部,并使用逻辑运算符合并条件,从而避免使用单独的 end if

    以下是修改后的代码:

    \documentclass[11pt,twoside,a4paper]{article}
    
    \usepackage{algorithm}
    \usepackage{algorithmicx}
    \usepackage{algpseudocode}
    
    \usepackage{amsmath}
    \usepackage[top=1cm, bottom=2cm, left=2cm, right=2cm]{geometry}
    \begin{document}
    
    %% 写算法伪代码或者流程的前期准备
    \renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm
    \renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
    
    \begin{algorithm}[h]
      \caption{The Incentive Mechanism TACT} % 名称
      \begin{algorithmic}[1]
        \Require
          $T$,
          $N$,
          $K$,
          $\Delta$,
          $\Delta_{min}$,
          $a$,
          $b$,
          $c$,
          $d$,
          $\xi$,
          $\Phi$
        \Ensure
           $\Psi$,\{{$\beta^{t*}|t \in [1,T]$}\},\{{$p^{t*}|t \in [1,T]$}\,,\{{$\delta^{t*}|t \in [1,T]$}\}
           \State{\bfseries Initialization}:
           \State $\psi_{i}^{t}=0,\beta^{t}=0,p_{i}^{t}=0,\delta_{i}^{t}=0,\forall i \in \mathcal{N},t \in [1,T]$ ;
           \State{\bfseries Initial exploration phase}:
           \State Recruit all social-aware workers when t=1,i.e.,
           \For{each worker $i\in \mathcal{N}$,$t=1$} $\psi_{{i}}^{{1}}$=1;set $q_{{i,k}}^{{1}}=1$;
           \EndFor
           \State Determine the salary for recruited workers:\ $p_{{i}}^{{1*}} = p_{\max}$;\\
           Requester:pay $\beta^{{1*}}=argmin_{\beta^{{1}}}$ $(U_{p}^{t} \geq  0$) to the platform;\\
           Workers:perform the task;upload and share answers;
           \For {each worker $i\in \mathcal{N}$} update $n_{i}^{1}, \tilde q_{i}^{1}, \phi_{i}^{1}, \hat{q}_{i}^{1}$;
           \EndFor
          \State{\bfseries Learning of quality and the THS game}:
          \While{each period $t<T$}
          \If{$t==1$}
              \State Sort the workers according to the CUCB-based quality 
              \Statex  \hspace{0.6cm}value $\hat {q}_{i}^{t}:\hat {q}_{w1}^{t}\geq \hat {q}_{w2}^{t}\geq...\geq \hat {q}_{wN}^{t}$; t=t+1;
              \State Select the top $K$ workers as winners, denoted as $\mathcal{W}^t$;
              \For{each worker $w_i \in \mathcal{W}^t$} $\psi_{wi}^{t}=1$;
              \EndFor
              \State Carry out the THS game: determine the optimal strategy 
              \Statex \hspace{0.6cm}group $\langle \beta^{t*},p^{t*},\delta^{t*}\rangle$ for each party according to Eqs. (18),
              \Statex \hspace{0.5cm} (19),and (25);
              
              \State Workers: perform the task; upload and share answers;
              \For{each worker $i \in \mathcal{N}$} update $n_{i}^{t},\tilde{q}_{i}^{t},\phi_{i}^{t},\hat {q}_{i}^{t}$;
              \EndFor
          \Else
              \State Sort the workers according to the CUCB-based quality 
              \Statex  \hspace{0.6cm}value $\hat {q}_{i}^{t}:\hat {q}_{w1}^{t}\geq \hat {q}_{w2}^{t}\geq...\geq \hat {q}_{wN}^{t}$; t=t+1;
              \State Select the top $K$ workers as winners, denoted as $\mathcal{W}^t$;
              \For{each worker $w_i \in \mathcal{W}^t$} $\psi_{wi}^{t}=1$;
              \EndFor
              \State Carry out the THS game: determine the optimal strategy 
              \Statex \hspace{0.6cm}group $\langle \beta^{t*},p^{t*},\delta^{t*}\rangle$ for each party according to Eqs. (18),
              \Statex \hspace{0.5cm} (19),and (25);
              
              \State Workers: perform the task; upload and share answers;
              \For{each worker $i \in \mathcal{N}$} update $n_{i}^{t},\tilde{q}_{i}^{t},\phi_{i}^{t},\hat {q}_{i}^{t}$;
              \EndFor
          \EndIf
          \EndWhile
          \State{\bfseries Return} $\mathcal{W}^t,\Psi,\langle \beta^{t*},p^{t*},\delta^{t*}\rangle,\forall t \in [1,T]  $.
          
      
      \end{algorithmic}
    \end{algorithm}
    
    \end{document}
    

    这段代码通过将所有的 if 嵌套在 while 循环内,并且只检查 $t==1$ 的情况,实现了仅显示 end while 且不显示 end if。这样,当 $t==1$ 时执行初始化阶段的代码,否则执行学习质量与THS游戏的常规步骤。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月11日

悬赏问题

  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大
  • ¥15 使用spss做psm(倾向性评分匹配)遇到问题