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 is a NumPy ndarray of Gurobi decision variables. The ndarray can have an arbitrary number of dimensions, but you will generally need to slice a multi-dimensional array into 1-D objects to use an MVar to build constraints.

You can multiply a 1-D MVar by a 2-D matrix (a NumPy dense ndarray or a SciPy sparse matrix), using overloaded Python matrix-multiply operators (@), to create a linear matrix expression or quadratic matrix expression, which can then be used to build linear or quadratic objectives or constraints

Note that the returned MVar object supports standard NumPy slicing.

Arguments:

shape: 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.

Return value:

New MVar object.

Example usage:

  x = model.addMVar(10)                      # add a 1-D array of 10 variables
  y = model.addMVar((3,4), vtype=GRB.BINARY) # add a 3x4 2-D array of binary variables
  print(y[:,1:3])                            # take a slice of a 2-D array

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