2

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.

Simon
  • 75
  • 1
  • 7

1 Answers1

2

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$

RobPratt
  • 45,619
  • Thanks a lot, RobPratt. If I set boundary for such that -3 ≤ a ≤ 5 and -3 ≤ b ≤ 5. When inputting variable a = 3 and b =3, the pa will be 1. Otherwise, if pa is 0, the constraint eq. (1) and (2) will not be satisfied for input variable a = 3 and b =3. Am I correct? Thanks, again. – Simon Nov 28 '21 at 18:05
  • 1
    Yes, if $a=3$, constraint (1) forces $p_a=1$, and if $b=3$, constraint (2) forces $p_b=1$, so then constraint (5) forces $c=1$. – RobPratt Nov 28 '21 at 18:14
  • Thanks for your reply. Another thing is that if a=3 and b =-3, constraint eq. (3) to eq. (5) will be c ≤ 1, c ≤ 0, c ≥ 0, respectively. In the conditions, I agree c will be 0 because of eq. (4) and (5). How to explain the eq. (3) c ≤ 1? It also has a possibility that c could be 1. – Simon Nov 28 '21 at 18:33
  • 1
    If $p_a=1$, constraint (3) is redundant. Constraints (3)-(5) are the standard linearization of a product of binary variables: https://or.stackexchange.com/questions/37/how-to-linearize-the-product-of-two-binary-variables – RobPratt Nov 28 '21 at 18:36
  • Thanks for your reply. I was wondering if I input the constraint (3)-(5) in the software like Lingo, because the eq. (3) may be redundant, it doesn't influence the results? I was wondering am I correct? Thanks, again. – Simon Nov 28 '21 at 19:56
  • Constraint (3) is not always redundant. Without it, you could get $p_a=0$ and $c=1$, which violates your desired behavior. – RobPratt Nov 28 '21 at 20:07
  • I mean if a = 3 and b = -3, the constraint (3) will be redundant. In the case, can software still run? – Simon Nov 28 '21 at 20:21
  • Because in my objective function, I need to input 20 samples and each sample has a and b values. Both a and b are input variable. The if-else statement will decide action. If c=1 use the action 1, otherwise c=0 use the action 2. Action 1 and 2 are the equations in my objective function. Thanks for your reply. – Simon Nov 28 '21 at 20:22
  • Hmm, I thought $a$ and $b$ are decision variables for the solver to determine. Now it sounds like they are input parameters with fixed values. Which is it? – RobPratt Nov 28 '21 at 20:27
  • Yes, they are input parameters with fixed values. – Simon Nov 28 '21 at 20:31
  • I am sorry I should write the question clearly. Do you think I open a new poster and write more clearly about my discussion with you? I love your current answers. – Simon Nov 28 '21 at 20:31
  • No, please just edit your question to clarify. – RobPratt Nov 28 '21 at 20:32
  • Thanks, RobPratt. I update my question. Hope it will be more clearly. Again, thanks a lot. – Simon Nov 28 '21 at 20:42
  • I think your equations can work. Even though a and b are random variables without fixed values. That says assuming a and b are random variables that are decided by the model. In this case, I am still trying to figure out if the model select a =3 and b =-3, would eq. (3) influence? – Simon Nov 28 '21 at 21:01