Skip to main content

solverforge_solver/termination/
time.rs

1// Time-based termination.
2
3use std::time::Duration;
4
5use solverforge_core::domain::PlanningSolution;
6use solverforge_scoring::Director;
7
8use super::Termination;
9use crate::scope::BestSolutionCallback;
10use crate::scope::SolverScope;
11
12/* Terminates after a time limit.
13
14# Example
15
16```
17use std::time::Duration;
18use solverforge_solver::termination::TimeTermination;
19
20let term = TimeTermination::new(Duration::from_secs(30));
21let term = TimeTermination::seconds(30);
22let term = TimeTermination::millis(500);
23```
24*/
25#[derive(Debug, Clone)]
26pub struct TimeTermination {
27    limit: Duration,
28}
29
30impl TimeTermination {
31    pub fn new(limit: Duration) -> Self {
32        Self { limit }
33    }
34
35    pub fn millis(ms: u64) -> Self {
36        Self::new(Duration::from_millis(ms))
37    }
38
39    pub fn seconds(secs: u64) -> Self {
40        Self::new(Duration::from_secs(secs))
41    }
42}
43
44impl<S: PlanningSolution, D: Director<S>, BestCb: BestSolutionCallback<S>> Termination<S, D, BestCb>
45    for TimeTermination
46{
47    fn is_terminated(&self, solver_scope: &SolverScope<S, D, BestCb>) -> bool {
48        solver_scope.elapsed().is_some_and(|e| e >= self.limit)
49    }
50}