Try our new documentation site (beta).
Attribute Examples
Gurobi attribute handling is designed to be orthogonal, meaning that you only need to use a small number of routines to work with a large number attributes. In particular:
- The names and meanings of the various Gurobi attributes remain constant across the different programming language APIs, although some decoration is required in each language.
- Given the type of an attribute (double, integer, etc.) and the programming language you wish to use it from, you simply need to identify the appropriate routine for that attribute type in that language in order to query or modify that attribute.
Consider the LB
attribute, which captures the lower bound on a
variable. You would refer to this attribute as follows in the
different Gurobi APIs:
Language | Attribute |
---|---|
C | GRB_DBL_ATTR_LB |
C++ | GRB_DoubleAttr_LB |
Java | GRB.DoubleAttr.LB |
.NET | GRB.DoubleAttr.LB, or just var.LB |
Python | GRB.Attr.LB, or just var.lb |
To query the value of this attribute for an individual variable in the different APIs, you would do the following:
Language | Attribute Query Example |
---|---|
C | GRBgetdblattrelement(model, GRB_DBL_ATTR_LB, var_index, &value); |
C++ | var.get(GRB_DoubleAttr_LB) |
Java | var.get(GRB.DoubleAttr.LB) |
.NET | var.Get(GRB.DoubleAttr.LB), or just var.LB |
Python | var.getAttr(GRB.Attr.LB), or just var.lb |
Our APIs also include routines for querying attribute values for multiple variables or constraints at once, which is more efficient.
Attributes are referred to using a set of enum
types in C++,
Java, and .NET (one enum for double-valued attributes, one for
int-valued attributes, etc.). In C and Python, the names listed above
are simply constants that take string values. For example,
GRB_DBL_ATTR_LB
is defined in the C layer as:
#define GRB_DBL_ATTR_LB "LB"In C and Python, you have the option of using the strings directly when calling attribute methods. If you wish to do so, note that character case and underscores are ignored. Thus,
MIN_COEFF
and MinCoeff
are equivalent.
One important point to note about attributes modification is that it is done in a lazy fashion. Modifications don't actually affect the model until the next request to either update or optimize the model (GRBupdatemodel or GRBoptimize in C).
Refer to the following sections for more detailed examples of how to query or modify attributes from our various APIs:
You can also also browse our Examples to get a better sense of how to use our attribute interface.
Subsections