Try our new documentation site (beta).
Var
Gurobi variable object. Variables are always associated with a
particular model. You create a variable object by adding a variable
to a model (using
Model.addVar), rather than
by using a Var
constructor.
Variable objects have a number of attributes. The full list can be found in the Attributes section of this document. Some variable attributes can only be queried, while others can also be set. Recall that the Gurobi optimizer employs a lazy update approach, so changes to attributes don't take effect until the next call to Model.update, Model.optimize, or Model.write on the associated model.
We should point out a few things about variable attributes. Consider
the lb
attribute. Its value can be queried using
var.lb
. The Gurobi library ignores letter case in attribute
names, so it can also be queried as var.LB
. It can be set
using a standard assignment statement (e.g., var.lb = 0
).
However, as mentioned earlier, attribute modification is done in a
lazy fashion, so you won't see the effect of the change immediately.
And some attributes can not be set (e.g., the x
attribute), so
attempts to assign new values to them will raise an exception.
You can also use Var.getAttr/
Var.setAttr to access
attributes. The attribute name can be passed to these routines as a
string, or you can use the constants defined in the
GRB.Attr class (e.g.,
GRB.Attr.LB
).
To build expressions using variable objects, you generally use operator overloading. You can build either linear or quadratic expressions:
expr1 = x + 2 * y + 3 * z + 4.0 expr2 = x ** 2 + 2 * x * y + 3 * z + 4.0The first expression is linear, while the second is quadratic. An expression is typically then passed to setObjective (to set the optimization objective) or addConstr (to add a constraint).
Subsections