#![allow(clippy::needless_range_loop)]
pub(crate) mod driver;
pub(crate) mod geometry;
pub(crate) mod halton;
pub(crate) mod progressive_barrier;
use std::marker::PhantomData;
use crate::core::constraint::{BoxConstraints, NonlinearInequalityConstraints};
use crate::core::math::{Scalar, VectorLen};
use crate::core::problem::{CostFunction, Problem};
use crate::core::solver::Solver;
use crate::core::state::{ConstrainedMadsState, MadsState};
use crate::core::termination::TerminationReason;
use crate::solver::nelder_mead::Unbounded;
use driver::{MadsWork, Transition};
use progressive_barrier::PbWork;
pub struct Bounded;
pub struct Constrained;
pub struct Mads<Mode = Unbounded, F = f64> {
poll_size_init: F,
poll_size_min: F,
work: Option<MadsWork<F>>,
pb_work: Option<PbWork<F>>,
_mode: PhantomData<fn() -> Mode>,
}
impl<F: Scalar> Mads<Unbounded, F> {
pub fn new() -> Self {
Self {
poll_size_init: F::from_f64(1.0).expect("1.0 representable"),
poll_size_min: F::from_f64(1e-6).expect("1e-6 representable"),
work: None,
pb_work: None,
_mode: PhantomData,
}
}
}
impl<Mode, F: Scalar> Mads<Mode, F> {
pub fn with_initial_poll_size(mut self, poll_size_init: F) -> Self {
self.poll_size_init = poll_size_init;
self
}
pub fn with_min_poll_size(mut self, poll_size_min: F) -> Self {
self.poll_size_min = poll_size_min;
self
}
}
impl<F: Scalar> Mads<Unbounded, F> {
pub fn bounded(self) -> Mads<Bounded, F> {
Mads {
poll_size_init: self.poll_size_init,
poll_size_min: self.poll_size_min,
work: None,
pb_work: None,
_mode: PhantomData,
}
}
pub fn constrained(self) -> Mads<Constrained, F> {
Mads {
poll_size_init: self.poll_size_init,
poll_size_min: self.poll_size_min,
work: None,
pb_work: None,
_mode: PhantomData,
}
}
}
impl<F: Scalar> Default for Mads<Unbounded, F> {
fn default() -> Self {
Self::new()
}
}
fn fill_from<V, F>(template: &V, slice: &[F]) -> V
where
V: Clone + std::ops::IndexMut<usize, Output = F>,
F: Copy,
{
let mut v = template.clone();
for (i, &x) in slice.iter().enumerate() {
v[i] = x;
}
v
}
fn to_vec<V, F>(v: &V, n: usize) -> Vec<F>
where
V: std::ops::Index<usize, Output = F>,
F: Copy,
{
(0..n).map(|i| v[i]).collect()
}
fn out_of_box<F: Scalar>(x: &[F], lower: &[F], upper: &[F]) -> bool {
x.iter()
.zip(lower)
.zip(upper)
.any(|((&xi, &lo), &up)| xi < lo || xi > up)
}
impl<P, V, F> Solver<P, MadsState<V, F>> for Mads<Unbounded, F>
where
F: Scalar,
P: CostFunction<Param = V, Output = F>,
V: Clone
+ VectorLen
+ std::ops::Index<usize, Output = F>
+ std::ops::IndexMut<usize, Output = F>,
{
type Error = P::Error;
fn init(
&mut self,
problem: &mut Problem<P>,
mut state: MadsState<V, F>,
) -> Result<MadsState<V, F>, Self::Error> {
let n = state.param.vec_len();
assert!(n >= 1, "Mads requires a non-empty start point");
let x0: Vec<F> = (0..n).map(|i| state.param[i]).collect();
let template = state.param.clone();
let (work, best_x, best_f) = {
let mut eval =
|slice: &[F]| -> Result<F, P::Error> { problem.cost(&fill_from(&template, slice)) };
MadsWork::try_init(x0, self.poll_size_init, self.poll_size_min, &mut eval)?
};
state.param = fill_from(&template, &best_x);
state.cost = Some(best_f);
state.poll_size = work.poll_size();
state.mesh_index = work.mesh_index();
self.work = Some(work);
Ok(state)
}
fn next_iter(
&mut self,
problem: &mut Problem<P>,
mut state: MadsState<V, F>,
) -> Result<(MadsState<V, F>, Option<TerminationReason>), Self::Error> {
let template = state.param.clone();
let work = self
.work
.as_mut()
.expect("Mads::init must run before next_iter");
let out = {
let mut eval =
|slice: &[F]| -> Result<F, P::Error> { problem.cost(&fill_from(&template, slice)) };
work.step(&mut eval)?
};
state.poll_size = work.poll_size();
state.mesh_index = work.mesh_index();
let mut best_f = state.cost.expect("Mads::init seeds the cost");
for (xabs, f_new) in &out.evaluated {
if *f_new < best_f {
best_f = *f_new;
state.param = fill_from(&template, xabs);
state.cost = Some(best_f);
}
}
let reason = match out.transition {
Transition::Converged => Some(TerminationReason::SolverConverged),
Transition::Continue => None,
};
Ok((state, reason))
}
}
fn violation<V, F>(c: &V, m: usize) -> F
where
V: std::ops::Index<usize, Output = F>,
F: Scalar,
{
let mut h = F::zero();
for j in 0..m {
let cj = c[j];
if cj > F::zero() {
h = h + cj * cj;
}
}
h
}
impl<P, V, F> Solver<P, ConstrainedMadsState<V, F>> for Mads<Constrained, F>
where
F: Scalar,
P: CostFunction<Param = V, Output = F> + NonlinearInequalityConstraints,
V: Clone
+ VectorLen
+ std::ops::Index<usize, Output = F>
+ std::ops::IndexMut<usize, Output = F>,
{
type Error = P::Error;
fn init(
&mut self,
problem: &mut Problem<P>,
mut state: ConstrainedMadsState<V, F>,
) -> Result<ConstrainedMadsState<V, F>, Self::Error> {
let n = state.param.vec_len();
assert!(n >= 1, "Mads requires a non-empty start point");
let m = problem.inner().num_constraints();
let x0: Vec<F> = (0..n).map(|i| state.param[i]).collect();
let template = state.param.clone();
let (work, best_x, best_f, best_h) = {
let mut eval = |slice: &[F]| -> Result<(F, F), P::Error> {
let xv = fill_from(&template, slice);
let f = problem.cost(&xv)?;
let c = problem.inner().constraints(&xv)?;
Ok((f, violation(&c, m)))
};
PbWork::try_init(x0, self.poll_size_init, self.poll_size_min, &mut eval)?
};
state.param = fill_from(&template, &best_x);
state.cost = Some(best_f);
state.constraint_violation = best_h;
state.poll_size = work.poll_size();
state.mesh_index = work.mesh_index();
self.pb_work = Some(work);
Ok(state)
}
fn next_iter(
&mut self,
problem: &mut Problem<P>,
mut state: ConstrainedMadsState<V, F>,
) -> Result<(ConstrainedMadsState<V, F>, Option<TerminationReason>), Self::Error> {
let m = problem.inner().num_constraints();
let template = state.param.clone();
let work = self
.pb_work
.as_mut()
.expect("Mads::init must run before next_iter");
let out = {
let mut eval = |slice: &[F]| -> Result<(F, F), P::Error> {
let xv = fill_from(&template, slice);
let f = problem.cost(&xv)?;
let c = problem.inner().constraints(&xv)?;
Ok((f, violation(&c, m)))
};
work.step(&mut eval)?
};
state.poll_size = work.poll_size();
state.mesh_index = work.mesh_index();
let (x_star, f_star, h_star) = out.incumbent;
state.param = fill_from(&template, &x_star);
state.cost = Some(f_star);
state.constraint_violation = h_star;
let reason = match out.transition {
Transition::Converged => Some(TerminationReason::SolverConverged),
Transition::Continue => None,
};
Ok((state, reason))
}
}
impl<P, V, F> Solver<P, MadsState<V, F>> for Mads<Bounded, F>
where
F: Scalar,
P: CostFunction<Param = V, Output = F> + BoxConstraints,
V: Clone
+ VectorLen
+ std::ops::Index<usize, Output = F>
+ std::ops::IndexMut<usize, Output = F>,
{
type Error = P::Error;
fn init(
&mut self,
problem: &mut Problem<P>,
mut state: MadsState<V, F>,
) -> Result<MadsState<V, F>, Self::Error> {
let n = state.param.vec_len();
assert!(n >= 1, "Mads requires a non-empty start point");
let lower = to_vec(problem.inner().lower(), n);
let upper = to_vec(problem.inner().upper(), n);
let x0: Vec<F> = (0..n)
.map(|i| {
let xi = state.param[i];
if xi < lower[i] {
lower[i]
} else if xi > upper[i] {
upper[i]
} else {
xi
}
})
.collect();
let template = state.param.clone();
let (work, best_x, best_f) = {
let mut eval = |slice: &[F]| -> Result<F, P::Error> {
if out_of_box(slice, &lower, &upper) {
Ok(F::infinity()) } else {
problem.cost(&fill_from(&template, slice))
}
};
MadsWork::try_init(x0, self.poll_size_init, self.poll_size_min, &mut eval)?
};
state.param = fill_from(&template, &best_x);
state.cost = Some(best_f);
state.poll_size = work.poll_size();
state.mesh_index = work.mesh_index();
self.work = Some(work);
Ok(state)
}
fn next_iter(
&mut self,
problem: &mut Problem<P>,
mut state: MadsState<V, F>,
) -> Result<(MadsState<V, F>, Option<TerminationReason>), Self::Error> {
let n = state.param.vec_len();
let lower = to_vec(problem.inner().lower(), n);
let upper = to_vec(problem.inner().upper(), n);
let template = state.param.clone();
let work = self
.work
.as_mut()
.expect("Mads::init must run before next_iter");
let out = {
let mut eval = |slice: &[F]| -> Result<F, P::Error> {
if out_of_box(slice, &lower, &upper) {
Ok(F::infinity())
} else {
problem.cost(&fill_from(&template, slice))
}
};
work.step(&mut eval)?
};
state.poll_size = work.poll_size();
state.mesh_index = work.mesh_index();
let mut best_f = state.cost.expect("Mads::init seeds the cost");
for (xabs, f_new) in &out.evaluated {
if *f_new < best_f {
best_f = *f_new;
state.param = fill_from(&template, xabs);
state.cost = Some(best_f);
}
}
let reason = match out.transition {
Transition::Converged => Some(TerminationReason::SolverConverged),
Transition::Continue => None,
};
Ok((state, reason))
}
}