ockham/
errors.rs

1use thiserror::Error;
2
3/// Errors that can occur during optimization solving
4#[derive(Error, Debug, Clone, PartialEq)]
5pub enum OptimizationError {
6    #[error("The problem is infeasible - no solution exists")]
7    Infeasible,
8
9    #[error("The problem is unbounded - the objective function can be improved indefinitely")]
10    Unbounded,
11
12    #[error("Numerical instability detected: {reason}")]
13    NumericalInstability { reason: String },
14
15    #[error("Invalid problem formulation: {reason}")]
16    InvalidProblem { reason: String },
17
18    #[error("Solver configuration error: {reason}")]
19    SolverError { reason: String },
20
21    #[error("Maximum iterations reached ({max_iter}) without convergence")]
22    MaxIterationsReached { max_iter: usize },
23
24    #[error("Degenerate solution detected")]
25    Degenerate,
26
27    #[error("Cycling detected in simplex algorithm")]
28    Cycling,
29}
30
31/// Errors that can occur during problem construction or validation
32#[derive(Error, Debug, Clone, PartialEq)]
33pub enum ProblemError {
34    #[error("Dimension mismatch: expected {expected} variables but got {actual}")]
35    DimensionMismatch { expected: usize, actual: usize },
36
37    #[error("Empty problem: no variables defined")]
38    EmptyProblem,
39
40    #[error("Invalid variable bounds: lower bound {lower} > upper bound {upper}")]
41    InvalidBounds { lower: f64, upper: f64 },
42
43    #[error("Invalid constraint coefficients: length {actual} doesn't match number of variables {expected}")]
44    InvalidConstraintLength { expected: usize, actual: usize },
45
46    #[error("Variable name '{name}' already exists")]
47    DuplicateVariableName { name: String },
48
49    #[error("Invalid constraint: {reason}")]
50    InvalidConstraint { reason: String },
51
52    #[error("Invalid objective: {reason}")]
53    InvalidObjective { reason: String },
54}
55
56/// Errors that can occur during parsing
57#[derive(Error, Debug, Clone, PartialEq)]
58pub enum ParseError {
59    #[error("Failed to parse file: {reason}")]
60    ParseError { reason: String },
61
62    #[error("Unsupported format: {format}")]
63    UnsupportedFormat { format: String },
64
65    #[error("IO error: {reason}")]
66    IoError { reason: String },
67
68    #[error("Invalid syntax at line {line}: {reason}")]
69    SyntaxError { line: usize, reason: String },
70}
71
72/// Main result type for the library
73pub type Result<T> = std::result::Result<T, OptimizationError>;
74
75/// Result type for problem construction
76pub type ProblemResult<T> = std::result::Result<T, ProblemError>;
77
78/// Result type for parsing operations
79pub type ParseResult<T> = std::result::Result<T, ParseError>;