Skip to main content

production/
production.rs

1//! Runs the dummy backend on the production-planning LP of the project's LP/IP
2//! tutorial (Example 1) and checks the solution and duals that Gurobi and
3//! HiGHS report for it: `cargo run -p lp-ip-craft --example production`.
4//!
5//! ```text
6//! maximize 45 chair + 80 table + 110 desk
7//! s.t.      5 chair + 20 table + 25 desk <= 400   (wood)
8//!          10 chair + 15 table + 20 desk <= 450   (labor)
9//!             chair +  3 table +  5 desk <=  60   (finishing)
10//!          x >= 0
11//! ```
12
13use lp_ip_craft::check::lp_residuals;
14use lp_ip_craft::{
15    Bounds, CancellationToken, CsrMatrix, Duals, DummySolver, LpIpSolver, Model, ModelData,
16    Options, Result, Sense, VariableKind, WarmStart,
17};
18
19fn main() -> Result<()> {
20    let model = Model::new(ModelData {
21        sense: Sense::Maximize,
22        objective: vec![45.0, 80.0, 110.0],
23        objective_offset: 0.0,
24        variables: Bounds::non_negative(3),
25        kinds: vec![VariableKind::Continuous; 3],
26        matrix: CsrMatrix::from_dense(3, 3, &[5.0, 20.0, 25.0, 10.0, 15.0, 20.0, 1.0, 3.0, 5.0])?,
27        rows: Bounds::new(vec![f64::NEG_INFINITY; 3], vec![400.0, 450.0, 60.0]),
28    })?;
29
30    // The published optimum; the dummy only evaluates it.
31    let optimum = vec![30.0, 10.0, 0.0];
32    let result = DummySolver.solve(
33        &model,
34        &Options::default(),
35        Some(&WarmStart::from_primal(optimum.clone())),
36        &CancellationToken::default(),
37    )?;
38    println!("status: {:?} ({})", result.status, result.message);
39    let evaluation = result.evaluation.expect("the start was evaluated");
40    println!("evaluation: {evaluation:?}");
41    assert_eq!(evaluation.objective, 2150.0);
42
43    // Row duals (Gurobi Pi, HiGHS row_dual) and reduced costs (RC, col_dual) as
44    // published, in the crate's convention c = A^T y + z for the maximization.
45    let duals = Duals {
46        row_duals: vec![0.0, 11.0 / 3.0, 25.0 / 3.0],
47        reduced_costs: vec![0.0, 0.0, -5.0],
48    };
49    let residuals = lp_residuals(&model, &optimum, &duals)?;
50    println!("LP residuals with the published duals: {residuals:?}");
51    assert!(residuals.max() < 1e-12);
52    Ok(())
53}