Trait grb::expr::GurobiSum[][src]

pub trait GurobiSum {
    fn grb_sum(self) -> Expr;
}
Expand description

Convenience trait for summing over iterators to produce a single Expr.

The c! macro uses Expr::from to convert inputs to Expr objects. Because Sum is generic over the iterator item type, this code

let mut model = Model::new("")?;
let x = add_binvar!(model)?;
let y = add_binvar!(model)?;
let z = add_binvar!(model)?;
let vars = [x, y, z];
let constraint = c!( vars.iter().sum() == 1 );

produces a compilation error:

  | let constraint = c!( vars.iter().sum() == 1 );
  |                      ^^^^ cannot infer type for enum `Expr`

GurobiSum specialises the type parameter to work around this, so the following compile:

let constraint = c!( vars.iter().grb_sum() == 1 );

Note that unlike Sum, the iterator bound is IntoIterator rather than Iterator, so the .iter() call above can be replaced with a borrow:

let constraint = c!( (&vars).grb_sum() == 1 );

This may or may not be more ergonomic.

TLDR: Use .grb_sum() instead of sum() when summing over an iterator of variables or variable expressions.

Required methods

fn grb_sum(self) -> Expr[src]

Expand description

Additively combine an iterator (or container) of one or more expressions into a single expression.

Implementors

impl<T, I> GurobiSum for I where
    T: Into<Expr>,
    I: IntoIterator<Item = T>, 
[src]

fn grb_sum(self) -> Expr[src]