Skip to main content

Problem

Struct Problem 

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

A specification of a linear programming problem.

Implementations§

Source§

impl Problem

Source

pub fn new(direction: OptimizationDirection) -> Self

Create a new problem instance.

Source

pub fn set_time_limit(&mut self, duration: Duration)

Set a time limit for the solver. If the solver exceeds this duration, the solution will have StopReason::Limit as its stop reason.

The implementation uses web_time::Instant under the hood, which works on both native and WebAssembly targets.

Source

pub fn add_var(&mut self, obj_coeff: f64, (min, max): (f64, f64)) -> Variable

Add a new real variable to the problem.

obj_coeff is a coefficient of the term in the objective function corresponding to this variable, min and max are the minimum and maximum (inclusive) bounds of this variable. If one of the bounds is absent, use f64::NEG_INFINITY for minimum and f64::INFINITY for maximum.

Source

pub fn add_integer_var( &mut self, obj_coeff: f64, (min, max): (i32, i32), ) -> Variable

Add a new integer variable to the problem.

obj_coeff is a coefficient of the term in the objective function corresponding to this variable, min and max are the minimum and maximum (inclusive) bounds of this variable. If one of the bounds is absent, use i32::MIN for minimum and i32::MAX for maximum.

Source

pub fn has_integer_vars(&self) -> bool

Check if the problem has any integer variables.

Source

pub fn add_binary_var(&mut self, obj_coeff: f64) -> Variable

Add a new binary variable to the problem.

obj_coeff is a coefficient of the term in the objective function corresponding to this variable.

Source

pub fn add_constraint( &mut self, expr: impl Into<LinearExpr>, cmp_op: ComparisonOp, rhs: f64, )

Add a linear constraint to the problem.

§Panics

Will panic if a variable was added more than once to the left-hand side expression.

§Examples

Left-hand side of the constraint can be specified in several ways:

let mut problem = Problem::new(OptimizationDirection::Minimize);
let x = problem.add_var(1.0, (0.0, f64::INFINITY));
let y = problem.add_var(1.0, (0.0, f64::INFINITY));

// Add an x + y >= 2 constraint, specifying the left-hand side expression:

// * by passing a slice of pairs (useful when explicitly enumerating variables)
problem.add_constraint(&[(x, 1.0), (y, 1.0)], ComparisonOp::Ge, 2.0);

// * by passing an iterator of variable-coefficient pairs.
let vars = [x, y];
problem.add_constraint(vars.iter().map(|&v| (v, 1.0)), ComparisonOp::Ge, 2.0);

// * by manually constructing a LinearExpr.
let mut lhs = LinearExpr::empty();
for &v in &vars {
    lhs.add(v, 1.0);
}
problem.add_constraint(lhs, ComparisonOp::Ge, 2.0);
Source

pub fn solve(&self) -> Result<Solution, Error>

Solve the problem, finding the optimal objective function value and variable values.

If a time limit was set and exceeded, the returned Solution will have StopReason::Limit as its stop reason, containing the best solution found so far.

§Errors

Will return an error if the problem is infeasible (constraints can’t be satisfied) or if the objective value is unbounded.

Trait Implementations§

Source§

impl Clone for Problem

Source§

fn clone(&self) -> Problem

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Problem

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, 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.