Skip to main content

LineSearch

Trait LineSearch 

Source
pub trait LineSearch {
    // Required method
    fn search<F, G>(
        &self,
        f: &F,
        grad: &G,
        x: &Vector<f32>,
        d: &Vector<f32>,
    ) -> f32
       where F: Fn(&Vector<f32>) -> f32,
             G: Fn(&Vector<f32>) -> Vector<f32>;
}
Expand description

Trait for line search strategies.

Given a function f, current point x, and search direction d, finds a step size α > 0 such that x + α*d satisfies certain decrease conditions.

§Example

use aprender::optim::{LineSearch, BacktrackingLineSearch};
use aprender::primitives::Vector;

let ls = BacktrackingLineSearch::default();
let f = |x: &Vector<f32>| x[0] * x[0];
let grad = |x: &Vector<f32>| Vector::from_slice(&[2.0 * x[0]]);

let x = Vector::from_slice(&[1.0]);
let d = Vector::from_slice(&[-2.0]); // Descent direction
let alpha = ls.search(&f, &grad, &x, &d);
assert!(alpha > 0.0);

Different strategies enforce different conditions on the step size.

Required Methods§

Source

fn search<F, G>(&self, f: &F, grad: &G, x: &Vector<f32>, d: &Vector<f32>) -> f32
where F: Fn(&Vector<f32>) -> f32, G: Fn(&Vector<f32>) -> Vector<f32>,

Finds a suitable step size along the search direction.

§Arguments
  • f - Objective function f: ℝⁿ → ℝ
  • grad - Gradient function ∇f: ℝⁿ → ℝⁿ
  • x - Current point
  • d - Search direction (typically descent direction, ∇f(x)·d < 0)
§Returns

Step size α > 0 satisfying the line search conditions

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§