Trait grb::expr::AttachModel[][src]

pub trait AttachModel {
    fn attach<'a>(&'a self, model: &'a Model) -> Attached<'a, Self>
    where
        Self: Sized
, { ... } }
Expand description

A convenvience trait for displaying variable names with the Debug trait.

Variable names are not stored inside Var objects, but instead are queried from the Gurobi C API. This means the printing an expression with println!("{:?}", &expr) will give some rather ugly output

let mut model = Model::new("")?;
let x: Vec<_> = (0..5)
.map(|i| add_ctsvar!(model, name: &format!("x[{}]", i)).unwrap() )
.collect();

println!("{:?}", x[0]);

Output:

Var { id: 0, model_id: 0 }

Printing Expr and constraints this way quickly gets unreadable.

The AttachModel trait provides an .attach(&model) method, with simply bundles a &Model with a reference to the object. The Debug trait is implemented for this bundled type (Attached) and properly queries the model for variable names. Because querying the VarName of a variable can fail (for example if the model hasn’t been updated since the variable was added or the .attach(...) was called with the wrong model), a formatting error can occur.

model.update()?; // Important! Otherwise, the formatter will panic.
println!("{:?}", x[0].attach(&model));
println!("{:?}", (x[0] + x[1]).attach(&model));
println!("{:?}", c!( x[1..].grb_sum() >= x[0] ).attach(&model));
println!("{:?}", c!( x.grb_sum() in 0..1 ).attach(&model));

Output:

x[0]
x[1] + x[0]
x[4] + x[1] + x[3] + x[2] ≥ x[0]
x[4] + x[1] + x[0] + x[3] + x[2] ∈ [0, 1]

Provided methods

Attach a model reference to this object for formatting with Debug

Implementors