Formatting the typing of files to improve model readability

Top  Previous  Next

I think there are things you can do to improve the readability as you type.  A list of such practices follows:

Enter the code in a fixed order by the following sections and keep the section together such as follows
Data related sets
Data definitions organized by type of data possibly intermixed with calculations
Variable and equation definitions
Algebraic equation definitions
Model and solve
Report writing sets and parameter definitions
Report writer calculations
Report writer displays
Keep the sections together
Format the code for readability using spacing and indents as illustrated below
Align item names, descriptions and definitions
Indent in sums, loops and ifs to delineate terms. Through indentation, and closing parentheses formatting you can reveal structure.
Use blank lines to set things off
Don't split variables between lines in equations, but rather keep them together with all their index positions.

By follow such typing practices you can make a file more readable using spacing, indents, and set labels

Case A (badtype.gms)

 

Sets  products  available production process / low uses low new materials

medium uses medium new materials, high uses high new materials/

rawmateral    source of raw materials  / scrap, new /

Quarters    long horizon   / spring, summer, fall ,winter /

quarter(Quarters) short horizon  / spring, summer, fall /

Scalar mxcapacity maximum production capacity/ 40 /;

Variables  production(products,Quarters)  production and sales

openstock(rawmateral,Quarters)  opening stocks, profit ;

Positive variables production, openstock;

Equations  capacity(quarter)     capacity constraint,         

profitacct.. profit =e= sum(quarter, sum(products, expectprof(

products,quarter) *production(products,quarter))-sum(

rawmateral,miscdata("store-cost",rawmateral)*openstock(rawmateral

  ,quarter)))+ sum(rawmateral, miscdata("endinv-value",rawmateral)  *openstock(rawmateral,"winter"));

 

Case B (better.gms)

 

Sets  products      available production process

                   / low uses a low amount of new materials,

                     medium uses a medium amount of new materials,

                     high uses a high amount of new materials/

       rawmateral    source of raw materials  / scrap, new /

       Quarters    long horizon   / spring, summer, fall ,winter /

       quarter(Quarters) short horizon  / spring, summer, fall /

 Variables  production(products,Quarters)  production and sales

            openstock(rawmateral,Quarters)  opening stocks

            profit ;

 Positive variables production, openstock;

 Equations  capacity(quarter)     capacity constarint

            stockbalan(rawmateral,Quarters) stock balance

            profitacct              profit definition ;

 profitacct.. 

    profit =e=   

    sum(quarter,

       sum(products, expectprof(products,quarter)

                    *production(products,quarter)

           )

      -sum(rawmateral, miscdata("store-cost",rawmateral)*

                       openstock(rawmateral,quarter)

           )

       )

   +sum(rawmateral, miscdata("endinv-value",rawmateral)

                   *openstock(rawmateral,"winter")

       )

  ;

 

You may like case A, I don't.  I find B much more readable.  This matters when you have 1000 lines or more. (asmmodel.gms)