Good modeling practices

Top  Previous  Next

Above I have covered the essential GAMS features one would employ in any modeling exercise.  However I have not done very good job of exploiting a major GAMS capability involved self-documentation. In any modeling exercise there are an infinite variety of choices that can be made in naming the variables, equations, parameters, sets etc. and formatting their presentation in the GMS instruction file.  Across these choices that can be large differences in the degree of self-documentation within the GMS code.  In particular, as explained in the chapter on Rules for Item Names, Element names and Explanatory Text, one employs short names like x(j) as in optalgebra.gms or longer names (up to 63  characters) for the variables like production(products).  I advocate use of longer names to enhance the readability of the document.

The GAMS also permits one to add comments, for example telling what is being done by particular instructions or indicating data sources.  This can be done by a number of means including typing lines beginning with an * in column one or encasing longer comments between a $ONTEXT and $OFFTEXT.  GAMS elements for including comments are discussed in the chapter entitled Including Comments.

I illustrate the longer name and comment capability along with improved spacing and line formatting in the context of the model optalgebra.gms creating the new model goodoptalgebra.gms.  The two models use the same data and get the same answer only the item names and formatting have been changed.  In my judgment, the longer names substantially contribute to self-documentation and make it easier to go back to use a model at a future time or transfer a model to others for their use.  More material on the formatting subject appears in the Writing Models and Good Modeling Practices chapter.

Original version

(optalgebra.gms)

SET     j               /Corn,Wheat,Cotton/

        i               /Land ,Labor/;

PARAMETER

  c(j)      / corn     109    ,wheat   90 ,cotton    115/

  b(i)     /land 100 ,labor 500/;

TABLE a(i,j)

              corn    wheat  cotton

  land          1       1       1

  labor         6       4       8      ;

POSITIVE VARIABLES   x(j);

VARIABLES            PROFIT             ;

EQUATIONS            OBJective          ,  constraint(i) ;

 OBJective.. PROFIT=E=   SUM(J,(c(J))*x(J)) ;

 constraint(i).. SUM(J,a(i,J) *x(J))  =L= b(i);

MODEL RESALLOC /ALL/;

SOLVE RESALLOC USING LP MAXIMIZING PROFIT;

 

Revised version with comments in blue

(goodoptalgebra.gms)

*well formatted algebraic version of model optalgebra.gms

SET       Products  Items produced by firm

              /Corn   in acres,

               Wheat  in acres ,

               Cotton in acres/

          Resources  Resources limiting firm production

              /Land   in acres,

               Labor  in hours/;

PARAMETER Netreturns(products)  Net returns per unit produced

              /corn 109 ,wheat 90 ,cotton 115/

          Endowments(resources) Amount of each resource available

              /land 100 ,labor 500/;

TABLE     Resourceusage(resources,products) Resource usage per unit produced

                          corn    wheat  cotton

               land          1       1       1

               labor         6       4       8      ;

POSITIVE VARIABLES   Production(products) Number of units produced;

VARIABLES            Profit               Total fir summed net returns ;

EQUATIONS            ProfitAcct           Profit accounting equation ,

                     Available(Resources) Resource availability limit;

$ontext

      specify definition of profit

$offtext

 ProfitAcct..

      PROFIT

      =E= SUM(products,netreturns(products)*production(products)) ;

 

$ontext

      Limit available resources

      Fix at exogenous levels

$offtext

 available(resources)..

      SUM(products,

          resourceusage(resources,products) *production(products))

      =L= endowments(resources);

 

MODEL RESALLOC /ALL/;

SOLVE RESALLOC USING LP MAXIMIZING PROFIT;