First version of the optimizer

This commit is contained in:
Volodymyr Orlov
2019-10-29 08:59:06 -07:00
parent f4aec2b35e
commit 4488cc110e
10 changed files with 521 additions and 5 deletions
+88
View File
@@ -0,0 +1,88 @@
use crate::math::EPSILON;
use crate::optimization::FunctionOrder;
pub trait LineSearchMethod {
fn search<'a>(&self, f: &(dyn Fn(f64) -> f64), df: &(dyn Fn(f64) -> f64), alpha: f64, f0: f64, df0: f64) -> LineSearchResult;
}
#[derive(Debug, Clone)]
pub struct LineSearchResult {
pub alpha: f64,
pub f_x: f64
}
pub struct Backtracking {
pub c1: f64,
pub max_iterations: usize,
pub phi: f64,
pub plo: f64,
pub order: FunctionOrder
}
impl Default for Backtracking {
fn default() -> Self {
Backtracking {
c1: 1e-4,
max_iterations: 1000,
phi: 0.5,
plo: 0.1,
order: FunctionOrder::SECOND
}
}
}
impl LineSearchMethod for Backtracking {
fn search<'a>(&self, f: &(dyn Fn(f64) -> f64), _: &(dyn Fn(f64) -> f64), alpha: f64, f0: f64, df0: f64) -> LineSearchResult {
let (mut a1, mut a2) = (alpha, alpha);
let (mut fx0, mut fx1) = (f0, f(a1));
let mut iteration = 0;
while fx1 > f0 + self.c1 * a2 * df0 {
if iteration > self.max_iterations {
panic!("Linesearch failed to converge, reached maximum iterations.");
}
let a_tmp;
match self.order {
FunctionOrder::FIRST | FunctionOrder::SECOND => {
a_tmp = - (df0 * a2.powf(2.)) / (2. * (fx1 - f0 - df0*a2))
},
FunctionOrder::THIRD => {
let div = 1. / (a1.powf(2.) * a2.powf(2.) * (a2 - a1));
let a = (a1.powf(2.) * (fx1 - f0 - df0*a2) - a2.powf(2.)*(fx0 - f0 - df0*a1))*div;
let b = (-a1.powf(3.) * (fx1 - f0 - df0*a2) + a2.powf(3.)*(fx0 - f0 - df0*a1))*div;
if (a - 0.).powf(2.).sqrt() <= EPSILON {
a_tmp = df0 / (2. * b);
} else {
let d = f64::max(b.powf(2.) - 3. * a * df0, 0.);
a_tmp = (-b + d.sqrt()) / (3.*a); //root of quadratic equation
}
}
}
a1 = a2;
a2 = f64::max(f64::min(a_tmp, a2*self.phi), a2*self.plo);
fx0 = fx1;
fx1 = f(a2);
iteration += 1;
}
LineSearchResult {
alpha: a2,
f_x: fx1
}
}
}