Introduction to GAMS. Ulf Brännlund



Similar documents
Welcome to GAMS 1. Jesper Jensen TECA TRAINING ApS This version: September 2006

Linear Programming. March 14, 2014

IBM ILOG CPLEX for Microsoft Excel User's Manual

Using CPLEX. =5 has objective value 150.

Linear Programming Notes V Problem Transformations

Solving Linear Programs in Excel

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

USING EXCEL 2010 TO SOLVE LINEAR PROGRAMMING PROBLEMS MTH 125 Chapter 4

Multiproduct Batch Plant Scheduling

EXCEL SOLVER TUTORIAL

Chapter 7: Additional Topics

Introduction to Linear Programming (LP) Mathematical Programming (MP) Concept

Grade Level Year Total Points Core Points % At Standard %

v w is orthogonal to both v and w. the three vectors v, w and v w form a right-handed set of vectors.

Linear Programming. April 12, 2005

Linear Programming: Chapter 11 Game Theory

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

PTC Mathcad Prime 3.0 Keyboard Shortcuts

Statistical Machine Translation: IBM Models 1 and 2

Solving Linear Programs using Microsoft EXCEL Solver

Using EXCEL Solver October, 2000

Using GMPL/GLPK for IE 1079/2079

Several Views of Support Vector Machines

Linear Programming. Solving LP Models Using MS Excel, 18

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Airport Planning and Design. Excel Solver

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

Linear Programming Notes VII Sensitivity Analysis

CSI 333 Lecture 1 Number Systems

Creating Cost Recovery Layouts

Solving Math Programs with LINGO

Computational Mathematics with Python

CPLEX Tutorial Handout

Computational Mathematics with Python

Computational Mathematics with Python

1. Overview. 2. Requirements

Mathematical finance and linear programming (optimization)

DATA ANALYSIS II. Matrix Algorithms

Circuit Analysis using the Node and Mesh Methods

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

Definition 8.1 Two inequalities are equivalent if they have the same solution set. Add or Subtract the same value on both sides of the inequality.

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

Linear Programs: Variables, Objectives and Constraints

MATLAB Programming. Problem 1: Sequential

Relational Database: Additional Operations on Relations; SQL

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Column Generation in GAMS Extending the GAMS Branch-and-Cut-and-Heuristic (BCH) Facility

Microsoft Excel Basics

I PUC - Computer Science. Practical s Syllabus. Contents

PHP Tutorial From beginner to master

LAYOUT OF THE KEYBOARD

Lecture 2 Mathcad Basics

Nonlinear Programming Methods.S2 Quadratic Programming

Creating Basic Excel Formulas

Linear Program Solver

The Center for Teaching, Learning, & Technology

Portfolio Construction with OPTMODEL

Simple File Input & Output

Vector and Matrix Norms

Chapter 6. Linear Programming: The Simplex Method. Introduction to the Big M Method. Section 4 Maximization and Minimization with Problem Constraints

Command Scripts Running scripts: include and commands

Microsoft Excel 2010 Part 3: Advanced Excel

INTEGER PROGRAMMING. Integer Programming. Prototype example. BIP model. BIP models

Moving from CS 61A Scheme to CS 61B Java

SBB: A New Solver for Mixed Integer Nonlinear Programming

What is Linear Programming?

TheFinancialEdge. Configuration Guide for General Ledger

Programming Languages & Tools

Creating a Simple Macro

Graphing Parabolas With Microsoft Excel

Bindel, Spring 2012 Intro to Scientific Computing (CS 3220) Week 3: Wednesday, Feb 8

2.1 Increasing, Decreasing, and Piecewise Functions; Applications

Excel supplement: Chapter 7 Matrix and vector algebra

Figure 1.1 Vector A and Vector F

Review of Fundamental Mathematics

Series and Parallel Resistive Circuits

Excel 2007 Basic knowledge

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Section IV.1: Recursive Algorithms and Recursion Trees

3.1 Solving Systems Using Tables and Graphs

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Business Portal for Microsoft Dynamics GP. Key Performance Indicators Release 10.0

CHAPTER 11: BASIC LINEAR PROGRAMMING CONCEPTS

Engineering Problem Solving and Excel. EGN 1006 Introduction to Engineering

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Scheduling Algorithm with Optimization of Employee Satisfaction

Introduction to Matrix Algebra

Using SPSS, Chapter 2: Descriptive Statistics

