Skip to content

API reference — Rust generic seam

The Rust core owns the shared optimization seam. Python callers reach it through mltreelib.solvers.optimize. Guide: Generic optimizer.

Reach the module through mltreelib_core::generic or mltreelib_core::solver::generic. The short names Bounds, Budget, Constraint, ConstraintKind, Objective, ObjectiveError, OptimizationRequest, OptimizationResult, OptimizerKind, SmoothObjective, and SolveStatus are re-exported from mltreelib_core::solver.

solver/generic/annealing.rs

simulated_annealing

pub fn simulated_annealing( objective: &dyn Objective, budget: &Budget, options: &AnnealingOptions, seed: u64, start: Option<&[f64]>, ) -> OptimizationResult {

Runs bounded simulated annealing.

The starting point defaults to the middle of the box. The initial temperature comes from the observed objective spread when the caller does not supply one.

solver/generic/basin_hopping.rs

basin_hopping

pub fn basin_hopping( objective: &dyn Objective, smooth: &dyn SmoothObjective, budget: &Budget, options: &BasinHoppingOptions, seed: u64, start: Option<&[f64]>, ) -> OptimizationResult {

Runs basin hopping.

The inner local solver comes from the shared seam. The perturbation scales with the bound width, so one step size fits heterogeneous parameter scales.

solver/generic/bfgs.rs

bfgs

pub fn bfgs( objective: &dyn Objective, smooth: &dyn SmoothObjective, budget: &Budget, options: &BfgsOptions, start: Option<&[f64]>, ) -> OptimizationResult {

Runs BFGS on an unbounded smooth objective.

The starting point defaults to the origin. The run stops when the gradient falls under the tolerance, when the iteration limit is reached, or when a budget ends the run.

solver/generic/bounds.rs

Bounds

pub struct Bounds {

Lower and upper limits for each parameter.

A bound is either finite or infinite. lower[i] <= upper[i] holds for every dimension. A fully unbounded domain uses negative and positive infinity.

Bounds.new

pub fn new(lower: Vec<f64>, upper: Vec<f64>) -> Result<Self, String> {

Builds bounds from per-dimension lower and upper limits.

Returns an error when the vectors differ in length, when a limit is NaN, when a lower limit is positive infinity, when an upper limit is negative infinity, or when a lower limit exceeds its upper limit.

solver/generic/budget.rs

BudgetStatus

pub enum BudgetStatus {

Which budget ended a run.

Budget

pub struct Budget {

Counts user objective evaluations and solver iterations.

One counter covers every user objective call, including calls inside local solves, perturbation steps, population initialization, finite-difference gradients, and archive updates. The counters use interior mutability, so the objective wrapper and the solver share one budget without aliasing borrows.

Budget.new

pub fn new(max_evaluations: Option<usize>, max_iterations: Option<usize>) -> Self {

Builds a budget with optional limits. None means unlimited.

solver/generic/cobyla.rs

cobyla

pub fn cobyla( objective: &dyn Objective, budget: &Budget, options: &CobylaOptions, start: Option<&[f64]>, constraints: Option<&dyn ConstraintEvaluator>, kind: ConstraintKind, ) -> OptimizationResult {

Runs COBYLA on a bounded problem with inequality constraints.

The starting point defaults to the middle of the box. The method stops when the trust region collapses, when the model step vanishes, when the iteration limit is reached, or when a budget ends the run.

solver/generic/conjugate_gradient.rs

nonlinear_conjugate_gradient

pub fn nonlinear_conjugate_gradient( objective: &dyn Objective, smooth: &dyn SmoothObjective, budget: &Budget, options: &NonlinearCgOptions, start: Option<&[f64]>, ) -> OptimizationResult {

Runs nonlinear conjugate gradient on an unbounded smooth objective.

The starting point defaults to the origin. The run stops when the gradient falls under the tolerance, when the iteration limit is reached, or when a budget ends the run.

solver/generic/constraint.rs

ConstraintKind

pub enum ConstraintKind {

Constraint type.

ConstraintKind.as_str

pub fn as_str(self) -> &'static str {

Canonical name.

Constraint

pub struct Constraint {

Constraint values evaluated at one point.

Constraint.new

pub fn new(kind: ConstraintKind, values: Vec<f64>) -> Self {

Builds evaluated constraint values.

violation

pub fn violation(kind: ConstraintKind, value: f64) -> f64 {

Largest violation of one constraint value.

An inequality value below zero violates the constraint by its magnitude. An equality value violates the constraint by its absolute distance from zero.

max_violation

pub fn max_violation(kind: ConstraintKind, values: &[f64]) -> f64 {

Largest violation across all constraint values.

is_feasible

pub fn is_feasible(kind: ConstraintKind, values: &[f64], tolerance: f64) -> bool {

Reports whether constraint values satisfy the constraint.

Inequality values accept a small negative slack. Equality values accept an absolute distance from zero up to the tolerance.

ConstraintEvaluator

pub trait ConstraintEvaluator {

Evaluates constraint values at a point.

ConstraintEvaluator.values

fn values(&self, x: &[f64]) -> Result<Vec<f64>, String>;

Returns one value per constraint.

solver/generic/finite_difference.rs

FiniteDifferenceObjective

pub struct FiniteDifferenceObjective<'a> {

Objective that supplies a central-difference gradient.

The perturbation calls the wrapped objective, so the shared budget counts them like every other objective call.

solver/generic/lbfgs_b.rs

solve_lbfgs_b

pub fn solve_lbfgs_b( objective: &dyn Objective, budget: &Budget, options: &LbfgsBOptions, start: Option<&[f64]>, smooth: &dyn SmoothObjective, ) -> OptimizationResult {

Runs L-BFGS-B on one bounded smooth objective.

The returned point is always projected into the box. Evaluation budget exhaustion is reported as EvaluationLimit; iteration budget exhaustion is reported as IterationLimit.

solver/generic/line_search.rs

LineSearchOptions

pub struct LineSearchOptions {

Line search parameters.

LineSearchResult

pub struct LineSearchResult {

Result of one line search.

strong_wolfe

pub fn strong_wolfe( objective: &dyn Objective, smooth: &dyn SmoothObjective, point: &[f64], direction: &[f64], options: &LineSearchOptions, ) -> Result<LineSearchResult, ObjectiveError> {

Runs a strong Wolfe line search along one direction.

The search returns the step, the objective value, and the gradient at the accepted point. It falls back to the steepest descent direction when the supplied direction is not a descent direction.

solver/generic/mod.rs

FINITE_DIFFERENCE_STEP

pub const FINITE_DIFFERENCE_STEP: f64 = 1e-6;

Step size used by the central-difference gradient fallback.

optimize

pub fn optimize(request: &OptimizationRequest) -> Result<OptimizationResult, String> {

Runs one optimization with the shared contracts.

The method dispatch resolves the method-specific options, evaluates the objective through one shared budget, and returns the shared result type.

optimize_with_budget

pub fn optimize_with_budget( request: &OptimizationRequest, budget: &Budget, ) -> Result<OptimizationResult, String> {

Runs one optimization against a budget that the caller owns.

A composition method uses this entry point so the inner local solve spends the outer budget and no objective call escapes the count.

solver/generic/nelder_mead.rs

nelder_mead

pub fn nelder_mead( objective: &dyn Objective, budget: &Budget, options: &NelderMeadOptions, start: Option<&[f64]>, ) -> OptimizationResult {

Runs bounded Nelder-Mead.

The starting point defaults to the middle of the box. The method stops when the objective spread and the simplex spread fall under the configured tolerances, when the iteration limit is reached, or when a budget ends the run.

solver/generic/objective.rs

ObjectiveError

pub enum ObjectiveError {

Failure reported by an objective or gradient evaluation.

Objective

pub trait Objective {

A bounded scalar objective.

Implementations return one finite value for one point. Solvers reject non-finite values through [ObjectiveError::NotFinite] and report the [crate::solver::generic::SolveStatus::InvalidObjective] status.

Objective.dim

fn dim(&self) -> usize;

Number of parameters.

Objective.bounds

fn bounds(&self) -> &Bounds;

Parameter domain.

Objective.value

fn value(&self, x: &[f64]) -> Result<f64, ObjectiveError>;

Objective value at one point.

SmoothObjective

pub trait SmoothObjective: Objective {

An objective that also provides an exact gradient.

SmoothObjective.gradient

fn gradient(&self, x: &[f64]) -> Result<Vec<f64>, ObjectiveError>;

Gradient at one point. The returned vector has one entry per dimension.

require_finite

pub fn require_finite(value: f64) -> Result<f64, ObjectiveError> {

Checks that an objective value is finite.

require_dimension

pub fn require_dimension(expected: usize, x: &[f64]) -> Result<(), ObjectiveError> {

Checks that a point matches the objective dimension.

CountedObjective

pub struct CountedObjective<'a> {

Objective wrapper that charges the shared budget for every value call.

Wrapping the user objective keeps one counter for every objective call, including the calls a finite-difference gradient makes.

solver/generic/powell.rs

powell

pub fn powell( objective: &dyn Objective, budget: &Budget, options: &PowellOptions, start: Option<&[f64]>, ) -> OptimizationResult {

Runs bounded Powell.

The starting point defaults to the middle of the box. The method stops when the objective and the point stop moving, when the iteration limit is reached, or when a budget ends the run.

solver/generic/random.rs

latin_hypercube_samples

pub fn latin_hypercube_samples( bounds: &Bounds, samples: usize, seed: u64, ) -> Result<Vec<Vec<f64>>, String> {

Builds Latin hypercube samples inside finite bounds.

Every dimension is stratified independently into samples strata. One point takes one stratum per dimension. The stratum order is shuffled per dimension with the seeded generator.

pub fn random_search( objective: &dyn Objective, budget: &Budget, options: &RandomSearchOptions, seed: u64, initial_population: Option<&[Vec<f64>]>, ) -> OptimizationResult {

Runs uniform random search inside bounds.

Candidates come from initial_population first when one is supplied. The remaining candidates are uniform draws.

pub fn latin_hypercube_search( objective: &dyn Objective, budget: &Budget, options: &LatinHypercubeOptions, seed: u64, ) -> OptimizationResult {

Runs Latin hypercube sampling inside bounds.

solver/generic/request.rs

DEFAULT_SEED

pub const DEFAULT_SEED: u64 = 0;

Default seed used by every stochastic method when no seed is supplied.

The fixed seed keeps omitted seeds reproducible across runs.

OptimizerKind

pub enum OptimizerKind {

One optimizer method.

OptimizerKind.as_str

pub fn as_str(self) -> &'static str {

Canonical method name.

SharedOptions

pub struct SharedOptions {

Options shared by every method.

RandomSearchOptions

pub struct RandomSearchOptions {

Options for random search.

LatinHypercubeOptions

pub struct LatinHypercubeOptions {

Options for Latin hypercube sampling.

LbfgsBOptions

pub struct LbfgsBOptions {

Options for L-BFGS-B.

BfgsOptions

pub struct BfgsOptions {

Options for full-memory BFGS.

NonlinearCgOptions

pub struct NonlinearCgOptions {

Options for nonlinear conjugate gradient.

NelderMeadOptions

pub struct NelderMeadOptions {

Options for Nelder-Mead.

PowellOptions

pub struct PowellOptions {

Options for Powell.

BoundaryPolicy

pub enum BoundaryPolicy {

Boundary policy for proposals that leave the box.

AnnealingOptions

pub struct AnnealingOptions {

Options for bounded simulated annealing.

CobylaOptions

pub struct CobylaOptions {

Options for COBYLA.

BasinHoppingOptions

pub struct BasinHoppingOptions {

Options for basin hopping.

MethodOptions

pub enum MethodOptions {

Method-specific options.

MethodOptions.kind

pub fn kind(&self) -> OptimizerKind {

Method that owns these options.

OptimizationRequest

pub struct OptimizationRequest<'a> {

One optimization request.

solver/generic/result.rs

SolveStatus

pub enum SolveStatus {

Termination status of one optimization run.

Budget exhaustion never reports success. Only [SolveStatus::Converged] and [SolveStatus::Tolerance] report success.

SolveStatus.as_str

pub fn as_str(self) -> &'static str {

Canonical name used by the Python surface and result files.

HistoryEntry

pub struct HistoryEntry {

One convergence history sample.

OptimizationResult

pub struct OptimizationResult {

Result of one optimization run.

OptimizationResult.new

pub fn new(best_x: Vec<f64>, best_value: f64) -> Self {

Builds a result with the given point and value.