Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# 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:

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 `var.LB`
Python | `var.getAttr(GRB.Attr.LB)` or `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 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`.

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 using the `get` method:

    cols = m.get(GRB_IntAttr_NumVars);

If you’ve performed optimization on the model, the optimal objective value can be obtained by querying the ObjVal model attribute:

    obj = m.get(GRB_DoubleAttr_ObjVal);

If you’d like to query the value that a variable takes in the computed solution, you can query the X attribute for the corresponding variable object:

    vars = m.getVars();
    for (int j = 0; j < cols; j++)
      xj = vars[j].get(GRB_DoubleAttr_X);

You can also query the value of `X` for multiple variables in a single `get` call on the model `m`:

    double xvals[] = m.get(GRB_DoubleAttr_X, m.GetVars());

For each attribute query method, there’s an analogous `set` routine. To set the upper bound of a variable, for example:

    v = m.getVars()[0];
    v.set(GRB_DoubleAttr_UB, 0);

(In this example, we’ve set the upper bound for the first variable in the model to 0).

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 (which is implemented as a .NET property):

    cols = m.NumVars;

If you’ve performed optimization on the model, the optimal objective value can be obtained by querying the ObjVal model attribute:

    obj = m.ObjVal;

If you’d like to query the value that a variable takes in the computed solution, you can query the X attribute for the corresponding variable object:

    vars = m.GetVars();
    for (int j = 0; j < cols; j++)
      xj = vars[j].X;

You can also query the value of `X` for multiple variables in a single call using the `Get` method on the model `m`:

    double[] xvals = m.Get(GRB.DoubleAttr.X, m.GetVars());

For each attribute query method, there’s an analogous `Set` routine. To set the upper bound of a variable, for example:

    v = m.GetVars()[0];
    v.UB = 0;

(In this example, we’ve set the upper bound for the first variable in the model to 0).

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 using the `get` method:

    cols = m.get(GRB.IntAttr.NumVars);

If you’ve performed optimization on the model, the optimal objective value can be obtained by querying the ObjVal model attribute:

    obj = m.get(GRB.DoubleAttr.ObjVal);

If you’d like to query the value that a variable takes in the computed solution, you can query the X attribute for the corresponding variable object:

    vars = m.getVars();
    for (int j = 0; j < cols; j++)
      xj = vars[j].get(GRB.DoubleAttr.X);

You can also query the value of `X` for multiple variables in a single `get` call on the model `m`:

    double[] xvals = m.get(GRB.DoubleAttr.X, m.getVars());

For each attribute query method, there’s an analogous `set` routine. To set the upper bound of a variable, for example:

    v = m.getVars()[0];
    v.set(GRB.DoubleAttr.UB, 0);

(In this example, we’ve set the upper bound for the first variable in the model to 0).

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:

    print(m.NumVars)

If you’ve performed optimization on the model, the optimal objective value can be obtained by querying the ObjVal model attribute:

    print(m.ObjVal)

If you’d like to query the value that a variable takes in the computed solution, you can query the X attribute for the corresponding variable object:

    for v in m.getVars():
        print(v.X)

You can also query the value of `X` for multiple variables in a single `getAttr` call on the model `m`:

    print(m.getAttr(GRB.Attr.X, m.getVars()))

For each attribute query method, there’s an analogous `set` routine. To set the upper bound of a variable, for example:

    v = m.getVars()[0]
    v.UB = 0

(In this example, we’ve set the upper bound for the first variable in the model to 0).

Note

In gurobipy, attribute names are case-insensitive, so, for example, you can also query NumVars as `m.numVars` or `m.numvars`. However, we recommend using the same capitalization as the documented attribute name (in this case, `m.NumVars`) for consistency and compatibility with developer tools such as static type checkers and IDE autocompletion.

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 (which is implemented as a .NET property):

    cols = m.NumVars;

If you’ve performed optimization on the model, the optimal objective value can be obtained by querying the ObjVal model attribute:

    obj = m.ObjVal;

If you’d like to query the value that a variable takes in the computed solution, you can query the X attribute for the corresponding variable object:

    vars = m.GetVars();
    For j As Integer = 0 To cols - 1
      xj = vars[j].X;

You can also query the value of `X` for multiple variables in a single call using the `Get` method on the model `m`:

    xvals = m.Get(GRB.DoubleAttr.X, m.GetVars());

For each attribute query method, there’s an analogous `Set` routine. To set the upper bound of a variable, for example:

    v = m.GetVars()[0];
    v.UB = 0;

(In this example, we’ve set the upper bound for the first variable in the model to 0).

Help and Feedback

Press `/` or click to search this documentation | Gurobi website | Gurobi community forum
Go to other Gurobi documentation | Gurobi download | Contact support