Friday, June 18, 2010

NA in GAMS

Q: Does the value of A make sense; why is NA in there? There is no NA in B

set i /i1*i10/;
parameter A(i) /i1=1, i2=NA, i3=1e-10/;
display A;
set tiny(i);

tiny(i)$[  A(i)
and
          (Abs(A(i)) < 1e-5)
and
          (A(i) <>
NA)
       ] =
yes;
display tiny;

parameter B(i);
B(i)$(A(i) <>
NA) = A(i);
display B;

The purpose is to find the small values and store their index in set tiny. The result is:

----      5 PARAMETER A 

i1       1.000,    i2          NA,    i3 1.00000E-10

----     12 SET tiny 

i2,    i3

----     16 PARAMETER B 

i1       1.000,    i3 1.00000E-10

We see element i2 is present, although we included A(i)<>NA as condition. This is not completely intuitive, but for element i2, with A(‘i2’)=NA, the condition reads as:

tiny(‘i2’)$(NA and NA and NO) =  yes;

Now the rule is that any operation on NA (including ‘and’) results in NA. So the whole condition becomes:

NA and NA and NO => NA

As $NA evaluates to $1, we actually get that element tiny(i) set to YES.

Working with special values requires special attention, especially in complex expressions. Usually it is best to split this in parts:

set tiny2(i);
tiny2(i)$[A(i)
and (Abs(A(i)) < 1e-5)] = yes;
tiny2(i)$(A(i)=
NA) = no;
display tiny2;

Now this set will only contain the element i3.