7
$\begingroup$

Given are two integer variables $L_x \leq x \leq U_x$ and $L_y \leq y \leq U_y$. I'd like to formulate the constraint

$$ \text{If} \;\; x = k, \;\; \text{ then } \;\; y = c, $$

where $k$ and $c$ are given integer constants. Any help is highly appreciated!

$\endgroup$
1
  • 1
    $\begingroup$ Just to share; If you are using a commercial solver, e.g. CPLEX: cplex.IfThen(cplex.Eq(x, k), cplex.Eq(y, c)); $\endgroup$
    – alamaranka
    Sep 6 at 10:29
10
$\begingroup$

By introducing the binary helper variables $z_1,z_2,z_3,w_1,w_2,w_3$, you can use the constraints

$$ \begin{align} L_y z_1 + c z_2 + (c+1)z_3 &\leq y \leq (c-1)z_1 + c z_2 + U_y z_3, \tag{1} \\ L_x w_1 + k w_2 + (k+1)w_3 &\leq x \leq (k-1)w_1 + k w_2 + U_x w_3, \tag{2}\\ z_1 + z_2 + z_3 &= 1, \tag{3}\\ w_1 + w_2 + w_3 &= 1, \tag{4}\\ z_1 + z_3 &\leq w_1 + w_3 \tag{5}. \end{align} $$


Explanation: The constraint $x = k \implies y = c$ is equivalent to the contraposition $y \neq c \implies x \neq k$. Hence, we want to formulate

$$ y \leq c - 1 \;\vee\; y \geq c+1 \implies x \leq k-1 \; \vee \; x \geq k+1. \tag{*} $$

Then, (1) and (2) model the constraints

$$ \begin{align} z_1 = 1 &\implies y \leq c - 1, \\ z_2 = 1 &\implies y = c, \\ z_3 = 1 &\implies y \geq c + 1, \\ w_1 = 1 &\implies x \leq k - 1, \\ w_2 = 1 &\implies x = k, \\ w_3 = 1 &\implies x \geq k + 1, \\ \end{align} $$

while (3) and (4) guarantee that only one of the three cases for $x$ and $y$ can appear. Finally, (5) expresses constraint (*) by means of the binary helper variables.

$\endgroup$
5
$\begingroup$

In OPL CPLEX this is very easy to read and write.

Let me change https://github.com/AlexFleischerParis/zooopl/blob/master/zooifthen.mod to your exact question:

(nbBus40==6)=>(nbBus30==3);

means nbBus40==6 implies nbBus30==3

int nbKids=300;
float costBus40=500;
float costBus30=400;
 
dvar int+ nbBus40;
dvar int+ nbBus30;
minimize
 costBus40*nbBus40  +nbBus30*costBus30;
 
subject to
{
 40*nbBus40+nbBus30*30>=nbKids;
 
 // with if nb buses 40 is 6 then nb buses 30 is 3
 
 (nbBus40==6)=>(nbBus30==3);
 
}
$\endgroup$
3
$\begingroup$

Introduce binary variable $\delta$ and we can write following constraints $$ \begin{align} k \cdot \delta + L_{x} \cdot (1-\delta) &\le x \le k \cdot \delta + U_{x} \cdot (1-\delta) \\ c \cdot \delta + L_{y} \cdot (1-\delta) &\le y \le c \cdot \delta + U_{y} \cdot (1-\delta) \\ \end{align} $$ The way it works is when $\delta$ equals $1$, $x = k$ and $y = c$ and when $\delta$ equals 0, they take feasible bounds.

$\endgroup$
8
  • 1
    $\begingroup$ When $\delta=0$, $L_x \le x \le U_x$ (typo). $\endgroup$
    – Kuifje
    Sep 6 at 10:59
  • 3
    $\begingroup$ Note that this also models the converse statement $y=c \Rightarrow x=k$, which may be too constraining. $\endgroup$
    – Kuifje
    Sep 6 at 11:38
  • 3
    $\begingroup$ This enforces only $\delta =1\implies (x=k \land y=c)$. It does not prevent $x=k \land y\not= c$. $\endgroup$
    – RobPratt
    Sep 6 at 14:27
  • 1
    $\begingroup$ @Kuifje it is actually not constraining enough. It enforces neither the desired implication nor its converse. $\endgroup$
    – RobPratt
    Sep 6 at 14:32
  • 2
    $\begingroup$ @hanhu When $\delta=0$, anything can happen. For example, take $(\delta,x,y)=(0,k,c+1)$. $\endgroup$
    – RobPratt
    Sep 6 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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