Page 1 of 1

If/then logic with variables

Posted: Fri Nov 22, 2019 12:22 pm
by Laurus
I'm modelling a problem on GAMS (interfaced with Matlab) and I'm trying to sort some data before returning the values to Matlab in order to lower the amount of variables transferred back through MATOUT (which increase processing time).

pen, P_linha, sumPen and sumPen2 are variables; lim_linhas is a parameter;

This is basically what I'm trying to do:

Code: Select all

penCalc(l,t,ind)..            pen(l,t,ind) =e= (P_linha(l,t,ind) - lim_linhas(l))*10000;
penCalc2(l,t,ind)..           IF (pen(l,t,int) =l= 0) THEN pen(l,t,int) = 0;
penCalc3(l,ind)..             sum(t, pen(l,t,ind)) =e= sumPen(l,ind);
penCalc4(ind)..               sum(l, sumPen(l,ind)) =e= sumPen2(ind);
I've tried doing this:

Code: Select all

penCalc(l,t,ind)..            pen(l,t,ind) =e= (P_linha(l,t,ind) - lim_linhas(l))*10000;
penCalc2(l,t,ind)..           pen(l,t,ind) $ (pen(l,t,int) =l= 0) = 0;
penCalc3(l,ind)..             sum(t, pen(l,t,ind)) =e= sumPen(l,ind);
penCalc4(ind)..               sum(l, sumPen(l,ind)) =e= sumPen2(ind);
and this:

Code: Select all

penCalc(l,t,ind)$(P_linha(l,t,ind) - lim_linhas(l) > 0)..      pen(l,t,ind) =e= (P_linha(l,t,ind) - lim_linhas(l))*10000;
penCalc2(l,t,ind)$(P_linha(l,t,ind) - lim_linhas(l) < 0)....   pen(l,t,int) =e= 0;
penCalc3(l,ind)..             sum(t, pen(l,t,ind)) =e= sumPen(l,ind);
penCalc4(ind)..               sum(l, sumPen(l,ind)) =e= sumPen2(ind);
I believe these don't work because $ if verified at model generation and not passed on to the solver. Also, this data does not influence the optimization but is a consequence of it.

Any feedback is appreciated,

Re: If/then logic with variables

Posted: Fri Nov 29, 2019 2:35 pm
by Fred
Hi,
Basically your approaches all try to state that

Code: Select all

if pen<0 then pen=0
. That doesn't work.This has not so much to do with GAMS but is rather a general question on how to model logical conditions (see for example this thread: viewtopic.php?f=2&t=10931&p=25589&hilit ... ion#p25589).

I think what you want is to split variable pen into a positive (or non-negative) and a negative part and then use only the positive part in certain equations. One way to do this is with the help of an additional binary variable that indicates whether pen>=0 or not.

Here is some pseudo code (assuming pen.up>0 and pen.lo<0):

Code: Select all

postive variables penPos, penNeg;
binaryVariable isPos;
pen = penPos - penNeg
penPos <= pen.up*isPos
penNeg <= -pen.lo*(1-isPos)
I hope this helps!

Best,
Fred