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`), two-phase and dual simplex.
8//! All variables are non-negative. Every optimal solution includes a [`Sensitivity`] analysis
9//! (shadow prices, reduced costs and ranges), and [`reoptimize`] repairs it with dual simplex
10//! pivots after constraints are added, instead of solving again.
11//!
12//! With the **`serde`** feature, [`Problem`], [`Solution`], [`Trace`] and every [`Step`] can be
13//! (de)serialized, e.g. to JSON for a web front end. Numbers are exact strings such as `"3/2"`.
14//!
15//! ```
16//! use simplex_trace::{solve, Method, Options, Problem, Relation, Status, ratio};
17//!
18//! // min 4x1 + x2  s.t.  3x1 + x2 = 3,  4x1 + 3x2 >= 6,  x1 + 2x2 <= 4
19//! let problem = Problem::minimize([4, 1])
20//!     .subject_to([3, 1], Relation::Eq, 3)
21//!     .subject_to([4, 3], Relation::Ge, 6)
22//!     .subject_to([1, 2], Relation::Le, 4);
23//!
24//! let solution = solve(&problem, Method::TwoPhase, &Options::default()).unwrap();
25//! assert_eq!(
26//!     solution.status,
27//!     Status::Optimal { objective: ratio(17, 5), values: vec![ratio(2, 5), ratio(9, 5)] }
28//! );
29//!
30//! // Every tableau and pivot is available for display or replay.
31//! for snapshot in solution.trace.tableaus() {
32//!     println!("{snapshot}");
33//! }
34//! ```
35
36#![forbid(unsafe_code)]
37
38mod error;
39mod num;
40mod problem;
41mod sensitivity;
42#[cfg(feature = "serde")]
43mod serde_rational;
44mod solver;
45mod trace;
46
47pub use error::Error;
48pub use num::{Coef, IntoRational, Rational, ratio};
49pub use problem::{Constraint, Problem, Relation, Sense};
50pub use sensitivity::{ConstraintSensitivity, Interval, Sensitivity, Source, VariableSensitivity};
51pub use solver::{Method, Options, PivotRule, Solution, Status, reoptimize, solve};
52pub use trace::{DualRatioTest, NamedValue, Phase, RatioTest, Snapshot, Step, Termination, Trace};
53
54// Compile the Rust examples of the README as doctests, so it cannot drift from the API.
55#[cfg(doctest)]
56#[doc = include_str!("../README.md")]
57struct ReadmeDoctests;