Using different equations based on the value of a function

Frequently asked questions about GAMS

Moderator: aileen

Forum rules
Please ask questions in the other sub-forums
Locked
aileen
User
User
Posts: 136
Joined: 3 years ago

Using different equations based on the value of a function

Post by aileen »

How to arrive at a system in which another equation is enforced based on the value of a function?
aileen
User
User
Posts: 136
Joined: 3 years ago

Re: Using different equations based on the value of a function

Post by aileen »

To be fairly general, use the following statement:

Code: Select all

z = f(x) when y > 0
z = g(x) when y < 0
Note that the distinction between >= and just > is not meaningful for a numerical algorithm on a finite precision machine.

Start by writing, in GAMS,

Code: Select all

Variable fs, gs;
z =E= (f(x) - fs) + (g(x) - gs);
So, when y > 0, we want fs = 0 and gs = g(x). When y < 0, we want fs = f(x) and gs = 0;

Now declare a binary variable, b, that will be 1 when y > 0 and 0 and y < 0.

Code: Select all

Binary Variable b;
Positive Variable yp, yn;
y =E= yp - yn;

yp =L= ymax * b;
yn =L= ymax * (1 - b);
Next, split the two terms of z into positive and negative parts.

Code: Select all

Positive Variable fp, fn, gp, gn;
f(x) - fs =E= fp - fn;
g(x) - gs =E= gp - gn;

fp + fn =L= fmax * b;
gp + gn =L= gmax * (1 - b);
So, b = 0fs = f(x) and b = 1gs = g(x).

Finally split just the f and g slacks fs and gs into positive and negative components.

Code: Select all

Positive Variable fsp, fsn, gsp, gsn;
fs =E= fsp - fsn;
gs =E= gsp - gsn;
fsp + fsn =L= fmax* (1 - b);
gsp + gsn =L= gmax * b;
So, b = 0gs = 0 and b = 1fs = 0.

Taken together, the last two sections give b = 0z = g(x) and b = 1z = f(x).
Locked