Generate publication-quality pseudo-code for algorithms in LaTeX (algpseudocode/algorithm2e) or markdown. Covers ML/deep learning (training loops, optimizers, backprop), reinforcement learning (Q-learning, PPO, actor-critic, REINFORCE), and evolutionary algorithms (GA, CMA-ES, NEAT, GP). Use when: (1) Writing pseudo-code for a paper, report, or assignment, (2) Converting Python/PyTorch code to pseudo-code, (3) Presenting an algorithm in LaTeX algorithm environment, (4) Creating pseudo-code for ML training, RL, or evolutionary methods, (5) User asks to "write pseudo-code", "algorithmize", or "present as algorithm".
Generate pseudo-code following established conventions from seminal ML/RL/EA papers. For full examples and domain-specific patterns, read references/algorithms-research.md.
Classify the algorithm into one of:
| Domain | Structural Pattern | Style |
|---|---|---|
| Neural Network / DL | forward-backward-update loop | CLRS/Goodfellow style |
| Reinforcement Learning | interact-compute-update loop | Sutton-Barto or CLRS style |
| Evolutionary | initialize-evaluate-select-reproduce loop | Procedural with set notation |
| General / Other | varies | CLRS style (default) |
algpseudocode + algorithm package unless user specifies algorithm2eThese 10 rules apply to ALL pseudo-code regardless of domain:
nabla, sum, argmax -- not for x in range(n))theta <- ..., not =). Equals is for equality testing onlyWhile, If, Return)theta), calligraphic for sets/losses (L, D), greek for scalars (alpha)Neural Networks -- Four canonical steps in the inner loop:
forward pass -> loss computation -> backward pass (backprop) -> parameter update
Early stopping sits between epochs. Validation is NOT inside the batch loop.
Reinforcement Learning -- Three-phase skeleton:
1. Initialize (parameters, value estimates, environment)
2. Loop (episodes or iterations):
a. Interact with environment (collect experience)
b. Compute targets (returns, advantages, TD errors)
c. Update parameters (policy, value function, or both)
3. Return (policy, value function)
Use uppercase single letters for random variables (S, A, R). Advantage uses hat notation (A_hat).
Evolutionary Algorithms -- Population-level loop:
1. Initialize population
2. while not converged:
a. Evaluate fitness
b. Select parents
c. Apply variation operators (crossover, mutation)
d. Select survivors
3. Return best
Use (mu, lambda) notation. Set notation for population operations (union, empty set).
loss.backward(), for x in range(n))= for assignment\Call{}\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{algorithm}[t]
\caption{Algorithm Name}\label{alg:name}
\begin{algorithmic}[1]
\Require Input description
\Ensure Output description
\State $\boldsymbol{\theta} \gets \boldsymbol{\theta}_0$ \Comment{Initialize}
\While{not converged}
\State $\mathbf{g} \gets \nabla_{\boldsymbol{\theta}} \mathcal{L}$
\State $\boldsymbol{\theta} \gets \boldsymbol{\theta} - \alpha \mathbf{g}$
\EndWhile
\State \textbf{return} $\boldsymbol{\theta}$
\end{algorithmic}
\end{algorithm}
Key commands: \Require, \Ensure, \State, \If{} ... \EndIf, \While{} ... \EndWhile,
\For{} ... \EndFor, \ForAll{} ... \EndFor, \Procedure{Name}{params} ... \EndProcedure,
\Function{Name}{params} ... \EndFunction, \Call{Name}{args}, \Comment{text}.
\usepackage[ruled,lined,linesnumbered]{algorithm2e}
\begin{algorithm}[H]
\caption{Algorithm Name}\label{alg:name}
\KwIn{Input description}
\KwOut{Output description}
\While{not converged}{
$\mathbf{g} \gets \nabla_{\boldsymbol{\theta}} \mathcal{L}$\;
$\boldsymbol{\theta} \gets \boldsymbol{\theta} - \alpha \mathbf{g}$\;
}
\Return{$\boldsymbol{\theta}$}
\end{algorithm}
Key commands: \KwIn{}, \KwOut{}, \KwData{}, \KwResult{}, lines end with \;,
\eIf{cond}{true}{false}, \If{cond}{body}, \While{cond}{body}, \For{cond}{body},
\tcc{multi-line comment}, \tcp{single-line comment}.
Critical: These two package families are mutually exclusive. Never load both.
For complete examples of every algorithm type (Adam, PPO, GA, CMA-ES, NEAT, Q-learning, etc.) with full LaTeX code, read references/algorithms-research.md.
Key sections to grep for:
## Neural Network Algorithms -- training loops, Adam, backprop, architectures## Reinforcement Learning Algorithms -- Q-learning, SARSA, REINFORCE, PPO, A2C## Evolutionary Algorithms -- GA, ES, CMA-ES, OpenAI ES, NEAT, GP## Code Examples -- complete copy-paste LaTeX for Adam, PPO, GA, Q-learning## Recommended References -- seminal papers and textbooksPyTorch深度学习模式与最佳实践,用于构建稳健、高效且可复现的训练流程、模型架构和数据加载。