How do I formulate logical expressions in equations?

Frequently asked questions about GAMS

Moderator: aileen

Forum rules
Please ask questions in the other sub-forums
Posts: 131
Joined: 1 year ago

How do I formulate logical expressions in equations?

Post by aileen » 2 months ago

GAMS does not reformulate logical expressions. You need to do this by hand. A common way to model logical expressions is using the so-called big-M method. The interested reader is referred to the book by H.P. Williams on Model Building in Mathematical Programming. It explains how to use the big-M method to model logical constraints.

Here is an example of the big-M method:
If you have a logical expression i=1 <=> a>b, where i is a binary variable and a and b are free variables that can take any real value. You can reformulate this as follows:

a <= b + bigM*i
a > b - bigM*(1-i)

To interpret these constraints, a good way is to consider both values of the binary variable, in this case, i. If i=1, the first constraint will not be active if bigM is big enough while the second constraint ensures a>b. If i=0, the first constraint ensures a<=b while the second will not be active if bigM is big enough. Since there are no strict "greater than" constraints in mathematical programming, you need to rewrite the second constraint as a >= b + epsilon - bigM*(1-i), where epsilon is a small number (e.g., 1e-6):

Code: Select all

constraint1.. a =l= b + bigM*i;
constraint2.. a =g= b + 1e-6 - bigM*(1-i);
Note that the value for the constant bigM can usually be calculated from the data of the problem, just making it "big" can cause numerical problems with solvers (see How large should BIG M be?).

It should be further noted that using variables in a $-condition is not allowed. The $-condition must depend on data only. Therefore, the error "Endogenous $-control operations not allowed" appears if you try to model logical expressions as follows:

Code: Select all

constraint1$(i=0).. a =l= b;
constraint2$(i=1).. a =g= b + 1e-6;
To avoid this, logical expressions are modeled with the help of big-M constraints as shown above. Alternatively, one can model logical expressions using logic equations (e.g. using the =b= notation) with GAMS model type EMP or JAMS/LOGMIP.