9.2 Summation Notation

56:171 Operations Research Midterm Exam Solutions Fall 2001

Transportation Polytopes: a Twenty year Update

Creating, Solving, and Graphing Systems of Linear Equations and Linear Inequalities

Beginner s Matlab Tutorial

Sources: On the Web: Slides will be available on:

1 Solving LPs: The Simplex Algorithm of George Dantzig

Binary Adders: Half Adders and Full Adders

Summer Assignment for incoming Fairhope Middle School 7 th grade Advanced Math Students

Performing Simple Calculations Using the Status Bar

Transcription:

Introduction to GAMS Ulf Brännlund Optimization and Systems Theory, KTH 1 / 41 GAMS Introduction, 2010/2011

What is GAMS? GAMS: General Algebraic Modeling System High-level optimization modeling language. Makes interpretation from model to algorithm. Optimization and Systems Theory, KTH 2 / 41 GAMS Introduction, 2010/2011

Advantages of GAMS Advantages of GAMS: Efficient. Create a set of equations by one statement. Insert data only once. Create prototypes fast. State-of-the-art optimization software. Utilizes structure. Self-documenting. Algebraic representation. Large model library. Relatively easy to learn. Optimization and Systems Theory, KTH 3 / 41 GAMS Introduction, 2010/2011

Other alternatives Examples of other modeling languages: ILOG Studio AMPL MPL LINGO Examples of other optimization tools: Your own C or Fortran code. Optimization packages, e.g., CPLEX, MPSX, OSL. Interactive optimization programs, e.g., LINDO, LP88. Matlab + Optimization Toolbox. Excel. Optimization and Systems Theory, KTH 4 / 41 GAMS Introduction, 2010/2011

Example Assume that your model contains the following constraint: x ij b j for all j. In GAMS you write i demand(j).. sum(i,x(i,j)) =g= b(j); Note: The format is general, in an algebraic notation. Not specific for the problem instance. The statements are independent of the solver. GAMS offers data manipulation of the solution. Optimization and Systems Theory, KTH 5 / 41 GAMS Introduction, 2010/2011

A simple transportation problem Sets: i factories j customers Given data: a i supply at factory i b j demand of customer j transportation cost per unit from factory i to customer j c ij Decision variables: x ij amount transported from factory i to customer j Optimization and Systems Theory, KTH 6 / 41 GAMS Introduction, 2010/2011

A simple transportation problem, cont. Objective function Transportation cost: i,j c ijx ij Constraints Supply: j x ij a i for all i. Demand: i x ij b j for all j. Nonnegativity: x ij 0 for all i, j. Optimization and Systems Theory, KTH 7 / 41 GAMS Introduction, 2010/2011

Example of GAMS output Generating the instance of the demand constraints. DEMAND (J).. SUM (I, X (I, J) ) =G= B(J); DEMAND(NEW-YORK).. X(SEATTLE,NEW-YORK) + X(SAN-DIEGO, NEW-YORK) =G= 325 DEMAND (CHICAGO).. X (SEATTLE, CHICAGO) + X(SAN-DIEGO,CHICAGO) =G= 300; DEMAND (TOPEKA).. X (SEATTLE, TOPEKA) + X(SAN-DIEGO,TOPEKA) =G= 275; Optimization and Systems Theory, KTH 8 / 41 GAMS Introduction, 2010/2011

Example of GAMS output, cont. DISPLAY X.L. X.M; OPTIMAL SOLUTION (IN CASES) NEW-YORK CHICAGO TOPEKA SEATTLE 50.000 300.000 SAN-DIEGO 275.000 275.000 MARGINAL COSTS (IN $K/CASE) CHICAGO TOPEKA SEATTLE 0.036 SAN-DIEGO 0.009 Optimization and Systems Theory, KTH 9 / 41 GAMS Introduction, 2010/2011

Comments on the transportation problem SETS Assignments within / / Hyphens (no spaces) "Specials": SET M Machines /MACH01 * MACH24 /; Defines machine MACH01, MACH02,..., MACH24. ALIAS(T,TP); TP is another name for the index set T. Optimization and Systems Theory, KTH 10 / 41 GAMS Introduction, 2010/2011

Comments on the transportation problem, cont. PARAMETER, TABLE Lists delimited by / /, elements delimited by comma or return. Zero is default. Domain check: GAMS compiler does not accept "SEATLE" SCALAR F Optimization and Systems Theory, KTH 11 / 41 GAMS Introduction, 2010/2011

