Calculation statements

Top  Previous  Next

A calculation of a parameter that is defined over a large of number of sets such as the following can inadvertently cover a huge number of cases

 

X(A, B, C, D, E) = 5

 

If each set had 20 members then the calculation would have to cover 3.2 million cases and would take a long time. This can be helped by narrowing attention only to good cases employing conditionals.

 

X(A, B, C, D, E) $ GOODCASE (A, B, C, D, E) = 5

 

where the user must somehow define the good case item, perhaps as a tuple.

Example:

(landcal.gms versus landexam.gms)

Suppose I have states and counties in my data set and wish to sum.  In the absence of other information GAMS would consider all counties as possibly being in all states.  Thus, New Jersey counties are considered in doing sums involving Texas. Consider 2 cases of the same calculation

In landcal.gms every county is treated as if it is in every state

 

  12  landarea(state,county,landtype)=1;

  13  parameter totalland(state);

  14  totalland(state)=

  15        sum(county,sum(landtype,landarea(state,county,landtype)));

 

which generates the profile lines

 

----     12 ASSIGNMENT landarea      0.811     0.811 SECS   39.8 Mb  1500000

----     14 ASSIGNMENT totalland     0.731     1.542 SECS   39.8 Mb     50

 

In landexam.gms a county is treated in a state only when the county is matched up with the state in the matchup array

 

  16  landarea(state,county,landtype)$matchup(state,county)=1;

  17  parameter totalland(state);

  18  totalland(state)=

  19        sum(matchup(state,county),sum(landtype,landarea(state,county,landtype)));

 

The resultant profile is

 

----     16 ASSIGNMENT landarea      0.020     0.380 SECS    2.1 Mb  30490

----     18 ASSIGNMENT totalland     0.010     0.390 SECS    2.1 Mb     50

 

and the second version treats a lot less cases, executes in executes in about 1/4 the total time and greatly reduces memory use.  In general, speed can often be helped by using tuples, subsets or conditionals to reduce attention in calculations only to relevant cases.