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)

  • 1
    $\begingroup$ 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. $\endgroup$
    – Sune
    Oct 20, 2022 at 10:29
  • $\begingroup$ @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 – $\endgroup$
    – M.Badaoui
    Oct 20, 2022 at 12:22
  • 1
    $\begingroup$ @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 $\endgroup$
    – Sune
    Oct 20, 2022 at 14:59


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.