Skip to main content

Model

Struct Model 

Source
pub struct Model { /* private fields */ }
Expand description

A validated LP or mixed-integer LP.

A Model can only be obtained through Model::new, so every instance satisfies the invariants below and solvers do not re-validate it. It is immutable and can be shared (for example behind an Arc) by any number of independent solves.

Invariants: n >= 1 variables and m >= 0 rows; finite objective coefficients, offset and matrix values; valid Bounds (infinite values only as -inf lower or +inf upper bounds); binary bounds inside [0, 1]; a canonical CSR matrix of shape m x n (see CsrPattern).

Implementations§

Source§

impl Model

Source

pub fn new(data: ModelData) -> Result<Self>

Validates and wraps data. Errors name the offending field and entry.

Examples found in repository?
examples/production.rs (lines 20-28)
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}
Source

pub fn n(&self) -> usize

Number of variables n.

Source

pub fn m(&self) -> usize

Number of rows m.

Source

pub fn nnz(&self) -> usize

Number of stored matrix entries.

Source

pub fn sense(&self) -> Sense

Optimization direction.

Source

pub fn objective(&self) -> &[f64]

Objective coefficients.

Source

pub fn objective_offset(&self) -> f64

Constant objective term.

Source

pub fn variables(&self) -> &Bounds

Variable bounds.

Source

pub fn kinds(&self) -> &[VariableKind]

Variable domains.

Source

pub fn matrix(&self) -> &CsrMatrix

Constraint matrix (m x n).

Source

pub fn rows(&self) -> &Bounds

Row bounds.

Source

pub fn is_mip(&self) -> bool

Whether any variable is integer or binary.

Source

pub fn data(&self) -> &ModelData

The validated parts.

Source

pub fn into_data(self) -> ModelData

Gives the parts back, for example to modify and re-validate them.

Trait Implementations§

Source§

impl Clone for Model

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Model

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Model

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Model

Auto Trait Implementations§

§

impl Forget for Model

§

impl Freeze for Model

§

impl Move for Model

§

impl RefUnwindSafe for Model

§

impl Send for Model
where RawVecInner: Send,

§

impl Sync for Model
where RawVecInner: Sync,

§

impl Unpin for Model
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for Model

§

impl UnwindSafe for Model
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.