GAMS: A POWERFUL
OPTIMIZATION TOOL AND ITS
INTERFACE TO MATLAB
Muhammad Ismail
Outline
Introduction
GAMS Input File
GAMS Output
MATLAB Interface
2
Introduction
- GAMS = General Algebraic Modeling System
- Model linear, nonlinear, and mixed integer
optimization problems
- GAMS can be downloaded from its home page:
http://www.gams.com/
- Without a valid GAMS license, GAMS will operate as
a free demo with limitations on number of constraints
and variables [GAMS license is placed, when
available, in the GAMS directory]
3
GAMS Structure and MATLAB Interface
GAMS Input File
(name.gms)
GAMS Output File
(name.lst)
GAMS
Compilation
Optimization
Solver
MATLAB
4
Outline
Introduction
GAMS Input File
GAMS Output
MATLAB Interface
5
GAMS Input File Structure
Sets
Data
Variables
Equations
Model & Call Solver
Correspond to indices in you
problem
Can be: parameters, tables,
and scalar
Can be: free (-β,+β), +ve, -ve,
binary, integer
Equalities and inequalities
LP, NLP, MIP, MINLP
6
Problem Example
- Consider π networks with overlapped coverage and π users
- Out of π users, we have:
π1 users can get service (bandwidth) using only single-network
π2 users can get service (bandwidth) using all available networks,
i.e. multi-homing
- Obj.: Find network assignment for single-network users and
bandwidth allocation for both single-network and multi-homing
users which maximizes some utility function
7
Mathematical Formulation
Max {log 1 + π₯πππππ β ππππ₯πππππ}ππ
S.t. π΅πππ,π β€ π₯πππππ β€ π΅πππ₯,ππ βπ
π₯ππ = 1, π₯ππ = 0,1 , βπ β π1π
π₯ππ = 1, βπ β π2
Assignment variable
Binary
Bandwidth allocation variable
Positive
Priority parameter
Matrix Table
Total max. and
min. required
BW
Vector Problem MINLP
π₯πππππ β€ πΆππ βπ
Capacity of each network
Vector
8
GAMS Input File
Sets
Data
Variables
Equations
Model & Call Solver
π,π
πππ Table, π Scalar
πΆπ, π΅πππ₯,π, π΅πππ,π parameters
π₯ππ Binary
πππ Positive
Objective function and constraints
(equalities and inequalities)
MINLP, solver = BARON
9
π§ free
GAMS Input File Cont.
Set declaration
End Set declaration
Sets names
Comments
Sets members
1. Sets:
2. Data:
Table declaration Table name Domains = rows and columns
Columns = same as M
Rows = same as N Data End Table
10
GAMS Input File Cont.
2. Data Cont.: Parameters
Declaration
Parameters
Names
And
Domain
Data
End
Parameters
Declaration
Scalar
Declaration
Scalar Name
Scalar Value End
3. Variables:
Declare
Variables Obj. fn. Value BW & Assign.
Variables types
z = free by default
11
GAMS Input File Cont.
4. Equations:
Declare Equations
End Equations
Equations
Names and
Domain =
β€
β₯
Condition:
Apply this only
for M = 1:3, i.e.
only for multi-
homing users
12
GAMS Input File Cont.
4. Model & Call Solver:
Problem Type Solver Name
Problem Modeling
Maximize obj. fn.
13
GAMS Input File Cont.
Problem type
& Solver Sets
Data
Variables
Equations
Model & Solve
14
Outline
Introduction
GAMS Input File
GAMS Output
MATLAB Interface
15
GAMS Outputs
Run your Model
Name of
my GAMS
input file
16
GAMS Outputs Cont.
Listing file
Check the
results of your
variables
B is a Matrix
π΅ =0.101 β¦ .0.411 β¦ .
Rows =
Networks
Columns = Users
Add bounds
Necessary for BARON to guarantee global optimality
17
GAMS Outputs Cont.
Double click on error gives you
its location in your code
18
Outline
Introduction
GAMS Input File
GAMS Output
MATLAB Interface
19
MATLAB Interface
Why?
- Test how performance changes with some parameters, e.g.
how does bandwidth allocation changes with number of users?
- If you have a simulation on MATLAB and part of your MATLAB
code needs to solve a complex optimization problem
- Better view of your results Matrix form, or a plot
GDX Utilities GAMS Data Exchange
20
MATLAB Interface Cont.
Step 1: Update MATLAB Path to Include GAMS Directory:
MATLAB: File > Set path > Add folder
Browse
Add
21
MATLAB Interface Cont.
Step 2: Update System Variables to Include GAMS Directory:
Computer: Right click > Properties > Advanced system settings
Environment Variables Edit and Type GAMS Path
22
MATLAB Interface Cont.
Step 3: Modify your GAMS Input File:
No members
No Values
Comes from MATLAB
1. Open a file tstdat.gdx: βgdxin
filenameβ tstdat created by MATLAB
2. Load set members and parameters
values from this file
3. Close the file: βgdxinβ
Put the variables you want to read
on MATLAB in file tstsol.gdx ;
23
MATLAB Interface Cont.
Step 4: MATLAB File:
A. Create MATLAB function: inputs = set members and parameters
data, outputs: problem variables
B. Define Sets
Same as in my GAMS input file
24
MATLAB Interface Cont.
C. Define Parameters
Vector
Scalar Table
25
MATLAB Interface Cont.
D. Write Values in tstdat.gdx
Write function
E. Run the GAMS Input File
Avoid crash
with loops
Function: Run
GAMS input file
Name of my
GAMS input file
26
MATLAB Interface Cont.
F. Read GAMS Results on MATLAB
My GAMS Variables
Read function
27
MATLAB Interface Cont.
Define sets
Define
Parameters
Write set members and
parameters values Run GAMS input file
Read GAMS results
28
MATLAB Interface Cont.
G. Save GAMS Input File and MATLAB function on Same Directory
H. On MATLAB Command Window
Input
Data Run GAMS and Read Results
Results
29
MATLAB Interface Cont.
Also, I can make a for loop in MATLAB on one of the parameters and
fix other parameters to study some performance and plot results on
MATLAB
30
Summary
Introduction
GAMS Input File
GAMS Output
MATLAB Interface
31