Skip to content

Commit

Permalink
Rename all with_tol methods to with_tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-k committed Apr 9, 2022
1 parent 6190c1a commit c9498ba
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion argmin/examples/goldensectionsearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl CostFunction for TestFunc {
fn main() {
let cost = TestFunc {};
let init_param = -0.5;
let solver = GoldenSectionSearch::new(-2.5, 3.0).tolerance(0.0001);
let solver = GoldenSectionSearch::new(-2.5, 3.0).with_tolerance(0.0001);

let res = Executor::new(cost, solver)
.configure(|state| state.param(init_param).max_iters(100))
Expand Down
11 changes: 6 additions & 5 deletions argmin/src/solver/gaussnewton/gaussnewton_linesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ impl<L, F: ArgminFloat> GaussNewtonLS<L, F> {
/// # use argmin::core::Error;
/// # fn main() -> Result<(), Error> {
/// # let linesearch = ();
/// let gauss_newton_ls = GaussNewtonLS::new(linesearch).with_tol(1e-4f64)?;
/// let gauss_newton_ls = GaussNewtonLS::new(linesearch).with_tolerance(1e-4f64)?;
/// # Ok(())
/// # }
/// ```
pub fn with_tol(mut self, tol: F) -> Result<Self, Error> {
pub fn with_tolerance(mut self, tol: F) -> Result<Self, Error> {
if tol <= F::from_f64(0.0).unwrap() {
return Err(ArgminError::InvalidParameter {
text: "Gauss-Newton-Linesearch: tol must be positive.".to_string(),
Expand Down Expand Up @@ -260,15 +260,16 @@ mod tests {
let tol1: f64 = 1e-4;

let linesearch = ();
let GaussNewtonLS { tol: t1, .. } = GaussNewtonLS::new(linesearch).with_tol(tol1).unwrap();
let GaussNewtonLS { tol: t1, .. } =
GaussNewtonLS::new(linesearch).with_tolerance(tol1).unwrap();

assert_eq!(t1.to_ne_bytes(), tol1.to_ne_bytes());
}

#[test]
fn test_tolerance_error_when_negative() {
let tol = -2.0;
let error = GaussNewtonLS::new(()).with_tol(tol);
let error = GaussNewtonLS::new(()).with_tolerance(tol);
assert_error!(
error,
ArgminError,
Expand All @@ -279,7 +280,7 @@ mod tests {
#[test]
fn test_tolerance_error_when_zero() {
let tol = 0.0;
let error = GaussNewtonLS::new(()).with_tol(tol);
let error = GaussNewtonLS::new(()).with_tolerance(tol);
assert_error!(
error,
ArgminError,
Expand Down
8 changes: 4 additions & 4 deletions argmin/src/solver/gaussnewton/gaussnewton_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ impl<F: ArgminFloat> GaussNewton<F> {
/// # use argmin::solver::gaussnewton::GaussNewton;
/// # use argmin::core::Error;
/// # fn main() -> Result<(), Error> {
/// let gauss_newton = GaussNewton::new().with_tol(1e-4f64)?;
/// let gauss_newton = GaussNewton::new().with_tolerance(1e-4f64)?;
/// # Ok(())
/// # }
/// ```
pub fn with_tol(mut self, tol: F) -> Result<Self, Error> {
pub fn with_tolerance(mut self, tol: F) -> Result<Self, Error> {
if tol <= F::from_f64(0.0).unwrap() {
return Err(ArgminError::InvalidParameter {
text: "Gauss-Newton: tol must be positive.".to_string(),
Expand Down Expand Up @@ -183,15 +183,15 @@ mod tests {
fn test_tolerance() {
let tol1: f64 = 1e-4;

let GaussNewton { tol: t, .. } = GaussNewton::new().with_tol(tol1).unwrap();
let GaussNewton { tol: t, .. } = GaussNewton::new().with_tolerance(tol1).unwrap();

assert_eq!(t.to_ne_bytes(), tol1.to_ne_bytes());
}

#[test]
fn test_tolerance_error() {
let tol = -2.0;
let error = GaussNewton::new().with_tol(tol);
let error = GaussNewton::new().with_tolerance(tol);
assert_error!(
error,
ArgminError,
Expand Down
19 changes: 12 additions & 7 deletions argmin/src/solver/goldensectionsearch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! Golden Section Search
//! # Golden Section Search
//!
//! # References:
//! The golden-section search is a technique for finding an extremum (minimum or maximum) of a
//! function inside a specified interval.
//!
//! [Wikipedia](https://en.wikipedia.org/wiki/Golden-section_search)
//! See [`GoldenSectionSearch`] for details.
//!
//! ## Reference
//!
//! <https://en.wikipedia.org/wiki/Golden-section_search>

use crate::core::{
ArgminError, ArgminFloat, CostFunction, Error, IterState, Problem, Solver, TerminationReason,
Expand All @@ -23,7 +28,7 @@ const GOLDEN_RATIO: f64 = 1.618_033_988_749_895;
const G1: f64 = -1.0 + GOLDEN_RATIO;
const G2: f64 = 1.0 - G1;

/// Golden-section search
/// # Golden-section search
///
/// The golden-section search is a technique for finding an extremum (minimum or maximum) of a
/// function inside a specified interval.
Expand All @@ -36,9 +41,9 @@ const G2: f64 = 1.0 - G1;
///
/// The `min_bound` and `max_bound` arguments define values that bracket the expected minimum.
///
/// # References:
/// ## Reference
///
/// [Wikipedia](https://en.wikipedia.org/wiki/Golden-section_search)
/// <https://en.wikipedia.org/wiki/Golden-section_search>
#[derive(Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct GoldenSectionSearch<F> {
Expand Down Expand Up @@ -79,7 +84,7 @@ where

/// Set tolerance
#[must_use]
pub fn tolerance(mut self, tol: F) -> Self {
pub fn with_tolerance(mut self, tol: F) -> Self {
self.tolerance = tol;
self
}
Expand Down
4 changes: 2 additions & 2 deletions argmin/src/solver/newton/newton_cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where
}

/// Set tolerance for the stopping criterion based on cost difference
pub fn with_tol(mut self, tol: F) -> Result<Self, Error> {
pub fn with_tolerance(mut self, tol: F) -> Result<Self, Error> {
if tol <= F::from_f64(0.0).unwrap() {
return Err(ArgminError::InvalidParameter {
text: "Newton-CG: tol must be positive.".to_string(),
Expand Down Expand Up @@ -221,7 +221,7 @@ mod tests {
MoreThuenteLineSearch::new();

let NewtonCG { tol: t, .. }: NewtonCG<MoreThuenteLineSearch<Vec<f64>, Vec<f64>, f64>, f64> =
NewtonCG::new(linesearch).with_tol(tol1).unwrap();
NewtonCG::new(linesearch).with_tolerance(tol1).unwrap();

assert!((t - tol1).abs() < std::f64::EPSILON);
}
Expand Down
8 changes: 4 additions & 4 deletions argmin/src/solver/quasinewton/bfgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ where

/// Sets tolerance for the stopping criterion based on the change of the norm on the gradient
#[must_use]
pub fn with_tol_grad(mut self, tol_grad: F) -> Self {
pub fn with_tolerance_grad(mut self, tol_grad: F) -> Self {
self.tol_grad = tol_grad;
self
}

/// Sets tolerance for the stopping criterion based on the change of the cost stopping criterion
#[must_use]
pub fn with_tol_cost(mut self, tol_cost: F) -> Self {
pub fn with_tolerance_cost(mut self, tol_cost: F) -> Self {
self.tol_cost = tol_cost;
self
}
Expand Down Expand Up @@ -226,8 +226,8 @@ mod tests {
tol_cost: t2,
..
} = BFGS::new(init_hessian, linesearch)
.with_tol_grad(tol1)
.with_tol_cost(tol2);
.with_tolerance_grad(tol1)
.with_tolerance_cost(tol2);

assert!((t1 - tol1).abs() < std::f64::EPSILON);
assert!((t2 - tol2).abs() < std::f64::EPSILON);
Expand Down
4 changes: 2 additions & 2 deletions argmin/src/solver/quasinewton/dfp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where

/// Sets tolerance for the stopping criterion based on the change of the norm on the gradient
#[must_use]
pub fn with_tol_grad(mut self, tol_grad: F) -> Self {
pub fn with_tolerance_grad(mut self, tol_grad: F) -> Self {
self.tol_grad = tol_grad;
self
}
Expand Down Expand Up @@ -193,7 +193,7 @@ mod tests {

let tol: f64 = 1e-4;

let DFP { tol_grad: t, .. } = DFP::new(init_hessian, linesearch).with_tol_grad(tol);
let DFP { tol_grad: t, .. } = DFP::new(init_hessian, linesearch).with_tolerance_grad(tol);

assert!((t - tol).abs() < std::f64::EPSILON);
}
Expand Down
8 changes: 4 additions & 4 deletions argmin/src/solver/quasinewton/lbfgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ where

/// Sets tolerance for the stopping criterion based on the change of the norm on the gradient
#[must_use]
pub fn with_tol_grad(mut self, tol_grad: F) -> Self {
pub fn with_tolerance_grad(mut self, tol_grad: F) -> Self {
self.tol_grad = tol_grad;
self
}

/// Sets tolerance for the stopping criterion based on the change of the cost stopping criterion
#[must_use]
pub fn with_tol_cost(mut self, tol_cost: F) -> Self {
pub fn with_tolerance_cost(mut self, tol_cost: F) -> Self {
self.tol_cost = tol_cost;
self
}
Expand Down Expand Up @@ -221,8 +221,8 @@ mod tests {
tol_cost: t2,
..
}: LBFGS<_, Vec<f64>, Vec<f64>, f64> = LBFGS::new(linesearch, 7)
.with_tol_grad(tol1)
.with_tol_cost(tol2);
.with_tolerance_grad(tol1)
.with_tolerance_cost(tol2);

assert!((t1 - tol1).abs() < std::f64::EPSILON);
assert!((t2 - tol2).abs() < std::f64::EPSILON);
Expand Down
8 changes: 4 additions & 4 deletions argmin/src/solver/quasinewton/sr1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ where

/// Sets tolerance for the stopping criterion based on the change of the norm on the gradient
#[must_use]
pub fn with_tol_grad(mut self, tol_grad: F) -> Self {
pub fn with_tolerance_grad(mut self, tol_grad: F) -> Self {
self.tol_grad = tol_grad;
self
}

/// Sets tolerance for the stopping criterion based on the change of the cost stopping criterion
#[must_use]
pub fn with_tol_cost(mut self, tol_cost: F) -> Self {
pub fn with_tolerance_cost(mut self, tol_cost: F) -> Self {
self.tol_cost = tol_cost;
self
}
Expand Down Expand Up @@ -226,8 +226,8 @@ mod tests {
tol_cost: t2,
..
} = SR1::new(init_hessian, linesearch)
.with_tol_grad(tol1)
.with_tol_cost(tol2);
.with_tolerance_grad(tol1)
.with_tolerance_cost(tol2);

assert!((t1 - tol1).abs() < std::f64::EPSILON);
assert!((t2 - tol2).abs() < std::f64::EPSILON);
Expand Down
4 changes: 2 additions & 2 deletions argmin/src/solver/quasinewton/sr1_trustregion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where

/// Sets tolerance for the stopping criterion based on the change of the norm on the gradient
#[must_use]
pub fn with_tol_grad(mut self, tol_grad: F) -> Self {
pub fn with_tolerance_grad(mut self, tol_grad: F) -> Self {
self.tol_grad = tol_grad;
self
}
Expand Down Expand Up @@ -270,7 +270,7 @@ mod tests {
let tol: f64 = 1e-4;

let SR1TrustRegion { tol_grad: t, .. }: SR1TrustRegion<TestProblem, CauchyPoint<f64>, f64> =
SR1TrustRegion::new(subproblem).with_tol_grad(tol);
SR1TrustRegion::new(subproblem).with_tolerance_grad(tol);

assert!((t - tol).abs() < std::f64::EPSILON);
}
Expand Down

0 comments on commit c9498ba

Please sign in to comment.