Expand description
This crate provides Rust bindings for Gurobi Optimizer. It currently requires Gurobi 9.5
§Quick Start
The example below sets up and solves a Mixed Integer Program (MIP). Additional examples covering the more specific aspects of this crate’s API can be found here.
The documention for Model
contains most of the details for defining, solving and querying models.
use grb::prelude::*;
let mut model = Model::new("model1")?;
// add decision variables with no bounds
let x1 = add_ctsvar!(model, name: "x1", bounds: ..)?;
let x2 = add_intvar!(model, name: "x2", bounds: ..)?;
// add linear constraints
let c0 = model.add_constr("c0", c!(x1 + 2*x2 >= -14))?;
let c1 = model.add_constr("c1", c!(-4 * x1 - x2 <= -33))?;
let c2 = model.add_constr("c2", c!(2* x1 <= 20 - x2))?;
// model is lazily updated by default
assert_eq!(model.get_obj_attr(attr::VarName, &x1).unwrap_err(), grb::Error::ModelObjectPending);
assert_eq!(model.get_attr(attr::IsMIP)?, 0);
// set the objective function, which updates the model objects (variables and constraints).
// One could also call `model.update()`
model.set_objective(8*x1 + x2, Minimize)?;
assert_eq!(model.get_obj_attr(attr::VarName, &x1)?, "x1");
assert_eq!(model.get_attr(attr::IsMIP)?, 1);
// write model to the file.
model.write("model.lp")?;
// optimize the model
model.optimize()?;
assert_eq!(model.status()?, Status::Optimal);
// Querying a model attribute
assert_eq!(model.get_attr(attr::ObjVal)? , 59.0);
// Querying a model object attributes
assert_eq!(model.get_obj_attr(attr::Slack, &c0)?, -34.5);
let x1_name = model.get_obj_attr(attr::VarName, &x1)?;
// Querying an attribute for multiple model objects
let val = model.get_obj_attr_batch(attr::X, vec![x1, x2])?;
assert_eq!(val, [6.5, 7.0]);
// Querying variables by name
assert_eq!(model.get_var_by_name(&x1_name)?, Some(x1));
§Errors
Due to the nature of C APIs, almost every Gurobi routine can return an error. Unless otherwise stated,
if a method or function returns a Result
, the error will be Error::FromAPI
.
Re-exports§
Modules§
- attribute
- Gurobi Attributes for models, constraints and variables.
- callback
- Interface to Gurobi’s callback API
- constr
- This module contains the structs passed to the
Model::add_constr(s)
andModel::add_range(s)
methods. - expr
- Algebraic expressions involving variables used to construct constraints and a helper trait for pretty-printing.
- parameter
- Gurobi parameters for
Env
andModel
objects. See the manual for a list of parameters and their uses. - prelude
- Most commonly used items from this crate bundled for convenient import.
Macros§
- add_
binvar - Equivalent to calling
add_var!
(model, Binary, ...)
- add_
ctsvar - Equivalent to calling
add_var!
(model, Continuous, ...)
- add_
intvar - Equivalent to calling
add_var!
(model, Integer, ...)
- add_var
- Convenience wrapper around
Model::add_var
; adds a new variable to aModel
object. The macro keyword arguments are optional. - c
- A proc-macro for creating constraint objects.
Structs§
- Async
Handle - A handle to an
AsyncModel
which is currently solving. - Async
Model - A wrapper around
Model
that supports async optimisation in the background. - Constr
- A linear constraint added to a
Model
- Empty
Env - Gurobi environment object (see the Gurobi manual)
A Gurobi environment which hasn’t been started yet. Some Gurobi parameters,
such as
Record
need to be set before the environment has been started. - Env
- A Gurobi Environment object.
- GenConstr
- A general constraint added to a
Model
- Model
- Gurobi Model object.
- QConstr
- A quadratic constraint added to a
Model
- SOS
- An SOS constraint added to a
Model
- Var
- A Gurobi variable.
Enums§
- Constr
Sense - Sense for new linear/quadratic constraint
- Error
- The error type for operations in Gurobi Rust API
- Expr
- An algbraic expression of variables.
- GenConstr
Type - Type of general constraint
- Model
Sense - Sense of objective function, aka direction of optimisation.
- Relax
Type - Type of cost function at feasibility relaxation
- SOSType
- Type of SOS constraint
- Status
- Status of a model
- VarType
- Gurobi variable types (see manual)
Constants§
- INFINITY
- A large constant used by Gurobi to represent numeric infinity.
Traits§
- Model
Object - This trait encompasses all Gurobi model objects:
Var
,Constr
,GenConstr
,QConstr
andSOS
. EachModelObject
is associated with a particular model, and can only be used with that model. EachModelObject
also has a unique, fixed 32-bit ID. Gurobi itself uses ani32
to index objects (only positive indices are used), so the 32-bit limitation is already there. Note that IDs are only guaranteed to be unique if the concrete types of theModelObject
are the same and the objects belong to the same model. For example, ifv
is aVar
andc
is aConstr
, thenv
andc
may have the same ID. Additionally, ifs
is also aVar
, but doesn’t belong to the sameModel
asv
,s
andv
may have the same ID.
Functions§
- version
- Returns the version number of Gurobi
Type Aliases§
- Result
- A specialized
std::result::Result
for library errors