Skip to main content

Objective

Trait Objective 

Source
pub trait Objective {
    // Required methods
    fn value(&self, x: &[f64]) -> f64;
    fn grad(&self, x: &[f64]) -> Vec<f64>;

    // Provided method
    fn hessian(&self, x: &[f64]) -> Vec<Vec<f64>> { ... }
}
Expand description

A differentiable scalar objective f: ℝⁿ → ℝ to be minimized.

Implementors must supply value and grad. The Hessian defaults to a central finite-difference approximation built from grad, so second-order optimizers work for any objective without an analytic Hessian; objectives that have one may override hessian for accuracy.

Required Methods§

Source

fn value(&self, x: &[f64]) -> f64

Evaluates the objective at x.

§Arguments
  • x — the point at which to evaluate; any finite coordinates.
§Returns

The scalar objective value f(x).

Source

fn grad(&self, x: &[f64]) -> Vec<f64>

Evaluates the gradient ∇f(x).

§Arguments
  • x — the point at which to evaluate the gradient.
§Returns

The gradient vector, the same length as x.

Provided Methods§

Source

fn hessian(&self, x: &[f64]) -> Vec<Vec<f64>>

Approximates the Hessian ∇²f(x) by central differences of the gradient.

The default uses a step of √ε ≈ 1.49e-8 per coordinate and symmetrizes the result so it is exactly symmetric (rounding can otherwise break symmetry). Objectives with an analytic Hessian should override this.

§Arguments
  • x — the point at which to approximate the Hessian.
§Returns

The n × n Hessian in row-major order (n = x.len()).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§