Ask Your Question
4

How do you utilize addVars in Gurobi when using tuples as indexes?

asked 2023-07-21 11:24:24 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-21 11:35:01 +0000

ladyg gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-21 11:24:24 +0000

Seen: 14 times

Last updated: Jul 21 '23