use super::EnginePolicy;
use crate::engine::{EngineAction, EngineContext, EventBatch};
pub struct MaxIterationPolicy {
max_iters: usize,
}
impl MaxIterationPolicy {
pub fn new(max_iters: usize) -> Self {
Self { max_iters }
}
}
impl<F> EnginePolicy<F> for MaxIterationPolicy {
fn decide(&mut self, _batch: &EventBatch<F>, context: &EngineContext) -> EngineAction {
if context.iter > self.max_iters {
return EngineAction::Stop(crate::Termination::ExceededMaxIterations);
}
EngineAction::Continue
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::engine::policy::PolicyStack;
use crate::progress::Progress;
#[test]
fn max_iteration_policy_terminates_when_iter_exceeds_limit() {
let mut stack = PolicyStack::<f64>::new().add(MaxIterationPolicy::new(100));
let batch: EventBatch<f64> = EventBatch::new().add(Progress::Complete);
let ctx = EngineContext {
iter: 101,
..Default::default()
};
assert!(matches!(
stack.decide(&batch, &ctx),
EngineAction::Stop(crate::Termination::ExceededMaxIterations)
))
}
#[test]
fn max_iteration_policy_does_not_terminate_when_iter_is_less_than_limit() {
let mut stack = PolicyStack::<f64>::new().add(MaxIterationPolicy::new(100));
let batch: EventBatch<f64> = EventBatch::new().add(Progress::Complete);
let ctx = EngineContext {
iter: 99,
..Default::default()
};
assert!(matches!(stack.decide(&batch, &ctx), EngineAction::Continue))
}
}