How to add modulo constraints in gurobi
Answeredmodel.addConstr(gp.quicksum(x[i] for i in lst) %2 == 1)
model.addConstr(gp.quicksum(x[i] for i in lst) == odd_value[cnt])
model.addConstr(odd_value[cnt] % 2 == 1)
-
Hi Vaihab,
If I understand correctly, you are wanting to ensure that the following sum is odd.
gp.quicksum(x[i] for i in lst)
This sum is odd if, and only, if there is an even number 1 less than it. If we introduce an integer variable named aux then 2*aux will be even. Once this variable is added you can form the following constraint
model.addVar(vtype="I", lb=-float("inf"))
model.addConstr(gp.quicksum(x[i] for i in lst) == 1 + 2*aux)- Riley
0 -
Hi,
Thanks, its working now. So basically to ensure that "gp.quicksum(x[i] for i in lst)" is odd , the aux variable takes its values accordingly, there is no need to add more constraints corresponding to the aux variable.
So in same way I can define many such aux variables if 'x' is 2D array.
x=model.addVars(len(lst),len(lst), vtype="I")
aux=model.addVars(len(lst),vtype="I", lb=-float("inf"))for j in range(len(lst)):
model.addConstr(gp.quicksum(x[i,j] for i in lst) == 1 + 2*aux[j])pls correct me, if I'm wrong.
-Vaibhav
0 -
Correct!
Only thing I have to add is that if x are non-negative then the sum can never be negative so you don't need the lower bound of -infinity on the integer variables, and can let them take their default lower bound of 0.
0
Please sign in to leave a comment.
Comments
3 comments