Element definition by computation

Top  Previous  Next

Elements of subsets may also be computed.  This is done by using a statement such as

 

Set("nameofelement")=yes;

 

which is equivalent to including a set element named nameofelement in the set declaration.  One may also use computations to remove elements by using a statement like

 

Set("nameofelement")=no;

Examples:

(sets.gms)

 Set         nSuperset                                 /r1*r15/

         nSubset(nsuperset);

 nSubset(nsuperset)=yes;

 nSubset("r4")=no;

 

Set         nallitems                                /Corn,wheat,water,land/

      nCrop(nallitems)        ;

Parameter yield(nallitems)         /Corn 100,Wheat 40/;

nCrop(nallitems)$(yield(nallitems) gt 0)=yes;

Notes:

By default all elements in a set without definition are undefined (set to no), so one only has to identify the items present (setting them to yes).
Setting an element to no removes it from the set.
Complex conditionals can be employed in defining the sets.  Conditionals are discussed in the Conditionals chapter.
Computed sets cannot be used to define the domain of data items.  One must use the superset for the domain.
Sometimes one will compute a set and set all elements to yes then go back and selectively remove items using a command setting them to no.
More on set computations appears in the Calculating Items chapter.