Try our new documentation site (beta).
Attribute Examples
The same attributes exist in all of the Gurobi APIs, but the
approaches used to query and modify them, and the means by which you
refer to them vary. 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 API's, 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 API's:
You can also also browse our Examples to get a better sense of how to use our attribute interface.
Subsections