Conditional modelling of a binary variable based on the values of two continous variables

367 Views Asked by At

I want to model a binary variable $(b)$ from two continous variables $(x_{in},\:x_{out})$.

These variables are $ 0\leq x_{in} \leq x_{max},\: 0\leq x_{out} \leq x_{max})$. I want the following three conditions to apply on my binary variable

$$Condition\:1: b=1\:if\: x_{in}>0\:and\:x_{out}=0\\ Condition\:2: b=0\:if\: x_{in}=0\:and\:x_{out}>0\\ Condition\:3: b=0\:if\: x_{in}=0\:and\:x_{out}=0$$

I have tried two methods and neither of them worked.

First method

Method is explained here. $$x_{in}=b \times x_{max}$$ $$x_{out}=(1-b) \times x_{max}$$ The above solution works for the first two condition but it doesn't work for the 3rd condition.

Second method

Method is explained under this discussion. $$\epsilon\times b \leq x_{in} \leq \epsilon\times b \times x_{max}\\ \epsilon\times b \leq x_{out} \leq \epsilon\times b \times x_{max}$$ where $\epsilon>0$ and it is a very small number $(e.g. 10^{-9})$.

I am using most recent version of Gurobi.

Can you please help? I have tried everything (that I could understand from forums). My knowledge of optimisation algorithms is limited so I apologise in advance if this question is duplicate.

2

There are 2 best solutions below

3
On

Based on your conditions, it is sufficient to introduce the following formulations

b <= M x X_in ; ( where M is a constant number greater than X_max )

b >= epsilon x ( X_in - X_out )

b in {0,1}

in this manner and due to the above formulations and

Condition 1: we have b<=M and b>=(epsilon). Hence, b=1.

Condition 2: we have b<=0 and b>=(-epsilon). Hence, b=0.

Condition 3: we have b<=0 and b>=0. Hence, b=0.

Good luck

0
On

Notice that conditions 2 and 3 can be merged as: $b=0$ if $x_\text{in}=0$. You want to enforce \begin{align} (x_\text{in} > 0 \land x_\text{out} = 0) &\implies b=1 \\ x_\text{in} = 0 &\implies b=0 \end{align} Equivalently, you want to enforce their contrapositives \begin{align} b=0 &\implies (x_\text{in} = 0 \lor x_\text{out} > 0) \\ b=1 &\implies x_\text{in} > 0 \end{align} Equivalently, introduce binary variables $c$ and $d$ and enforce \begin{align} b=0 &\implies (c = 1 \lor d = 1) \\ c=1 &\implies x_\text{in} = 0 \\ d=1 &\implies x_\text{out} > 0 \\ b=1 &\implies x_\text{in} > 0 \end{align} Now replace $>0 $ with $\ge \epsilon$, and you obtain linear constraints \begin{align} 1 - b &\le c + d \\ x_\text{in} &\le x_\text{max}(1-c) \\ x_\text{out} &\ge \epsilon d \\ x_\text{in} &\ge \epsilon b \end{align}