Finding sets from data

Top  Previous  Next

Sometimes it is desirable to find the set that characterizes an item then use it from then on.  One may accomplish this by using an alias with the universal set and then compute set elements based on data using conditionals.

Examples:

Consider the example trnsprt.gms where this is done using several steps.  First I define sets without specifying elements (sources and places here) as equivalent to universal (unspecified) set.

 

alias(sources,places,*)

 

Then I enter data which contains an indicator of which set elements are valid entries in the set to be computed where in this case to be associated with the set sources the named place must have totalsupply.

 

table trandata (sources,places) data from spreadsheet

                     newyork     chicago    totalsupply

      seattle          2.5          1.7         350

      Sandiego         2.5          1.8         300

      totalneed        325           75

 

Now in preparation of set calculation I define subsets for the sets I will compute.  These sets will be set to yes based on the data.

 

set source(sources)    sources in spreadsheet data

    destinaton(places) destinations in spreadsheet data;

 

Then I compute the set elements based on the data.  In this case a source is defined (set to yes) if that location has an entry for totalsupply and a destination is defined if that place has an entry for totalneed.

 

source(sources)$(trandata(sources,"totalsupply"))=yes;

destinaton(places)$(trandata("totalneed", places ))=yes;

 

These sets can be used from then on.

 

Such computations are useful if a report has been specified with indefinite elements but needs to be manipulated or if one gets in a data table from elsewhere which defines the problem dimensions (set elements).