iter_solver/
error.rs

1//! Error types.
2
3use std::time::Duration;
4
5use thiserror::Error;
6
7use crate::IterState;
8
9/// Error indicating that the solver reached the maximum iteration.
10///
11/// This error occurs when the solver exceeds the specified maximum number of iterations
12/// without reaching termination condition. It contains both the iteration limit that was
13/// reached and the final state the solver had achieved when the limit was exceeded.
14#[derive(Clone, Debug, Error)]
15#[error("Solver reached the maximum iteration: {max_iteration}.")]
16pub struct ReachMaxIteration<State: IterState> {
17    /// The maximum iteration count that was reached.
18    pub max_iteration: u64,
19    /// The final state of the solver at the time the iteration limit was reached.
20    pub(crate) final_state: State,
21}
22
23impl<State: IterState> ReachMaxIteration<State> {
24    /// Consumes self and returns the final state reached by the solver.
25    pub fn take_final_state(self) -> State {
26        self.final_state
27    }
28
29    /// Returns an immutable reference to the final state reached by the solver.
30    pub fn final_state_ref(&self) -> &State {
31        &self.final_state
32    }
33
34    /// Directly obtains [`IterState::Solution`] from the final state reached by the solver.
35    pub fn get_solution(&self) -> State::Solution {
36        self.final_state.to_sol()
37    }
38}
39
40/// Error indicating that the solver timed out before completing.
41///
42/// This error occurs when the solver exceeds the specified time limit without
43/// reaching a termination condition. It contains both the timeout duration and
44/// the final state the solver had reached when the timeout occurred.
45#[derive(Clone, Debug, Error)]
46#[error("Solver timeout: {}ms or {}s", timeout.as_millis(), timeout.as_secs_f64())]
47pub struct TimeOut<State: IterState> {
48    /// The time limit that was reached.
49    pub timeout: Duration,
50    /// The final state of the solver at the time of timeout.
51    pub(crate) final_state: State,
52}
53
54impl<State: IterState> TimeOut<State> {
55    /// Consumes self and returns the final state reached by the solver.
56    pub fn take_final_state(self) -> State {
57        self.final_state
58    }
59
60    /// Returns an immutable reference to the final state reached by the solver.
61    pub fn final_state_ref(&self) -> &State {
62        &self.final_state
63    }
64
65    /// Directly obtains [`IterState::Solution`] from the final state reached by the solver.
66    pub fn get_solution(&self) -> State::Solution {
67        self.final_state.to_sol()
68    }
69}