Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If-Then-Else with multiple assignment in Integer Linear Programming (ILP)

I have an ILP problem in which I expressed some constraint to implement A OR B, where A and B are results of logical AND (let's say that A = A1 AND A2, B = B1 AND B2 AND B3). At this point of my problem, one between A and B is said to be equal to 1. Both A and B are binary variables.

I want to express, with If-Then-Else, this assertion:

if (A == true)
   /* Choose one between C and D */
   C_OR_D >= C;
   C_OR_D >= D;
   C_OR_D <= C + D;
   C_OR_D = 1; 
else                                     /* or if (B == true), that's the same */
   /* Choose one between E and F */
   E_OR_F >= E;
   E_OR_F >= F;
   E_OR_F <= E + F;
   E_OR_F = 1; 

I know how to write simple If-Condition, like

/* if (x == true) then y = true */
y >= x;

but I don't know how to write a set of constraints in order to express a "complex" if.

Does someone of you know how to resolve this in LPSolve?

like image 709
dash991 Avatar asked Jan 28 '26 18:01

dash991


1 Answers

To answer the first part:

The AND can be modeled as

A >= A1 + A2 -1
A <= A1
A <= A2

The OR can be modeled as

B >= B1
B >= B2
B <= B1 + B2

To model the if-else constraint you can use the "big M" technique .

Let M be a sufficiently large number.

You can "disable" an inequality by adding M to one side of it. Then the inequality holds regardless of the variable values.

For instance

if (A == 1)
   C_OR_D >= C;
else
   E_OR_F >= E;

can be modeled as

(1-A) * M + C_OR_D >= C
A * M + E_OR_F >= E
like image 194
Christian Avatar answered Feb 01 '26 11:02

Christian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!