Select a Gurobi variable from a set of items
AnsweredHello Gurobi team,
I'm generating variables for my problem in the following format:
model.addVars(gurobi_vars, name='n', vtype=GRB.INTEGER, lb=n_lb, ub=n_ub)
However, instead of putting a lower bound and upper bound for the variable I want it to be added from a set of integers x={0, 1, 4, 16 , 64}. Is that possible? If yes, how?
Thanks!

Hi Saeid,
According to the documentation of the addVars method, you can provide a list for the \(\texttt{lb}\) and \(\texttt{ub}\) arguments. For example the snippet
y = model.addVars(3, ub=[1, 2, 3])
would generate 3 continuous variables where the upper bound of the first variable is \(1\), the upper bound of the second variable is \(2\), and the upper bound of the third variable is \(3\). Would this work for you?
Best regards,
Jaromił0 
Hi Jaromil,
Thank you for your answer. Actually, that's not what I'm looking for. What I want is that for a single variable like x I want all its values be selected from a set of values like e.g. x in {0, 1, 4, 16 , 64} instead of having a lower bound and upper bound as lb=0, ub=64. I want only the five candidate values {0, 1, 4, 16, 64} for variable x instead of all the integers in 1 to 64. e.g. something like:
model.addVar(name='x', vtype=GRB.INTEGER, set=[0, 1, 4, 16, 64])
instead of:
model.addVars(name='x', vtype=GRB.INTEGER, lb=n_lb, ub=n_ub)
Thank you!
0 
Hi Saeid,
Thank you for the clarification. Now I understand what you want to achieve. There is currently no feature in Gurobi which would allow defining integers over a specific discrete set. However, you can formulate the discrete variables on your own by introducing additional binary variables and equality constraints.
\[\begin{align*}
b_1 + 4 b_2 + 16 b_3 + 64 b_4 &= x\\
\sum_{i=1}^4 b_i &\leq 1\\
b_i \in \{0,1\}
\end{align*}\]Best regards,
Jaromił0 
Hi Jaromil,
Thank you! That's exactly what I was looking for.
Best,
Saeid
0
Please sign in to leave a comment.
Comments
4 comments