Try our new documentation site (beta).
Filter Content By
Version
Text Search
${sidebar_list_label} - Back
Filter by Language
C++ Attribute Examples
Consider the case where you have a Gurobi model . 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
![<span>$</span>X<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img132.png?x70725)
![<span>$</span>m<span>$</span>](https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/11.0/refman/img129.png?x70725)
double xvals[] = m.get(GRB_DoubleAttr_X, m.GetVars()))
For each attribute query method, there's an analogous 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).
![](/wp-content/plugins/hd_documentations/content/images/documentation-ampl-guide.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-cloud.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-example-tour.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-quick-start.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-reference-manuals.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-remote-services.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-linux.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-mac-osx.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-windows.webp?x70725)