0% found this document useful (0 votes)
22 views72 pages

Lecture 1

Introducción a Gams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views72 pages

Lecture 1

Introducción a Gams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Applied Economic Policy using GAMS

Carl von Ossietzky Universität Oldenburg, Department of Economics,


Chair of Economic Policy

Lecture 1

1/72
Agenda

1. Motivation

2. This week’s agenda

3. First steps

4. Basics GAMS Studio

5. Scalar vs. Vector Format

6. Homework 1

2/72
What is GAMS?

G General All kinds of imaginable problems...


A Algebraic that contain (in)equational relations...
M Modeling can be put in a model...
S System and be ‘solved’.

▶ GAMS is a modeling language / system.


▶ GAMS focuses on optimization problems.

▶ You might know SPSS, STATA. Both are powerful solving


environments for statistics.
▶ GAMS is a powerful tool for "theory with numbers"!

3/72
Theory with numbers?

Traditionally in economics:
▶ Theory & Empirics
▶ Theory "lacks" quantification of proven causalities (only
qualitative messages).
▶ Empirics needs large amounts of (accessible) data, data
generation also needs time, ex post view.

For (informed) real-time policy making we need information about


qualitative theoretical relationships and at the same time some
kind of quantification to evaluate the strength of the relationships
found.
GAMS is a professional – though accessible – environment to
enhance (your) messages from economic theory with some
numbers!
4/72
Areas of application

Applied General Equilibrium Agricultural Economics


Economic Development Chemical Engineering
Energy (Economics) Econometrics
Environmental Economics Engineering
Finance Forestry
International Trade Logistics
Macro/Micro Economics Mathematics
Management Science, OR Military, Physics

5/72
Scientific software comparison chart

6/72
Moving to GAMS

▶ Mathematical models in Excel are easily accessible, but must


be highly simplified to be tractable.
▶ Using a higher level programming language enables us to
include more economic structure and behavior.
▶ You can build your own (economic) models in a natural,
logical structure (aligned to »handwritten« mathematical
expressions) using compact algebraic statements.

GAMS...
▶ automates the process of going from a mathematical
statement of the problem to the solution.
▶ transforms the mathematical representation to representations
required by specific solver engines.
▶ models and solves complex linear, nonlinear and integer
programming problems.
7/72
Optimization via freshman’s calculus

▶ Express the objective function to be minimized or maximized


in terms of one independent variable.
▶ Differentiate with respect to this variable.
▶ Set the derivative equal to zero.
▶ Solve for the independent variable.
▶ If in doubt as to whether it’s a max, min or saddle point, take
second derivative and evaluate the sign.
▶ If the independent variable is restricted to an interval of the
real line, check the endpoints – the optimal solution could be
there.

8/72
Moving to GAMS

Freshman calculus:
▶ One (or few) variable(s).
▶ (Non-)linear objective function.
▶ Sometimes the variable(s) are constrained to an interval, or
depend on equality side-constraints.

Graduate economics research:


▶ Thousands (millions) of variables.
▶ Some objective function.
▶ Some constraints (e.g., inequality side-constraints).

9/72
Advantages of GAMS

1. Similar to mathematical notation.


2. Rather easy to learn, only few basic language elements.
3. Model is an algebraic executable description of the problem.

10/72
Some data sources

▶ National statistical offices (www.destatis.de)


▶ Federal Ministries (www.bmwi.de)
▶ Arbeitsgruppe Energiebilanzen (www.ag-energiebilanzen.de)
▶ LAK-Energiebilanzen (www.lak-energiebilanzen.de/)
▶ International Energy Agency (IEA) (www.iea.org)
▶ BP Statistical Review of World Energy (www.bp.com/)
▶ Energy Information Administration (www.eia.gov)
▶ US Department of Energy (www.energy.gov)
▶ Eurostat (www.ec.europa.eu/eurostat) etc.
▶ INKAR - Indikatoren und Karten zur Raum- und
Stadtentwicklung (www.inkar.de/)
▶ etc.

11/72
2. This week’s agenda

