AGENT SKILL

numerical-optimization

Practical optimization — choosing algorithms, handling constraints, tuning, and diagnosing convergence failures.

License 许可
MIT
所属仓库
TanNhatCMS/jarvis
最近更新
2026-11-07
Stars
★ 0
Forks
1

01 前言 Front Matter

name
numerical-optimization
category
scientific
description
Practical optimization — choosing algorithms, handling constraints, tuning, and diagnosing convergence failures.

03 SKILL.md 正文

Overview

Optimization is everywhere in science: fitting models, designing experiments, training ML, calibrating simulations. This skill covers selecting the right algorithm for the problem structure (smooth vs non-smooth, constrained vs free, convex vs non-convex), setting up objectives correctly (scaling, gradients), and diagnosing the failures that waste weeks of compute.

When to use

  • Fitting a model to data (least squares, maximum likelihood)
  • Choosing between gradient-based, derivative-free, and global optimizers
  • Handling constraints: bounds, equalities, inequalities
  • Tuning hyperparameters or calibrating simulation parameters
  • Debugging an optimizer that stalls, diverges, or returns nonsense

Core concepts

  • Problem taxonomy: convex (one global minimum — use gradient methods confidently) vs non-convex (many minima — need global search or multi-start); smooth (gradients help) vs non-smooth/noisy (derivative-free: Nelder–Mead, CMA-ES, Bayesian optimization).

  • Gradient methods: steepest descent (slow), conjugate gradient, (L-)BFGS (quasi-Newton, the default for smooth unconstrained problems), Newton (needs Hessians). Supply analytic or autodiff gradients — finite differences are slower and noisier.

  • Constrained optimization: bounds (L-BFGS-B), general constraints (SLSQP, trust-constr, augmented Lagrangian, interior-point). Reformulate when possible: log-transforms turn positivity into unconstrained problems.

  • Global optimization: basin-hopping, differential evolution, dual annealing, Bayesian optimization (expensive objectives) — for rugged landscapes; always follow with a local polish.

  • Scaling: optimizers assume ~O(1) variables; rescale parameters to similar magnitudes or the Hessian conditioning destroys convergence.

  • Least squares structure: separable linear/nonlinear parameters (variable projection), sparse Jacobians — exploit structure instead of treating everything as a black box.

  • Automatic differentiation: exact gradients at the cost of a function evaluation (JAX, autograd) — eliminates finite-difference noise entirely; the single biggest upgrade for gradient-based optimization of coded objectives.

  • Stochastic optimization: SGD and Adam for noisy/large-scale objectives (machine learning) — the noise is a feature (escapes sharp minima); learning-rate schedules and batch sizes are the tuning knobs.

  • Derivative-free for the truly black box: Bayesian optimization builds a surrogate (Gaussian process) and explores efficiently — the method of choice when each evaluation costs minutes or dollars.

Practical workflow

1. Formulate well

from scipy.optimize import minimize
# Scale variables to O(1); supply a gradient; bound what must stay physical
res = minimize(fun, x0_scaled, jac=grad, method="L-BFGS-B",
               bounds=bounds, options={"ftol": 1e-10, "maxiter": 10000})
  1. Define the objective precisely: what is minimized, over what variables, subject to what — write it mathematically before coding.
  2. Scale all variables to order unity; log-transform strictly positive parameters.
  3. Provide gradients (analytic or autodiff via JAX/autograd) — finite-difference gradients fail on noisy objectives.

2. Choose the algorithm

Problem First choice
Smooth, unconstrained L-BFGS (with gradients)
Smooth, bounds L-BFGS-B
Smooth, general constraints trust-constr / SLSQP
Non-smooth, low-dim Nelder–Mead
Noisy/expensive, low-dim Bayesian optimization
Non-convex, global search Differential evolution → local polish
Least squares Levenberg–Marquardt / trust-region reflective

3. Multi-start and validate

  1. For non-convex problems: multi-start from diverse initial points (Latin hypercube) — the best of many local optima beats one hopeful run.
  2. Check convergence: gradient norm small, constraints satisfied, objective stable across restarts.
  3. Validate the solution: does it make physical sense? Perturb the data slightly — stable solutions survive, overfit ones don't.

4. Diagnose failures

  1. Stalls immediately: scaling (variables differ by 10⁶), bad gradient, or starting at a bound — check and rescale.
  2. Hits maxiter without converging: loosen tolerances for noisy objectives, or the landscape is flat — reparameterize.
  3. Different answers per run: non-convex landscape or stochastic objective — go global or multi-start; fix random seeds for reproducibility.
  4. Constraint violations: penalty weights too small, or the feasible set is empty — check feasibility first.

5. Optimize an expensive black-box function

  1. Define tight bounds from physical plausibility — Bayesian optimization wastes evaluations exploring absurd regions otherwise.
  2. Choose the surrogate and acquisition function (expected improvement is the default); seed with a space-filling design (Latin hypercube, ~10 points per dimension).
  3. Run in batches where parallel evaluation is possible; stop on a budget (evaluations or time), not on a tolerance — expensive objectives never get tight tolerances.

Common pitfalls

  • Unscaled variables: the #1 convergence killer — a parameter in nanometers next to one in gigapascals breaks every quasi-Newton method.
  • Finite-difference gradients on noisy objectives: noise gets amplified into garbage search directions — use autodiff or derivative-free methods.
  • Single start on non-convex problems: finding a local minimum and declaring victory — multi-start is mandatory.
  • Over-tight tolerances: demanding 1e-12 on a noisy experimental objective wastes iterations chasing noise.
  • Ignoring constraints in the formulation: optimizing then clipping to bounds gives infeasible "optima" — constrain properly.
  • Black-boxing structured problems: least-squares and separable problems have specialized solvers 10–100× faster than generic ones.
  • Optimizing a stochastic objective as if deterministic: re-evaluating the same point gives different values — use noise-aware methods (replicated evaluations, stochastic kriging) or the optimizer chases noise.
  • Ignoring integer/discrete variables: rounding continuous optima to integers can be arbitrarily bad — use mixed-integer methods (or enumerate when the discrete space is small).

04 同仓库更多 Skills

TanNhatCMS
TanNhatCMS/jarvis

web-scrape

Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.

★ 0
TanNhatCMS
TanNhatCMS/jarvis

design-taste-frontend

tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.

★ 0
TanNhatCMS
TanNhatCMS/jarvis

test-driven-development

Use when implementing any feature or bugfix, before writing implementation code

★ 0
TanNhatCMS
TanNhatCMS/jarvis

systematic-debugging

Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes

★ 0
TanNhatCMS
TanNhatCMS/jarvis

requesting-code-review

Use when completing tasks, implementing major features, or before merging to verify work meets requirements

★ 0
TanNhatCMS
TanNhatCMS/jarvis

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

★ 0