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/// ```
17/// use std::time::Duration;
18/// use solverforge_solver::termination::TimeTermination;
19///
20/// let term = TimeTermination::new(Duration::from_secs(30));
21/// let term = TimeTermination::seconds(30);
22/// let term = TimeTermination::millis(500);
23/// ```
24#[derive(Debug, Clone)]
25pub struct TimeTermination {
26    limit: Duration,
27}
28
29impl TimeTermination {
30    pub fn new(limit: Duration) -> Self {
31        Self { limit }
32    }
33
34    pub fn millis(ms: u64) -> Self {
35        Self::new(Duration::from_millis(ms))
36    }
37
38    pub fn seconds(secs: u64) -> Self {
39        Self::new(Duration::from_secs(secs))
40    }
41}
42
43impl<S: PlanningSolution, D: Director<S>, BestCb: BestSolutionCallback<S>> Termination<S, D, BestCb>
44    for TimeTermination
45{
46    fn is_terminated(&self, solver_scope: &SolverScope<S, D, BestCb>) -> bool {
47        solver_scope.elapsed().is_some_and(|e| e >= self.limit)
48    }
49}