Mixing logical expressions, sets and numbers

Top  Previous  Next

One can mix logical expressions and sets with numerical calculations.  Namely, if a logical item is included in a numerical calculation, it takes on a value of one if the result is true and zero if it is false.  Similarly a set is treated as a logical item and returns a 1 if an element is defined and zero otherwise.  Items that are Sets can also be equated to numbers and get a yes value if the item is nonzero and no otherwise.  The following example illustrates (mixlogical.gms)

 

X=1;

z = 100*(x < 4) + ( 3 < 3);

set j(i);

j(i)=x+1;

parameter zz(i);

zz(i)=j(i)*100;

 

Note z evaluates to 100 since only the logical condition on the left is true.  Note that this is different from the assignment below,

 

z = (x < 4) or ( 3 < 3);

 

where z evaluates to 1 due to the or operator behaving as explained in the conditional notes.