Skip to content

math::optimize

The optimize module implements the Nelder-Mead (downhill simplex) method for unconstrained or box-constrained minimization of a scalar objective.

For gradient-based optimization (L-BFGS), see math::minimize in std::core::math.

use std::math::optimize
pub type OptimizeOptions {
tol: number,
max_iter: int,
bounds: Array<Array<number>>?,
}
  • tol — convergence tolerance (function-value std-dev across the simplex).
  • max_iter — maximum number of simplex iterations.
  • bounds — optional per-dimension box constraints [[lo, hi], ...]. Pass None for unconstrained optimization.
pub type OptimizeResult {
x: Array<number>,
fun: number,
converged: bool,
iterations: int,
}

optimize::minimize(f, x0, options) -> OptimizeResult

Section titled “optimize::minimize(f, x0, options) -> OptimizeResult”

Minimize a scalar objective f(Array<number>) -> number starting from x0.

use std::math::optimize
let result = optimize::minimize(
|x| x[0] * x[0] + x[1] * x[1],
[5.0, 5.0],
OptimizeOptions {
tol: 0.000001,
max_iter: 1000,
bounds: None,
}
)
// result.x ~ [0, 0]; result.fun ~ 0; result.converged == true

With bounds:

let result = optimize::minimize(
|x| (x[0] - 2.0) * (x[0] - 2.0),
[0.0],
OptimizeOptions {
tol: 0.000001,
max_iter: 500,
bounds: [[0.0, 1.0]], // restrict x[0] to [0, 1]
}
)
// result.x ~ [1.0] (bound is binding)