argmin/solver/linesearch/mod.rs
1// Copyright 2018-2024 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! # Line search methods
9//!
10//! Line searches are given a position in parameter space and a direction. They obtain a step
11//! length in this direction which fulfills a given set of [acceptance conditions](`condition`).
12//!
13//! These methods are often an integral part of other methods, such as gradient descent.
14//! Each algorithm which implements the [`LineSearch`] trait can be used in these optimization
15//! methods.
16//!
17//! ## Available line searches
18//!
19//! * [Backtracking line search](`BacktrackingLineSearch`)
20//! * [More-Thuente line search](`MoreThuenteLineSearch`)
21//! * [Hager-Zhang line search](`HagerZhangLineSearch`)
22//!
23//! ## References
24//!
25//! \[0\] Jorge Nocedal and Stephen J. Wright (2006). Numerical Optimization.
26//! Springer. ISBN 0-387-30303-0.
27//!
28//! \[1\] Jorge J. More and David J. Thuente. "Line search algorithms with guaranteed sufficient
29//! decrease." ACM Trans. Math. Softw. 20, 3 (September 1994), 286-307.
30//! DOI: <https://doi.org/10.1145/192115.192132>
31//!
32//! \[2\] William W. Hager and Hongchao Zhang. "A new conjugate gradient method with guaranteed
33//! descent and an efficient line search." SIAM J. Optim. 16(1), 2006, 170-192.
34//! DOI: <https://doi.org/10.1137/030601880>
35
36mod backtracking;
37/// Acceptance conditions
38pub mod condition;
39mod hagerzhang;
40mod morethuente;
41
42pub use self::backtracking::BacktrackingLineSearch;
43pub use self::hagerzhang::HagerZhangLineSearch;
44pub use self::morethuente::MoreThuenteLineSearch;
45
46/// # Line search trait
47///
48/// For a method to be used as a line search, it has to implement this trait.
49///
50/// It enables the optimization method to set the search direction and the initial step length of
51/// the line search.
52///
53/// ## Example
54///
55/// ```
56/// use argmin::solver::linesearch::LineSearch;
57///
58/// struct MyLineSearch<D, F> {
59/// // `D` is the type of the search direction
60/// search_direction: D,
61/// // `F` is a floating point value (f32 or f64)
62/// init_step_length: F,
63/// // ...
64/// }
65///
66/// impl<D, F> LineSearch<D, F> for MyLineSearch<D, F> {
67/// fn search_direction(&mut self, direction: D) {
68/// self.search_direction = direction;
69/// }
70///
71/// fn initial_step_length(&mut self, step_length: F) -> Result<(), argmin::core::Error> {
72/// self.init_step_length = step_length;
73/// Ok(())
74/// }
75/// }
76/// ```
77pub trait LineSearch<D, F> {
78 /// Set the search direction
79 ///
80 /// This is the direction in which the line search will be performed.
81 fn search_direction(&mut self, direction: D);
82
83 /// Set the initial step length
84 ///
85 /// This indicates the first step length which will be tried.
86 fn initial_step_length(&mut self, step_length: F) -> Result<(), crate::core::Error>;
87}