Struct AsyncModel

Source
pub struct AsyncModel(/* private fields */);
Expand description

A wrapper around Model that supports async optimisation in the background.

From the Gurobi manual, regarding solving models asynchronously:

“[modifying or performing non-permitted] calls on the running model, or on any other models that were built within the same Gurobi environment, will fail with error code OPTIMIZATION_IN_PROGRESS.”

For this reason, creating an AsyncModel requires a Model whose Env wasn’t previously been used to construct other models.

Model implements From<AsyncModel>, so you can recover the Model using .into() (see examples).

Implementations§

Source§

impl AsyncModel

Source

pub fn new(model: Model) -> AsyncModel

§Panics

This function will panic if the model does not have sole ownership over its Env. This means the Model cannot be created with Model::new, instead you must use Model::with_env.

§Examples

This example panics because env has two references - inside m and the bound variable in the current scope

use grb::prelude::*;
use grb::AsyncModel;

let env = Env::new("")?;
let mut m = Model::with_env("model", &env)?;
let mut m =  AsyncModel::new(m); // panic - env is still in scope

This is easily resolved by ensuring env is no longer in scope when the AsyncModel is created.

let mut m = Model::with_env("model", &env)?;
drop(env);
let mut m =  AsyncModel::new(m); // ok

You can also pass an owned Env to Model::with_env:

let mut m = Model::with_env("model", env)?;
let mut m =  AsyncModel::new(m); // also ok

This example panics because m uses the default Env, which is also stored globally. Models created with Model::new can never be made into AsyncModels for this reason.

let m = Model::new("model1")?;
let m =  AsyncModel::new(m); // panic
Source

pub fn optimize(self) -> Result<AsyncHandle, (Self, Error)>

Optimize the model on another thread. This method will always trigger a Model::update on the underlying Model.

On success, returns an AsyncHandle that provides a limited API for model queries. The AsyncModel can be retrieved by calling AsyncHandle::join.

§Errors

An grb::Error::FromAPI may occur. In this case, the Err variant contains this error and gives back ownership of this AsyncModel.

§Examples
use grb::prelude::*;
use grb::AsyncModel;

let mut m = Model::with_env("model", &Env::new("")?)?;
let x = add_ctsvar!(m, obj: 2)?;
let y = add_intvar!(m, bounds: 0..100)?;
m.add_constr("c0", c!(x <= y - 0.5 ))?;
let m = AsyncModel::new(m);

let handle = match m.optimize() {
  Err((_, e)) => panic!("{}", e),
  Ok(h) => h
};

println!("The model has explored {} MIP nodes so far", handle.node_cnt()?);
let (m, errors) = handle.join(); // the AsyncModel is always returned
errors?; // optimisation errors - as if Model::optimize were called.
let m: Model = m.into(); // get original Model back

Trait Implementations§

Source§

impl From<AsyncModel> for Model

Source§

fn from(model: AsyncModel) -> Model

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.