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
impl AsyncModel
Sourcepub fn new(model: Model) -> AsyncModel
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 scopeThis 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); // okYou 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 okThis 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); // panicSourcepub fn optimize(self) -> Result<AsyncHandle, (Self, Error)>
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