Comments on the transportation problem, cont. Assignment PARAMETER C(I,J) transportation cost C(I,J) = F * D(I,J) / 1000; = is the assignment operator. F and D(I,J) muste have been assigned first. C(J,I) is not tolerated by the compiler. There exist many mathematical functions. The right hand side of = must be computable for GAMS. Use assignments instead of constraints when possible. Optimization and Systems Theory, KTH 12 / 41 GAMS Introduction, 2010/2011

Comments on the transportation problem, cont. Variables Name, domain and comments in the first sentence. Type of variable assigned separately or directly. Domain in type assignment. Free variable is default. Types are FREE, POSITIVE, NEGATIVE, BINARY, INTEGER, SOS1, SOS2. Note! There is no objective function. Use an equation to define a (free) variable that you want to optimize. Optimization and Systems Theory, KTH 13 / 41 GAMS Introduction, 2010/2011

Comments on the transportation problem, cont. Constraints EQUATION means equality or inequality. =E= is different from = and EQ. =L= instead of " " and LE. There must be at least one variable in each constraint. Format: NAME(DOMAIN).. LHS =E= RHS; Optimization and Systems Theory, KTH 14 / 41 GAMS Introduction, 2010/2011

Comments on the transportation problem, cont. MODEL means a set of constraints. /ALL/ means all constraints. Alternatively one may specify them, i.e., MODEL TRANSPRT /COST, SUPPLY, DEMAND/; Optimization and Systems Theory, KTH 15 / 41 GAMS Introduction, 2010/2011

Comments on the transportation problem, cont. SOLVE MODELNAME USING Effect: LP NLP MIP RMIP MINIMIZING MAXIMIZING Generates an instance of the model. OBJECTIVE Creates in put to the solver and hands over the control to the solver. Puts the solver s output in GAMS internal database. Gives the control back to GAMS. Optimization and Systems Theory, KTH 16 / 41 GAMS Introduction, 2010/2011

Some basic rules Create GAMS files with a text editor, alternatively GAMS IDE. It is a pure ASCII file. No tabs. (OK with Emacs.) Do note use Swedish characters å, ä, ö. Arbitrary order of sentences, but declare before use. The layout is arbitrary, several rows per statement, several statements per row. End each statement by a ;. Optimization and Systems Theory, KTH 17 / 41 GAMS Introduction, 2010/2011

Some basic rules, cont. Not case sensitive. Documenting text in three ways: 1 Within a declaration, less than 80 characters, no,, ;, /. 2 Asterisk, *, at beginning of row. 3 $ONTEXT ($ at beginning of row) This is an explation of the model... $OFFTEXT Optimization and Systems Theory, KTH 18 / 41 GAMS Introduction, 2010/2011

Some basic rules, cont. Rules for naming parameters, variables, etc. Referred to as identifiers in user s guide and error output. At most 10 characters (letters or digits). First character must be a letter. Do not use reserved words. Rules for naming SETS. Referred to as labels in user s guide and error output. At most 10 characters (letters or digits). First character may be a letter or a digit. Do not use reserved words. Declaration and assignment separately or jointly. Optimization and Systems Theory, KTH 19 / 41 GAMS Introduction, 2010/2011

GAMS internal database GAMS has the following four attributes for variables and equations..lo Lower bound..up Upper bound..l "Level" (Primal value)..m "Marginal" (Dual value). These values may be read and written at any time. The solver reads all attributes, but only writes in.l and.m. The format is: IDENTIFIER.ATTRIBUTE(DOMAIN) Optimization and Systems Theory, KTH 20 / 41 GAMS Introduction, 2010/2011

GAMS internal database, cont. Example Assigning attributes: X.UP(I,J) = CAPACITY(I,J); X.LO(I,J) = 10; X.FX("SEATTLE", "NEW-YORK") = 180;.FX means fixing, i.e., setting.lo =.UP. Setting inital values: QUANTITY.L(K) = 0.5 * EOQ(K); Reading attributes: DISPLAY X.L, X.M; REPORT(I,"CASES") = SUM(J, X.L(I,J)); Optimization and Systems Theory, KTH 21 / 41 GAMS Introduction, 2010/2011

GAMS output All output given on a file (filename.lst). In case of error Program listing Error messages Cross references List of symbols No error Program listing Cross references List of symbols List of equations List of variables Model statistics Status report Solution report Optimization and Systems Theory, KTH 22 / 41 GAMS Introduction, 2010/2011

