mod base;
mod cache;
pub mod lj;
pub(crate) mod common {
pub use gut::prelude::*;
}
pub fn fire() -> fire::FIRE {
fire::FIRE::default()
}
pub fn monitor<G>(cb: G) -> Option<UserTermination<G>>
where
G: FnMut(&Progress) -> bool,
{
Some(UserTermination(cb))
}
mod fire;
pub use crate::fire::FIRE;
use vecfx::*;
#[derive(Debug, Clone)]
pub struct Progress<'a> {
gnorm: f64,
niter: usize,
ncall: usize,
step: f64,
displacement: &'a [f64],
x: &'a [f64],
gx: &'a [f64],
fx: f64,
}
impl<'a> Progress<'a> {
pub fn gradient(&self) -> &'a [f64] {
self.gx
}
pub fn value(&self) -> f64 {
self.fx
}
pub fn ncalls(&self) -> usize {
self.ncall
}
pub fn displacement(&self) -> &'a [f64] {
self.displacement
}
pub fn report(&self) {
println!(
"niter = {:5}, ncall= {:5}: fx = {:-10.4}, gnorm = {:-10.4}, dnorm = {:-10.4} step = {:-10.4}",
self.niter,
self.ncall,
self.fx,
self.gnorm,
self.displacement.vec2norm(),
self.step,
);
}
}
pub trait TerminationCriteria {
fn met(&mut self, progress: &Progress) -> bool;
}
#[derive(Debug, Clone)]
pub struct UserTermination<G>(pub G)
where
G: FnMut(&Progress) -> bool;
impl<G> TerminationCriteria for UserTermination<G>
where
G: FnMut(&Progress) -> bool,
{
fn met(&mut self, progress: &Progress) -> bool {
(self.0)(progress)
}
}
#[derive(Debug, Clone)]
pub(crate) struct Termination {
pub max_cycles: usize,
pub max_gradient_norm: f64,
}
impl Default for Termination {
fn default() -> Self {
Termination {
max_cycles: 0,
max_gradient_norm: 0.01,
}
}
}
impl TerminationCriteria for Termination {
fn met(&mut self, progress: &Progress) -> bool {
if self.max_cycles > 0 && progress.niter >= self.max_cycles {
return true;
}
if progress.gnorm <= self.max_gradient_norm {
return true;
}
false
}
}
#[deprecated(note = "plan to be removed, please use minimize_iter method instead")]
pub trait GradientBasedMinimizer: Sized {
fn minimize<E>(mut self, x: &mut [f64], mut f: E)
where
E: FnMut(&[f64], &mut [f64]) -> f64,
{
self.minimize_alt::<E, _>(x, f, monitor(|_| false))
}
fn minimize_alt<E, G>(mut self, x: &mut [f64], mut f: E, mut stopping: Option<G>)
where
E: FnMut(&[f64], &mut [f64]) -> f64,
G: TerminationCriteria,
{
unimplemented!()
}
}
pub use crate::base::Output;
pub use crate::fire::*;