Skip to main content

solverforge_solver/termination/
step_count.rs

1//! Step count termination.
2
3use solverforge_core::domain::PlanningSolution;
4use solverforge_scoring::Director;
5
6use super::Termination;
7use crate::scope::BestSolutionCallback;
8use crate::scope::SolverScope;
9
10/// Terminates after a step count.
11///
12/// # Example
13///
14/// ```
15/// use solverforge_solver::termination::StepCountTermination;
16///
17/// let term = StepCountTermination::new(1000);
18/// ```
19#[derive(Debug, Clone)]
20pub struct StepCountTermination {
21    limit: u64,
22}
23
24impl StepCountTermination {
25    pub fn new(limit: u64) -> Self {
26        Self { limit }
27    }
28}
29
30impl<S: PlanningSolution, D: Director<S>, BestCb: BestSolutionCallback<S>> Termination<S, D, BestCb>
31    for StepCountTermination
32{
33    fn is_terminated(&self, solver_scope: &SolverScope<S, D, BestCb>) -> bool {
34        solver_scope.total_step_count() >= self.limit
35    }
36
37    fn install_inphase_limits(&self, solver_scope: &mut SolverScope<S, D, BestCb>) {
38        // Take the minimum if multiple step limits are registered
39        let limit = match solver_scope.inphase_step_count_limit {
40            Some(existing) => existing.min(self.limit),
41            None => self.limit,
42        };
43        solver_scope.inphase_step_count_limit = Some(limit);
44    }
45}