Dollar commands Dollar commands controls GAMS output. The $" sign must be in the first column. Example: $TITLE A transportation model page. $OFFUPPER Title on top of each Output as upper case and lower case. $OFFSYMLIST OFFSYMXREF reference listing. Elminates sybol listing and cross $ONTEXT and OFFTEXT GAMS ignores all commands between these statements. Optimization and Systems Theory, KTH 23 / 41 GAMS Introduction, 2010/2011

Error messages Search for ****. Accept error messages. Concentrate on one error mesage at a time. Optimization and Systems Theory, KTH 24 / 41 GAMS Introduction, 2010/2011

Advanced features Undesirable domain control for tables: SETS I car types /VOLVO, BMW, SAAB / J years /Y96*Y99/ TABLE DATA(I,J,*) Y96."HAVE" Y96."NEED" Y97."HAVE" Y97."NEED" VOLVO 1 4 4 6 BMW 2 1 6 0 SAAB 3 5 3 4 ; Optimization and Systems Theory, KTH 25 / 41 GAMS Introduction, 2010/2011

Advanced features Undesirable domain control for variables. SOLVE TRANSPRT USING LP MINIMIZING Z PARAMETER REPORT(I,*) optimal produktion per fabrik REPORT(I,"CASES") = SUM(J, X.L(I,J)); REPORT(I,"SHIP-COST") = SUM(J, C(I,J)*X.L(I,J)); REPORT(I,"SHIPMENTS") = SUM(J$X.L(I,J), 1); DISPLAY REPORT; Optimization and Systems Theory, KTH 26 / 41 GAMS Introduction, 2010/2011

Lags and leads Lags and leads may be constructed via the funktions ORD(T) and CARD(T). Example: SET T time periods /SPRING, SUMMER, FALL, WINTER /; PARAMETERS TEST1(T), TEST2(T), TEST3(T), TEST4(T), REPORT(T,*); TEST1(T) = ORD(T); TEST2(T) = TEST1(T+1); TEST3(T) = TEST1(T) + 1; TEST4(T) = TEST1(T++1); REPORT(T, "TEST1") = TEST1(T); REPORT(T, "TEST2") = TEST2(T) REPORT(T, "TEST3") = TEST3(T); REPORT(T, "TEST4") = TEST4(T); NB! ORD("FALL") = 3, and CARD(T) = 4. Optimization and Systems Theory, KTH 27 / 41 GAMS Introduction, 2010/2011

Lags and leads, cont. Here is the result: TEST1 TEST2(T) TEST3(T) TEST4(T) SPRING 1 2 2 2 SUMMER 2 3 3 3 FALL 3 4 4 4 WINTER 4 5 1 T-1 and T- -1 are analogus. Optimization and Systems Theory, KTH 28 / 41 GAMS Introduction, 2010/2011

Dynamic modeling SET T weeks /W1*W4/; PARAMETER D(T) /W1 10, W2 12, W3 14, W4 16/; POSITIVE variables X(T) produced quantity I(T) storage; EQUATIONS STOREBAL(T) storage balance equations; STOREBAL(T).. I(T-1) + X(T) =E= D(T) + I(T); NB! GAMS ignores indices outside their domains. Optimization and Systems Theory, KTH 29 / 41 GAMS Introduction, 2010/2011

Dynamic modeling, cont. GAMS generates the following equations: --- STOREBAL =E= storage balance equations STOREBAL(W1).. + X(W1) - I(W1) =E= 10; STOREBAL(W2).. I(W1) + X(W2) - I(W2) =E= 12; STOREBAL(W3).. I(W2) + X(W3) - I(W3) =E= 14; STOREBAL(W4).. I(W3) + X(W4) - I(W4) =E= 16; Special cases "at the ends" are handled separately. Optimization and Systems Theory, KTH 30 / 41 GAMS Introduction, 2010/2011

The dollar operator The dollar operator is helpful in handling exception in summations or definitions of equations etc. Example in assignment: REPORT(I,"SHIPMENTS") = SUM(J$X.L(I,J), 1); Example in definition of equation: DEMAND(J).. SUM(I, X(I,J)$(D(I,J) LT DMAX)) =G= B(J); DEMAND(J).. SUM(I$(D(I,J) LT DMAX), X(I,J)) =G= B(J); Optimization and Systems Theory, KTH 31 / 41 GAMS Introduction, 2010/2011

