Skip to main content

simplex_trace/
error.rs

1use std::fmt;
2
3/// Errors returned by [`solve`](crate::solve) and [`reoptimize`](crate::reoptimize).
4///
5/// Infeasible and unbounded problems are *not* errors: they are reported
6/// through [`Status`](crate::Status).
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum Error {
10    /// The problem is malformed (e.g. a constraint with the wrong number of coefficients).
11    InvalidProblem(String),
12    /// [`Method::Simplex`](crate::Method::Simplex) was requested but the problem has `>=` or `=`
13    /// constraints, which need artificial variables. Use `BigM` or `TwoPhase`.
14    ArtificialVariablesRequired,
15    /// [`Method::DualSimplex`](crate::Method::DualSimplex) was requested but the slack basis is
16    /// not dual feasible (a cost with the wrong sign, or an equality constraint), so the dual
17    /// simplex cannot start. Use `BigM` or `TwoPhase`.
18    NotDualFeasible(String),
19    /// [`reoptimize`](crate::reoptimize) was given a previous solution that is not optimal, so
20    /// there is no basis to start from. Solve the extended problem from scratch instead.
21    NotOptimal,
22    /// The iteration limit in [`Options`](crate::Options) was reached (possible cycling).
23    IterationLimit(usize),
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Error::InvalidProblem(msg) => write!(f, "invalid problem: {msg}"),
30            Error::ArtificialVariablesRequired => write!(
31                f,
32                "this problem needs artificial variables; use Method::BigM or Method::TwoPhase"
33            ),
34            Error::NotDualFeasible(msg) => write!(
35                f,
36                "the dual simplex cannot start from the slack basis: {msg}; use Method::BigM or Method::TwoPhase"
37            ),
38            Error::NotOptimal => write!(
39                f,
40                "the previous solution is not optimal; solve the extended problem from scratch"
41            ),
42            Error::IterationLimit(n) => write!(f, "iteration limit of {n} reached"),
43        }
44    }
45}
46
47impl std::error::Error for Error {}