Beruflich Dokumente
Kultur Dokumente
Maria Sassi
Department of Economics and Management
University of Pavia – Italy
e-mail : msassi@eco.unipv.it
http://economia.unipv.it/naf/
0
Scientific Board
Pavia -IT
naf@eco.unipv.it
ISBN: 978-88-961889-11-5
1
3
GAMS modeling structure: the input file
3.1. Introduction
This Chapter considers the input file, that is one of the core component of the GAMS modeling
framework. Basic commands and their specification are illustrated following the structure of an
equilibrium market model for a closed economy. Additional information on GAMS language are
introduced in the following developing specific models.
For further details it is recommended to consider the GAMS’ user guide included in the Help of the
software.
2
Figure 16 – Equilibrium market model for two commodities in GAMS
3
Table 2 – GAMS’ commands
Inputs
Sets
Declaration
Definition: Assignment of members
Data (Parameters, Tables, Scalars)
Declaration
Definition: Assignment of values
Variables
Declaration
Definition: Assignment of type (optional)
Optional: assignments of bounds and/or
initial values
Equations
Declaration
Definition
Model statement
Solve statement
Optional: Display statement
4
Table 3 – Use of semicolon
USE SEMICOLON DO NOT USE SEMICOLON
- after the end of each declaration block - after each line within a block (for blocks of
(parameters, variables, equations, etc.) parameters, variables, etc.)
- after each defined parameter - after each declared scalar and parameter, only at the
end of the block
- after each defined table - after each declared variable
- after each defined equation - after each declared equation
d. upper- and lower-case letters have the same meaning - GAMS compiler does not distinguish between
that, so the user is free to use either:
e. Comments and documentary text can be included in the model - They are useful to explain parts of
the model and can be inserted in two ways. Any line that starts with an asterisk (*) is disregarded as a
comment line by the GAMS compiler. In Figure 16, this statement has been adopted to specify the type
of model developed before model design (* GAMS model market equilibrium in a closed economy).
Characters are in grey. A second way to include comments and documentary text is with the specific
GAMS statements $ontext and $offtext. $ontext opens comments or documentary text and $offtext close
it. In the example in Figure 16, the asterisk should have been replaced by the following syntax: $ontext
GAMS model market equilibrium in a closed economy $offtext,that is
f. Start entities with a letter and stop at nine alpha-numerical characters - The names given to the
entities of the model must start with a letter and can be followed by up to nine more letters or digits.
5
Figure 17 – Linear equilibrium market model for meat and fish
- Two commodities
𝑖𝑖 = 𝑚𝑚𝑚𝑚𝑚𝑚𝑚𝑚, 𝑓𝑓𝑓𝑓𝑓𝑓ℎ
- The system of equations
𝐷𝐷𝑖𝑖 = 𝑎𝑎𝑖𝑖 + 𝑏𝑏𝑖𝑖 ∗ 𝑃𝑃𝑖𝑖
𝑆𝑆𝑖𝑖 = 𝑓𝑓𝑖𝑖 + 𝑧𝑧𝑖𝑖 ∗ 𝑃𝑃𝑖𝑖
𝐷𝐷𝑖𝑖 = 𝑆𝑆𝑖𝑖
- The value of the observed variables
𝑎𝑎(𝑚𝑚𝑚𝑚𝑚𝑚𝑚𝑚) = 1.07 𝑎𝑎(𝑓𝑓𝑓𝑓𝑓𝑓ℎ) = 1.012
𝑏𝑏(𝑚𝑚𝑚𝑚𝑚𝑚𝑚𝑚) = −0.804 𝑏𝑏(𝑓𝑓𝑓𝑓𝑓𝑓ℎ) = −3.09
𝑓𝑓(𝑚𝑚𝑚𝑚𝑚𝑚𝑚𝑚) = 1.03 𝑓𝑓(𝑓𝑓𝑓𝑓𝑓𝑓ℎ) = 0.47
𝑧𝑧(𝑚𝑚𝑚𝑚𝑚𝑚𝑚𝑚) = 1.08 𝑧𝑧(𝑓𝑓𝑓𝑓𝑓𝑓ℎ) = 4.87
6
Table 4 – Elements of a model and GAMS’ command for their specification
Element Type GAMS’ Command
i Index Sets
Observed values
ai Observed exogenous variable Parameters
bi Observed parameter Parameters
fi Observed exogenous variable Parameters
zi Observed parameter Parameter
Unknown variables
Di Endogenous variable Variables
Si Endogenous variable Variables
Pi Endogenous variable Variables
System of equations
Demand Demand function Equations
Supply Supply function Equations
Equil Equilibrium condition Equations
3.4.1. Sets
The command Sets defines the indices used in the model, which must be first declared and then defined
(Figure 18).
Declaration implies to list the names of the sets (i in our case) with a short description (in our case
Commodity type) while definition consists in listing the members of each set (in our case Meat and
Fish).
Figure 19 shows the syntax for this command where the words in between < > describe what type of
words should go in that place. < > are not GAMS syntax
7
Figure 19 – Syntax for the command Set
Definition of the set members must be put in slashes with set members one below the other or in the
same row, but in this case each set is separated by a comma.
A semicolon must be written at the end of the definition of Sets to conclude the command specification.
The choice of the name of the set members does not affect the overall result: the two members meat
and fish might be called 1 and 2, or Mary and Susy and still they would be equally working. Name are
not part of GAMS syntax.
The command Sets is only necessary when one or more elements in the model are indexed, if not the
command must be excluded. Sets is the only compulsory when there are indices.
3.4.2. Parameters
The command Parameters specifies the observed exogenous variables and coefficients of the model. It
is articulated in a section where these elements are declared and another where they are defined (Figure
20).
8
With the declaration the names of the parameters with a short description is provided while definition
consists in the assignment of a value or a formula to each parameter. As illustrated in Figure 20 indices
must be put in quotation marks.
A semicolon is located at the end of the Parameters declaration and after each defined parameter.
Figure 21 shows the syntax for this command.
Two alternative ways for the specification of the command is illustrated in Figure 22.
GAMS also accepts to break this one statement into four, putting a semicolon at the end of each of
them, as illustrated for the first alternative by Figure 23.
The use of tables and direct assignment for data entry will be illustrated in the following, when
requested by the examples.
9
Figure 23 – Alternative use of semicolon
in Parameters entry
3.4.3. Variables
The command Variables defines the endogenous variables used in the model. They must be declared
and optionally defined.
Declaration consists in listing the variables with a short description of each of them and end with a
semicolon, as illustrated by Figure 24 for our example.
Definition is an optional component that usually refers to the specification of the variable type, as
reported in Table 5.
By default all variables are free. However, if the user wants to specify the variable type, the command
corresponding to the chosen variable type must be added.
10
Table 5 – Variable type
Variable type Allowed range of variable
Free -∞ to +∞
Positive 0 to +∞
Negative -∞ to 0
Binary 0 or 1
Integer 0, 1, …, 100
For example, if in our example the user wants the prices-an endogenous variable-to be positive, the
definition for Variables must be written as in Figure 26.
The user has the possibility to apply special records in order to assign a bound or an initial value to a
variable. This can be done with the peculiar variable suffixes shown in Table 6.
For example, if the user wants equilibrium prices to be positive the command to be included is
P.LO(i) = 1
3.4.4. Equations
The command Equation specifies the equations that characterize the relationships between the variables
and parameters in the model. They must be declared and defined, as in Figure 27 for our example.
Equation declaration lists the names of the equations with a brief description of each one and end with
a semicolon. Equation definition specifies the equations themselves.
11
Figure 27 – Declaration and definition of Equations
The two points (..) after the equation name are compulsory. In equations =E= represent the equal sign.
A semicolon must be used after each defined equation.
The syntax is illustrated in Figure 28.
Table 7 provides a selection of GAMS notations for standard arithmetic operations, indexed operations
and functions.
12
3.4.5. Model and Solve
The command Model names the model and, in brackets, lists the equations that are part of it followed
by the semicolon (Figure 29).
The name given to the model (in our case CLMKEQ) does not affect the overall result. It is not GAMS
language and can be selected as the user likes.
The list of each single equation that become part of the model can be substituted by the statement All as
follows:
As in the case of the command Sets, also with this command the equation names can be put on the
same line, but they must be separated by a comma.
13
The command Solve tells GAMS the method to use in order to “solve” the model, that is to find the
values of the endogenous variables that make all equations true. The defined command in our example
and the syntax are shown in Figure 31.
With this command the user selects the method adopted to solve the model. In our example the Mixed
Complementarity Problems (MCP) which can be used both for linear and nonlinear programming.
The list of available solution procedures see the GAMS’ user guide.
The solve statement completes the modeling structure.
3.5. Checking errors in the code string or file and running the model
Before running the model, a check for compilation errors, such as typing mistakes, is advisable.
Two commands can be alternatively used for compiling mistakes. The user can:
- Click on File in the Menu headings and then on Compile; or
- press Shift+F9.
When the GAMS compiler encounters an error in the input file, it inserts a coded error message inside
the echo print, a new window, on the line immediately following the scene of the offense.
These messages always start with **** and contain a $ directly below the point at which the compiler
thinks the error occurred. The $ is followed by a numerical error code, which is explained after the
echo print.
Making reference to our example, let us assume that, by mistake, an error in the definition of one
parameters has been done: the intercept of the fish demand function has been written as fis instead of
fish (Figure 32).
14
Figure 32 - Typing mistake in Parameters definition in the input file
Checking error, the echo print will highlight the mistake as shown in Figure 33, and at the end the
typology of error will be clarified. In our case there is a violation of the domain of the specific element.
Once all the errors have been corrected, the model can be run following three possible ways:
- Clicking on the red arrow on the Buttons line;
- Pressing F9; or
- Clicking on File in the Menu headings and then on Run.
Two new windows will provide the output of the model.
15
4
GAMS modeling structure: the output files
4.1. Introduction
Once a model is run with GAMS, two windows appear (Figure 34).
The one provides information about the solution procedures, such as the time spent to find the solution,
the other show the solution: it is the window where the output can be red.
The solution window is structured into the six sections. They are, in the order in which they are
presented in the output file:
- Echo Print;
- Reference Maps;
- Equation Listings;
16
- Model Statistics;
- Status Reports;
- Results.
The Echo Print, Reference Maps and Model Statistics give information on the model and its
components while the other sections are the result of the Solve command.
The aim of this Chapter is to illustrate each of these components.
Reference Maps contain summaries and analyses of the input file for the purposes of debugging and
documentation. They are a Cross-reference map and a Model entities map. The IDE version of GAMS
does not show these maps by default. They must be included with particular statements:
- $ONSYMXREF for the Cross-reference map;
- $ONSYMLIST for the Model entities map;
written at the beginning of the input file (Figure 36).
They are not compulsory; the user can decide to include none, one or both the maps.
17
Figure 36 – Statements for Reference Maps
to be included at the beginning of the input file
The Cross-reference map is an alphabetical, cross-referenced list of all the entities (sets, parameters,
variables, and equations) of the model. Figure 37 shows a part this map related to our example.
18
For each element of the model it provides three types of information, the symbol, the type and
references.
Let us consider the first element of the map. It is the intercept of the demand function. The cross-
reference map informs that that this coefficient is represented in the model by the symbol “a”, is
specified as a parameter that was declared in line 11, defined (assigned two values) in line 15, and
referenced in line 28. The number of the lines are those in the Echo Print, as illustrated in Figure 38
with reference to our example.
The Model entities map provides a list of model entities grouped by type and listed with their
associated description, as in Figure 39 for our example.
The user can compare this map with the previously written Table 4 in Par 3.4 in order to verify if all the
elements have been included in the model.
19
Figure 39 - Model entities map
DEMAND(i)..
D(i) =E= a(i) + b(i)*P(i);
20
PARAMETERS
a('Meat')=1.07; a('Fish')=1.012;
b('Meat')=-0.804; b('Fish')= -3.09;
The constant parameter (i.e. the demand coefficient) is on the right-hand side.
By default the equation listing contains a maximum of three specific equations for each generic
equation (in our example, the generic equation in ---- DEMAND =E= Demand equation and the
specific equations are two). In order to change this number a command must be inserted in the input
file prior to the solve statement. It is
option limrow = r ;
with r the number of desired equations.
The default output also contains a section called the Column Listing, analogous to the equation listing,
which shows the coefficients of three specific variables for each generic variable. To change the default
number of specific column printouts per generic variable, the above command can be extended:
option limrow = r, limcol = c ;
where c is the desired number of columns.
Setting limrow and limcol to 0 is a good way to save paper after the model has been debugged.
21
Figure 40 – Model Statistics
The Single counts refer to individual rows and columns in the specific model instance being generated.
In our example, there are six specific equations (Meat Demand, Meat Supply, Meat Equilibrium, Fish
Demand, Fish Supply and Fish Equilibrium) and six specific variables (Meat Demand, Meat Supply,
Meat Price, Fish Demand, Fish Supply and Fish Price).
The Non zero elements entry refers to the number of non-zero coefficients.
For nonlinear models, some other statistics are given to describe the degree of non-linearity in the
problem:
- The non linear n-z entry refers to the number of nonlinear matrix entries in the model.
- The derivative pool and constant pool provide more information about the nonlinear information
passed to the nonlinear solver.
- The code length entry reports on the complexity of the nonlinear part of the model. The numerical
value reports the amount of code GAMS passes to the nonlinear solver that describes all the nonlinear
terms in the model
Generation time if the time used since the syntax check finished, while execution time is the requested
time for running the model.
22
Figure 41 – Status Report
1 NORMAL COMPLETION This means that the solver terminated in a normal way: i.e., it was not
interrupted by an iteration or resource limit or by internal difficulties.
2 ITERATION INTERRUPT This means that the solver was interrupted because it used too many
iterations. Use ‘option iterlim ‘ to increase the iteration limit if everything
seems normal.
3 RESOURCE INTERRUPT This means that the solver was interrupted because it used too much time.
Use ‘option reslim’ to increase the time limit if everything seems normal
4 TERMINATED BY SOLVER This means that the solver encountered difficulty and was unable to continue.
More detail will appear following the message.
5 EVALUATION ERROR Too many evaluations of nonlinear terms at undefined values. You should use
LIMIT bounds to prevent forbidden operations, such as division by zero. The rows in
which the errors occur are listed just before the solution.
6 CAPABILITY PROBLEMS The solver does not have the capability required by the model, for example,
BARON has a more limited set of functions than other solvers.
7 LICENSING PROBLEMS The solver cannot find the appropriate license key needed to use a specific
subsolver.
8 USER INTERRUPT The user has sent a message to interrupt the solver via the interrupt button in
the IDE or sending a control C from a command line.
9 ERROR SETUP FAILURE The solver encountered a fatal failure during problem set-up time.
23
The model status describes the characteristics of the accompanying solution. Usual linear programming
termination states are 1 OPTIMAL, 3 UNBOUNDED, 4 INFEASIBLE, while in non-linear
programming, the status to look for is 2 LOCALLY OPTIMAL.
If solver and model status are acceptable results of the model can be examined. Otherwise, the problem
must be solved working on the model structure in the input file.
4.6. Results
Results are split up into three sections. The are:
- Equations, indicated with solEQU in the output file;
- Variables, indicated with solVAR in the output file;
- Report summary.
For equations and variables there is a line of printout giving the lower limit, level, upper limit, and
marginal.
Results are in the Variable part.
Figure 42 shows solEQU for our example.
Figure 42 - solEQU
It is organized into three parts, each of them related to one equation in the model. The values in the
LOWER and UPPER columns are obtained from the constant right-hand-side value of the equation, as shown in
the Equation listing, which for clarity reasons is reported in the following for demand equation:
24
LEVEL and MARGINAL VALUE are determined by the solver. LEVEL represents the optimal level
for the equation, which is equal to the level of all terms involving variables (VERIFY IF IT IS THE
RESULT OF THE CALIBRATION PROCEDURE). The MARGINAL value of each equation is
instead equal to the resulting value of the corresponding variable.
As previously underlined, the solution of the model is in solVAR, which in our example is structured as
reported in Figure 43.
Figure 43 - solVAR
The solution level for the variables of the model is in the column LEVEL.
LOWER and UPPER limits reflect what has been declare in the model. In our example all variables are
declared as (FREE) VARIABLES and for this reason the upper and lower bound is unlimited, with the
GAMS syntax –INF and +INF.
Concerning the language rules, a single dot (.) means zero while EPS very small but nonzero.
MARGINAL is the current reduced cost to force a variable in the solution. Generally it does not have
much meaning in a MCP or CNS model.
For models that are not solved to optimality, some constraints may additionally be marked with specific
notations. They are listed and described in Table 9.
25
Table 9 – Notations for models not solved to optimality
Syntax Description
INFES The row or column is infeasible. This mark is made for any entry whose level value
is not between the upper and lower bounds.
NOPT The row or column is non-optimal. This mark is made for any non-basic entries for
which the marginal sign is incorrect.
UNBND The row or column that appears to cause the problem to be unbounded.
Results end with a Report Summary. It gives information on the total number of non-optimal, infeasible, and
unbounded rows and columns, as in Figure 44 for our example.
26
5
Demand analysis
5.1. Introduction
This Chapter introduce how to model demand with GAMS. It starts with a succinct presentation of the
theoretical concepts that find application in the modelling section. More precisely, the focus is on the
concept of demand, the objective of demand analysis and the presentation of a selection of functional
forms. Afterwards, the introduced theoretical concepts are applied in a group of models solved for the
base run. These latter are structured in order to work with some preliminary GAMS components mainly
aimed at explaining how to write in GAMS’ language the basic structure of a model. This Chapter also
introduces the steps to be followed in writing a model with GAMS.
5.2. Demand
Demand refers to the amount of a given product that consumers are willing and able to purchase, over
a certain period of time. Factors affecting consumer demand are:
- Price (P) of the demanded product (i);
- Price of related goods (j);
- Income of consumers (Y);
- Consumer preferences, such as age, education, professional status, and geographic environment and
other factors (demographic characteristics, such as population size, and expectations) (T).
5.3. Elasticity
According to the factor affecting demand, a specific typology of elasticity finds definition, as illustrated
in Table 10.
Table 10 – Factors affecting demand and elasticity typology
Factor affecting demand Elasticity type
Price of the demanded product Own-price elasticity of demand
Price of related goods Cross-price elasticity of demand
Income of consumers Income elasticity of demand
Consumer preferences Consumer preferences elasticity of
demand
The own-price elasticity measures the percentage change in the quantity demanded given a one percent
change in its price. According to theory, holding constant all the other factors the higher the price of the
product the less consumers are willing to buy. This is the so called low of demand, which suggests a
negative relationship between price and demand of a good: the own-price elasticity has a negative sign.
According to sign and magnitude of the own-price elasticity, demand can be classified in the typologies
listed in Table 11.
Cross-price elasticity indicates the percentage change in the quantity demanded of a good (i) given a
one percent change in the price of another related good (j).
According to the sign of the cross-price elasticity the categories of goods listed in Table 12 can be
distinguished.
Income elasticity of demand measures the percentage change in demand for a good when income
changes by one percent. Table 13 lists the categories of goods according to the sign and magnitude of
this type of elasticity.
28
Table 11 – Own-price elasticity
Value Descriptive terms Meaning
Ed = 0 Perfectly inelastic demand When quantity demanded does not change to the
percentage change in price
-1<E d <0 Inelastic or relatively inelastic when the percentage change in quantity
demand demanded is less than the percentage change in
price
E d = -1 Unit elastic, unit elasticity, when the percentage change in quantity
unitary elasticity, or unitarily demanded is equal to the percentage change in
elastic demand price
-∞<E d <-1 Elastic or relatively elastic when the percentage change in quantity
demand demanded is greater than the percentage change
in price
E d =-∞ Perfectly elastic demand Any increase in the price, no matter how small,
will cause demand for the good to drop to zero
E d >0 Giffen good Consumers paradoxically consume more as the
price rises (violation of the law of demand)
Table 13 – Categories of goods according to the sign of the income elasticity of demand
Value Definition Description
29
Even if prices and incomes are the same two or more consumers may differ in their demand due to
different preferences. The specific effect on demand is measured by the so called consumer preferences
elasticity of demand.
with q i the per capita quantity demanded (purchased) of good “i”, a i the intercept of demand function,
c i the own-price elasticity of good “i”, p i the price of the demanded good “i”, P the consumer price
index, d j the cross-price elasticities, p j the price of other good “j”, e i the income elasticity of demand, y
the total expenditure per capita, f k the preferences elasticities, z k the consumer preferences.
Dividing prices and income by the consumer price index, demand equation is homogeneous of degree
zero in prices and income. In other words, there is no money illusion: demand is not affected by a
proportional increase in all prices and income.
This approach is attractive in its simplicity but it has three major drawbacks.
First, the choice of the functional form for the demand function and the variables to be included is
arbitrary and for this reason they might not reflect the actual consumer behavior.
Second, elasticities are postulated to be constant over all values of the exogenous variables. The
assumption might not be always sustainable, such as in the case of commodities that are luxuries and
that become necessity at the increase in per capita income.
Third, the pragmatic approach does not consider the consumer budget constraint, which limits total
expenditure, as suggested by theory.
When the price of the demanded and related goods is not available, the pragmatic approach uses the
Engel function. Table 14 provides a list of functional form for this demand function.
30
Table 14 – Engle function
Engel curve Mathematical formulation
Linear q i =a i +e i* y
Double logarithmic ln(q i )=a i +e i* ln(y)
Semilogarithmic q i =a i +e i* ln(y)
Logarithmic reciprocal ln(q i )=a i -e i* (1/y)
with n the number of commodities demanded, p i the price of demanded good “i”, q i the demanded
quantity (purchased) of good “i” per capita, y the total expenditure, s i the minimum subsistence or
“committed” quantities below which consumption cannot fall and b i the marginal budget share.
This latter is defined as
∆𝑝𝑝𝑖𝑖 ∗ 𝑞𝑞𝑖𝑖
𝑏𝑏𝑖𝑖 =
∆𝑦𝑦
and expresses how expenditure on each commodity changes as income changes.
In the model the component �∑𝑗𝑗 𝑠𝑠𝑗𝑗 𝑝𝑝𝑗𝑗 � represents the subsistence expenditure, while �𝑦𝑦 − ∑𝑗𝑗 𝑠𝑠𝑗𝑗 𝑝𝑝𝑗𝑗 � is
the uncommitted or “supernumerary” income that is the share of income is spent in fixed proportions b i
between the n goods.
The interpretation of the model is that consumers spend a committed amount on each good and, after
that, divide the remaining uncommitted income among all products in fixed amounts b i .
As LES assumes b i >0, this system does not allow for inferior goods, an assumption that is not
supported by empirics; it implies a linear Engel function.
The Almost Ideal Standard System (AIDS) allows overcoming this issue introducing flexibility to the
estimated elasticities. An additional advantage of this demand model is that, as supported by demand
31
theory, it consistently considers the mutual interdependence of large numbers of commodities in the
choice made by consumers.
An AIDS is specified as follows:
𝑝𝑝𝑖𝑖 𝑞𝑞𝑖𝑖 𝑦𝑦
= 𝑎𝑎𝑖𝑖 + � 𝑑𝑑𝑖𝑖,𝑗𝑗 𝑙𝑙𝑙𝑙�𝑝𝑝𝑗𝑗 � + 𝑒𝑒𝑖𝑖 𝑙𝑙𝑙𝑙 � �
𝑦𝑦 𝑃𝑃
𝑗𝑗
with n the number of commodities demanded, p i the price of demanded good “i”, q i the demanded
quantity (purchased) of good “i” per capita, y the total expenditure, a i the intercept of demand function,
d j the cross-price elasticities, p j the price of other good “j”, e i the income elasticity of demand, and P
the consumer price index.
𝑝𝑝𝑖𝑖 𝑞𝑞𝑖𝑖
𝑦𝑦
is the budget share, that is the share if income spent in good “i”.
32