Try our new documentation site (beta).


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 Model.addMVar.

Many concepts, properties and methods of the MVar class lean on equivalents in NumPy's ndarray class. For explanations of concepts like shape, dimensions, or broadcasting, we refer you to the NumPy documentation.

You generally use MVar objects to build matrix expressions, typically using overloaded operators. You can build linear matrix expressions or quadratic 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.

In the examples above (and in general), <span>$</span>A<span>$</span> and <span>$</span>B<span>$</span> can be NumPy ndarray objects or any of the sparse matrix classes defined in SciPy.sparse. Dimensions of the operands must compatible, in the usual sense of Python's matrix multiplication operator. For example, in the expression <span>$</span>A @ x<span>$</span>, both <span>$</span>A<span>$</span> and <span>$</span>x<span>$</span> must have at least one dimension, and their inner-most dimensions must agree. For a complete description of shape compatibility rules, we refer you to Python's documentation

An expression is typically then passed to setObjective (to set the optimization objective) or addConstr (to add a constraint).

MVar objects support standard NumPy indexing and slicing. An MVar of size <span>$</span>1<span>$</span> can be passed in all places where gurobipy accepts a Var object.

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 The Gurobi library ignores letter case in attribute names, so it can also be queried as var.LB. Attribute values are returned as a NumPy ndarray that has the same shape as mvar, where each element contains the attribute value for the corresponding element of the MVar object. An attribute can be set, using a standard assignment statement (e.g., = l), with l being either an ndarray with 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., 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., GRB.Attr.LB).


Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.