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

add a comment

2

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`

.

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

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

Seen: **28 times**

Last updated: **Jul 21 '23**

How to efficiently perform a cross join (Cartesian product) on a large data set using pandas?

How can the boolean variables be represented over time using a bar graph?

How can variables be interpreted as strings?

What is the best approach to calculate pairwise mutual information using numpy?

How to create a bar chart using ggplot for two different dataframes that have the same variables?

Why are environment variables not being loaded from the dotenv file?

How can a linear regression model be constructed when dealing with dummy/categorical variables?

Copyright QStack.ai, 2010-2023. Content on this site is licensed under the Creative Commons Attribution Share Alike 3.0 license.