pub use search_core::Budget;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Stats {
pub evaluations: u64,
pub generations: u64,
pub best_cost: f64,
}
impl Default for Stats {
fn default() -> Self {
Self {
evaluations: 0,
generations: 0,
best_cost: f64::INFINITY,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Limits {
pub max_evaluations: Option<u64>,
pub max_generations: Option<u64>,
pub target_cost: Option<f64>,
}
impl Limits {
pub const DEFAULT: Self = Self {
max_evaluations: Some(1_000_000),
max_generations: None,
target_cost: None,
};
#[must_use]
pub const fn unlimited() -> Self {
Self {
max_evaluations: None,
max_generations: None,
target_cost: None,
}
}
#[must_use]
pub const fn evaluations(max: u64) -> Self {
Self {
max_evaluations: Some(max),
..Self::unlimited()
}
}
#[must_use]
pub const fn generations(max: u64) -> Self {
Self {
max_generations: Some(max),
..Self::unlimited()
}
}
#[must_use]
pub const fn with_target(self, target: f64) -> Self {
Self {
target_cost: Some(target),
..self
}
}
}
impl Default for Limits {
fn default() -> Self {
Self::DEFAULT
}
}
impl Budget<Stats> for Limits {
fn exhausted(&mut self, stats: &Stats) -> bool {
self.max_evaluations
.is_some_and(|max| stats.evaluations >= max)
|| self
.max_generations
.is_some_and(|max| stats.generations >= max)
|| self
.target_cost
.is_some_and(|target| stats.best_cost <= target)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stop {
Budget,
Stagnation,
CmaEs(crate::cmaes::Termination),
}