# How to model $A_i=B_i$ for only one $i$

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?

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$$.
• 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". – prubin Feb 10 at 20:11
• Yes, see this post, and take $x=A_i-B_i$, $b=0$, and $y=d_i$. – RobPratt Feb 10 at 20:27