solverforge_solver/termination/
time.rs1use std::fmt::Debug;
4use std::time::Duration;
5
6use solverforge_core::domain::PlanningSolution;
7
8use super::Termination;
9use crate::scope::SolverScope;
10
11#[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}