Handout 1 Intro. To GAMS
Handout 1 Intro. To GAMS
Ignacio E. Grossmann
Department of Chemical Engineering
Carnegie. Mellon University
Pittsburgh, PA 15213
GAMS is a modelling system for optimisation that provides an interface with a variety of
different algorithms. Models are supplied by the user to GAMS in an input file in the
form of algebraic equations using a higher level language. GAM then compiles the model
and interfaces automatically with a solver (i.e. optimising algorithm). The compiled
model as well as the solution found by the solver are then reported back to the user
through an output file. The simple diagram below illustrates this process.
Input file
Model
GAMS
Compilatio
n of Model
Output file
Results
Optimisation
solver
The conventions for naming the extension of the files are as follows:
Input file:
Filename.gms
Output file:
In order to compile and execute the input file, the command is simply:
GAMS filename
The GAMS input file is in general organised into the following sections:
(1) Specification of indices and data
(2) Listing of names and types of variables and equations (constraints and objective function)
(3) Definition of the equations (constraints and objective function)
x1 x3 0
x1 x22 x1 x2 4 0
0 x1 5
(1)
0 x2 3
0 x3 3
(2)
(3)
Using (3) instead of (2) is a much better choice, not only because we avoid the
potential for a division by zero, but also because we obtain a linear constraint which is
easier to handle.
The GAMS input file TEST.gms for the rearranged problem in (1) is given in the
next page.
First note that the $ sign in column 1 is a control directive, the first for specifying
the title, the other two for suppressing some details in the output (e.g. map of symbols).
In general, you will always include these keywords (see also pp. 112-113, GAMS Users
Guide).
The keyword VARIABLES is used to list our variables X1, X2 and X3. Note that Z,
the objective function value must also be included. The keyword POSITIVE
VARIABLES is used to specify the non-negativity of x 1, x2, and x3. The objective
function values should not be included here as in general it might take positive or
negative values. Finally, note that the semicolon (;) must be included to specify the end of
the lists.
Next, the keyword EQUATIONS is for listing the names of constraints and
objective function. The names are arbitrary and here we have selected the names CON1,
CON2, CON3, for the constraints, and OBJ for the objective function.
The actual equations are then defined by first listing the name followed by two
periods. Note that the following syntax must be used for the equality and inequality signs.
=E=
for =
=G=
for
=L=
for
Also note that the basic arithmetic operations are
+, -
addition, subtraction
*, /
multiplication, division
**
exponent
In the objective function we could have expressed the equation as:
OBJ.
Z E X 1 * *2 X 2 * *2 X 3 * *2 ;
However, for illustration purposes, we have used the function SQR, which
performs the square of the variables. Table 6.1 in p. 69 of the Users Guide gives a
complete listing of the standard functions that are available in GAMS.
Next in the input file we specify the upper bounds and initial values. This is done
by adding a subfield to the variables. The format is a period followed by a character, and
they are as follows
.LO
lower bound
.UP
upper bound
.L
.M
* TEST.gms
$TITLE TEST PROBLEM
$OFFSYMXREF
$OFFSYMLIST
* Example from problem 8.26 in "Engineering Optimisation" by
* Reklaitis, Ravindran and Ragsdell (1983)
VARIABLES
POSITIVE VARIABLES
EQUATIONS
Z ;
X1, X2, X3 ;
CON1..
X2 - X3 =g= 0;
CON2..
X1 - X3 =g= 0;
CON3..
OBJ..
* Upper Bounds ;
X1.UP = 5 ;
X2.UP = 3 ;
X3.UP = 3 ;
* Initial Point ;
X1.L = 4 ;
X2.L = 2 ;
X3.L = 2 ;
MODEL TEST / ALL / ;
OPTION LIMROW = 0 ;
OPTION LIMCOL = 0 ;
SOLVE TEST USING NLP MINIMIZING Z ;
The main solver types available in GAMS are as follows:
LP
linear programming
NLP
non-linear programming
MIP
RMIP
MIDNLP
The optimisation software that is available in GAMS is as follows for each solver type:
LP
MIP, RMIP
ZOOM, SCICONIC*
NLP
MINOS5.2, SQP*
MIDNLP
DICOPT++
If we now run GAMS with our input file TEST.gms we obtain the output file
TEST.lst which is shown in the next two pages.
Note that the first part of the output is identical to the input file. NEXT, statistics
on the problem size are reported (e.g. 4 variables: X1, X2, X3, Z ; 4 equations: 3
constraints and objective). The derivative pool refers to the fact that analytical gradients
for the non-linear model have been generated by GAMS.
The solve summary indicates that the optimum has been found (local because it is
NLP) with an objective function value Z = 7.2177. A detailed explanation of the other
output in this section can be found in pp. 117-119 of the Users Guide.
Finally, information on the equations and variables are listed. The column labeled
LEVEL gives the actual values. So for instance X 1 = 2.526, X2 = 0.916, X3 = 0, Z =
7.218. The columns LOWER and UPPER give the lower and upper bounds, while the
column MARGINAL gives the dual process or multipliers. So for instance, the third
constraint has a multiplier of 2.637, as the equation is an active constraint. The first two
constraints have zero multipliers since they are not active at the lower or upper bounds.
More details on the output can be found in pp. 120-121 of the Users Guide.
09/03/08 10:11:44
Page 1
TEST PROBLEM
Compilation
4
5 * Example from problem 8.26 in "Engineering Optimization" by
6 * Reklaitis, Ravindran and Ragsdell (1983)
7
8 VARIABLES X1, X2, X3,
Z ;
X2 - X3 =g= 0 ;
14 CON2..
X1 - X3 =g= 0 ;
15 CON3..
16 OBJ..
17
18 *Upper Bounds ;
19 X1.UP = 5 ;
20 X2.UP = 3 ;
21 X3.UP = 3 ;
22
23 *Initial Point ;
24 X1.L = 4 ;
25 X2.L = 2 ;
26 X3.L = 2 ;
27
28 MODEL TEST / ALL / ;
29
30 OPTION LIMROW = 0 ;
31 OPTION LIMCOL = 0 ;
32
33 SOLVE TEST USING NLP MINIMIZING Z ;
COMPILATION TIME
= 0.000 SECONDS
TEST PROBLEM
Model Statistics
MODEL STATISTICS
BLOCKS OF EQUATIONS
SINGLE EQUATIONS
BLOCKS OF VARIABLES
SINGLE VARIABLES
10
DERIVATIVE POOL
CODE LENGTH
42
GENERATION TIME
EXECUTION TIME
CONSTANT POOL
16
0.016 SECONDS
0.016 SECONDS
TEST PROBLEM
Solution Report
SOLVE
S U M M AR Y
MODEL TEST
OBJECTIVE Z
TYPE NLP
DIRECTION MINIMIZE
SOLVER CONOPT
FROM LINE 33
1 NORMAL COMPLETION
2 LOCALLY OPTIMAL
7.2177
0.031
11
EVALUATION ERRORS
LOWER
1000.000
10000
0
LEVEL
UPPER
MARGINAL
0.916
+INF
2.526
+INF
4.000
4.000
4.000
2.637
1.000
.
LOWER
.
LEVEL
UPPER
MARGINAL
---- VAR X1
2.526
5.000
---- VAR X2
0.916
3.000
4.2054E-8
---- VAR X3
3.000
EPS
7.218
+INF
---- VAR Z
-INF
10
NONOPT
0 INFEASIBLE
0 UNBOUNDED
0
ERRORS
EXECUTION TIME
USER: J M Chawla
0.000 SECONDS
G070921:1723CP-WIN
DC6648
C:\WINDOWS\gamsdir\learning nlpp.gms
D:\My Documents\gamsdir\projdir\learning nlpp.lst
11
Example 2
Consider the problem of assigning process streams to heat exchangers as
described in pp. 409-410 of the book Optimisation of Chemical Process by Edger and
Himmelblau. The optimisation problem is given by
n
s.t.
ij
i 1
x
j 1
ij
j 1.......n
i 1......n
xij 0,1
i 1....n,
j 1....n
(4)
which corresponds to the well known assignment problem. Here I represent the index for
the n streams and j the index for the n exchangers. The binary variable xij = 1 if stream I
is assigned to exchanger j, and xij = 0 if it is not. The two equations simply state that
every exchanger j must be assigned to one stream, and every stream I must be assigned to
one exchanger.
The cost Cij of assigning stream I to exchanger j is as follows:
Exchangers
Stream
94
54
68
74
10
88
82
73
88
76
11
74
81
21
We can formulate the above problem in GAMS almost in the form of the
formulation in (4) using index sets. As shown in the output file HEAT.lst in the next page,
the structure of this file is similar to the one of example 1, except mainly for the use
indices. Note that the data are given in terms of SETS and the TABLE. (see pp. 43-47 and
pp. 53-57 in the Users Guide). The elements of a set can be names (A, B, C, D) or
12
numbers. In the latter case we can use the * sign to denote the range (1*4, means 1, 2, 3,
4). For the TABLE there is no need to place the numbers in precise positions; they only
have to be consistent. Data can also be entered using the keywords SCALAR and
PARAMETERS (see p. 53 and pp. 51-53 of the Users Guide). Note that we can specify
xij as a variable with indices, X (I, J); and likewise the two equations ASSI (J) (means for
j = 1, 2, 3, 4), ASSJ (I) (means for I = A, B, C, D). Also, in this case since xij is restricted
to 0-1 values, we use the keyword BINARY VARIABLES. The summation is expressed
in the form of SUM) index of summation, summoned) (see pp.17-18 of Users Guide).
Last, for the input, we have used the OPTION SOLPRINT=OFF statement, so as
to only print the values of the variables xij and the objective Z. This is done with
DISPLAY keyword where we list the level value of the xij (X.L no indices are needed)
and of Z (Z.L). More information on DISPLAY can be found in pp. 143-148 of the Users
Guide. Finally, note that in the SOLVE statement we specify the solver MIP due to the
fact that the xij are binary variables. The solve summary indicates that the optimum
objective function is Z=97. We also note that the solution was obtained from the relaxed
LP. This is nor surprising since it is well known that the assignment problem has a
unimodular matrix and therefore the solutions for the x ij are guaranteed to be 0-1 if we
solve the problem as an LP (you may try this as simple experiment).
Finally, due to the use of the DISPLAY statement the requested variables are
printed. Note that stream A is assigned to exchanger 4, B to 2, C to 3 and D to 1, with a
minimum cost of 97.
13
14
G e n e r a l Al g e b r a i c M o d e l i n g S y s t e m
Compilation
1 *
2 *ASSIGNMENT PROBLEM FOR HEAT EXHCANGES FROM..
3
4 SETS
5
STREAMS
/A, B, C, D /
EXCHANGES
/ 1*4 /
7
8
9
10
11
94
54
68
12
74
10
88
82
13
73
88
76
14
11
74
81
21
15
16
VARIABLES
X(i, j) ,
17
BINARY VARIABLES
Z ;
X(i,j) ;
18
19
20
21
22
23
OBJ..
24
25
26
OPTION LIMROW = 0
;
15
27
OPTION LIMCOL = 0
28
29
30
COMPILATION TIME
0.000 SECONDS
G e n e r a l Al g e b r a i c M o d e l i n g S y s t e m
Model Statistics
MODEL STATISTICS
BLOCKS OF EQUATIONS
SINGLE EQUATIONS
BLOCKS OF VARIABLES
SINGLE VARIABLES
17
49
=
DISCRETE VARIABLES
0.015 SECONDS
0.015 SECONDS
16
G e n e r a l Al g e b r a i c M o d e l i n g S y s t e m
Solution Report
16
SOLVE
S U M M AR Y
MODEL HEAT
OBJECTIVE Z
TYPE
DIRECTION MINIMIZE
MIP
SOLVER CPLEX
FROM LINE 30
1 NORMAL COMPLETION
1 OPTIMAL
97.0000
0.062
1000.000
10000
97.000000
Final Solve:
97.000000
Best possible:
97.000000
Absolute gap:
0.000000
Relative gap:
0.000000
(7 iterations, 0 nodes)
(0 iterations)
17
LEVEL UPPER
MARGINAL
1.000
1.000
1.000
65.000
1.000
1.000
1.000
1.000
1.000
1.000
1.000
54.000
1.000
1.000
1.000
68.000
LEVEL
UPPER
MARGINAL
1.000
1.000
1.000
1.000
1.000
1.000
9.000
1.000
1.000
1.000
8.000
1.000
1.000
1.000
-54.000
LOWER
---- EQU OBJ
LEVEL
UPPER
MARGINAL
1.000
---- VAR X
LOWER
LEVEL
UPPER
MARGINAL
A.1
1.000
29.000
A.2
1.000
EPS
A.3
11.000
18
A.4
B.1
B.2
1.000
1.000
1.000
1.000
1.000
B.3
1.000
25.000
B.4
1.000
5.000
C.1
1.000
C.2
1.000
79.000
C.3
1.000
-54.000
C.4
1.000
.
1.000
19
G e n e r a l Al g e b r a i c M o d e l i n g S y s t e m
VAR X
LOWER
LEVEL
UPPER
MARGINAL
D.1
1.000
1.000
D.2
1.000
127.000
D.3
1.000
81.000
D.4
1.000
7.000
LOWER
---- VAR Z
LEVEL
-INF
97.000
EXECUTION TIME
USER: J M Chawla
UPPER
MARGINAL
+INF
NONOPT
INFEASIBLE
UNBOUNDED
0.000 SECONDS
G070921:1723CP-WIN
DC6648
20
21