Intro to GAMS Notes - The University of Texas at Austin

advertisement
INTRODUCTION TO GAMS1
Daene C. McKinney
CE385D Water Resources Planning and Management
The University of Texas at Austin
Table of Contents 1. Introduction ............................................................................................................................. 2 2. GAMS Installation................................................................................................................... 2 3. GAMS Operation..................................................................................................................... 2 4. Examples ............................................................................................................................... 11 4.1. Algebraic Equation ............................................................................................................ 11 4.1.1. Background and GAMS Code .................................................................................... 11 4.1.2. What's Going On in the Model? ................................................................................. 12 4.2. Least Squares Parameter Estimation............................................................................... 15 4.2.1. Background and GAMS Code ................................................................................. 15 4.2.2. What's Going On in the Model? ................................................................................. 16 5. A Brief Overview of the GAMS Language ........................................................................... 17 5.1. Model Structure ................................................................................................................. 17 5.2. Sets .................................................................................................................................. 18 5.3. Scalars, Parameters and Tables ....................................................................................... 20 5.4. Variables ......................................................................................................................... 21 5.5. Equations ........................................................................................................................ 22 5.6. Assembling a Model ....................................................................................................... 23 5.7. Solving a Model .............................................................................................................. 24 5.8. Model Output .................................................................................................................. 24 5.9. IF-ELSE Operator ........................................................................................................... 26 References .................................................................................................................................. 27 1
This Introduction to GAMS Modeling benefited from research by Andrey G. Savitsky. The use and distribution of
GAMS is made possible by the GAMS Development Corporation (www.gams.com) and it greatly appreciated.
1
1.
INTRODUCTION
The General Algebraic Modeling System (GAMS) facilitates the creation of mathematical
models, particularly optimization models, and allows the modeler to build models using the same
logical structures as the mathematical equations that need to be solved. In this section, the river
basin model described in Annex 3 will be described using the GAMS language to allow users to
understand the model and to develop additional models. This section should be supplemented by
the official GAMS Guide and Tutorials (Brooke et al., 2006), the monograph on optimization
models for water and energy management (McKinney and Savitsky, 2006), and the GAMS
website (www.gams.com).
2.
GAMS INSTALLATION
A. Run setup.exe: Run the file “setup.exe” directly from the CD. Use the Windows Explorer to
browse the CD and double click on the file “setup.exe”. The setup program will prompt you
for the name of the directory in which to install GAMS. Accept the default choice which the
install program offers. Then the GAMS software will be installed.
B. License file: You will be prompted for a license file during the installation. You do not have
one, so choose ‘No’ when asked if you wish to copy a license file. GAMS will function in
demonstration mode and will only solve small problems.
3. GAMS OPERATION
A. Start GAMS: Start GAMS by selecting “Start  All Programs  GAMS  GAMSIDE”
from the Windows Desktop
2
Figure 1. Starting GAMS.
B. Create new GAMS project: To create a new project in GAMS, choose “File  Project 
New project” from the GAMSIDE.
Figure 2. Creating a new project in GAMS.
Navigate from “My Documents” to the “C\:” drive. Create a new directory by pressing the
3
“folder” icon
. Name the new directory “C:\Example”. Double click on the “Example”
folder. Type “Eq1” in the “File name” box and press “Open”.
Figure 3. Naming a new project in GAMS.
The GAMS window should now show the new Shapley.gpr project window .
Figure 4. Naming a new project in GAMS.
4
C. Create a new GAMS code file: To create a new GAMS code file, select “File  New”
from the GAMSIDE.
This will create the file “Untitled_1.gms” in the “C:\Example”
directory”. We will then copy the “EQ1” GAMS code into this file from a text file.
Figure 5. Creating a new GAMS code file.
D. Open GAMS code file: Select “File  Open”. Select “Text files” as the “Files of type”
type. Navigate to the directory where you stored the and open the file “EQ1.txt”. Click on
this files. Now it is automatically copied to GAMS.
Figure 6. Opening the EQ1 text file.
E. Tabs and Files: Now the window shows that you have two files open. This is indicated by
the two “Tabs” along the top of the window in GAMS. The first tab displays the file
“Eq1.txt”. This file, which we just opened, is shown in the active window. The second tab
displays the “untitled_1.gms” file that we recently created. This is the GAMS Code file that
5
we are creating. To move back and forth between the two files, simply click on the
appropriate tab at the top of the window.
Figure 7. Multiple files open in GAMS.
F. Copy text file: Now we will copy all of the text from the “Eq1.txt” file to the
Untitled_1.gms” file. Click on the “Eq`.txt” tab. Click the cursor in the “Eq1.txt” file
window. Select “Edit  Select All”. The entire file should become highlighted. Select
“Edit  Copy”.
Figure 8. Selecting GAMS code from text file.
6
G. Paste into GAMS Code file: Now we will paste the text we just copied into the
“Untitled_1.gms” file. Click the “Untitled_1.gms” tab. Click the cursor in the
“Untitled_1.gms” file window. Select “Edit  Paste”. Now the text is pasted into the file.
Figure 9. Pasting GAMS code into the “Untitled_1.gms” file.
H. Save GAMS Code file: Now we will save the new GAMS code file “Untitled_1.gms” in the
new project directory “C:\Example” as “Eq1.gms”. Select “File  Save As”. Navigate to
the directory you created earlier: “C:\Example”. Name the file “Untitled_1.gms” as
“Eq1.gms”. Save the file “Eq1.gms”.
Figure 10. Saving the new GAMS file “Eq1.gms”.
Now the files “Eq1.txt” and “Eq1.gms” are displayed in the window.
7
Figure 11. Window showing “Eq1.txt” and “Eq1.gms”.
I. Run the model: Now we are ready to run the Shapley GAMS model. Press the red arrow
button
at the top of the GAMSIDE window. Alternatively, you can select “File 
Run” from the main menu
Figure 12. Running the GAMS model.
J. GAMS Model Results: The results should appear as below. There are two windows
displayed by GAMS now. One is titled “No active process” and this window displays all of
8
the control commands and information that were produced by GAMS when it ran the model.
The most important information is the line that reads “Optimal Solution. …”, which indicates
to us that the model was solved successfully.
Figure 13. Control screen from running the GAMS model.
K. Viewing the GAMS model results: Near the bottom of this window, there is a line that
reads: “Putfile result C:\Example\Eq1.txt”. The file “Eq1.txt” now contains the GAMS
model results. Double clicking on this line will open the results file automatically in another
window.
Figure 14. Viewing GAMS model results.
9
L. Rerunning the model: You can make modifications to the Eq1 GAMS model and rerun the
model to see the results of your changes. Select the “Eq1.gms” tab. Make your changes to
the GAMS code in that window. Press the red arrow button
at the top of the
GAMSIDE window or select “File  Run” from the main menu. Once the model is
finished running, you will see the same window as in Figure 12 and you can again double
click on the line with the name of the output file to display the new results. When you do
this you will be asked if you want to “Reload the Results”. Select “Yes”.
Figure 14. Rerunning the model.
or
Figure 15. Reloading the results.
M. Exiting and Restarting GAMS: Select: “File  Exit” to quit the GAMS program. When
you restart GAMS, you will be returned to the place where you exited the program.
10
4.
EXAMPLES
4.1. Algebraic Equation
4.1.1. Background and GAMS Code
The problem is to find the roots of the following nonlinear equation:
(10 x 2 + 10 x + 10) − (−10 x + 100) = 0
One can see the roots in the following graph:
350
f1(x)
f2(x)
f1(x)-f2(x)
300
250
10x 2+10x +10
f1(x), f2(x), f1(x)-f2(x)
200
-10x +100
150
100
50
0
-50
10x 2+10x +10-(-10x+100)
-100
-150
-5
-4
-3
-2
-1
0
x
The GAMS code is:
VARIABLES x, e, obj;
EQUATIONS Eq1, Objective;
Eq1.. e =E= 10*x*x+10*x+10-(-10*x+100);
11
1
2
3
4
5
Objective.. obj =E= e*e;
MODEL Eq /ALL/
SOLVE Eq USING NLP MINIMIZING obj;
FILE res /Eq1.txt/
PUT res;
put "Solution x =
", put x.l, put /;
The steps to enter this model into the GAMS/IDE are shown in the file Introduction to
GAMS.pdf
4.1.2. What's Going On in the Model?
A. VARIABLES
Variables can be zero-dimensional or multidimensional. The dimensions are determined by the
availability and number of indices. The declaration of variables is based on the following
principle. For example, consider the variables from the nonlinear equation model presented
above:
VARIABLES x, e, obj;
These are all aero-dimensional (scalar) variables. All these variables are allowed to take on any
vales, positive or negative. Variables are included in equations along with parameters and
constants. Variables are always defined in the solution process.
B. EQUATIONS
The block of equations in GAMS models consists of two parts:
1. Declaration
The syntax of declaring equation names is as follows:
EQUATION
name
comment ;
EQUATION is a keyword which must appear before the name of each equation if there is
semicolon at the end of the line. It is used only once at the beginning of a list of equation
names. The name of an equation may include no more than 10 symbols and must always begin
with a letter. Consider the equation declaration in the model above
EQUATIONS Eq1, Objective;
This declares two scalar equations, the first equation is named Eq1 and the second objective.
12
2. Definition
The definition of equations is where the mathematical form of the model is defined. The syntax
for defining equations in GAMS is as follows:
name..
expression;
name is the name of the equation as previously defined in the list of declared equation names,
then two periods “..” follow separating the name of the equation and the mathematical expression
(algebraic statement) of the equation. The equation must contain a relation operator, such as:
=E=
=G=
=L=
(greater
(less than or equal to)
than
or
equal
(equality)
to)
Again, consider the equations from the model above
Eq1..
e
Objective.. obj =E= e*e;
=E=
10*x*x+10*x+10-(-10*x+100);
C. MODEL
After all the VARIABLES and EQUATIONS of a model have been declared, a system (called a
MODEL) can be formed from the equations and given a name. When all the declared equations
are included in the model, the syntax of declaring the model is as follows:
MODEL
name
comment /ALL/;
For example:
MODEL Eq /ALL/
The model is named "Eq", and the keyword ALL means that the model includes all of the
equations.
D. SOLVE
Once a model has been defined, the next step is to solve it. This is ordered by including a
SOLVE statement in the model. Consider the example of the river system management model in
Section 3.3
SOLVE Eq USING NLP MINIMIZING obj;
The SOLVE statement contains 6 parts:
1. The keyword SOLVE;
2. The previously defined name of the model (Eq);
3. The keyword USING;
13
4. The type of “solver” to use in the solution (NLP in this case since the model is nonlinear);
5. The keyword MINIMIZING; and
6. The name of a scalar variable (obj in this case) to be optimized.
E. OUTPUT
GAMS allows writing information about the model and its solution to text files. In addition, a
text output file “model_name.LST” will always be produced. This file is created automatically,
but its format is not entirely useful for analysis purposes. The following is the full syntax for
defining output text files
FILE pointer comments / filename /
where FILE
pointer
filename
is a keyword used to define files;
is a pointer to direct output to the external text file filename;
is an external text file which receives the output.
Consider the example above
FILE res /Eq1.txt/
where res
is the pointer to the output file; and
Eq1.txt is the output file.
The PUT operator is used to write information into the output file. Consider the example:
PUT
put "Solution x = ", put x.l, put /;
res;
This example shows how the pointer res is associated with the output file Eq1.txt. In the first
line, the output is directed into this file. In the next line, some text and the value (level) of the
variable x is inserted into the file.
14
4.2. Least Squares Parameter Estimation
4.2.1.
Background and GAMS Code
Independent variables (x1, x2, x3) are related to the dependent variable (y) through the following
function
y (t ) = Ax12 (t ) −
2
B
C
−
= e − y (t )
x3 (t ) x2 (t )
where A, B, and C are unknown coefficients. Data have been observed at times t on the variables
x1(t), x2(t), x3(t), and y(t). The measured values of y are designated as yˆ (t ) The residual between
the model value and the observed value is
e(t ) = y (t ) − yˆ (t )
Numerical values of the coefficients are to be be determined by minimizing the squared residuals
8
2
8
2
Minimize ∑ [e(t )] = ∑ [y(t ) − yˆ (t )]
t =1
t =1
In this case we need to introduce the notion of sets and parameters into the GAMS model. Sets
are a way of defining indices in GAMS and we need one for time in this problem. Consider the
following model developed in the GAMS language:
SETS t / 1, 2, 3, 4, 5, 6, 7, 8 /;
PARAMETER
PARAMETER
PARAMETER
PARAMETER
x1(t) /1
x2(t) /1
x3(t) /1
y_hat(t)
2, 2 3, 3 3, 4 3, 5 5, 6 5, 7 6, 8 7/;
30, 2 60, 3 70, 4 60, 5 80, 6 90, 7 100, 8 100/;
1, 2 6, 3 7, 4 3, 5 5, 6 9, 7 8, 8 17/;
/1 10, 2 20, 3 30, 4 20, 5 40, 6 50, 7 60, 8 70/;
VARIABLES
a, b, c, y(t), e(t), obj;
EQUATION mod(t), residual(t), objective;
mod(t).. a*x1(t)*x1(t)-b/x3(t)-c/x2(t)+EXP(-y(t)*y(t)) =E= y(t);
residual(t).. e(t) =E= y(t)-y_hat(t);
objective.. obj=E=sum(t,power(e(t),2));
MODEL Leastsq / ALL /;
SOLVE Leastsq USING NLP MINIMIZING obj;
FILE res /Eq2.txt/
PUT res;
15
PUT "t x1(t) x2(t) x3(t) y(t) y_hat(t)"/;
LOOP(t, PUT t.TL, x1(t), x2(t), x3(t), y.L(t), y_hat(t)/;);
PUT /" a b c"/;
PUT a.L, b.L, c.L;
The steps to enter this model into GAMS and solve it are as follows:
1. Use the same subdirectory “Example” on the “c:/” drive of your computer as you created in
the previous example.
2. In GAMS, create a new project named “Eq2”.
3. Within this new project create a new GAMS file named “Eq2.gms”.
4. Open the file EQ1.txt and copy and paste the GAMS code into the new file “Eq2.gms” and
save it in the "Example" directory.
5. Run the "Eq2.gms" model to ensure that it is working and examine the results file "Eq2.txt".
4.2.2. What's Going On in the Model?
A. SETS
SETS, the equivalent of indices in a typical programming language, are used to make
connections between variables and equations in models. In the model above, a SET is used to
denote the number of the measurements:
SETS t
/ 1, 2, 3, 4, 5, 6, 7, 8 /;
B. PARAMETERS
Digital data are contained in arrays (zero, scalar, or multidimensional matrices called parameters
in GAMS). The SETs, described above, can play the role of indices for these arrays. To declare
an array to contain data values, we use the functional words:
SCALAR
PARAMETER
TABLE
(zero-dimensional),
(one-dimensional), and
(multidimensional).
Parameters are declared in a free format with the following general structure
Parameter a(i,j)
where
Parameter
input-output matrix
data-type-keyword
16
(required)
a
(i,j)
input-output matrix
identifier (required)
domain list (optional)
text (optional)
Scalars are defined similarly but without the domain list. Tables are simply multi-dimensional
versions of parameters. For example, in the model above parameters are used to store the
measurements:
PARAMETER x1(t) /1 2, 2 3, 3 3, 4 3, 5 5, 6 5, 7 6, 8 7/;
C. LOOPS
The LOOP operator is often used with the PUT operator for printing out and displaying
information on the screen or in files. The operator has the following syntax:
LOOP (control_ area operator [; operator]);
For example
LOOP(t, PUT t.TL, x1(t), x2(t), x3(t), y.L(t), y_hat(t)/;);
5.
A BRIEF OVERVIEW OF THE GAMS LANGUAGE
5.1. Model Structure
GAMS models have a structure shown in Table 1.
Using the GAMS Integrated Design
Environment (GAMS-IDE, a graphic user interface for creating and running GAMS models), a
GAMS model is prepared in text form in a file with the extension “.gms”. During model
execution, information is displayed on the screen in the GAMS interface related to the
compilation and solution characteristics and progress.
The results of the model and
computations are automatically sent to a special "LIST" file with the same name as the model
file, but with an extension “.lst”. In addition, as discussed below, the user can program output to
be directed to separate output files.
17
Table 1. Structure of a GAMS Model.
1. SETS
Structures consisting of indices or names
2. DATA
SCALARS (zero-dimentional), PARAMETERS (one-dimentional), and
TABLES (multi-dimensional)
Determination of values of input parameters
3. VARIABLES
Variables or arrays of variables
Declaration with assigning a type of variable
Declaration of limits for possible changes, initial level
4. EQUATIONS Equations or complexes and arrays of equations (includes both declaration
and definition)
5. MODEL
Model declaration (which equations to include)
6. SOLVE
Method of solution (which algorithm to use)
7. OUTPUT
Output of information to files
5.2. Sets
SETS are a major item in the GAMS language. SETS can be used to construct connections
between variables and equations in models. SETS are the equivalent of indices in a typical
programming language. Numerical values or text values can be used as indices, e.g., 1, 2, 3…
can be used or “first”, “second”, etc. A set name must begin with either a letter or a digit, but the
next symbol can be a letter, digit or the marks “+” and “-”. For example:
SET t Months /Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec/;
where the set is named “t”, the word “Months” is descriptive text to identify the purpose of the
set, and the values contained between the “/” symbols are the indices or elements of the set. The
elements in a SET are numbered sequentially or instance here, Feb is second element and Sep is
the ninth element of the set. Elements of a set are separated by commas.
LAG and LEAD operators are used to relate an element of a set with the next or preceding
18
element of the set. In circular LAG and LEAD operators (++, --), after the final element of the
set, the first is repeated again. Sometimes this can be useful in reservoir modeling, especially if
one wants the beginning values of variables in a time period (say, one year) to be the same as the
ending values. Linear LAG and LEAD operators (+, -) are very convenient for modeling time
periods that are not cycled. For example, if the initial value of reservoir storage over a time
period is not to be identical to the final value. It is unlikely that the initial storage of a large,
multi-year storage reservoir will be identical to its ending storage.
As an example, consider the basic reservoir volume balance equation
⎧S + Qt − Rt
St = ⎨ t −1
⎩beg _ S + Qt − Rt
t = 2,...T
t =1
where St and St-1 are the storage in a reservoir at the end and beginning of a time period,
respectively, Qt is the inflow to the reservoir during period t, and Rt is the reservoir release
during period t. Here we can write the equation in general, for all elements of the set t=1,2,…,
using a single equation and making special provision for the first month:
S(t) =E= beg_S$(ord(t) EQ 1) + S(t-1)$(ord(t) GT 1) + Q(t) - R(t);
If we do not want the starting and ending storage values to be identical, we need to specify a
starting value for the previous month’s storage when we write the equation for January, since t1
is undefined when the equation is written for t = 1 (i.e., Jan). In this case, when t = 1, the
initial storage (beg_s) of the reservoir is used in the balance equation, but when the index t > 1
the variable storage is used in the calculation. The ordinal (ord) operator is used in this equation
to return an ordinal of t for a specific instance of the equation.
In the equation above, we have used the conditional ($) operator which provides the ability
modify equations according to logical conditions. The general format of the operator is
item$(condition)
19
This is understood to mean "fulfill the item if the condition is true". So in our reservoir storage
equation example, the “$” operator is operating on the conditions “ord(t) EQ 1” and “ord(t)
GT 1”.
beg_S
Thus, in the first case, if t takes on the value of the first element in the set, the value
is added to the equation, otherwise it is skipped. In the second case, it means that if t
takes on any value of the set greater than the first value, then S(t-1) is added to the equation,
otherwise it is skipped.
5.3. Scalars, Parameters and Tables
Data in GAMS models are contained in arrays (zero-, one-, or multi-dimensional matrices called
scalars, parameters, and tables). SETs can play the role of indices for these arrays. An example
of declaring a scalar and assigning a value to it is:
SCALAR beg_S
Beginning Storage of Reservoir (million m3) /14000/
where the scalar is beg_S and its value is 14000. All of the text between beg_S and 14000 is
simply documentation of the purpose of the scalar. An example of declaring a parameter and
assigning values to all of its elements is the reservoir inflow in a river basin model. This can be
written in one of two ways:
PARAMETER
Q(t) inflow to Reservoir A (million m3) /Jan 236, Feb 284, Mar 315, Apr
445, May 1388, Jun 2306, Jul 2749, Aug 1538, Sep 676, Oct 497, Nov 394,
Dec 391/;
or
PARAMETER
Q(t) inflow to Reservoir A (million m3)
/
Jan 236
Feb 284
Mar 315
Apr 445
May 1388
Jun 2306
Jul 2749
Aug 1538
Sep 676
20
Oct
Nov
Dec
/;
497
394
391
Often the second method is preferred since this facilitates cutting and pasting from spreadsheets.
5.4. Variables
There are five types of variables that can be used in GAMS models:
•
VARIABLE - Can take on any real values;
•
POSITIVE VARIABLE - Can take on only positive real values;
•
NEGATIVE VARIABLES - Can take on only negative real values;
•
INTEGER VARIABLES - Can take on only integer values; and
•
BINARY VARIABLES - Can take on only the integer values 0 and 1
Variables can be zero- or multi-dimensional. The dimensions are determined by the number of
indices used in the definition of the variable. For example, consider the following variable
declaration statement for the zero-dimensional (scalar) objective function value a model:
VARIABLE obj Objective function value ($);
Everything after the variable obj is documentation to explain the purpose of the variable. An
example of a positive one-dimensional (vector) variable declaration is the reservoir storage value
from a river basin model:
POSITIVE VARIABLE S(t) Storage in Reservoir (million m3)
The variable S is defined over the range of the set t. That is, is has elements for all the members
of the index set. In this case S(t) is declared to be a positive variable, since we do not want to
allow negative storage volumes and this saves us having to make specific lower bond
declarations for the variables.
Boundaries can be defined for variables with the help of specific suffixes. For example, in the
21
river basin model, several bounds on storage are set:
S.UP(t)
= K;
S.LO(t)
= S_min;
S.LO('Dec') = end_S;
The first example defines the maximum storage allowed in the reservoir (its “capacity”), hence
the use of the suffix “.UP”; the second defines the minimum storage volume allowed in the
reservoir (known as “dead” storage), hence the use of the suffix “.LO”; and the third defines the
volume at the end of the year.
5.5. Equations
Equations are defined in two parts in GAMS models: (1) Declaration of the names of equations;
and (2) Definition of the equation structure. An example of declaring equations names is:
EQUATIONS
Res_balance(t)
objective
Reservoir mass balance
Objective Function;
Note that the first equation is declared to have the name Res_balance and GAMS will create an
instance of the equation for each member of the set t, that is, it is a vector equation. The second
equation objective is a scalar equation and only one instance of this equation is created by
GAMS. The name of an equation must begin with a letter and contain less than 10 symbols.
The definition of the structure of an equation follows the syntax:
Equation_name.. expression1 =operator= expression2;
First, we have the name of the equation (Equation_name) as declared in the list of equation
names, then two periods “..” separate the name of the equation and the mathematical definition
of the equation structure. The expression of the equation is an algebraic statement using allowed
GAMS algebraic operations. The equation must contain a GAMS “relation” operator, and these
22
are:
=E=
right hand side is equal to the left hand side;
=G=
right hand side is greater than or equal to the left hand side; and
=L=
right part is less than or equal to the left hand side
An example of an equation definition from a river basin model is:
Res_balance(t).. S(t) =E= beg_S$(ord(t) EQ 1)
+ S(t-1)$(ord(t) GT 1) + Q(t) - R(t);
Equations can be continued over as many lines as necessary and they end with a semicolon. This
equation is written in the same manner as we would write the mathematical equation. GAMS
will include an instance of the equation for each element of the set t. An example of an equation
using the less than or equal to relation operator is:
LimitB(t)..
D(t) =L= alpha*R(t);
This equation sets the upper limit of water diversion in a model to be less than or equal to the
flow in the river R(t) times the fraction alpha of the flow that is allowed to be diverted (the
remaining portion being available to downstream users).
5.6. Assembling a Model
After the SETS, PARAMETERS, VARIABLES, and EQUATIONS of a model have been declared a
system (called a MODEL) can be formed from a collection of equations and given a name.
When all of the declared equations are included in the model, the syntax of declaring the model
is as follows (Note – this is the usual situation):
MODEL name
comment /ALL/;
23
For example:
MODEL Reservoir / ALL /;
The model is named "Reservoir", and the keyword ALL means that the model includes all of the
equations. Sometimes, the user may want to create numerous variants of equations and unite
them in groups, forming different models with distinct names and they may have various
optimization objectives.
5.7. Solving a Model
Once a model has been defined, the next step is to solve it. This is ordered by including a
SOLVE statement in the model. Consider the following example:
SOLVE Reservoir USING NLP MAXIMIZING obj;
The SOLVE statement contains several parts:
•
The keyword SOLVE;
•
The previously defined name of the model (Reservoir in our example);
•
The keyword USING;
•
The type of “solver” to use in the solution (NLP or nonlinear programming, in this case).
•
The keyword MAXIMIZING (in out case, but other models will use MINIMIZING); and
•
The name of a scalar variable (obj in this case) to be optimized
5.8. Model Output
The GAMS software can write information about the model and its solution to text files. The
syntax for defining output text files is:
FILE pointer_name comments / external_filename /
24
where FILE is a keyword used to define files; pointer_name is a pointer used by GAMS model
to direct output to the external text file external_ filename; and external_filename is an
external text file which receives the output from GAMS. In a river basin model, we may have
FILE result /Basin_Results.txt/;
PUT result
where result is a pointer to the external file named “Basin_Results.txt” that will receive the
model output. The pointer to the output file is named result in the model and it directs
information into the file Basin_Result.txt. The PUT operator is used for both assigning the
name of the “current_file” to a declared text file and writing information into this file. This
example shows how the pointer res is associated with the external text file Basin_Result.
Then, the output is directed into the file associated with the pointer result. Consider the
following Put statements that occur later on in the river basin model:
PUT /"Weights ";
PUT /" wA
= ", wA:7:3
PUT /" wB
= ", wB:7:3;
PUT /" wC
= ", wC:7:3/;
In the first line, some text (Weights) is inserted into the file. Then, the values of the objective
function weights are written to the file preceded by text stating what each value corresponds to
and the format for printing (field seven spaces wide with three places to the right of the decimal
point).
Sometimes, it is necessary to output the value of parameters or tables. This usually
requires the use of the LOOP function. Consider the following example:
PUT /"Reservoir (Mm3)";
PUT /"
PUT /"Initial
Storage
Input
Release ";
", beg_S:9:0/;
LOOP(t,PUT t.TL, S.L(t):9:0, Q(t):9:0, R.L(t):9:0/;);
25
The first line prints a title for the table, and then, on the second line, a header at the top of the
table. The third line prints the initial value of storage used in the calculation. The fourth line
loops over all the elements of the set t and prints the value of t, S(t), Q(t), and R1(t).
Note that the variables S(t) and R(t) need the suffix ”.L”, but the parameter Q(t) does not.
This is because Q(t) is a defined data value whose “level” is not determined by GAMS.
However, the others are variables and their levels are determined in the solution process.
5.9. IF-ELSE Operator
The IF-ELSE operator is useful for transferring from one operator to another. In some cases, it
can be written down as a set of $ conditions. The IF operator can be used for making GAMS
code more understandable.
The optional "ELSE" part allows formulating the traditional construction "IF-THEN-ELSE".
The following syntax is for the "IF-THEN-ELSE" operator:
if (condition,
operators;
{condition ELSEIF, operators}
[operators ELSE;]
);
Note, the braces and brackets are not required, but can be used. "Condition" means logical
conditions described in the paragraph on conditional operations of GAMS.
Declaration or definition of equations can not be performed within the IF operator. Consider the
following expressed through the IF-ELSEIF-ELSE operators:
IF (f <= 0,
p(i) = -1;
26
q(j) = -1;
ELSEIF ((f > 0) AND (f < 1)),
p(i) = p(i)**2;
q(j) = q(j)**2;
ELSE
p(i) = p(i)**3;
q(j) = q(j)**3;
);
REFERENCES
Brooke, A., D. Kendrick, A. Meeraus, and R. Raman (2006). GAMS Language Guide. Gams
Development Corporation. Washington D.C.
McKinney, D.C., and A. Savitsky, Basic Optimization Models for Water and energy
management, Technical Report, Center for Research in Water Resources, University of Texas at
Austin, 2006
27
Download