Top Banner
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
31

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.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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.

Good modeling practicesAGEC 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

Page 2: 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.

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?

Page 3: 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.

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.

Page 4: 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.

$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

Page 5: 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.

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

Using Longer Names

Same algebras but are given different names

Page 6: 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.

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

Page 7: 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.

$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.

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

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.

Page 12: 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.
Page 13: 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.

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

Page 14: 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.

Repeated Static: calculations within a GAMS flow control

structure (e.g. loop) which are executed repeatedly but are

static within the control structure.

Page 15: 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.

The data on revenue is previously calculated using PARAMETER statement.

Then this revenue is used in the OBJ.. equation

Dynamic vs. Static Calculation

Page 16: 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.

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.

Page 17: 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.

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.

Page 18: 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.

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?

Page 19: 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.

To solve this problem, we should use a dynamic calculation.

Using dynamic calculation, price, yield, and cost will be updated every timethe model is generated

Page 20: 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.

Dynamic vs. Static Calculation Example

Is there anything wrong with the 2nd run solutions?

Page 21: 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.

* 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

Page 22: 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.

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

Page 23: 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.

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

Page 24: 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.

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.

Page 25: 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.

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.

Page 26: 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.

SET vs. SubSet

Original SET called ALLI

Page 27: 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.

Setting Up Data – SETS vs. SubSets

Page 28: 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.

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

Page 29: 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.

Do you prefer this?

Improve Readability

Page 30: 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.

… or this?

Page 31: 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.

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