Monday:
▶ Motivation & Background GAMS
▶ First steps & Basics GAMS Studio
Tuesday:
▶ Solution homework (& recap)
▶ Fundamentals in linear programming (LP)
Wednesday:
▶ Solution homework (& recap)
▶ GAMS & Excel Interface
Thursday:
▶ Solution homework (& recap)
▶ Open session (questions, further exercises)
▶ Outlook: research possibilities & course design
12/72
3. First steps: GAMS syntax, basics and a 1st example

A baker has 150 kilograms of flour, 22 kilograms of sugar and 27.5


kilograms of butter with which to make two types of cake. A
dozen A cakes requires 3 kg of flour, 1 kg of sugar and 1 kg of
butter. A dozen B cakes requires 6 kg of flour, 0.5 kg of sugar and
1 kg of butter. The profit from a dozen A cakes is 20 and from
one dozen B cakes is 30.
▶ How many dozen A cakes (x1 ) and B cakes (x2 ) will maximize
the baker’s profit?

13/72
Cake production

max z = 20x1 + 30x2 s.t.

3x1 + 6x2 ≤ 150 (flour constraint)


x1 + 0.5x2 ≤ 22 (sugar constraint)
x1 + x2 ≤ 27.5 (butter constraint)
x1 , x2 ≥ 0 (non-negativity)

14/72
Structure of a GAMS model

15/72
Input file

▶ model definition and solve statement


▶ model definition:
▶ What is in the model?
▶ (indices): sets
▶ (data): parameters, tables, input files
▶ What are you looking for? variables
▶ Relationships with constraint choice? equations
▶ Which equations go in which model? model statement
▶ Invoke the solution algorithm: solve statement

16/72
Structure of a GAMS model

17/72
Cake production model

18/72
Variables

▶ these define what the model needs to find


▶ each variable must be declared

19/72
Variable types

20/72
Variable attributes

21/72
Equations

▶ main purpose of equations is to establish relationships


between variables and parameters
▶ an equation must be declared and defined before it can be
added to a model

22/72
Quick Note: Declaration vs. Definition

▶ an entity of the model cannot be referenced before it is


declared to exist
▶ declaration: declaring the existence of something and giving it
a unique name (identifier)
▶ definition/ Assignment: giving something a specific value or
form, e.g. a set (list of labels) or parameter (numeric values)
▶ equations: you must make the declaration and definition in
separate GAMS statements
▶ all other GAMS entities: you can make declarations and
assignments in the same statement or separately

23/72
Equation types

24/72
Quick Note: " = " vs. " =E= "

The equal sign (=):


▶ a procedure operator used only in assignments
▶ gives a specified value to a parameter or other data item
▶ executed in sequence as part of the procedural elements of a
GAMS program
▶ can involve variable attributes (e.g., .L (level) or .M
(marginal)), but cannot reference variables without attributes

The equality symbol (=E=):


▶ a declarative symbol using only in equation definitions
▶ only executed with a solve statement
▶ must contain at least one variable

25/72
Model statement

▶ a model is a collection of equations (and the variables


referenced in those)
▶ the model statement lists the equations to include in a model
with a specific name
▶ the keyword " all" can be used to include all previously
declared equations
▶ there may be multiple models in a single GAMS program, and
a single equation may appear in more than one model

26/72
Solve statement

▶ once a model has been declared and assigned equations, we


are ready to call the solver with the solve statement
▶ the variable to be optimized must be a scalar and it must be
free (i.e., unbounded)

▶ The solve statement instructs GAMS to populate a model


with data, passes it on solver and loads the resulting level
values and marginal values in the appropriate locations.
27/72
Model types / solution procedures

28/72
Cake production model

29/72
Graphical representation

feasible – A point
(x1 ; x2 ) is feasible if
it satisfies the con-
straints.
feasible region – Set
of all points which
are feasible.

30/72
Geometric solution

The optimal
solution is x1 = 5
and x2 = 22.5.
The baker’s profit is
20x1 + 30x2 = 775.

31/72
GAMS solution & listing file

32/72
Structure listing file

