Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

If you have defined your variable with tuples as indexes, you can use the addVars method in Gurobi to create an array of variables. The syntax for addVars is as follows:

model.addVars(indices, lb=0.0, ub=GRB.INFINITY, obj=0.0, vtype=GRB.CONTINUOUS, name="")

where indices is the tuple of indexes, lb is the lower bound for the variables, ub is the upper bound, obj is the objective coefficient for the variables, vtype is the variable type (continuous, binary, or integer), and name is the name of the variables.

For example, if you have a tuple variable x[i,j] where i and j are the indexes, you can create an array of variables using the following code:

x = model.addVars((I, J), lb=0, ub=GRB.INFINITY, name="x", vtype=GRB.CONTINUOUS)

where I and J are the set of values for i and j. This will create a two-dimensional array of variables, x[i,j], for every value of i and j. You can then use these variables in your objective function and constraints.

For example, to set the objective coefficient for the variable x[i,j] to be equal to c[i,j], where c is a two-dimensional parameter array, you can use the following code:

model.setObjective(quicksum(c[i,j]*x[i,j] for i in I for j in J))

This will set the objective function to be the sum of c[i,j]*x[i,j] over all values of i and j.