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

Move a WeightedVariable into the Constraint.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.