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.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
The Web Warrior Guide to Web Design Technologies
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
GAMSCHK and GAMS Documentation AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl; Improved upon by many previous.
Conditionals, Subsets and Tuples in GAMS AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by McCarl and Elbakidze; Improved upon.
Comparative Analysis Multiple Submissions vs. Loops AGEC 641 Lab, Fall 2010 Mario Andres Fernandez Based on material written by Gillig and McCarl; Improved.
GAMS/GAMSIDE AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl and improved upon by previous lab instructors.
Formulation of a General Problem AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl; Improved upon by many previous.
Examining a model for Flaws AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl. Improved upon by many previous.
Power of GAMS AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by Gillig and McCarl. Improved upon by many previous lab instructors.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Understanding the Mainline Logical Flow Through a Program (continued)
Guide To UNIX Using Linux Third Edition
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Slide 6a-1 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns.
Microsoft Excel How to make a SPREADSHEET. Microsoft Excel IT is recommended that you have EXCEL running at the same time. You can try what you are reading.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
Introduction to GAMS: Good modeling practices Prof. Boyan Bonev Ivanov, Ph.D. Institute of Chemical Engineering-BAS.
Spreadsheet-Based Decision Support Systems Chapter 22:
Microsoft Office 2003: Advanced 1 ADVANCED MICROSOFT ACCESS Lesson 17 – Utilizing Advanced Management Tools.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
General Programming Introduction to Computing Science and Programming I.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
CS 122 Engineering Computation Lab Lab 2 Dan De Sousa and Tim Cheeseman Department of Computer Science Drexel University April 2009 ©By the author. All.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Problem Solving using the Science of Computing MSE 2400 EaLiCaRA Spring 2015 Dr. Tom Way.
30/10/ Iteration Loops Do While (condition is true) … Loop.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
Shell Scripting – Putting it All Together. Agenda Escaping Characters Wildcards Redirecting Output Chaining and Conditional Chaining Unnamed and Named.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
Overview Excel is a spreadsheet, a grid made from columns and rows. It is a software program that can make number manipulation easy and somewhat painless.
The for loop.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
13-1 ANSYS, Inc. Proprietary © 2009 ANSYS, Inc. All rights reserved. April 28, 2009 Inventory # Chapter 13 Solver.out File and CCL Introduction to.
GoldSim Monthly Webinar Series. Goals  Learn the basics of simple scripts  Learn the basics of the GoldSim Script Element  Not a lesson on numerical.
Lecture 5 Good modeling Chengcheng Fei 2017 Fall
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Lecture 4 Power of GAMS Chengcheng Fei 2017 Fall
An Introduction to Programming and VB.NET
Introduction To Flowcharting
Intro to PHP & Variables
CS190/295 Programming in Python for Life Sciences: Lecture 1
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lecture 6 Conditionals, Subsets and Tuples in GAMS
Faculty of Computer Science & Information System
Algorithm and Ambiguity
Lecture 10 Comparative analysis
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Variables in C Topics Naming Variables Declaring Variables
Lecture 2 General Problem
Presentation transcript:

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

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?

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.

$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

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

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

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

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

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

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.

 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

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

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

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 calculation, price, yield, and cost will be updated every time the model is generated

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

* 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

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 “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

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.

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

Do you prefer this? Improve Readability

… or this?

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