cnvx_core/status.rs
1use std::fmt::Display;
2
3/// Represents the state of a solution after attempting to solve an optimization problem.
4///
5/// Used in [`Solution`](crate::solution::Solution) to indicate whether the solver found an optimal solution,
6/// whether the problem is infeasible or unbounded, or if it encountered some other state.
7///
8/// # Examples
9///
10/// ```rust
11/// # use cnvx_core::SolveStatus;
12/// let status = SolveStatus::Optimal;
13/// assert_eq!(status.to_string(), "Optimal");
14/// ```
15#[derive(Debug, Eq, PartialEq, Clone)]
16pub enum SolveStatus {
17 /// The solver has not attempted to solve the model yet.
18 NotSolved,
19
20 /// The solver found an optimal solution.
21 Optimal,
22
23 /// The problem is infeasible: no solution satisfies all constraints.
24 Infeasible,
25
26 /// The problem is unbounded: the objective can increase/decrease without limit.
27 Unbounded,
28
29 Other(String),
30}
31
32impl Display for SolveStatus {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 match self {
35 SolveStatus::NotSolved => write!(f, "Not Solved"),
36 SolveStatus::Optimal => write!(f, "Optimal"),
37 SolveStatus::Infeasible => write!(f, "Infeasible"),
38 SolveStatus::Unbounded => write!(f, "Unbounded"),
39 SolveStatus::Other(s) => write!(f, "Other: {}", s),
40 }
41 }
42}
43
44/// Represents the various errors that can occur during modeling or solving
45/// an optimization problem.
46///
47/// This type is used by solvers and the modeling API to communicate problems
48/// such as missing objectives, invalid models, or numerical issues.
49#[derive(Debug, Eq, PartialEq)]
50pub enum SolveError {
51 /// The model has no objective function defined.
52 NoObjective,
53
54 /// The model is invalid (e.g., constraints are inconsistent or malformed).
55 InvalidModel(String),
56
57 /// A numerical failure occurred during solving (e.g., singular matrix).
58 NumericalFailure(String),
59
60 /// Internal solver error (unexpected state or panic inside the solver).
61 InternalSolverError(String),
62
63 /// The solver does not support a required feature (e.g., non-linear constraints).
64 Unsupported(String),
65
66 Other(String),
67}
68
69impl Display for SolveError {
70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 match self {
72 SolveError::NoObjective => write!(f, "No objective function defined"),
73 SolveError::InvalidModel(msg) => write!(f, "Invalid model: {}", msg),
74 SolveError::NumericalFailure(msg) => write!(f, "Numerical failure: {}", msg),
75 SolveError::InternalSolverError(msg) => {
76 write!(f, "Internal solver error: {}", msg)
77 }
78 SolveError::Unsupported(msg) => write!(f, "Unsupported feature: {}", msg),
79 SolveError::Other(msg) => write!(f, "{}", msg),
80 }
81 }
82}