pumpkin_core/engine/termination/
time_budget.rs

1use std::time::Duration;
2use std::time::Instant;
3
4use super::TerminationCondition;
5
6/// A [`TerminationCondition`] which triggers when the specified time budget has been exceeded.
7#[derive(Clone, Copy, Debug)]
8pub struct TimeBudget {
9    /// The point in time from which to measure the budget.
10    started_at: Instant,
11    /// The amount of time before [`TimeBudget::should_stop()`] becomes true.
12    budget: Duration,
13}
14
15impl TimeBudget {
16    /// Give the solver a time budget, starting now.
17    pub fn starting_now(budget: Duration) -> TimeBudget {
18        let started_at = Instant::now();
19
20        TimeBudget { started_at, budget }
21    }
22}
23
24impl TerminationCondition for TimeBudget {
25    fn should_stop(&mut self) -> bool {
26        self.started_at.elapsed() >= self.budget
27    }
28}