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
Note that the returned MVar object supports standard NumPy slicing.
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, c, 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.
New MVar object.
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