Skip to main content

solverforge_solver/termination/
step_count.rs

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