I would like to model the following:

Only one of the following equalities can hold.

$$(A_1 = B_1)\quad\text{OR}\quad(A_2 = B_2)\quad\text{OR}\quad\dots\quad\text{OR}\quad(A_k = B_k)$$

I can introduce binary variables $d_1,\dots,d_k$

    if d1 = 1  then  (A1 = B1)
AND if d2 = 1  then  (A2 = B2)
AND if dk = 1  then  (Ak  = Bk)

AND d1 + d2 + ... + dk = 1 

Is there a better way than this?


1 Answer 1


What you have so far will enforce at least one $A_i = B_i$. To get exactly one, you need to also impose that $A_i = B_i$ implies $d_i=1$.

  • $\begingroup$ Note that enforcing $A_i=B_i \implies d_i=1$ (or, equivalently, the contrapositive $d_i=0 \implies A_i \neq B_i$) is straightforward if $A$ and $B$ are integer valued but tricky if they are real-valued. In the real-valued case, you will need to change $A_i \neq B_i$ to something like $|A_i - B_i| \ge \epsilon$ for some (small) positive $\epsilon$, which leaves $0 < |A_i-B_i| < \epsilon$ in mathematical "no man's land". $\endgroup$
    – prubin
    Feb 10, 2020 at 20:11
  • $\begingroup$ Yes, see this post, and take $x=A_i-B_i$, $b=0$, and $y=d_i$. $\endgroup$
    – RobPratt
    Feb 10, 2020 at 20:27

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.

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