1

Lets assume $x$, $y$ are non negative continuous variables and $P$ an integer variable assuming either the value $1$ or the value $2$.

How could I possibly model the relation

If $x = y$ then $P = 2$ else $P = 1$ ?

Clement
  • 2,252
  • 7
  • 13
  • 3
    Note that $P-1$ is binary and apply https://or.stackexchange.com/a/2632/500 with your $x-y$ as $x$, $0$ as $b$, and $P-1$ as $y$. – RobPratt Apr 06 '22 at 21:12
  • Hugh, I will need to introduce another two binaries? That is frustrating. Thank you very much Rob. – Clement Apr 06 '22 at 21:35
  • Well, you can eliminate one of the additional binary variables by substitution if you want. Also, if you instead wanted to enforce only $P=2 \implies x=y$, you wouldn't need any new variables. – RobPratt Apr 06 '22 at 21:42
  • No, this is not what I need to enforce. Anyway, how would the constraint then look like? – Clement Apr 06 '22 at 21:49
  • 1
    $(L_x - U_y)(2 - P) \le x -y \le (U_x - L_y)(2 - P)$, where $L$ and $U$ are lower and upper bounds on the variables. – RobPratt Apr 06 '22 at 21:55
  • I see it. Wow! Thanks again! – Clement Apr 06 '22 at 21:58
  • 1
    @RobPratt You should convert your comments to an answer. – prubin Apr 06 '22 at 22:47

1 Answers1

5

Note that $P−1$ is binary and apply the formulation in

In an integer program, how I can force a binary variable to equal 1 if some condition holds?

with your $x−y$ as $x$, $0$ as $b$, and $P−1$ as $y$.

If you instead wanted to enforce only $P=2 \implies x=y$, you wouldn't need any new variables. Just let $L$ and $U$ be lower and upper bounds on the variables, and impose $$(L_x−U_y)(2−P) \le x − y \le (U_x−L_y)(2−P).$$

RobPratt
  • 32,006
  • 1
  • 44
  • 84