# OR constraint in Gurobi - Python

I'm new to Gurobi in Python and I am wondering if there is way to code the following constraints:

1- $$\sum x_i =0$$ for all $$i\in A$$ OR for all $$i \in B$$
where A and B are distinch sets of integers and $$x_i$$ are binary variables.

2- $$\sum_{i=0}^{10} x_i =0$$ OR $$\sum_{i=0}^{10} x_i =1$$

Question: Is there a way to express the "Or" in one of the above constraints ?/code in Gurobi (maybe something like addConstrOr instead of addConstr)

My thoughts: maybe something like that would work for (1) and (2), but I am not sure if it is possible

• model. addConstr (sum( x [ i] for i in A) ==0) or model. addConstr (sum( x [ i] for i in B) ) ==0)

• model. addConstr (sum( x [ i] for i in range(0,10) ) ==0) or model. addConstr (sum( x [ i] for i in range(0,10) ) ==1)

• It is not Gurobi specific, but by introducing binary variables capturing the logic, you can easily model it directly. For point two, introduce a binary variable $y$ and have $\sum_{i=0}^{10}x_i=y$. Now $y$ controls whether the sum is zero or one, and it cannot be anything else.
– Sune
Oct 20, 2022 at 10:29
• @Sune Thank you! I'm wondering if there is a way to express this in Gurobi, maybe the equivalent in Cplex would be something like model. add ( (sum....)+(sum....)>=1) -as at least one of the both sums should hold-, but not sure of gurobi – Oct 20, 2022 at 12:22
• Oct 20, 2022 at 12:29
• @M.Badaoui well yes, in gurobi's python API this would be something like model.addConstr( sum( x[i] for i in range(0,11)) == y)  where $y$ is declared as a binary decision variable
– Sune
Oct 20, 2022 at 14:59