MVar


MVar

Gurobi matrix variable object. AnMVaris a NumPy ndarray of Gurobi variables. Variables are always associated with a particular model. You typically create these objects usingModel.addMVar.

You generally useMVarobjects to build matrix expressions, typically using overloaded operators. You can buildlinear matrix expressionsorquadratic matrix expressions:

expr1 = A @ x expr2 = A @ x + B @ y + z expr3 = x @ A @ x + y @ B @ y
The first two expressions are linear, while the third is quadratic.

Dimensions and data types must always be compatible. In the examples above, matrix<span>$</span>A<span>$</span>must be either a NumPy ndarray with two dimensions or a SciPy sparse matrix (which will always have two dimensions), and<span>$</span>x<span>$</span>must be a 1-DMVar. Inexpr1, the size of the second dimension of<span>$</span>A<span>$</span>must be equal to the length of<span>$</span>x<span>$</span>. The same must be true of<span>$</span>B<span>$</span>and<span>$</span>y<span>$</span>inexpr2. In addition, the size of the first dimension of<span>$</span>A<span>$</span>inexpr2must be equal to the size of the first dimension of<span>$</span>B<span>$</span>, and also to the length of<span>$</span>z<span>$</span>.

Forexpr3, the size of the first dimension of<span>$</span>A<span>$</span>must be equal to the length of theMVaron the left, and the size of the second dimension must be equal to the length of theMVaron the right. The same is true for<span>$</span>B<span>$</span>.

An expression is typically then passed tosetObjective(to set the optimization objective) oraddConstr(to add a constraint).

Variable objects have a number of attributes. The full list can be found in theAttributessection 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 toModel.update,Model.optimize, orModel.writeon the associated model.

We should point out a few things about variable attributes. Consider thelbattribute. Its value can be queried usingmvar.lb. The Gurobi library ignores letter case in attribute names, so it can also be queried asvar.LB. Attribute values are returns as a NumPyndarraythat has the same shape asmvar. An attribute can be set, using a standard assignment statement (e.g.,var.lb = l), withlbeing either anndarraywith the appropriate shape, or a scalar which is then applied to all of the associated variables. 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., thexattribute), so attempts to assign new values to them will raise an exception.

You can also useMVar.getAttr/MVar.setAttrto access attributes. The attribute name can be passed to these routines as a string, or you can use the constants defined in theGRB.Attrclass (e.g.,GRB.Attr.LB).



Subsections