Trait grb::callback::Callback[][src]

pub trait Callback {
    fn callback(&mut self, w: Where<'_>) -> CbResult;
}
Expand description

A trait that allows structs to be used as a callback object

Examples

This example shows how to store every integer solution found during a MIP solve

use grb::prelude::*;
use grb::callback::CbResult;

struct CallbackData {
  vars: Vec<Var>,
  solutions: Vec<Vec<f64>>,
}

impl Callback for CallbackData {
  fn callback(&mut self, w: Where) -> CbResult {
    match w {
      Where::MIPSol(ctx) => {
        self.solutions.push(ctx.get_solution(&self.vars)?)
      }
      _ => {}
    }
    Ok(())
  }
}

This example shows how to cache lazy cuts for later use (perhaps adding them as hard constraints with Model::add_constrs once optimisation has finished)

use grb::prelude::*;
use grb::constr::IneqExpr;
use grb::callback::CbResult;

struct LazyCutSep {
  vars: Vec<Var>,
  past_cuts : Vec<IneqExpr>,
}

impl LazyCutSep {
  fn separate_cuts(&self, solution: &[f64]) -> Vec<IneqExpr> {
    /* ... */
  }
}

impl Callback for LazyCutSep {
  fn callback(&mut self, w: Where) -> CbResult {
    if let Where::MIPSol(ctx) = w {
      let solution = ctx.get_solution(&self.vars)?;
      let cuts = self.separate_cuts(&solution);
      self.past_cuts.extend_from_slice(&cuts);
      for c in cuts {
        ctx.add_lazy(c)?;
      }
    }
    Ok(())
  }
}

Required methods

fn callback(&mut self, w: Where<'_>) -> CbResult[src]

Expand description

The main callback method. The pattern-matching the Where will give a context object (see module-level docs) which can be used to interact with Gurobi.

Implementors

impl<F: FnMut(Where<'_>) -> CbResult> Callback for F[src]

fn callback(&mut self, w: Where<'_>) -> CbResult[src]