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 of 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 var.LB

Python

GRB.Attr.LB or var.lb

To query the value of this attribute for an individual variable in the different APIs, you would do the following:

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 detailed examples of how to query or modify attributes from our various APIs. You can also browse our Examples to get a better sense of how to use our attribute interface.

Consider the case where you have a Gurobi model m. You can retrieve the number of variables in the model by querying the NumVars model attribute. This is an integer-valued, scalar attribute, so you use GRBgetintattr:

int cols;
error = GRBgetintattr(m, GRB_INT_ATTR_NUMVARS, &cols);

You can also use the name of the attribute directly:

int cols;
error = GRBgetintattr(m, "NumVars", &cols);

(Note that attribute capitalization doesn’t matter in the C interface, so you could also use "numVars" or "numvars").

If you’ve performed optimization on the model, the optimal objective value can be obtained by querying the ObjVal model attribute. This is a double-valued, scalar attribute, so you use GRBgetdblattr:

double objval;
error = GRBgetdblattr(m, GRB_DBL_ATTR_OBJVAL, &objval);

If you’d like to query the value that a variable takes in the computed solution, you can query the X variable attribute. This is a double-valued, vector attribute, so you have a few options for querying the associated values. You can retrieve the value for a single variable using GRBgetdblattrelement:

double x0;
error = GRBgetdblattrelement(m, GRB_DBL_ATTR_X, 0, &x0);

(we query the solution value for variable 0 in this example). You can also query attribute values for multiple variables using GRBgetdblattrarray or GRBgetdblattrlist:

double x[];
error = GRBgetdblattrarray(m, GRB_DBL_ATTR_X, 0, cols, x);

The former routine retrieves a contiguous set of values (cols values, starting from index 0 in our example). The latter allows you to provide a list of indices, and it returns the values for the corresponding entries.

For each attribute query routine, there’s an analogous set routine. To set the upper bound of a variable, for example, you would use GRBsetdblattrelement:

error = GRBsetdblattrelement(m, GRB_DBL_ATTR_UB, 0, 0.0);

(In this example, we’ve set the upper bound for variable 0 to 0). You can set attribute values for multiple variables in a single call using GRBsetdblattrarray or GRBsetdblattrlist.