Cookie Settings

Try our new documentation site (beta).

Filter Content By
Version
Table of contents
Filter by Language
C API Details
C++ API Details
GRBModel +
Java API Details
GRBModel +
.NET API Details
GRBModel +
Python API Details
Model +
Attributes
Parameters
Parameter Descriptions +


Java 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 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> for multiple variables in a single get call on the model <span>$</span>m<span>$</span>:
  double[] xvals = m.get(GRB.DoubleAttr.X, m.getVars()))

For each attribute query method, there's an analogous <span>$</span>set<span>$</span> 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).