Try our new documentation site (beta).


Model.addMVar()

addMVar ( shape, lb=0.0, ub=float('inf'), obj=0.0, vtype=GRB.CONTINUOUS, name="" )

Add an MVar object to a model. An MVar acts like a NumPy ndarray of Gurobi decision variables. An MVar can have an arbitrary number of dimensions, defined by the shape argument.

You can use arithmetic operations with MVar objects to create linear matrix expressions or quadratic matrix expressions, which can then be used to build linear or quadratic objectives or constraints.

The returned MVar object supports 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.

Arguments:

shape: An int, or tuple of int. The shape of the array.

lb (optional): Lower bound(s) for new variables.

ub (optional): Upper bound(s) for new variables.

obj (optional): Objective coefficient(s) for new variables.

vtype (optional): Variable type(s) for new variables.

name (optional): Names for new variables. The given name will be subscripted by the index of the generator expression, so if the index is an integer, c would become c[0], c[1], etc. Note that the generated names will be stored as ASCII strings, so you should avoid using names that contain non-ASCII characters. In addition, names that contain spaces are strongly discouraged, because they can't be written to LP format files.

The values of the lb, ub, obj, and vtype arguments can either be scalars, lists, or ndarrays. Their shapes should match the shape of the new MVar object, or they should be broadcastable to the given shape.

The name argument can either be a single string, used as a common base name that will be suffixed for each variable by its indices, or an ndarray of strings matching the shape of the new MVar object.

Return value:

New MVar object.

Example usage:

  # Add a 4-by-2 matrix binary variable
  x = model.addMVar((4,2), vtype=GRB.BINARY)
  # Add a vector of three variables with non-default lower bounds
  y = model.addMVar((3,), lb=[-1, -2, -1])

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.

Search

Gurobi Optimization