Skip to main content

pumpkin_core/engine/termination/
time_budget.rs

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