Struct rplex::Constraint [−][src]
pub struct Constraint { /* fields omitted */ }
Expand description
A Constraint (row) object for a Problem
.
The recommended way to build these is with the con!
macro.
#[macro_use]
extern crate rplex;
use rplex::{Env, Problem, Variable};
fn main() {
let env = Env::new().unwrap();
let mut prob = Problem::new(&env, "dummy").unwrap();
let x = prob.add_variable(var!("x" -> 4.0 as Binary)).unwrap();
let y = prob.add_variable(var!(0.0 <= "y" <= 100.0 -> 3.0 as Integer)).unwrap();
let z = prob.add_variable(var!(0.0 <= "z" <= 4.5 -> 2.0)).unwrap();
prob.add_constraint(con!("dummy": 20.0 = 1.0 x + 2.0 y + 3.0 z)).unwrap();
prob.add_constraint(con!("dummy2": 1.0 <= (-1.0) x + 1.0 y)).unwrap();
}
However, constraints can also be constructed manually from
WeightedVariables
. This can be useful if your constraints don’t
quite fit the grammar allowed by the con!
macro. You can create
part of the constraint using con!
, then augment it with
add_wvar
to obtain the constraint you need.The following example
is identical to the above.
#[macro_use]
extern crate rplex;
use rplex::{Env, Problem, Constraint, ConstraintType, WeightedVariable};
fn main() {
let env = Env::new().unwrap();
let mut prob = Problem::new(&env, "dummy").unwrap();
let x = prob.add_variable(var!("x" -> 4.0 as Binary)).unwrap();
let y = prob.add_variable(var!(0.0 <= "y" <= 100.0 -> 3.0 as Integer)).unwrap();
let z = prob.add_variable(var!(0.0 <= "z" <= 4.5 -> 2.0)).unwrap();
let mut dummy = Constraint::new(ConstraintType::Eq, 20.0, "dummy");
dummy.add_wvar(WeightedVariable::new_idx(x, 1.0));
dummy.add_wvar(WeightedVariable::new_idx(y, 2.0));
dummy.add_wvar(WeightedVariable::new_idx(z, 3.0));
prob.add_constraint(dummy).unwrap();
let mut dummy2 = Constraint::new(ConstraintType::GreaterThanEq, 1.0, "dummy2");
dummy2.add_wvar(WeightedVariable::new_idx(x, -1.0));
dummy2.add_wvar(WeightedVariable::new_idx(y, 1.0));
prob.add_constraint(dummy2).unwrap();
}
Implementations
pub fn new<S, F>(ty: ConstraintType, rhs: F, name: S) -> Constraint where
S: Into<String>,
F: Into<f64>,
Move a WeightedVariable
into the Constraint.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Constraint
impl Send for Constraint
impl Sync for Constraint
impl Unpin for Constraint
impl UnwindSafe for Constraint
Blanket Implementations
Mutably borrows from an owned value. Read more