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```
15use solverforge_solver::termination::StepCountTermination;
16
17let term = StepCountTermination::new(1000);
18```
19*/
20#[derive(Debug, Clone)]
21pub struct StepCountTermination {
22    limit: u64,
23}
24
25impl StepCountTermination {
26    pub fn new(limit: u64) -> Self {
27        Self { limit }
28    }
29}
30
31impl<S: PlanningSolution, D: Director<S>, BestCb: BestSolutionCallback<S>> Termination<S, D, BestCb>
32    for StepCountTermination
33{
34    fn is_terminated(&self, solver_scope: &SolverScope<S, D, BestCb>) -> bool {
35        solver_scope.total_step_count() >= self.limit
36    }
37
38    fn install_inphase_limits(&self, solver_scope: &mut SolverScope<S, D, BestCb>) {
39        // Take the minimum if multiple step limits are registered
40        let limit = match solver_scope.inphase_step_count_limit {
41            Some(existing) => existing.min(self.limit),
42            None => self.limit,
43        };
44        solver_scope.inphase_step_count_limit = Some(limit);
45    }
46}