pub struct Problem { /* private fields */ }Expand description
A specification of a linear programming problem.
Implementations§
Source§impl Problem
impl Problem
Sourcepub fn new(direction: OptimizationDirection) -> Self
pub fn new(direction: OptimizationDirection) -> Self
Create a new problem instance.
Sourcepub fn set_time_limit(&mut self, duration: Duration)
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.
Sourcepub fn add_var(&mut self, obj_coeff: f64, (min, max): (f64, f64)) -> Variable
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.
Sourcepub fn add_integer_var(
&mut self,
obj_coeff: f64,
(min, max): (i32, i32),
) -> Variable
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.
Sourcepub fn has_integer_vars(&self) -> bool
pub fn has_integer_vars(&self) -> bool
Check if the problem has any integer variables.
Sourcepub fn add_binary_var(&mut self, obj_coeff: f64) -> Variable
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.
Sourcepub fn add_constraint(
&mut self,
expr: impl Into<LinearExpr>,
cmp_op: ComparisonOp,
rhs: f64,
)
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);Sourcepub fn solve(&self) -> Result<Solution, Error>
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.