/
Good Good

Good - PowerPoint Presentation

celsa-spraggs
celsa-spraggs . @celsa-spraggs
Follow
367 views
Uploaded On 2016-06-01

Good - PPT Presentation

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 ID: 344719

set data model gams data set gams model calculation dynamic sets names static code price raw run comments endinv

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Good" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Good modeling practicesAGEC 641 Lab, Fall 2011Mario Andres Fernandez

Based on material written by

Gillig

and

McCarl

. Improved upon by many previous lab instructors.

Special thanks to

Yuquan

“Wolfgang” ZhangSlide2

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 possibleDo not use * as a set specificationUse sets to aid in readabilityFormat files to improve model readability

Why worry and what can be done?Slide3

GAMS allows 31 character long names and 80 characters of explanatory text defining each of the following items:SETS , PARAMETERS, TABLES, SCALARS, VARIABLES, EQUATIONS, MODELSIt 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.Slide4

$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

$OFFSYMLISTSlide5

One can affect readability by altering names of SET, PARAMETERS, etc. Using Longer Names

Same

algebras but are given different namesSlide6

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 positionSetting off by a $ONTEXT and $OFFTEXT

Setting off by a

$EOLCOM

Including CommentsSlide7

$ONTEXT $OFFTEXTTexts 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.Slide8

End of Line CommentsYou authorize end of line comments with a $EOLCOM statement and a designatorSlide9

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

Modelers often face two choices with respect to data.Enter raw data into GAMS and transform it to the extent needed inside GAMSExternally 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 DataSlide11

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.Slide12
Slide13

Dynamic: calculations repeated every time the model is generated. Only calculations in the model .. statements are dynamicStatic – calculations executed once only at the place the GAMS instruction appears in the code.

Cautions about Calculation Slide14

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

The data on revenue is previously calculated using PARAMETER statement. Then this revenue is used in the OBJ.. equation

Dynamic vs. Static

CalculationSlide16

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

Dynamic vs. Static Calculation ExampleNow, 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.Slide18

Dynamic vs. Static Calculation ExampleWhy are 2nd run solutions same as first?

The total revenue was not updated when the corn price increased to $2.5. WHY?Slide19

To solve this problem, we should use a dynamic calculation.Using dynamic calculation, price, yield, and cost will be updated every timethe model is generatedSlide20

Dynamic vs. Static Calculation ExampleIs there anything wrong with the 2nd run solutions?Slide21

* 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 SpecificationSlide22

Here if we replace * set with InputItem set,

then GAMS would have given the error messages.

Do Not Use * as an input data Set SpecificationSlide23

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 SpecificationSlide24

Setting Up Data – make SETS work for youYou 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.Slide25

SET vs. SubSetHere, 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. Slide26

SET vs. SubSetOriginal SET called ALLISlide27

Setting Up Data – SETS vs. SubSetsSlide28

Improve ReadabilityFormat the code for readability using spacing and indents.Align item names, descriptions, and definitionsIndent 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 positionsSlide29

Do you prefer this? Improve ReadabilitySlide30

… or this?Slide31

Learning Objectives (handson6.gms): 1. Learn about organizing data 2. Learn about SubSettingThings To Do1. 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). (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