▶ Echo-print: shows the model syntax again (echo or copy of


input file for the sake of future reference)
▶ Equation Listing: shows the specific instance of the model
that is created when the current values of the sets and
parameters are plugged into the general algebraic form,
default output: max. three specific equations for each generic
equation
▶ Column Listing: analogous to the equation listing, shows the
range of values and the coefficients of three specific variables
for each generic variable
▶ Model Statistics: number of generic equations, variables and
non-zero elements
▶ Solution Report: Solve Summary (resource usage: time
needed; iteration count: iterations needed to solve), SolEqu,
SolVar
33/72
SolEqu

▶ Indication of the LOWER and UPPER bounds of the


equations
▶ LEVEL: value of the left side of the equation
▶ MARGINAL: simplex multipliers of the restriction
▶ How does the objective value change with a marginal change
of the constants?

34/72
SolVar

▶ LOWER and UPPER constraints of the variable


▶ LEVEL: value of the variable in the solution (optimal value)
▶ MARGINAL: how the objective value changes when the
variable is increased by one marginal unit

35/72
File types

36/72
Further GAMS Syntax: Sets

▶ corresponding to the indices in the algebraic representations of


models
▶ GAMS uses slashes ’/’ rather than curly braces ’{}’ to
delineate the set (since not all keyboards have keys for curly
braces)

▶ in cases when the elements follow a sequence, use the


asterisk, e. g.

37/72
Further GAMS Syntax: Data

▶ can be included in the .gms file or integrated from external


sources
▶ three different formats for entering data in the .gms input file:
▶ Parameters / Lists
▶ Tables
▶ Direct assignments
▶ external sources:
▶ .gdx files
▶ .csv files
▶ .txt files
▶ ...
=⇒ we will look at that on Wednesday

38/72
Further GAMS Syntax: Parameters

▶ declares the existence of parameters, gives them their names


(e. g. a and b), and declares their domains if necessary (e. g.
i and j)
▶ a domain is the set, or tuple of sets, over which a parameter,
variable, or equation is defined
▶ scalar = parameter that has no domain
▶ zero is the default value for all parameters (you only need to
include the nonzero entries in the element-value list)

39/72
Further GAMS Syntax: Tables

▶ example for a two-dimensional table:

=⇒ declares the parameter d, specifies its domain as the set of


ordered pairs in the Cartesian product of i and j and states the
values of d under the appropriate heading
▶ if there are blank entries in the table, they are interpreted as
zeroes

40/72
Further GAMS Syntax: Direct Assignment

▶ divides the tasks of parameter declaration and parameter


assignment between separate statements

=⇒ 1st statement declares the parameter c, to specify the domain


(i,j), and to provide some documentary text
=⇒ 2nd statement assigns to c(i,j) the product of the values of
the parameters f and d(i,j)
▶ you can also assign a single value to a specific
multidimensional parameter:

41/72
Summary

▶ model definition and solve statement


▶ model definition:
▶ What is in the model?
▶ (indices): sets
▶ (data): parameters, tables, input files
▶ What are you looking for? variables
▶ Relationships with constraint choice? equations
▶ Which equations go in which model? model statement
▶ Invoke the solution algorithm: solve statement

42/72
Further indications

▶ always end all statements with a semicolon to avoid errors


▶ comments begin with an asterisk (*)
▶ multiline comments can be written between $Ontext and
$Offtext
▶ $Exit terminates the script in the corresponding line
▶ $ and * must always be placed at the beginning of the line
▶ spaces and blank lines can be inserted for better readability

43/72
Further indications

▶ use decimal numbers with period (e.g., 2.5)


▶ function round(x,y) rounds the number x to y digits
▶ GAMS is not case sensitive (lower and upper case letters may
be mixed freely but are treated identically)
▶ GAMS treats singular & plural synonymously (e.g., set and
sets)
▶ multi-word names are not allowed, use hyphens or quotes
(e.g., New-York or ’New York‘)

44/72
Exponentiation

▶ x**n
▶ x must be positive
▶ n can be any number
▶ power(x,n)
▶ positive or negative value of x
▶ n must be integral

45/72
Summations

▶ Format: sum(index of summation, summand), e.g.,

▶ slightly more complex:

46/72
Products

▶ Exactely the same format as summations, replacing sum by


