Write if-else statement in linear programming representation

integer programminglinear programming

Given a if-else statement:

If a > 0 and b > 0:
    c = 1
else:
    c = 0

Where a and b are input variable with fix values.

For example, I have 20 samples and each sample has a and b values. Those 20 samples will be inputted to my model. The c will decide whether to execute the action in my objective function. The action is a equation in my objective function.

I was wondering can I write the equation from the above if-else statement into the following statement for linear programming to represent if-else statement?

Set c is a binary variable (0, 1), and the constraint equation will be:

    a·b·(c-0.5)  >  0

Observing from the above equation, if a > 0 and b > 0, the binary variable c will be 1 because satisfying the equation a·b·(c-0.5) > 0. Otherwise, if c = 0 under a > 0 and b > 0, the equation cannot be satisfied.

I was wondering am I correct? If I want to run Lingo software and other linear programming software… Thanks.

Best Answer

Suppose $a$ and $b$ have constant bounds: $\ell_a \le a \le u_a$ and $\ell_b \le b \le u_b$. Let binary decision variable $p_a$ indicate whether $a>0$, and let binary decision variable $p_b$ indicate whether $b>0$. Let $\epsilon>0$ be a small constant tolerance; if $a$ and $b$ are integer, you can take $\epsilon=1$. Impose the following linear constraints: \begin{align} \ell_a (1-p_a) + \epsilon p_a \le a &\le u_a p_a \tag1 \\ \ell_b (1-p_b) + \epsilon p_b \le b &\le u_b p_b \tag2 \\ c &\le p_a \tag3 \\ c &\le p_b \tag4 \\ c &\ge p_a + p_b - 1 \tag5 \end{align} Constraint $(1)$ enforces $p_a = 1 \implies a \ge \epsilon$ and $p_a = 0 \implies a \le 0$. Constraint $(2)$ enforces $p_b = 1 \implies b \ge \epsilon$ and $p_b = 0 \implies b \le 0$. Constraints $(3)$-$(5)$ enforce $c \iff p_a \land p_b$