Computation over a set

Top  Previous  Next

One may wish to do conditional processing dependent upon whether the numerical value of a sum, prod, smax or smin over a set is nonzero or not.  The form of the syntax that can be employed in such a case is

 

Action$Sum(set,expression)

or

If(Smin(set,expression), statements);

or        

While(Smax(set,expression), statements);

Examples:

(dataconditional.gms)

Z=z+2$sum(I$q(i),1);

If(smin(I,q(i)), z=2);

Eq5$prod(I,q(i))..  zz=e=3;

Eq6(j)$sum(I,abs(data(I,j)))..  zz=e=3;

Loop(I$sum(j,abs(data(I,j))),z=z+1)

While(prod(I,q(i)), z=2;q(i)=q(i)-2);

Notes:

In these cases the term subject to the conditional will be evaluated or the calculation will be executed if the final result of the set dependent expression is nonzero.
This all could be expressed as $(Term ne 0) but the syntax $(Term) is often utilized because it is more compact.
The conditional as in the expression sum(I$q(i),1) reveals a commonly used GAMS trick for seeing whether there is any nonzero data in an item (by counting it where the count will be zero when all the data in the vector are zero).
Including the conditional expression $sum(j,abs(data(I,j)) is a common GAMS trick for seeing whether there is any nonzero data across all the j alternatives in a row I of data(I,j).