The dollar operator, cont. Rules: $(condition) means such that condition is fulfilled. The condition must be computable for GAMS. Format: $(expression1 GT expression2) (GT, GE, LT, LE, EQ, NE) $(expression1) is short for $(expression1 NE 0.0) $(expression1 AND condition2) (OR, XOR, NOT) Optimization and Systems Theory, KTH 32 / 41 GAMS Introduction, 2010/2011

The dollar operator, cont. In an assignment using =, there is a difference between dollar sign to the left and to the right. To the left: A$(condition) = B; Meaning: If the condition is fulfilled, let A = B. Otherwise, do nothing. To the right: A = B$(condition); Meaning: If the condition is fulfilled, let A = B. Otherwise, let A=0. Optimization and Systems Theory, KTH 33 / 41 GAMS Introduction, 2010/2011

The dollar operator, cont. End conditions SCALARS INITLAB initial experienced labor force /75/ ENDLAB ending experience labor force /90/; EW.FX(T)$(ORD(T) EQ 1) = INITLAB; EW.FX(T)$(ORD(T) EQ CARD(T)) = ENDLAB; Abort SCALARS TOTCAP, TOTDEM; TOTCAP = SUM(I, A(I)); TOTDEM = SUM(J, B(J)); ABORT$(TOTDEM GT TOTCAP) TOTCAP, TOTDEM, "Total demand exceeds total capacity" Conditional DISPLAY DISPLAY $SHOWX X.L; Optimization and Systems Theory, KTH 34 / 41 GAMS Introduction, 2010/2011

Loop Sometimes it may be useful to loop over an index. There is a LOOP statement for this purpose. It may be particularly useful prior and after a solution. Example: SET T years /1985 * 1995/; SCALARS INITBUD first year budget / 100000 / GROWTH budget growth rate /0.05/ ; PARAMETER BUDGET(T); BUDGET(T)$(ORD(T) EQ 1) = INITBUD; LOOP(T, BUDGET(T+1) = (1+GROWTH)*BUDGET(T); ); There may be an arbitrary number of statements inside the loop, including SOLVE statements. Optimization and Systems Theory, KTH 35 / 41 GAMS Introduction, 2010/2011

Other flow controls GAMS has the usual flow controls: for, while and if/else. They are seldom used. if/else IF(( X.L GT 0 ), S = 1; ELSEIF( X.L LT 0 ), S = -1; ELSE S = 0; ); Optimization and Systems Theory, KTH 36 / 41 GAMS Introduction, 2010/2011

Other flow controls, cont. while MODEL DSGN /ALL/; OPTION SOLPRINT = OFF; SCALAR T /1/; WHILE( (T LE 20), X.L(J) = UNIFORM( X.LO(J), X.UP(J) ); SOLVE DSGN USING NLP MINIMIZING COST; DISPLAY X.L, COST.L; T = T + 1; ); Optimization and Systems Theory, KTH 37 / 41 GAMS Introduction, 2010/2011

Other flow controls, cont. for MODEL DSGN /ALL/; OPTION SOLPRINT = OFF; SCALAR T; FOR( T = 1 TO 20, X.L(J) = UNIFORM( X.LO(J), X.UP(J) ); SOLVE DSGN USING NLP MINIMIZING COST; DISPLAY X.L, COST.L; ); Optimization and Systems Theory, KTH 38 / 41 GAMS Introduction, 2010/2011

A comment on tolerances For integer programming, there are two optimality tolerances: optca absolute optimality tolerance optcr relative optimality tolerance These are rather loose by default. To obtain best possible tolerance: option optca=0; option optcr=0; Optimization and Systems Theory, KTH 39 / 41 GAMS Introduction, 2010/2011

The meaning of solution in nonlinear programming Please make interpretation of optimal solution for nonconvex problems. If the method uses first-derivatives only, the point is known to (approximately) satisfy the first-order necessary optimality conditions. You may want to try with different initial points. Optimization and Systems Theory, KTH 40 / 41 GAMS Introduction, 2010/2011

A warning on powers The operator ** requires a positive argument. This may cause rntime interrupt due to a nonnegative variable being slightly negative. The operator power with argument 2 works for negative variables too. Optimization and Systems Theory, KTH 41 / 41 GAMS Introduction, 2010/2011