solverforge_solver/termination/
time.rs

1//! Time-based termination.
2
3use std::fmt::Debug;
4use std::time::Duration;
5
6use solverforge_core::domain::PlanningSolution;
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/// // Terminate after 30 seconds
20/// let term = TimeTermination::new(Duration::from_secs(30));
21///
22/// // Or use convenience constructors
23/// let term = TimeTermination::seconds(30);
24/// let term = TimeTermination::millis(500);
25/// ```
26#[derive(Debug, Clone)]
27pub struct TimeTermination {
28    limit: Duration,
29}
30
31impl TimeTermination {
32    pub fn new(limit: Duration) -> Self {
33        Self { limit }
34    }
35
36    pub fn millis(ms: u64) -> Self {
37        Self::new(Duration::from_millis(ms))
38    }
39
40    pub fn seconds(secs: u64) -> Self {
41        Self::new(Duration::from_secs(secs))
42    }
43}
44
45impl<S: PlanningSolution> Termination<S> for TimeTermination {
46    fn is_terminated(&self, solver_scope: &SolverScope<S>) -> bool {
47        solver_scope.elapsed().is_some_and(|e| e >= self.limit)
48    }
49}