solverforge_solver/termination/
time.rs

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