How to model the indicator function in Gurobi
AnsweredHello,
I would like to model the following constraint :
f_r are problem variables declared as follows :
for i in range(R):
f[i] = m.addVar( lb=0.0, vtype=GRB.CONTINUOUS, name="CPU frequency"+ " " +str(i))
and I need to make sure that the number of elements of the vector f = (f_1 , f_2 , f_3, ...) strictly greater than zero is less than C.
I thought of introducing a binary variable i_r which takes the value 1 if f_r> 0, 0 if f_r = 0 and adding the following conditions:
for i in range(R):
m.addConstrs(ind[i] == 1 if f[i] != 0 else 0)
so that then I can control the number of variables f_r different from zero in my vector f:
m.addConstr( quicksum( i[j] for j in range(R))<= C), but I can't do that because of != operator.
in the documentation, I found this option:
Model.addGenConstrIndicator()
but it seems like it's kind of the opposite of what I want to do. is there any way to adapt it?
# f_r > 0 -> i_r = 1
Thank you in advance !
Ibtissam
-
Yes, using an indicator is the right way to go. And it is very easy, you just need to invert the direction of the indicator. Instead of
f_r > 0 -> i_r = 1
you would enforce
i_r = 0 -> f_r <= 0
This is mathematically equivalent, and it fits to how indicators in Gurobi are defined.
0
Please sign in to leave a comment.
Comments
1 comment