argmin/solver/linesearch/condition/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 conditions
9//!
10//! For a step length to be accepted in a line search, it needs to satisfy one of several
11//! conditions.
12//!
13//! A condition exposes an interface defined by the trait
14//! [`LineSearchCondition`](`condition::LineSearchCondition`).
15//!
16//! # Available line search conditions
17//!
18//! * [`ArmijoCondition`](`condition::ArmijoCondition`)
19//! * [`WolfeCondition`](`condition::WolfeCondition`)
20//! * [`StrongWolfeCondition`](`condition::StrongWolfeCondition`)
21//! * [`GoldsteinCondition`](`condition::GoldsteinCondition`)
22//!
23//! ## Reference
24//!
25//! Jorge Nocedal and Stephen J. Wright (2006). Numerical Optimization.
26//! Springer. ISBN 0-387-30303-0.
27
28mod armijo;
29mod goldstein;
30mod strongwolfe;
31mod wolfe;
32
33pub use armijo::ArmijoCondition;
34pub use goldstein::GoldsteinCondition;
35pub use strongwolfe::StrongWolfeCondition;
36pub use wolfe::WolfeCondition;
37
38/// Interface which a condition needs to implement.
39///
40/// # Example
41///
42/// ```
43/// use argmin::solver::linesearch::condition::LineSearchCondition;
44///
45/// pub struct MyCondition {}
46///
47/// impl<T, G, F> LineSearchCondition<T, G, F> for MyCondition {
48/// fn evaluate_condition(
49/// &self,
50/// current_cost: F,
51/// current_gradient: Option<&G>,
52/// initial_cost: F,
53/// initial_gradient: &G,
54/// search_direction: &T,
55/// step_length: F,
56/// ) -> bool {
57/// // Use the current cost function value, the current gradient, the initial cost function
58/// // value, the initial gradient, the search direction and the current step length to
59/// // compute whether your condition is met (return `true`) or not (return `false`).
60/// # true
61/// }
62///
63/// fn requires_current_gradient(&self) -> bool {
64/// // Indicate to the calling method whether your condition requires the current gradient
65/// // (at the position defined by the current step length).
66/// true
67/// }
68/// }
69/// ```
70pub trait LineSearchCondition<T, G, F> {
71 /// Evaluate the condition
72 ///
73 /// This method has access to the initial cost function value and the initial gradient (at the
74 /// initial point from where to search from), the current cost function value at the initial
75 /// point plus a step in search direction of length `step_length` as well as (optionally) the
76 /// gradient at that point. It further has access to the search direction and the step length.
77 ///
78 /// It returns `true` if the condition was met and `false` if not.
79 fn evaluate_condition(
80 &self,
81 current_cost: F,
82 current_gradient: Option<&G>,
83 initial_cost: F,
84 initial_gradient: &G,
85 search_direction: &T,
86 step_length: F,
87 ) -> bool;
88
89 /// Indicates whether this condition requires the computation of the gradient at the new point
90 ///
91 /// This should return `false` if the evaluation of the condition does not require the gradient
92 /// at the current point and `true` otherwise.
93 fn requires_current_gradient(&self) -> bool;
94}