Gurobi matrix variable object. An
MVar is a NumPy ndarray of
Gurobi variables. Variables are always associated with a particular
model. You typically create these objects using
expr1 = A @ x expr2 = A @ x + B @ y + z expr3 = x @ A @ x + y @ B @ yThe first two expressions are linear, while the third is quadratic.
Dimensions and data types must always be compatible. In the examples
above, matrix must be either a NumPy ndarray with two dimensions
or a SciPy sparse matrix (which will always have two dimensions), and
must be a 1-D
expr1, the size of the
second dimension of must be equal to the length of . The same
must be true of and in
expr2. In addition, the size of
the first dimension of in
expr2 must be equal to the size
of the first dimension of , and also to the length of .
expr3, the size of the first dimension of must be equal
to the length of the
MVar on the left, and the size of the
second dimension must be equal to the length of the
MVar on the
right. The same is true for .
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
lb attribute. Its value can be queried using
mvar.lb. The Gurobi library ignores letter case in attribute
names, so it can also be queried as
Attribute values are returns as a NumPy
ndarray that has the
same shape as
An attribute can be set,
using a standard assignment statement (e.g.,
var.lb = l),
l being either an
ndarray with the appropriate
shape, or a scalar which is then applied to all of the associated
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 MVar.getAttr/
MVar.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.,