solverforge_solver/termination/
time.rs1use 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#[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}