Skip to main content

LpProblem

Struct LpProblem 

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

A linear program in builder form. See the module docs for the problem shape and the meaning of the results.

Every variable defaults to 0 ≤ xⱼ < ∞; change that with bounds or free. Constraint rows are kept in insertion order, which is also the order of LpSolution::duals and LpSolution::farkas.

§Examples

use symplex::linprog::{LpProblem, LpStatus, q, qi};

// min x + y  s.t.  x + 2y ≥ 1,  3x + y ≥ 1,  x, y ≥ 0   →  (1/5, 2/5)
let sol = LpProblem::minimize(vec![qi(1), qi(1)])
    .ge(vec![qi(1), qi(2)], qi(1))
    .ge(vec![qi(3), qi(1)], qi(1))
    .solve()
    .unwrap();
assert_eq!(sol.status, LpStatus::Optimal);
assert_eq!(sol.x, vec![q(1, 5), q(2, 5)]);
assert_eq!(sol.objective, Some(q(3, 5)));

Implementations§

Source§

impl LpProblem

Source

pub fn minimize(c: Vec<Q>) -> Self

Start a minimisation of cᵀx over x ∈ ℚ^{c.len()}.

Source

pub fn maximize(c: Vec<Q>) -> Self

Start a maximisation of cᵀx over x ∈ ℚ^{c.len()}.

Source

pub fn eq(self, row: Vec<Q>, rhs: Q) -> Self

Add the equality constraint row·x = rhs.

Source

pub fn le(self, row: Vec<Q>, rhs: Q) -> Self

Add the inequality constraint row·x ≤ rhs.

Source

pub fn ge(self, row: Vec<Q>, rhs: Q) -> Self

Add the inequality constraint row·x ≥ rhs.

Source

pub fn bounds(self, var: usize, bounds: Bounds<Q>) -> Self

Set the bounds lower ≤ x[var] ≤ upper of one variable: .bounds(j, Bounds::closed(q(1, 2), qi(3))), .bounds(j, Bounds::at_most(qi(3))), …. An absent side is unbounded; Bounds::free makes the variable free (see also free).

An out-of-range var is reported by solve.

Source

pub fn free(self, var: usize) -> Self

Make x[var] free (−∞ < x[var] < ∞).

Source

pub fn num_vars(&self) -> usize

Number of decision variables (the length of c).

Source

pub fn num_constraints(&self) -> usize

Number of constraint rows added so far.

Source

pub fn with_budget(self, budget: Budget) -> Self

Bound the solve by a Budget (deadline and/or pivot cap). When it runs out, solve returns LpStatus::BudgetExhausted. The default budget is unlimited.

Source

pub fn budget(&self) -> &Budget

The budget the solve runs under (unlimited unless with_budget was called).

Source

pub fn solve(&self) -> Result<LpSolution, SymplexError>

Solve the program exactly.

The returned LpSolution reports LpStatus::Optimal, LpStatus::Infeasible (with a Farkas certificate), LpStatus::Unbounded or — only under a Budget — LpStatus::BudgetExhausted; none of these is an error.

§Errors
  • SymplexError::InvalidArgument for malformed input: no variables, a constraint row whose length differs from c.len(), or a bound set for a variable index ≥ c.len().
  • SymplexError::ComputationFailed if the pivot cap (10 000 + 50·(m + n)) is exceeded — not expected in practice, since Bland’s rule rules out cycling.

Trait Implementations§

Source§

impl Clone for LpProblem

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 LpProblem

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more