Default for LipoOptions {
fn default() -> Self {
Self {
p_explore: 0.1,
@@ -72,31 +72,33 @@ impl Default for LipoOptions {
}
}
-/// LIPO solver. See [module](self) documentation for more details.
-pub struct Lipo {
- options: LipoOptions,
- alpha: F::Field,
- xs: Vec>,
- ys: Vec,
+/// LIPO solver.
+///
+/// See [module](self) documentation for more details.
+pub struct Lipo {
+ options: LipoOptions,
+ alpha: P::Field,
+ xs: Vec>,
+ ys: Vec,
best: usize,
- k: F::Field,
- k_inf: F::Field,
+ k: P::Field,
+ k_inf: P::Field,
rng: Rng,
p_explore: f64,
- tmp: OVector,
- x_tmp: OVector,
- local_optimizer: NelderMead,
+ tmp: OVector,
+ x_tmp: OVector,
+ local_optimizer: NelderMead,
iter: usize,
}
-impl Lipo {
+impl Lipo {
/// Initializes LIPO solver with default options.
- pub fn new(f: &F, dom: &Domain, rng: Rng) -> Self {
- Self::with_options(f, dom, LipoOptions::default(), rng)
+ pub fn new(p: &P, dom: &Domain, rng: Rng) -> Self {
+ Self::with_options(p, dom, LipoOptions::default(), rng)
}
/// Initializes LIPO solver with given options.
- pub fn with_options(f: &F, dom: &Domain, options: LipoOptions, rng: Rng) -> Self {
+ pub fn with_options(p: &P, dom: &Domain, options: LipoOptions, rng: Rng) -> Self {
let dim = Dyn(dom.dim());
let p_explore = options.p_explore.clamp(0.0, 1.0);
@@ -112,13 +114,13 @@ impl Lipo {
xs: Vec::new(),
ys: Vec::new(),
best: 0,
- k: F::Field::zero(),
- k_inf: F::Field::zero(),
+ k: P::Field::zero(),
+ k_inf: P::Field::zero(),
rng,
p_explore,
tmp: OVector::zeros_generic(dim, U1::name()),
x_tmp: OVector::zeros_generic(dim, U1::name()),
- local_optimizer: NelderMead::new(f, dom),
+ local_optimizer: NelderMead::new(p, dom),
iter: 0,
}
}
@@ -128,8 +130,8 @@ impl Lipo {
self.xs.clear();
self.ys.clear();
self.best = 0;
- self.k = F::Field::zero();
- self.k_inf = F::Field::zero();
+ self.k = P::Field::zero();
+ self.k_inf = P::Field::zero();
self.iter = 0;
}
@@ -140,8 +142,8 @@ impl Lipo {
/// the LIPO solver gives extra information for free.
pub fn add_evaluation(
&mut self,
- x: OVector,
- y: F::Field,
+ x: OVector,
+ y: P::Field,
) -> Result<(), LipoError> {
let alpha = self.alpha;
@@ -177,9 +179,9 @@ impl Lipo {
debug!("|| x - xi || = {}", dist);
}
- let it = try_convert(((*k_inf).ln() / (F::Field::one() + alpha).ln()).ceil())
+ let it = try_convert(((*k_inf).ln() / (P::Field::one() + alpha).ln()).ceil())
.unwrap_or_default() as i32;
- *k = (F::Field::one() + alpha).powi(it);
+ *k = (P::Field::one() + alpha).powi(it);
if !k.is_finite() {
return Err(LipoError::InfiniteLipschitzConstant);
@@ -396,27 +398,27 @@ where
}
}
-impl Solver for Lipo
+impl Solver for Lipo
where
- F::Field: Sample,
+ R::Field: Sample,
{
const NAME: &'static str = "LIPO";
type Error = LipoError;
- fn solve_next(
+ fn solve_next(
&mut self,
- f: &F,
- dom: &Domain<::Field>,
- x: &mut Vector<::Field, Dyn, Sx>,
- fx: &mut Vector<::Field, Dyn, Sfx>,
+ r: &R,
+ dom: &Domain<::Field>,
+ x: &mut Vector<::Field, Dyn, Sx>,
+ rx: &mut Vector<::Field, Dyn, Srx>,
) -> Result<(), Self::Error>
where
- Sx: StorageMut<::Field, Dyn> + IsContiguous,
- Sfx: StorageMut<::Field, Dyn>,
+ Sx: StorageMut<::Field, Dyn> + IsContiguous,
+ Srx: StorageMut<::Field, Dyn>,
{
- self.next_inner(f, dom, x)?;
- f.eval(x, fx);
+ self.next_inner(r, dom, x)?;
+ r.eval(x, rx);
Ok(())
}
}
diff --git a/src/algo/nelder_mead.rs b/src/algo/nelder_mead.rs
index b4e6b66..ba2c08a 100644
--- a/src/algo/nelder_mead.rs
+++ b/src/algo/nelder_mead.rs
@@ -2,9 +2,9 @@
//!
//! [Nelder-Mead](https://en.wikipedia.org/wiki/Nelder%E2%80%93Mead_method)
//! simplex-reflection method is a popular derivative-free optimization
-//! algorithm. It keeps a [simplex](https://en.wikipedia.org/wiki/Simplex) of *n
-//! + 1* points and the simplex is reflected, expanded or contracted based on
-//! squared norm of residuals comparison.
+//! algorithm. It keeps a [simplex](https://en.wikipedia.org/wiki/Simplex) of
+//! _n + 1_ points and the simplex is reflected, expanded or contracted based on
+//! the function values comparison.
//!
//! # References
//!
@@ -53,23 +53,23 @@ pub enum CoefficientsFamily {
/// Options for [`NelderMead`] solver.
#[derive(Debug, Clone, CopyGetters, Setters)]
#[getset(get_copy = "pub", set = "pub")]
-pub struct NelderMeadOptions {
+pub struct NelderMeadOptions {
/// Family for coefficients adaptation or fixed coefficients. Default:
/// balanced (see [`CoefficientsFamily`]).
family: CoefficientsFamily,
/// Coefficient for reflection operation. Default: `-1`.
- reflection_coeff: F::Field,
+ reflection_coeff: P::Field,
/// Coefficient for expansion operation. Default: `-2`.
- expansion_coeff: F::Field,
+ expansion_coeff: P::Field,
/// Coefficient for outer contraction operation. Default: `-0.5`.
- outer_contraction_coeff: F::Field,
+ outer_contraction_coeff: P::Field,
/// Coefficient for inner contraction operation. Default: `0.5`.
- inner_contraction_coeff: F::Field,
+ inner_contraction_coeff: P::Field,
/// Coefficient for shrinking operation. Default: `0.5`.
- shrink_coeff: F::Field,
+ shrink_coeff: P::Field,
}
-impl Default for NelderMeadOptions {
+impl Default for NelderMeadOptions {
fn default() -> Self {
Self {
family: CoefficientsFamily::Standard,
@@ -82,8 +82,8 @@ impl Default for NelderMeadOptions {
}
}
-impl NelderMeadOptions {
- fn overwrite_coeffs(&mut self, dom: &Domain) {
+impl NelderMeadOptions {
+ fn overwrite_coeffs(&mut self, dom: &Domain) {
let Self {
family,
reflection_coeff,
@@ -102,14 +102,14 @@ impl NelderMeadOptions {
*shrink_coeff = convert(0.5);
}
CoefficientsFamily::Balanced => {
- let n: F::Field = convert(dom.dim() as f64);
- let n_inv = F::Field::one() / n;
+ let n: P::Field = convert(dom.dim() as f64);
+ let n_inv = P::Field::one() / n;
*reflection_coeff = convert(-1.0);
*expansion_coeff = -(n_inv * convert(2.0) + convert(1.0));
- *outer_contraction_coeff = -(F::Field::one() - n_inv);
+ *outer_contraction_coeff = -(P::Field::one() - n_inv);
*inner_contraction_coeff = -*outer_contraction_coeff;
- *shrink_coeff = F::Field::one() - n_inv;
+ *shrink_coeff = P::Field::one() - n_inv;
}
CoefficientsFamily::GoldenSection => {
let alpha = 1.0 / (0.5 * (5f64.sqrt() + 1.0));
@@ -126,27 +126,29 @@ impl NelderMeadOptions {
}
}
-/// Nelder-Mead solver. See [module](self) documentation for more details.
-pub struct NelderMead {
- options: NelderMeadOptions,
- scale: OVector,
- centroid: OVector,
- reflection: OVector,
- expansion: OVector,
- contraction: OVector,
- simplex: Vec>,
- errors: Vec,
+/// Nelder-Mead solver.
+///
+/// See [module](self) documentation for more details.
+pub struct NelderMead {
+ options: NelderMeadOptions,
+ scale: OVector,
+ centroid: OVector,
+ reflection: OVector,
+ expansion: OVector,
+ contraction: OVector,
+ simplex: Vec>,
+ errors: Vec,
sort_perm: Vec,
}
-impl NelderMead {
+impl NelderMead {
/// Initializes Nelder-Mead solver with default options.
- pub fn new(f: &F, dom: &Domain) -> Self {
- Self::with_options(f, dom, NelderMeadOptions::default())
+ pub fn new(p: &P, dom: &Domain) -> Self {
+ Self::with_options(p, dom, NelderMeadOptions::default())
}
/// Initializes Nelder-Mead solver with given options.
- pub fn with_options(_: &F, dom: &Domain, mut options: NelderMeadOptions) -> Self {
+ pub fn with_options(_: &P, dom: &Domain, mut options: NelderMeadOptions) -> Self {
let dim = Dyn(dom.dim());
options.overwrite_coeffs(dom);
@@ -261,8 +263,6 @@ impl NelderMead {
}
sort_perm.extend(0..=n);
- // Stable sort is important for sort_perm[0] being consistent with
- // fx_best.
sort_perm.sort_by(|a, b| {
errors[*a]
.partial_cmp(&errors[*b])
@@ -402,8 +402,7 @@ impl NelderMead {
}
};
- // Establish the ordering of simplex points. Stable sort is important
- // for sort_perm[0] being consistent with fx_best.
+ // Establish the ordering of simplex points.
sort_perm.sort_by(|a, b| {
errors[*a]
.partial_cmp(&errors[*b])
@@ -411,7 +410,7 @@ impl NelderMead {
});
debug!(
- "performed {}{},\t|| fx || = {} - {}",
+ "performed {}{},\tfx = {} - {}",
transformation.as_str(),
if not_feasible { " with projection" } else { "" },
errors[sort_perm[0]],
@@ -420,7 +419,6 @@ impl NelderMead {
// Return the best simplex point.
x.copy_from(&simplex[sort_perm[0]]);
- // fx corresponding to x is stored in `self.fx_best`.
if transformation == Transformation::Shrinkage
|| transformation == Transformation::InnerContraction
@@ -467,24 +465,24 @@ impl Optimizer for NelderMead {
}
}
-impl Solver for NelderMead {
+impl Solver for NelderMead {
const NAME: &'static str = "Nelder-Mead";
type Error = NelderMeadError;
- fn solve_next(
+ fn solve_next(
&mut self,
- f: &F,
- dom: &Domain,
- x: &mut Vector,
- fx: &mut Vector,
+ r: &R,
+ dom: &Domain,
+ x: &mut Vector,
+ rx: &mut Vector,
) -> Result<(), Self::Error>
where
- Sx: StorageMut + IsContiguous,
- Sfx: StorageMut,
+ Sx: StorageMut + IsContiguous,
+ Srx: StorageMut,
{
- self.next_inner(f, dom, x)?;
- f.eval(x, fx);
+ self.next_inner(r, dom, x)?;
+ r.eval(x, rx);
Ok(())
}
}
diff --git a/src/algo/steffensen.rs b/src/algo/steffensen.rs
index 7e64b22..a70d2f6 100644
--- a/src/algo/steffensen.rs
+++ b/src/algo/steffensen.rs
@@ -7,7 +7,7 @@
//!
//! # References
//!
-//! \[1\] [Wikipedia](https://en.wikipedia.org/wiki/Steffensen%27s_method)
+//! \[1\] [Steffensen's method](https://en.wikipedia.org/wiki/Steffensen%27s_method) on Wikipedia
//!
//! \[2\] [A variant of Steffensen's method of fourth-order convergence and its
//! applications](https://www.sciencedirect.com/science/article/pii/S0096300310002705)
@@ -34,15 +34,15 @@ pub enum SteffensenVariant {
/// Options for [`Steffensen`] solver.
#[derive(Debug, Clone, CopyGetters, Setters)]
#[getset(get_copy = "pub", set = "pub")]
-pub struct SteffensenOptions {
+pub struct SteffensenOptions {
/// Variant of the Steffensen's method. Default: Liu (see
/// [`SteffensenVariant`]).
variant: SteffensenVariant,
#[getset(skip)]
- _phantom: PhantomData,
+ _phantom: PhantomData,
}
-impl Default for SteffensenOptions {
+impl Default for SteffensenOptions {
fn default() -> Self {
Self {
variant: SteffensenVariant::Liu,
@@ -51,19 +51,21 @@ impl Default for SteffensenOptions {
}
}
-/// Steffensen solver. See [module](self) documentation for more details.
-pub struct Steffensen {
- options: SteffensenOptions,
+/// Steffensen solver.
+///
+/// See [module](self) documentation for more details.
+pub struct Steffensen {
+ options: SteffensenOptions,
}
-impl Steffensen {
+impl