Skip to main content

simplex_trace/
lib.rs

1//! # simplex-trace
2//!
3//! A simplex solver for linear programs that uses **exact rational arithmetic** and records
4//! **every step** (tableaus, ratio tests, pivots, phase changes) as plain data, so a teaching
5//! tool or UI can replay the method exactly as it is done by hand.
6//!
7//! Supported methods: primal simplex, Big-M (with a symbolic `M`) and two-phase.
8//! All variables are non-negative. Every optimal solution includes a [`Sensitivity`] analysis:
9//! shadow prices, reduced costs and ranges.
10//!
11//! With the **`serde`** feature, [`Problem`], [`Solution`], [`Trace`] and every [`Step`] can be
12//! (de)serialized, e.g. to JSON for a web front end. Numbers are exact strings such as `"3/2"`.
13//!
14//! ```
15//! use simplex_trace::{solve, Method, Options, Problem, Relation, Status, ratio};
16//!
17//! // min 4x1 + x2  s.t.  3x1 + x2 = 3,  4x1 + 3x2 >= 6,  x1 + 2x2 <= 4
18//! let problem = Problem::minimize([4, 1])
19//!     .subject_to([3, 1], Relation::Eq, 3)
20//!     .subject_to([4, 3], Relation::Ge, 6)
21//!     .subject_to([1, 2], Relation::Le, 4);
22//!
23//! let solution = solve(&problem, Method::TwoPhase, &Options::default()).unwrap();
24//! assert_eq!(
25//!     solution.status,
26//!     Status::Optimal { objective: ratio(17, 5), values: vec![ratio(2, 5), ratio(9, 5)] }
27//! );
28//!
29//! // Every tableau and pivot is available for display or replay.
30//! for snapshot in solution.trace.tableaus() {
31//!     println!("{snapshot}");
32//! }
33//! ```
34
35mod error;
36mod num;
37mod problem;
38mod sensitivity;
39#[cfg(feature = "serde")]
40mod serde_rational;
41mod solver;
42mod trace;
43
44pub use error::Error;
45pub use num::{Coef, IntoRational, Rational, ratio};
46pub use problem::{Constraint, Problem, Relation, Sense};
47pub use sensitivity::{ConstraintSensitivity, Interval, Sensitivity, Source, VariableSensitivity};
48pub use solver::{Method, Options, PivotRule, Solution, Status, solve};
49pub use trace::{NamedValue, Phase, RatioTest, Snapshot, Step, Termination, Trace};