|
Use longer names and descriptions |
Top Previous Next |
|
One can radically affect the readability of a piece of GAMS code by using longer self explanatory names of parameters, sets etc. in that code. To illustrate the difference this might make let us look at the example robert.gms from the GAMS model library. A part of that model is reproduced below
Variables x(p,tt) production and sales s(r,tt) opening stocks profit Positive variables x, s; Equations cc(t) capacity constarint sb(r,tt) stock balance pd profit definition ; cc(t).. sum(p, x(p,t)) =l= m; sb(r,tt+1).. s(r,tt+1) =e= s(r,tt) - sum(p, a(r,p)*x(p,tt)); pd.. profit =e= sum(t, sum(p, c(p,t)*x(p,t)) - sum(r, misc("storagec",r)*s(r,t))) + sum(r, misc("resvalue",r)*s(r,"4")); s.up(r,"1") = misc("max-stock",r);
After an hour of so of looking at the model I reformatted it as follows (good.gms)
Variables production(process,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 ; capacity(quarter).. sum(process, production(process,quarter)) =l= mxcapacity; stockbalan(rawmateral,Quarters+1).. openstock(rawmateral,Quarters+1) =e= openstock(rawmateral,Quarters) - sum(process, usage(rawmateral,process) *production(process,Quarters)); profitacct.. profit =e= sum(quarter, sum(process, expectprof(process,quarter) *production(process,quarter)) - sum(rawmateral, miscdata("store-cost",rawmateral)* openstock(rawmateral,quarter))) + sum(rawmateral, miscdata("endinv-value",rawmateral) *openstock(rawmateral,"winter")); openstock.up(rawmateral,"spring") = miscdata("max-stock",rawmateral);
Note these two models do the same thing but different longer names are used in the second. Also note that for example instead of using the set name tt I use Quarters and instead of calling the variables x I call it production. The question is which tells you more and which would you want to face in 5 years? Lets look at some more from robert.gms. Another segment of code is
Sets p products / low, medium, high / r raw materials / scrap, new / tt long horizon / 1*4 / t(tt) short horizon / 1*3 / Table a(r,p) input coefficients low medium high scrap 5 3 1 new 1 2 3 Table c(p,t) expected profits 1 2 3 low 25 20 10 medium 50 50 50 high 75 80 100
This can be reformatted to become (good.gms)
Sets process production processes available / 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 / Table usage(rawmateral,process) input coefficients low medium high scrap 5 3 1 new 1 2 3 Table expectprof(process,quarters) expected profits spring summer fall low 25 20 10 medium 50 50 50 high 75 80 100
Here I use longer names for the set elements, the sets themselves along with explanatory text documenting the definition of the set elements. So again the question is which one makes more sense to you? I obviously feel the latter. |