Introduction to GAMS by McCarl & Gillig

Download Report

Transcript Introduction to GAMS by McCarl & Gillig

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
Why worry and what can be done?
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
GAMS allows 31 character long names and 80 characters of
explanatory text defining each of the following items:
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
$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
Using Longer Names
One can affect readability by altering names of SET, PARAMETERS, etc.
Same algebras but are given different names
Including Comments
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
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
Texts or statements
in between
are ignored by
GAMS but they are
printed on the
output file.
must have a
matching $OFFTEXT
in the same file.
End of Line Comments
You authorize end of line comments with a $EOLCOM statement and a designator
But using end line comments without using a $EOLCOM
statement will cause errors
Raw vs. Calculated Data
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
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 (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.
Cautions about Calculation
Dynamic: calculations repeated every time the model is
generated. Only calculations in the model .. statements are
Static – calculations executed once only at the place the
GAMS instruction appears in the code.
Repeated Static: calculations within a GAMS flow control
structure (e.g. loop) which are executed repeatedly but are
static within the control structure.
Dynamic vs. Static Calculation
The data on
revenue is
Then this
revenue is used
in the OBJ..
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.
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.
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?
To solve this problem, we should use a dynamic calculation.
Using dynamic
price, yield,
and cost will
be updated
every time
the model is
Dynamic vs. Static Calculation Example
Is there anything wrong with the 2nd run solutions?
Do Not Use * as an input data Set Specification
* 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 endinvvalu, 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
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
The solution from
solving the model
with mistyping on
“endinv-value” as
The solution from
solving the model
with correction on
Why these solutions are different?
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
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.
SET vs. SubSet
Original SET called ALLI
Setting Up Data – SETS vs. SubSets
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
Improve Readability
Do you prefer this?
… or this?
Hands On
Learning Objectives (handson6.gms):
1. Learn about organizing data
2. Learn about SubSetting
Things To Do
Open handson5a.gms and save it as handson6.gms. Please add descriptions to all
(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
commodities into this new
Run the model and look at your model, what commodities are
included in the model?