I'm using Gurobi and trying to set a constraint that two variables A, B are not equal model.addConstr(A != B) and seems like there isn't a not equal sign in Gurobi library. Is there any mathematical intuition that why we can't perform not equal in Optimization?

  • $\begingroup$ I believe the mathematical intuition is that an inequality constraint like $A \neq B$ (as well as constraints like $A \lneqq B$) causes the feasible region to be not a closed set. $\endgroup$
    – mhum
    Feb 3, 2022 at 0:07

1 Answer 1


If you want $x_1\neq x_2$, you can linearize $|x_1-x_2|\ge \varepsilon$, where $\varepsilon$ is your tolerance.

You can do this by introducing a boolean variable $y=1$ if and only if $x_1-x_2\ge \varepsilon$, and by imposing:

$$ x_1-x_2\le -\varepsilon +My\quad \mbox{and}\quad x_1-x_2\ge \varepsilon-(1-y)M $$

$M$ is the smallest large constant you can think of.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.