prod
▶ Format: prod(index, factor)
▶ e.g., xij (i. e. xi1 · xi2 · . . . ·xin ) is written in GAMS as
Q
j

47/72
Maximum over index

▶ function that calculates the minimum or maximum of set


indexed expressions or terms

▶ min is analogously represented by smin

48/72
Display statement

▶ output of desired values at the end of the listing file

49/72
If statements

▶ Dollar operator
▶ $ (condition) can be read as ‘such that condition is valid’
▶ Example:
▶ "If (b > 1.5) then a = 2" becomes
▶ a$(b > 1.5) = 2 ;
▶ very useful to handle exceptions
▶ If-Else
▶ elseif is optional and can be used as often as needed
▶ no declaration and no definition of equations inside if, but e.g.
solve to solve models multiple times

50/72
Tutorials and helpful material

▶ Written Online Tutorials


▶ A GAMS Tutorial by Richard E. Rosenthal
https://www.gams.com/latest/docs/UG_Tutorial.html
▶ Expanded GAMS User Guide by Bruce A. McCarl
https://www.gams.com/mccarlGuide/gams_user_guide_
2005.htm
▶ Video Tutorials
▶ GAMS Lessons YouTube Channel
https://www.youtube.com/user/GAMSLessons
▶ Uni Hamburg, Einführung in die Modellierung mit GAMS
(German)
Teil 1: https://www.youtube.com/watch?v=mmVgNezKnFI
Teil 2: https://www.youtube.com/watch?v=l265a2oF0eQ

51/72
4. Basics GAMS Studio - Welcome Page

52/72
New, Open, Save, Settings, Model Library

53/72
Clicking highlights the word

54/72
Strg + F — search & replace

55/72
Distraction free mode

56/72
Help & F1 – Index & Descriptions

57/72
Help & F1 – Index & Descriptions

58/72
Run the model

59/72
Run the model – Process Log

60/72
Run the model – Process Log

61/72
Run the model – Process Log

62/72
Reading the solution – Listing File & Navigation

63/72
Run and create a GDX File

64/72
Open / Close: Project Explorer, Process Log, Help

65/72
5. Scalar vs. Vector Format

▶ when using the vector format, one can easily change the input
data without changing the model structure
=⇒ Let‘s try to formulate our cake production model in vector
format!

66/72
Debugging tipps

▶ error messages are displayed with line specification in the


order of their occurrence in the logfile window
▶ double-clicking on the error message marked in red leads to
the (apparently) incorrect line in the .gms file
▶ an error often leads to several error messages =⇒ always
check carefully for the cause of the first error message
▶ look at the previous line (especially for missing semicolons) if
nothing seems obvious
▶ check for common mistakes: missing semicolons, missing
index or periods when defining equations, “=“ used instead of
“=E=“, comma instead of period for decimal numbers, use of
undeclared/undefined symbols/elements

67/72
Homework 1

68/72
Homework 1a

Open the cake example using vector format from today’s meeting.
How do results change if cake A requires also 1kg, and cake B 5kg
of ground almonds, given that 50kg of ground almonds are
available?

69/72
Homework 1b
The machine tool department of Delta Company was a bottleneck
to the company’s production capacity. The department had five
basic types of machine tools and worked two forty-hour-per-week
shifts. The number of machines of each type is shown in Table 1.
The machine tool department was scheduled to run six separate
jobs in a coming week. The number of units involved in each job
are given in Table 2. From previous experience, the department
knows about the hours of machine time needed per unit of job 1 to
6. This is displayed in Table 3.
Table 1: Number of machine tools
Type Number
A 12
B 8
C 15
D 6
E 2
70/72
Table 2: Number of units required

Job Number of units


1 100
2 150
3 40
4 50
5 100
6 200

71/72
Table 3: Hours machine time per unit

Job
Machine type
1 2 3 4 5 6
A 10 3 15 3 5 6
B 12 2 20 2 6 5
C 8 4 22 3 7 4
D 9 6 16 6 8 7
E 15 5 20 6 10 8

With this given data, prepare the 1st part of the model delta.gms
including the definition of the needed sets, variables, parameters
and a table. We will continue with the 2nd part tomorrow.

72/72

You might also like