Presentation is loading. Please wait.

Presentation is loading. Please wait.

Good modeling practices AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl. Improved upon by many previous lab.

Similar presentations


Presentation on theme: "Good modeling practices AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl. Improved upon by many previous lab."— Presentation transcript:

1 Good modeling practices AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl. Improved upon by many previous lab instructors. Special thanks to Yuquan “Wolfgang” Zhang

2 The effort often determines how EASILY it is to reuse or repair a model at a later time or how EASILY a colleague can work with that code. Several actions are possible:  Use longer names or descriptions  Include comments on nature and source of data  Include as much raw data as possible  Do not use * as a set specification  Use sets to aid in readability  Format files to improve model readability Why worry and what can be done?

3 GAMS allows 31 character long names and 80 characters of explanatory text defining each of the following items: SETS, PARAMETERS, TABLES, SCALARS, VARIABLES, EQUATIONS, MODELS It is wise to make GAMS code to be self documenting by using descriptive character names and make sure that there is no item that goes undefined (e.g. all names are somewhat apparent and all items have explanatory text). Enter units, sources, and descriptions. Check for completeness with $ONSYMLIST.

4 $ONSYMLIST controls the complete listing of all symbols that have been defined and their text, including pre-defined functions and symbols, in alphabetical order grouped by symbol type. The symbol listing in the listing file. Default in GAMS is $OFFSYMLIST

5 One can affect readability by altering names of SET, PARAMETERS, etc. Using Longer Names Same algebras but are given different names

6 Questions often asked when looking at a set of data are:  Where did the data come from?  What characteristics such as units, and year of applicability do those data possess? Such questions certainly apply to a TABLE of data in GAMS code. It is nice to go beyond the GAMS 80 character description by putting several lines of description identifying exactly what document a data set is from including sources, page numbers, table number, years, units, etc. This can be done by  Using an asterisk * in the first character position  Setting off by a $ONTEXT and $OFFTEXT  Setting off by a $EOLCOM Including Comments

7 $ONTEXT $OFFTEXT Texts or statements in between $ONTEXT $OFFTEXT are ignored by GAMS but they are printed on the output file. Every $ONTEXT must have a matching $OFFTEXT in the same file.

8 End of Line Comments You authorize end of line comments with a $EOLCOM statement and a designator

9 But using end line comments without using a $EOLCOM statement will cause errors

10 Modelers often face two choices with respect to data.  Enter raw data into GAMS and transform it to the extent needed inside GAMS  Externally process data entering the final results in GAMS  (e.g. from a spreadsheet where the data are previously manipulated) Recommendation: Put data in as close to the form as it was collected into GAMS and then manipulate the data in GAMS code (e.g. using the PARAMETER command) Justification: Over time spreadsheets change or get lost. Raw vs. Calculated Data

11 Raw vs. Calculated Data (cont) Instead of directly entering the transportation cost that was previously calculated in the spreadsheet in GAMS using TABLE statement, one should enter a raw data in GAMS and then let GAMS do a calculation.

12

13  Dynamic: calculations repeated every time the model is generated. Only calculations in the model.. statements are dynamic  Static – calculations executed once only at the place the GAMS instruction appears in the code. Cautions about Calculation

14  Repeated Static: calculations within a GAMS flow control structure (e.g. loop) which are executed repeatedly but are static within the control structure.

15 The data on revenue is previously calculated using PARAMETER statement. Then this revenue is used in the OBJ.. equation Dynamic vs. Static Calculation

16 An Aside – a report writing Example To aid in our dynamic static example, build a report writer that retains the solution values and data across runs.

17 Dynamic vs. Static Calculation Example Now, the corn price increases to $2.50 per bushel. What happens to the solutions (e.g. price, revenues, acres, total revenues)? 1st run where the corn price is $2.0 2nd run where the corn price increases to $2.50.

18 Dynamic vs. Static Calculation Example Why are 2nd run solutions same as first? The total revenue was not updated when the corn price increased to $2.5. WHY?

19 To solve this problem, we should use a dynamic calculation. Using dynamic calculation, price, yield, and cost will be updated every time the model is generated

20 Dynamic vs. Static Calculation Example Is there anything wrong with the 2nd run solutions?

21 * is used in the first index position of MiscData TABLE. GAMS allows anything in that position suppressing “domain” checking. Suppose we mistyped endinv-value as endinv- valu, then GAMS code would compile and execute w/o a GAMS error but the result would be wrong. Do Not Use * as an input data Set Specification

22 Here if we replace * set with InputItem set, then GAMS would have given the error messages. Do Not Use * as an input data Set Specification

23 The solution from solving the model with mistyping on “endinv-value” as “endinv-valu” The solution from solving the model with correction on “endinv-value” Why these solutions are different? Do Not Use * as an input data Set Specification

24 Setting Up Data – make SETS work for you You need to decide when to use a single or multiple sets. Sets should contain items treated similarly in the problem (i.e. resources like fertilizer, seed, and energy) but when there are two items crossed (i.e. monthly availability of land, labor, and water involves month and resource) one should have sets. Sometimes, it is desirable to treat items simultaneously in specific places, but separately elsewhere. SubSets will allow this.

25 SET vs. SubSet Here, we create a set called ALLI that includes all elements used in the model. Then we create a series of subsets of ALLI set called Primary, Input, Landtype, Resource, and Tcost.

26 SET vs. SubSet Original SET called ALLI

27 Setting Up Data – SETS vs. SubSets

28 Improve Readability Format the code for readability using spacing and indents.  Align item names, descriptions, and definitions  Indent in sums, loops, and ifs to delineate terms  Use blank lines to set things off  Do not split variables between lines in equations, but rather keep them together with all their index positions

29 Do you prefer this? Improve Readability

30 … or this?

31 Learning Objectives (handson6.gms) : 1. Learn about organizing data 2. Learn about SubSetting Things To Do 1.Open handson5a.gms and save it as handson6.gms. Please add descriptions to all SETS, PARAMETERS, TABLES, VARIABLES, and EQUATIONS (do not change in.. commands). 2.(a)Name your ingredient set name ingredients in SET, PARAMETER, and TABLE definitions (do not change in.. commands). (b)Then, make a new set called ingredient which is a subset of the ingredients SET (make sure that this subset is used in.. commands). (c)Then enter the first and second commodities into this new subset. (d) Run the model and look at your model, what commodities are included in the model? Hands On


Download ppt "Good modeling practices AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl. Improved upon by many previous lab."

Similar presentations


Ads by Google