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)]
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}