1. Basic Structure of GAMS programs .. 1 1. Basic Structure of GAMS

1. Basic Structure of GAMS programs
GAMS statements
-
Can be placed anywhere on a line
Each statement must end with a semi-colon.
Can be divided into or continued over several lines.
Example 1
statement1; statement2;
Example 2
statement1;
statement2;
Example 3
statement1;
statement2
;
Types of GAMS statements:
SET: Declare and define model sets and, accordingly, indices
PARAMETER, TABLE and SCALAR: Declare and define model data
VARIABLE: Declare decision variables
EQUATION: Declare model constraints
MODEL: Declare and define GAMS models
SOLVE: Solve GAMS models
DISPLAY: Print or display values of data (parameters, tables, and scalars) and
decision variables.
ABORT: Terminate GAMS when input data contain errors.
Assignment statements: Perform arithmetic operations on data and decision
variables
An '*' in the first column means that the line is a comment and it is ignored during
compilation.
Identifiers (names for SETS, PARAMETERS, TABLES, SCALARS,
VARIABLES, MODELS and EQUATIONS) must be:
Up to 10 characters in length
First character must be either a letter or a number
Other characters can be letters, numbers
Not a reserved word, e.g., set(s), param(eter)(s), table(s), sum, etc. (see
section 3.4.2 in the user‟s manual)
1. Basic Structure of GAMS programs
..1
Problem:
We wish to find the least expensive way to ship goods (e.g., repair parts,
ammunitions, personnel, etc.) from sources to destinations.
Supply
Sources
350
Seattle
X11
X21
Destinations
Demand
New York
325
Chicago
300
Topeka
275
X12
600
San
Diego
X22
X13
X23
Unitary shipping costs are proportional to the following distances:
To New
From
York
Seattle
2.5
San Diego
2.5
Chicago
Topeka
1.7
1.8
1.8
1.4
Formulate this model using the above decision variables (X11, X12, ..., X23)
1. Basic Structure of GAMS programs
..2
GAMS implementation following the above formulation:
See file Transport_Explict.gms (available from Common\Salmeron\Intoduction to
GAMS\Transport_Explict.gms)
* Declarations
POSITIVE VARIABLE
X11 Flow SE-NY, X12 Flow SE-CH, X13 Flow SE-TO
X21 Flow SD-NY, X22 Flow SD-CH, X23 Flow SD-TO
;
VARIABLE
Z total transportation cost
;
EQUATIONS
OBJ total transportation cost
SUPPLY_1 capacity at source 1 (SEATTLE)
SUPPLY_2 capacity at source 2 (SAN DIEGO)
DEMAND_1 demand at destination 1 (NEW YORK)
DEMAND_2 demand at destination 2 (CHICAGO)
DEMAND_3 demand at destination 2 (TOPEKA)
;
* End of declarations
* Formulation
OBJ..
Z =E=
2.5*X11 + 1.7*X12 + 1.8*X13 +
2.5*X21 + 1.8*X22 + 1.4*X23
;
SUPPLY_1..
SUPPLY_2..
X11
DEMAND_1..
DEMAND_2..
DEMAND_3..
X11
+ X12
+ X13
X21
+ X22
=L= 350;
+ X23 =L= 600;
+ X21
X12
+ X22
X13
+ X23
=G= 325;
=G= 300;
=G= 275;
* End of formulation
* Model Declaration, Solve, and Result Display
MODEL Transport /ALL/;
SOLVE Transport USING LP MINIMIZING Z;
DISPLAY
Transport.modelstat
Transport.solvestat
Z.l
X11.l, X12.l, X13.l, X21.l, X21.l, X23.l
SUPPLY_1.m, SUPPLY_2.m
DEMAND_1.m, DEMAND_2.m, DEMAND_3.m
;
Remark: Clearly, this formulation and implementation is not practical for real problems
(Why?)
1. Basic Structure of GAMS programs
..3
Look at the resulting LISTING (.LST) file: Transport_Explict.lst
FIRST THING TO LOOK UP IN THE LST FILE: THE SOLVE SUMMARY
S O L V E
MODEL
TYPE
SOLVER
S U M M A R Y
Transport
LP
CPLEX
**** SOLVER STATUS
**** MODEL STATUS
**** OBJECTIVE VALUE
OBJECTIVE
DIRECTION
FROM LINE
Z
MINIMIZE
29
1 NORMAL COMPLETION
1 OPTIMAL
1707.5000
Solver Status code: 1
This code means that the process was completed normally (e.g., it was not
interrupted due to exceeding a given maximum time, number of iterations,
memory allocation, etc.)
Model status code: 1
This code means that the solution obtained for this LP is guaranteed to be optimal.
(For a complete list of codes, see GAMS user‟s guide, pp. 105-106)
Now, we can look at the solution values printed by our DISPLAY statement:
MODEL Transport.ModelStat
MODEL Transport.SolveStat
VARIABLE Z.L
VARIABLE X11.L
VARIABLE X12.L
VARIABLE X13.L
VARIABLE X21.L
VARIABLE X22.L
VARIABLE X23.L
EQUATION SUPPLY_1.M =
EQUATION SUPPLY_2.M =
EQUATION DEMAND_1.M =
EQUATION DEMAND_2.M =
EQUATION DEMAND_3.M =
=
=
=
=
=
=
=
=
=
EPS
0.000
2.500
1.700
1.400
1. Basic Structure of GAMS programs
1.000
1.000
1707.500 total transportation cost
50.000
300.000
0.000
275.000
0.000
275.000
capacity at source 1 (SEATTLE)
capacity at source 2 (SAN DIEGO)
demand at destination 1 (NEW YORK)
demand at destination 2 (CHICAGO)
demand at destination 2 (TOPEKA)
..4
Model Formulation (NPS format):
Sets and indices
I,
set of sources, i I I={SE, SD}
J,
set of destinations, j J
Data
ai,
supply from source i I
bj ,
demand at destination j J
dij,
distance from source i to destination j, for every i
I, j
Decision Variables
xij,
units shipped from source i to destination j, for every i
Formulation
J
I, j
J
Next, we will learn how to implement this formulation in GAMS while learning the
general syntax for GAMS statements.
1. Basic Structure of GAMS programs
..5
Format for GAMS statements
SET(S) name1 comment / element1 comment, element2 comment, .../ , name2 comment / element1 comment, .../;
-
Commas can be replaced by end of line
SETS
I supply centers /SE Seattle, SD San Diego/, J demand centers /NY New-York, CH Chicago, TO Topeka/ ;
SETS I /SE Seattle, SD San Diego/
J demand centers /NY New-York, ch Chicago TO Topeka/ ;
SETS I supply centers /
SE Seattle
*
SD San Diego
/
J demand centers /
NY New-York
*
CH Chicago
TO Topeka
/
;
-
Comments are optional
SETS I / SE, SD /
J / NY, CH, TO /
;
-
At least one blank space between name and comment or element and
comment.
Each comment must fit on one line and contains no more than 80 characters
Names, comments and elements should not start with a reserved word or
symbol
Names for elements of a set must be:
Up to 10 characters in length
First character must be either a letter or a number
Other characters can be letters, numbers, plus sign ('+') or minus sign ('-')
Not a reserved word, e.g., set(s), param(eter)(s), table(s), sum, etc. (see
section 3.4.2 in the user‟s manual)
1. Basic Structure of GAMS programs
..6
SCALAR(S) name1 comment / value /, name2 comment / value / , name3 comment / value / ;
- Commas can be replaced by end of line
- Comments are optional
- At least one blank space between name and comment or element and comment
- „/value/‟ is optional
SCALAR F freight cost per mile /10/;
SCALAR
F /10/
G
H /30/
;
G = 20;
(The last statement is an “assignment” statement)
1. Basic Structure of GAMS programs
..7
PARAMETER(S) name1(indices) comment / indices = value, indices = value, ... /, name2(indices) comment / ... /;
-
Parameters can be single or multi-dimensional array
Latest versions of GAMS allow to define scalars as parameters
Commas can be replaced by end of lineComments are optional
At least one blank space between name(indices) and comment
„/indices = value, . . . /‟ is optional and it assigns a value to elements in the
array. „=‟ is optional too.
PARAMETERS
a(i) supply from source i /SE = 350/
b(j) demand at destination j /NY = 325, CH = 300, TO = 275/
;
PARAMETERS
a(i) /SE 350, SD 600/
b(j) /NY 325, CH 300, TO 275/
;
PARAMETERS A(i), B(j);
a('SE') = 350;
a('SD') = 600;
b("NY") = 325;
The first statement above just declares A and B as parameters that depend on the
elements of sets I and J, respectively.
The last three statements assign specific values to some components of these parameters.
Notice the equivalent use of „ ‟ and “ ”
PARAMETER
d(i,j) distance (in 1000 miles)
/
SE.NY= 2.5, SE.CH = 1.7, SE.TO = 1.8
SD.NY = 2.5, SD.CH = 1.8, SD.TO = 1.4
/
;
SETS I /i1,i2,i3/, J /j1,j2/, K /k1,k2,k3/;
PARAMETER T(i,j,k)
/i1.j1.k1 = 111, i3.j2.k3 = 323/ all the others are 0
;
PARAMETER Q(i,j), P(j,k), W(i,k);
Q(i,j)= 12;
P(j,k) = SUM(i, T(i,j,k));
1. Basic Structure of GAMS programs
Qi , j
Pjk
..8
TABLE name(indices) comment
colindices1
rowindices1 value
rowindices2 value
rowindices3 value
rowindices4 value
;
-
colindices2
value
value
value
value
colindices3
value
value
value
value
...
...
...
...
...
A TABLE statement can be used to declare and define only one table at a
time
Comment is optional
rowindices and colindices choices are arbitrary as long as the initial indices
order is maintained, starting with rowindices and continuing with colindices
TABLE d(i,j) distance (in 1000 miles)
NY
CH
TO
SE
2.5
1.7
1.8
SD
2.5
1.8
1.4
;
TABLE d(i,j) distance (in 1000 miles)
SE
SD
NY
2.5
2.5
CH
1.7
1.8
TO
1.8
1.4
;
SETS I /i1,i2,i3/, J /j1,j2/, K /k1,k2,k3/;
TABLE T(i,j,k)
k1
k2
k3
i1.j1 111
i1.j2
i2.j1
i2.j2
i3.j1
i3.j2
323
;
1. Basic Structure of GAMS programs
..9
SETS I /i1,i2,i3/, J /j1,j2/, K /k1,k2,k3/;
TABLE T(i,j,k)
j1.k1 j1.k2 j1.k3 j2.k1 j2.k2 j2.k3
i1
111
i2
i3
323
;
SETS I /i1,i2,i3/, J /j1,j2/, K /k1,k2,k3/;
TABLE T(i,j,k)
j1.k1 j1.k2 j1.k3
i1
111
i2
i3
+
j2.k1 j2.k2 j2.k3
i1
i2
i3
323
;
The “+” sign is used for defining a table with too many columns to fit on a line.
SETS I /i1,i2,i3/, J /j1,j2/, K /k1,k2,k3/;
TABLE T(i,j,k)
k1
k3
i1.j1 111
i3.j2
323
;
Rows and columns with all data equal to zero can be omitted
SETS I /i1,i2,i3/, J /j1,j2/, K /k1,k2,k3/;
TABLE T(i,j,k)
k1
i1.j1
;
However, you need to write at least one row and one column even if the table is
all zeros.
1. Basic Structure of GAMS programs
10..
VARIABLES name1(indices) comment, name2(indices) comment;
Used to define free (i.e., unrestricted) variables
POSITIVE VARIABLES name1(indices) comment, name2(indices) comment;
Used to define non-negative variables
NEGATIVE VARIABLES name1(indices) comment, name2(indices) comment;
Used to define non-positive variables
BINARY VARIABLES name1(indices) comment, name2(indices) comment;
Used to define 0-1 variables
INTEGER VARIABLES name1(indices) comment, name2(indices) comment;
Used to define integer variables
-
indices and comments are optional
Commas can be replaced by end of line
At least one blank space between name(indices) and comment
INTEGER variables can have values, by default, between 0 and 100
The decision variable that represents the value of the objective function must
be declared as a (free) VARIABLE.
POSITIVE VARIABLE
X(i,j) shipment quantities;
VARIABLE
Z total transportation cost;
INTEGER VARIABLE
X(i,j) shipment quantities;
VARIABLE
Z total transportation cost;
1. Basic Structure of GAMS programs
11..
EQUATIONS name1(indices) comment, name2(indices) comment;
-
Declare the existence of constraints (equalities, inequalities)
indices and comments are optional
Commas can be replaced by end of line
There is a lower bound, an upper bound, a level (or current value) and a
marginal (optimal dual) value associated with each equation and they are
referenced with the extension '.LO ', '.UP ', '.L' and '.M ', respectively. (Note:
you should never have to deal with these extensions because they should be
implicitly assigned by either GAMS or an algorithm/solver.)
To define the equation formulation:
name(indices)..
Left-hand side expression
Either “=E=” or “=G=” or “=L=”
Right-hand side expression
;
-
-
name should match one of the previously declared names in the
“EQUATIONS” section
indices are NOT optional
$(condition) is optional (see $ use)
Left-hand side expression and Right-hand side expression are symbolic
expressions involving data (SCALARS, PARAMETERS, TABLES and
VARIABLES). Expressions may include arithmetic operators, intrinsic
functions, conditionals (“$”), etc.
=E= is used for equality constraint, =L= for less than or equal to constraint,
and =G= for greater than or equal to constraint
1. Basic Structure of GAMS programs
12..
EQUATIONS
OBJ total transportation cost
SUPPLY(i) capacity at source i
DEMAND(j) demand at destination j
;
OBJ..
Z
=E=
Sum((i,j), d(i,j)*X(i,j) )
;
SUPPLY(i)..
Sum(j, X(i,j))
=L=
a(i)
;
DEMAND(j)..
Sum(i, X(i,j))
=G=
b(j)
;
1. Basic Structure of GAMS programs
13..
Bounding and Fixing Variables
There is a lower bound, an upper bound, a level (or current value) and a marginal optimal
reduced cost) value associated with each variable. They are referenced with the
extensions '.LO', '.UP', '.L' and '.M', respectively.
.FX is used to fix a variable to a given value
X.LO(i,j) = 2;
X.UP(i,j) = 100;
....
X.UP('SE','CH') = 100;
X.LO('SD','TO') = 50;
X.FX('SE','NY') = 10;
....
X.L('SD','TO') = 50;
1. Basic Structure of GAMS programs
14..
Declaring a Model
MODEL name1 / equation1, equation2, ... /, name2 / equation1, equation5, .../ ;
-
'equation1, equation2, ....' can be replaced by the word 'ALL', if the model
contains all equations that have been declared prior to the MODEL statement.
Commas can be replaced by end of line
MODEL Transport /ALL/;
MODEL Transport /OBJ, DEMAND/;
Solving a model
SOLVE model-name USING type-of-problem MINIMIZING variable-name;
or
SOLVE model-name USING type-of-problem MAXIMIZING variable-name;
type-of-problem:
LP (linear programming problem)
NLP (nonlinear programming problem)
MIP (mixed integer (linear) programming problem)
RMIP (Relaxed MIP - ask GAMS to solve the problem with integer and binary
variables treated as continuous (positive) variables, i.e., solve MIP as an LP
without changing from BINARY and INTEGER VARIABLES to POSITIVE
VARIABLES.
SOLVE Transport USING LP MINIMIZING Z;
SOLVE Transport USING MIP MINIMIZING Z;
1. Basic Structure of GAMS programs
15..
Displaying the Solution
DISPLAY name1, name2, name3;
-
name can be either name of PARAMETERS, SCALARS, TABLES, or
VARIABLE names with an extension .LO, .UP, .L, or .M
Commas can be replaced by end of line
Note that there is no index used in the DISPLAY statements
DISPLAY
Transport.modelstat
Transport.solvestat
X.l
X.m
d
Z.l
;
This statement asks GAMS to print ...
1. Basic Structure of GAMS programs
16..
So, putting everything together...
File Tansport.gms
SETS I supply centers /
SE Seattle
SD San Diego
/
J demand centers /
NY New-York
CH Chicago
TO Topeka
/
;
PARAMETERS
a(i) supply from source i /SE = 350, SD = 600/
b(j) demand at destination j /NY = 325, CH = 300, TO = 275/
;
TABLE d(i,j) distance (in 1000 miles)
NY
CH
TO
SE
2.5
1.7
1.8
SD
2.5
1.8
1.4
;
POSITIVE VARIABLE
X(i,j) shipment quantities;
VARIABLE
Z total transportation cost;
EQUATIONS
OBJ total transportation cost
SUPPLY(i) capacity at source i
DEMAND(j) demand at destination j
;
OBJ..
Z
=E=
Sum((i,j), d(i,j)*X(i,j) )
;
SUPPLY(i)..
Sum(j, X(i,j))
=L=
a(i)
1. Basic Structure of GAMS programs
17..
;
DEMAND(j)..
Sum(i, X(i,j))
=G=
b(j)
;
MODEL Transport /ALL/;
SOLVE Transport USING LP MINIMIZING Z;
DISPLAY
Z.l
X.l
Demand.m
Supply
;
1. Basic Structure of GAMS programs
18..
Results (File Transport.lst)
General Algebraic Modeling System
06/24/02 12:28:38 PAGE
1
Compilation
Windows NT/95/98
1
2 SETS
3
GAMS 2.50D
I supply centers /
SE Seattle
...
...
SOLVE
SUMMARY
MODEL Transport
OBJECTIVE Z
TYPE LP
DIRECTION MINIMIZE
SOLVER BDMLP
FROM LINE 55
**** SOLVER STATUS 1 NORMAL COMPLETION
**** MODEL STATUS 1 OPTIMAL
**** OBJECTIVE VALUE
1707.5000
...
----
58 VARIABLE Z.L
----
58 VARIABLE X.L shipment quantities
NY
CH
=
1707.500 total transportation cost
TO
SE
SD
50.000 300.000
275.000
275.000
----
58 EQUATION DEMAND.M demand at destination j
NY 2.500,
----
CH 1.700,
TO 1.400
58 EQUATION SUPPLY.M capacity at source i
SE EPS
1. Basic Structure of GAMS programs
19..
General Suggestions for Modeling with GAMS
-
Separate data from the formulation. In general, users are not knowledgeable in
GAMS or mathematical modeling and they should not be required to modify
your EQUATIONS or VARIABLES for each problem. Ideally, users would
have to change the set elements and values of SCALARS, PARAMETERS
and TABLES only.
-
Use a small data set with known solution to debug your GAMS program
(smaller data sets generate less variables and equations which are easier to
examine, and smaller problems which take less time to solve)
-
Use lower case for names of scalars, parameters, and tables. Use upper case
for name of variables. Or adopt any other convention (but always the same) in
your programs. Use the same index order for parameters, variables, etc. Be as
consistent as possible in you implementation to match your formulation (this
will help you debug innumerable problems easily when implementing largescale models).
1. Basic Structure of GAMS programs
20..