use crate::solution::StopReason;
#[derive(Debug, Clone, Copy)]
pub struct Termination {
pub max_evaluations: usize,
pub target: Option<f64>,
}
impl Termination {
pub fn budget(max_evaluations: usize) -> Self {
Termination {
max_evaluations,
target: None,
}
}
pub fn with_target(mut self, target: f64) -> Self {
self.target = Some(target);
self
}
#[inline]
pub fn target_met(&self, best: f64) -> bool {
matches!(self.target, Some(t) if best <= t)
}
#[inline]
pub fn reason(&self, evaluations: usize, best: f64) -> Option<StopReason> {
if self.target_met(best) {
Some(StopReason::TargetReached)
} else if evaluations >= self.max_evaluations {
Some(StopReason::BudgetExhausted)
} else {
None
}
}
}
impl Default for Termination {
fn default() -> Self {
Termination::budget(1000)
}
}