Try our new documentation site (beta).

C Attribute Examples

Consider the case where you have a Gurobi model <span>$</span>m<span>$</span>. 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 (<span>$</span>cols<span>$</span> values, starting from index <span>$</span>0<span>$</span> 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 <span>$</span>set<span>$</span> 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.

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.