10

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?

Kuifje
  • 13,324
  • 1
  • 23
  • 56
pigsun
  • 145
  • 6
  • 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. – mhum Feb 03 '22 at 00:07

1 Answers1

15

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.

Kuifje
  • 13,324
  • 1
  • 23
  • 56