Gams Tutorial

17
Introduction To Applied Optimization GAMS TUTORIAL

description

gams

Transcript of Gams Tutorial

Page 1: Gams Tutorial

Introduction To Applied

Optimization

GAMS TUTORIAL

Page 2: Gams Tutorial

BioE 494

2

GAMS Basics

• GAMS: General Algebraic Modeling System (GAMS)

• High-level modeling system for mathematical programming and optimization

• Useful only for solving optimization problems

• Student version (with restrictions on number of variables and number of constraints) available at: http://www.gams.com/

Page 3: Gams Tutorial

BioE 494

3

GAMS Basics • GAMS advantages:

– Problem representation is much simpler: Intuitive representation due to the representation syntax

– Various output options to analyze the results and carry out debugging

– Incorporates different solvers for the given problem and various options to select desired solver properties

– Possible to define an integer variable

• GAMS disadvantages:

– No option for data visualization

– No option to include breakpoints to debug the code

– Performs only optimization: Cannot carry out other related tasks such as ODE simulation.

Page 4: Gams Tutorial

BioE 494

4

GAMS Environment

Single window to write and execute the code

Page 5: Gams Tutorial

BioE 494

5

GAMS Model Components

Page 6: Gams Tutorial

BioE 494

6

Set Declaration • Very important aspect of a GAMS model

• Declaration necessary for multi-dimensional parameter/variable declaration

• Simple set example: – courses={BioE 123, BioE 234, BioE 345}

– set courses / BioE 123, BioE 234, BioE 345 / ;

• Sequence set: – Year={2000, 2001, 2002, …… 2010}

– set Year /2000 * 2010/ ;

• Multiple sets with the same elements: – alias(Year,Horizon) ;

– Another set named “Horizon” created with same elements

Page 7: Gams Tutorial

BioE 494

7

Data Declaration: Scalar

• Single dimensional constant value

• Syntax:

– scalar ‘scalar name’ / value /;

– scalar MaxCredits / 12 /;

– scalar MaxCredits / 12 /

scalar MinCredits / 8 /;

Semicolon only at the

end of the particular

type declaration

Page 8: Gams Tutorial

BioE 494

8

Data Declaration: Parameters

• Lists of constant data (list of scalars)

• Declared over a predefined set

• Prior definition of a set with correct dimension is

necessary

• Syntax:

– parameter(s) ‘parameter name’ / element1 value1, element2

value2, ….. /;

– parameter Number_of_Students(courses) / BioE123 24,

BioE234 21, BioE 345 17 /;

– Important to use the right set for defining the data

Page 9: Gams Tutorial

BioE 494

9

Data Declaration: Tables • Multi-dimensional data for constants

• Prior definition of a sets with correct dimension is necessary

• Syntax:

– table ‘table name’

Declaration of the data in table format

• Data of number of students in each class in each year

– table StudentInfo(year,courses)

BioE 123 BioE 234 BioE 345

2001 12 34 25

2002 15 31 30

2003 21 33 32

2004 18 21 22

……

2010 21 31 26;

• Data alignment

important

• Semicolan at

the end of the

data entry

Page 10: Gams Tutorial

BioE 494

10

Data Access • Data accessing for parameters and tables is different

• Parameter defined as:

– parameter Number_of_Students(courses) / BioE123 24,

BioE234 21, BioE 345 17 /;

• I want to know the number of students taking BioE123

• Number_of_Students(1) ---- WRONG Syntax

• Correct: Number_of_Students(‘BioE 123’)

• I want to know the number of students taking BioE234 in 2003

• Correct: StudentInfo(‘2002’,’BioE 234’)

• This is a fundamental difference from the programming

philosophy of MATLAB, FORTRAN, C etc.

Page 11: Gams Tutorial

BioE 494

11

Variable Declaration • Variables: Unknown quantities which are computed

as the solution of the problem

• Variables are part of the equations (constraints and objective) defined in the optimization problem

• Syntax: – variable ‘variable name’(set name)

• Different types of variables can be defined: – free (with no bounds): This is the default declaration

– positive variable

– negative variable

– binary variable

– integer variable

Page 12: Gams Tutorial

BioE 494

12

Equations • Used to define constraints and objectives

• An inequality is also defined as an equation

• Equation declaration

– equations

Equation1(set) ‘name of the equation’

Equation2(set) ‘name of the equation’;

• Equation definition

Equation_name(set).. … actual equation …..

• Equation types:

=e= Equality

=l= Less than or equal to

=g= Greater than or equal to

Page 13: Gams Tutorial

BioE 494

13

Model and Solve Statements • Once all the problem information has been written,

‘model’ statement is used to formulate the model

• Syntax:

– model mode_name “model description” / list of equations

to be included in the model /;

• If all the defined equations are to be included:

– model mode_name “model description” / all /;

• Solve statement to ask GAMS to solve using a

particular model type

– solve model_name using model_type

maximizing/minimzing variable_name;

Page 14: Gams Tutorial

BioE 494

14

Example Problem from LP

• Example linear programming problem:

Min 4x1-x2

Subject to: 2x1 + x2≤ 8

x2 ≤ 5

x1 - x2 ≤ 4

x1 ≥ 0, x2 ≥ 0

Page 15: Gams Tutorial

BioE 494

15

Example Problem from LP

•Press F9 to execute the code

• Errors are listed in the output list file and the solution is not reported

Page 16: Gams Tutorial

BioE 494

16

Example Problem from LP

Page 17: Gams Tutorial

BioE 494

17

Important Model Types • The list of models that can be defined in GAMS

– LP = Linear programming

– NLP = Nonlinear programming

– MIP = Mixed integer (linear) programming

– MINLP = Mixed integer nonlinear programming

– RMIP = Relaxed mixed integer (linear) programming

– RMINLP = Relaxed mixed integer nonlinear

programming