mod cobyla;
mod derivative_free;
mod gradient;
mod newton_cg;
pub use derivative_free::{Bobyqa, Cobyla};
pub use gradient::{LBfgs, NewtonExact};
pub use newton_cg::NewtonCg;
use nalgebra::DMatrix;
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
use crate::time::Instant;
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct Eval {
pub cost: f64,
pub fidelity: f64,
pub penalty: f64,
}
impl Eval {
pub fn score(&self) -> f64 {
self.fidelity - self.penalty
}
}
pub trait Objective: Sync {
fn dim(&self) -> usize;
fn value(&self, x: &[f64]) -> Result<Eval>;
fn gradient(&self, x: &[f64]) -> Result<(Eval, Vec<f64>)>;
fn hessian(&self, x: &[f64]) -> Result<(Eval, Vec<f64>, DMatrix<f64>)>;
fn hvp(&self, x: &[f64], v: &[f64]) -> Result<Vec<f64>> {
let (_, _, h) = self.hessian(x)?;
Ok((h * nalgebra::DVector::from_column_slice(v)).as_slice().to_vec())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Derivatives {
None,
Gradient,
Hessian,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct IterationReport {
pub iteration: usize,
pub fidelity: f64,
pub penalty: f64,
pub cost: f64,
pub grad_norm: Option<f64>,
pub evaluations: usize,
pub elapsed_s: f64,
}
pub trait ProgressSink {
fn on_iteration(&mut self, report: &IterationReport);
fn should_cancel(&self) -> bool {
false
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct NoProgress;
impl ProgressSink for NoProgress {
fn on_iteration(&mut self, _: &IterationReport) {}
}
#[derive(Clone, Debug, Default)]
pub struct RecordingSink {
pub reports: Vec<IterationReport>,
}
impl ProgressSink for RecordingSink {
fn on_iteration(&mut self, report: &IterationReport) {
self.reports.push(report.clone());
}
}
#[derive(Clone, Copy, Debug)]
pub struct ConsoleSink {
pub every: usize,
header: bool,
}
impl ConsoleSink {
pub fn new(every: usize) -> Self {
ConsoleSink {
every: every.max(1),
header: false,
}
}
}
impl ProgressSink for ConsoleSink {
fn on_iteration(&mut self, r: &IterationReport) {
if !self.header {
println!(
"{:>9} {:>10} {:>10} {:>10} {:>9}",
"iteration", "fidelity", "penalty", "|grad|", "time (s)"
);
self.header = true;
}
if r.iteration.is_multiple_of(self.every) {
let g = r.grad_norm.map_or("-".to_string(), |g| format!("{g:.3e}"));
println!(
"{:>9} {:>10.6} {:>10.3e} {:>10} {:>9.2}",
r.iteration, r.fidelity, r.penalty, g, r.elapsed_s
);
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Exit {
TargetReached,
Converged,
MaxIterations,
Cancelled,
Failed(String),
}
impl Exit {
pub fn message(&self) -> String {
match self {
Exit::TargetReached => "target fidelity reached".into(),
Exit::Converged => "converged".into(),
Exit::MaxIterations => "iteration limit reached".into(),
Exit::Cancelled => "cancelled".into(),
Exit::Failed(m) => format!("stopped: {m}"),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct OptimResult {
pub x: Vec<f64>,
pub eval: Eval,
pub iterations: usize,
pub evaluations: usize,
pub exit: Exit,
}
pub struct RunControl<'a> {
pub max_iter: usize,
pub target_fidelity: f64,
pub sink: &'a mut dyn ProgressSink,
}
pub trait Optimizer: Send + Sync {
fn name(&self) -> &'static str;
fn derivatives(&self) -> Derivatives;
fn minimize(&self, obj: &dyn Objective, x0: &[f64], ctl: &mut RunControl) -> Result<OptimResult>;
}
pub fn optimizer(name: &str) -> Result<Box<dyn Optimizer>> {
Ok(match name {
"l-bfgs" => Box::new(LBfgs),
"newton-cg" => Box::new(NewtonCg),
"newton-exact" => Box::new(NewtonExact),
"cobyla" => Box::new(Cobyla),
"bobyqa" => Box::new(Bobyqa),
_ => {
return Err(Error::NotSupported(format!(
"algorithm \"{name}\" is not available; choose one of {}",
optimizer_names().join(", ")
)));
}
})
}
pub fn optimizer_names() -> &'static [&'static str] {
&["l-bfgs", "newton-cg", "newton-exact", "cobyla", "bobyqa"]
}
pub struct Monitor<'s> {
sink: &'s mut dyn ProgressSink,
target: f64,
max_iter: usize,
started: Instant,
pub evaluations: usize,
pub best: Option<(Vec<f64>, Eval)>,
pub exit: Option<Exit>,
}
impl<'s> Monitor<'s> {
pub fn new(ctl: &'s mut RunControl) -> Monitor<'s> {
Monitor {
target: ctl.target_fidelity,
max_iter: ctl.max_iter,
sink: &mut *ctl.sink,
started: Instant::now(),
evaluations: 0,
best: None,
exit: None,
}
}
pub fn evaluated(&mut self, x: &[f64], e: Eval) {
self.evaluations += 1;
if self.best.as_ref().is_none_or(|(_, b)| e.cost < b.cost) {
self.best = Some((x.to_vec(), e));
}
}
pub fn report(&mut self, iteration: usize, e: Eval, grad_norm: Option<f64>) {
self.sink.on_iteration(&IterationReport {
iteration,
fidelity: e.fidelity,
penalty: e.penalty,
cost: e.cost,
grad_norm,
evaluations: self.evaluations,
elapsed_s: self.started.elapsed().as_secs_f64(),
});
if self.exit.is_some() {
return;
}
if e.score() >= self.target {
self.exit = Some(Exit::TargetReached);
} else if self.sink.should_cancel() {
self.exit = Some(Exit::Cancelled);
} else if iteration >= self.max_iter {
self.exit = Some(Exit::MaxIterations);
}
}
pub fn finish(self, iterations: usize, exit: Exit) -> Result<OptimResult> {
let exit = self.exit.unwrap_or(exit);
match self.best {
Some((x, eval)) => Ok(OptimResult {
x,
eval,
iterations,
evaluations: self.evaluations,
exit,
}),
None => Err(match exit {
Exit::Failed(m) => Error::Numerical(m),
other => Error::Numerical(format!(
"the optimiser stopped ({}) before any evaluation",
other.message()
)),
}),
}
}
}