If/then logic with variables

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:

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:

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:

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,

Hi,
Basically your approaches all try to state that

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: https://newforum.gams.com/t/variable-in-conditional-statement/2690/2).

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):

postive variables penPos, penNeg;
binaryVariable isPos;
pen = penPos - penNeg
penPos <= pen.up*isPos
penNeg <= -pen.lo*(1-isPos)

I hope this helps!

Best,
Fred