Working with GAMS Dr. Hussam Alshraideh
Working with GAMS
Dr. Hussam Alshraideh
Mathematical Modeling
Real-world
problem
Mathematical
model
assumptions,
abstraction, data
makes sense?
Solution to
model
Solution to
real-world problem
optimization
algorithm
interpretation
makes sense?
change the model,
assumptions?
General Algebraic Modeling System (GAMS)
• High-level modeling system for mathematical programming and optimization
• Algebraic formulation : closeness to mathematical notation
• Calling appropriate Algorithms• Calling appropriate Algorithms
• Efficient handling of mathematical optimization
problems
• Simple model building and solution process
• Increase productivity and maintainable models
GAMS Modeling Process
GAMS
Compilation
of Model
Input file:
MODEL
Output file:
RESULTS
Optimization
SOLVER
ExampleProblem Statement
Several forms of gasoline are produced during the petroleumrefining process, and a last step combines them to obtain marketproducts with specified quality measures. Suppose the followingfour gasoline products are available:
Type Quality Index-1 Quality Index-2 Cost($/Barrel)Type Quality Index-1 Quality Index-2 Cost($/Barrel)
1 99 210 48
2 70 335 43
3 78 280 58
4 91 265 46
Determine the minimum cost blend which has quality index-1
between 85 and 90 and quality index-2 between 270 and 280.
Mathematical Formulation
Mathematical Formulation
)1(909178709985
)(46584348
4321
4321
−≤+++≤
+++
ionSpecificatIndexQualityxxxx
toSubject
blendofCostxxxxMin
)2941.45,4706.0,0000.0,3529.0,1765.0:(
10101010
1
)2(280265280335210270
*
4
*
3
*
2
*
1
4321
4321
4321
=====
≤≤≤≤≤≤≤≤
=+++
−≤+++≤
CostxxxxSolution
xxxx
xxxx
ionSpecificatIndexQualityxxxx
sets
i set of gasoline /G1*G4/
j set of quality indices /Q1,Q2/;
parameters
cost(i) cost of gasoline set i
/G1 48
G2 43
G3 58
G4 46/
quality_spec_min(j) quality specifications -minimum
/Q1 85
Q2 270/
quality_spec_max(j) quality specifications-maximum
/Q1 90
equations
objective objective or cost function
min_spec_cons(j) minimum Quality indexconstraint
max_spec_cons(j) maximum Quality index constraint
tot_frac_cons total fraction=1 constraint ;
***********Model Definition********************
objective ..
tot_cost =e= sum(i,cost(i)*x(i));
min_spec_cons(j) ..
sum(i,quality_val(i,j)*x(i)) =g= quality_spec_min(j);
max_spec_cons(j) ..
sum(i,quality_val(i,j)*x(i)) =l= quality_spec_max(j);
tot_frac_cons ..
sum(i,x(i)) =e= 1;Q2 280/;
Table quality_val(i,j) quality indices
Q1 Q2
G1 99 210
G2 70 335
G3 78 280
G4 91 265;
variables
tot_costtotal cost in $
x(i) fraction of each Gasoline set;
sum(i,x(i)) =e= 1;
x.lo(i) = 0; x.up(i) = 1;
***********************************************
model blend /
objective,min_spec_cons,max_spec_cons,tot_frac_cons/;
***********Solve Statement*********************
solve blend using lp minimizing tot_cost;
***********************************************
displayx.l;
Page 1 Page 2
Structure of a GAMS Model
Inputs Outputs
• Sets
Declaration
Assignment of members
• Data
(Parameters, Tables, Scalar)
Declaration
Assignment of values
• Echo Print
• Reference Maps
• Equation Listings
• Status Reports
• Results
Assignment of values
• Variables
Declaration
Assignment of type
• Assignment of bounds and/or initial values
(optional)
• Equations
Declaration
Definition
• Model andSolve statements
• Display statement(optional)
sets
Sets are the basic building blocks of a GAMS
model, corresponding exactly to the indices in
the algebraic representations of models.
Text is optional. It’s like a sets
i set of gasoline /G1,G2,G3,G4/
j set of quality indices /Q1,Q2/;
•Any name of your choice
• No space in any name
• G1*G4 is same as G1,G2,G3,G4
Text is optional. It’s like a
comment
Mathematically:
i = {G1, G2,G3,G4}
j = {Q1,Q2}
parameters
parameters are for inserting given data
parameters
cost(i) cost of gasoline set i
/G1 48
G2 43 Name of the G2 43
G3 58
G4 46/
quality_spec_min(j) quality specifications -minimum
/Q1 85
Q2 270/
quality_spec_max(j) quality specifications-maximum
/Q1 90
Q2 280/;
Name of the
parameterGiven data
Optional text, useful
for understanding
Tables
Table quality_val(i,j) quality indices
Q1 Q2
G1 99 210
G2 70 335
G3 78 280G3 78 280
G4 91 265;
• Very useful for larger datasets
• GAMS will perform domain checking to make sure
that the row and column names of the table are
members of the appropriate sets.
General Comments on Data Entry
• Direct Assignment
e. g cost(‘Q1') = 48;
• Zero is the default value for all the parameters.
• A scalar is regarded as a parameter that has no domain. e.g..
Scalar f cost in dollars per mile/90/ ;Scalar f cost in dollars per mile/90/ ;
• The same parameter can be assigned a value more than once.
Each assignment statement takes effect immediately and
overrides any previous values.
• The same parameter may not be declared more than once
• Element-value can be separated by commas or entered as
separate lines
• Entire list must be enclosed in slashes
variables
variables
tot_cost total cost in $
x(i) fraction of each Gasoline set;
Every GAMS optimization problem must contain at least one variable which is
minimized or maximized (tot_cost in this example)
Variable Type Allowed range of variables
Free -∞ to +∞
Positive 0 to +∞
Negative -∞ to 0
Binary 0 or 1
Integer 0, 1, 2,….100
e. g Positive variable x;
Bounds
GAMS is designed with a small database system in which records
are maintained for the variables and equations.
.lo = lower bound
.l = level or primal value.l = level or primal value
.up = upper bound
.m = marginal or dual value
.fx = fixed value for the variable.
x.lo(i) = 0;
x.up(i) = 1;
Equations
equations
objective objective or cost function
min_spec_cons(j) minimum Quality index constraint
max_spec_cons(j) maximum Quality index constraint
tot_frac_cons total fraction=1 constraint;
Equations must be declared and defined in separate statements.
The power of algebraic modeling languages like GAMS is most
apparent in the creation of the equations and inequalities that
comprise the model under construction. This is because
whenever a group of equations or inequalities has the same
algebraic structure, all the members of the group are created
simultaneously, not individually.
Equation declaration :Summation
Format : Sum(index of summation, summand)
As a simple example, let us consider that the model has summation of x over i
1≥∑i
ix Sum(i,x(i)) =g=1
∑∑i
ij
j
ijxc Sum(i, Sum(j, c(i,j)*x(i,j)))
Sum((i,j), c(i,j)*x(i,j))
or
Other types of indexed operations
1≥∏i
ix prod(i,x(i)) =g=1
Product over controlling index
Maximum value over controlling indexMaximum value over controlling index
max_demand = smax((i,j), demand_array(i,j))
Minimum value over controlling index
min_distance = smin((p,q), distance(p,q))
Equation Declaration
• The format of the declaration is the same as
for other GAMS entities.
objective.. tot_cost =e= sum(i,cost(i)*x(i));objective.. tot_cost =e= sum(i,cost(i)*x(i));
1. The name of the equation being defined
2. The symbol '..’
3. Left-hand-side expression
4. Relational operator: =l=, =e=, or =g=
5. Right-hand-side expression
Equations
min_spec_cons(j) .. sum(i,quality_val(i,j)*x(i)) =g= quality_spec_min(j);
max_spec_cons(j) .. sum(i,quality_val(i,j)*x(i)) =l= quality_spec_max(j);
tot_frac_cons .. sum(i,x(i)) =e= 1;
1
)2(280265280335210270
)1(909178709985
4321
4321
4321
=+++
−≤+++≤
−≤+++≤
xxxx
ionSpecificatIndexQualityxxxx
ionSpecificatIndexQualityxxxx
Model
model blend /all/ ;
model blend /
objective,min_spec_cons,max_spec_cons,tot_frac_cons/;
or
• Model is a collection of Equations.
• Like other GAMS entities, it must be given a name in a
declaration.
• The format of the declaration is the keyword Model followed by
the name of the model, followed by a list of equation names
enclosed in slashes.
• If all the defined equations are to be used /all/ can be used
Solve
The format of the solve statement is as follows:
1. The keyword solve
2. The name of the model to be solved
3. The keyword using
4. An available solution procedure. Examples are
solve blend using lp minimizing tot_cost;
4. An available solution procedure. Examples are
• lp for linear programming
• nlp for nonlinear programming
• mip for mixed integer programming
• rmip for relaxed mixed integer programming
• minlp for mixed integer nonlinear programming
5. The keyword minimizing or maximizing
6. The name of the variable to be optimized
Display
• The solve statement will cause several things to happen when executed.
– The specific instance of interest of the model will be generated
– the appropriate data structures for inputting this problem to the solver will be createdsolver will be created
– the solver will be invoked
– the output from the solver will be printed to a file. (*.lst)
– To get the optimal values of the primal and/or dual variables, we can look at the solver output, or, if we wish, we can request a display of these result from GAMS as follows;
display x.l; for final values of x
Comments in the code
• Comments can be added in two ways;• Use the * sign at the beginning of the line
* This is a comment line
• Use the commands• Use the commands– $ontext
– $offtext
$ontext
This is a comment line
$offtext
GAMS output
• Echo print
• Error messages
• Reference maps
• Model statistics• Model statistics
• Status reports
• Solution reports
Echo Print and Error messages
• Echo Print:
– Copy of your input file with line numbers
– Irrespective of errors
• Error messages:• Error messages:
– Coded error message inside the echo print on the line
immediately following the scene of offense
– Messages start with **** and contain a $ followed
by a numerical error code
– Explanation of numerical code after the echo prints
Reference maps
• Include $onsymxrefas the first line in the script
• Cross reference maps
– This lists the named items (Sets, Parameters, Variables, Equations, Models, Files, Acronyms) in alphabetical order
– identifies them as to type, shows the line numbers where the symbols appear, and classifies each appearancesymbols appear, and classifies each appearance
SYMBOL TYPE REFERENCES
blend MODEL declared 55 defined 55 impl-asn 59 ref 59
cost PARAM declared 8 defined 9 ref 41
• List of model entities
– All the model entities are grouped by type
– Also lists documentary text associated with entities
Status and Solution reportsS O L V E S U M M A R Y
MODEL blend OBJECTIVE tot_cost
TYPE LP DIRECTION MINIMIZE
SOLVER CPLEX FROM LINE 59
**** SOLVER STATUS 1 Normal Completion1 Normal Completion**** MODEL STATUS 1 Optimal
**** OBJECTIVE VALUE 45.2941
RESOURCE USAGE, LIMIT 0.000 1000.000
ITERATION COUNT, LIMIT 4 2000000000
---- EQU min_spec_cons minimum Quality index constraint
LOWER LEVEL UPPER MARGINAL
G1 . 0.1765 1.0000 .
G2 . 0.3529 1.0000 .
G3 . . 1.0000 13.0000
G4 . 0.4706 1.0000 .
Useful links/Reference
• Online GAMS documentation
http://www.gams.com/docs/document.htm
• GAMS World : http://www.gamsworld.org/• GAMS World : http://www.gamsworld.org/
Exercise
• Assume a fifth product called Kerosene “K1” is
added to the list of products. The cost of this
product is $42 and it has quality index 1 of 80
and quality index 2 of 270. Modify the GAMS and quality index 2 of 270. Modify the GAMS
code to find the optimal mix that minimizes
the total cost.