Вы находитесь на странице: 1из 276

GAMS

A USER'S GUIDE

by:

Anthony Brooke David Kendrick Alexander Meeraus Ramesh Raman

Tutorial

by:

Richard E. Rosenthal

December 1998 © GAMS Development Corporation, 1998

GAMS Development Corporation 1217 Potomac Street, N.W. Washington, DC 20007, USA Tel: +1 202 342-0180 Fax: +1 202 342-0181 E-mail:sales@gams.com Http://www.gams.com/

GAMS

GAMS Software GmbH Eupener Str. 135-137 50933 Cologne, Germany Tel: +49 221-949-9170 Fax: +49 221-949-9171 E-mail:info@gams.de Http://www.gams.de/

 Introduction 1 1.1 MOTIVATION 1 1.2 BASIC FEATURES OF GAMS 1 1.2.1 GENERAL PRINCIPLES 1 1.2.2 DOCUMENTATION 2 1.2.3 PORTABILITY 2 1.2.4 USER INTERFACE 2 1.2.5 MODEL LIBRARY 2 1.3 ORGANIZATION OF THE BOOK 3 A Gams Tutorial 5 2.1 INTRODUCTION 5 2.2 STRUCTURE OF A GAMS MODEL 7 2.3 SETS 9 2.4 DATA 10 2.4.1 DATA ENTRY BY LISTS 10 2.4.2 DATA ENTRY BY TABLES 11 2.4.3 DATA ENTRY BY DIRECT ASSIGNMENT 11 2.5 VARIABLES 12 2.6 EQUATIONS 13 2.6.1 EQUATION DECLARATION 13 2.6.2 GAMS SUMMATION (AND PRODUCT) NOTATION 14 2.6.3 EQUATION DEFINITION 14 2.7 OBJECTIVE FUNCTION 16 2.8 MODEL AND SOLVE STATEMENTS 16 2.9 DISPLAY STATEMENTS 17 2.10 THE '.LO, .L, .UP, .M' DATABASE 17 2.10.1 ASSIGNMENT OF VARIABLE BOUNDS AND/OR INITIAL VALUES 17 2.10.2 TRANSFORMATION AND DISPLAY OF OPTIMAL VALUES 18 2.11 GAMS OUTPUT 19 2.11.1 ECHO PRINTS 19 2.11.2 ERROR MESSAGES 21 2.11.3 REFERENCE MAPS 23 2.11.4 EQUATION LISTINGS 24 2.11.5 MODEL STATISTICS 25 2.11.6 STATUS REPORTS 25 2.11.7 SOLUTION REPORTS 26 2.12 SUMMARY 27

1 This is version: ug254j

iv

Gams Programms

29

 3.1 INTRODUCTION 29 3.2 THE STRUCTURE OF GAMS PROGRAMS 29 3.2.1 FORMAT OF GAMS INPUT 29 3.2.2 CLASSIFICATION OF GAMS STATEMENTS 30 3.2.3 ORGANIZATION OF GAMS PROGRAMS 30 3.3 DATA TYPES AND DEFINITIONS 31 3.4 LANGUAGE ITEMS 32 3.4.1 CHARACTERS 32 3.4.2 RESERVED WORDS 33 3.4.3 IDENTIFIERS 33 3.4.4 LABELS 34 3.4.5 TEXT 34 3.4.6 NUMBERS 35 3.4.7 DELIMITERS 35 3.4.8 COMMENTS 36 3.5 SUMMARY 36 Set Definitions 37 4.1 INTRODUCTION 37 4.2 SIMPLE SETS 37 4.2.1 THE SYNTAX 37 4.2.2 SET NAMES 37 4.2.3 SET ELEMENTS 38 4.2.4 ASSOCIATED TEXT 38 4.2.5 SEQUENCES AS SET ELEMENTS 39 4.2.6 DECLARATIONS FOR MULTIPLE SETS 40 4.3 THE ALIAS STATEMENT: MULTIPLE NAMES FOR A SET 40 4.4 SUBSETS AND DOMAIN CHECKING 41 4.5 MULTI-DIMENSIONAL SETS 41 4.5.1 ONE-TO-ONE MAPPING 41 4.5.2 MANY-TO-MANY MAPPING 42 4.6 SUMMARY 43 Data Entry: Parameters, Scalars & Tables 45 5.1 INTRODUCTION 45 5.2 SCALARS 45 5.2.1 THE SYNTAX 45 5.2.2 AN ILLUSTRATIVE EXAMPLE 45 5.3 PARAMETERS 46 5.3.1 THE SYNTAX 46 5.3.2 AN ILLUSTRATIVE EXAMPLES 46 5.3.3 PARAMETER DATA FOR HIGHER DIMENSIONS 47 5.4 TABLES 48 5.4.1 THE SYNTAX 48 5.4.2 AN ILLUSTRATIVE EXAMPLE 48

v

 5.4.4 TABLES WITH MORE THAN TWO DIMENSIONS 49 5.4.5 CONDENSING TABLES 50 5.4.6 HANDLING LONG ROW LABELS 50 5.5 ACRONYMS 51 5.5.1 THE SYNTAX 51 5.5.2 ILLUSTRATIVE EXAMPLE 51 5.6 SUMMARY 51 Data Manipulations With Parameters 53 6.1 INTRODUCTION 53 6.2 THE ASSIGNMENT STATEMENT 53 6.2.1 SCALAR ASSIGNMENTS 53 6.2.2 INDEXED ASSIGNMENTS 53 6.2.3 USING LABELS EXPLICITLY IN ASSIGNMENTS 54 6.2.4 ASSIGNMENTS OVER SUBSETS 54 6.2.5 ISSUES WITH CONTROLLING INDICES 54 6.2.6 EXTENDED RANGE IDENTIFIERS IN ASSIGNMENTS 55 6.2.7 ACRONYMS IN ASSIGNMENTS 55 6.3 EXPRESSIONS 55 6.3.1 STANDARD ARITHMETIC OPERATIONS 56 6.3.2 INDEXED OPERATIONS 56 6.3.3 FUNCTIONS 58 6.3.4 EXTENDED RANGE ARITHMETIC AND ERROR HANDLING 58 6.4 SUMMARY 60 Variables 61 7.1 INTRODUCTION 61 7.2 VARIABLE DECLARATIONS 61 7.2.1 THE SYNTAX 61 7.2.2 VARIABLE TYPES 62 7.2.3 STYLES FOR VARIABLE DECLARATION 62 7.3 VARIABLE ATTRIBUTES 63 7.3.1 BOUNDS ON VARIABLES 64 7.3.2 FIXING VARIABLES 64 7.3.3 ACTIVITY LEVELS OF VARIABLES 64 7.4 VARIABLES IN DISPLAY AND ASSIGNMENT STATEMENTS 64 7.4.1 ASSIGNING VALUES TO VARIABLE ATTRIBUTES 64 7.4.2 VARIABLE ATTRIBUTES IN ASSIGNMENTS 65 7.4.3 DISPLAYING VARIABLE ATTRIBUTES 65 7.5 SUMMARY 66 Equations 67 8.1 INTRODUCTION 67 8.2 EQUATION DECLARATIONS 67 8.2.1 THE SYNTAX 67 8.2.2 AN ILLUSTRATIVE EXAMPLE 67

vi

 8.3.1 THE SYNTAX 68 8.3.2 AN ILLUSTRATIVE EXAMPLE 68 8.3.3 SCALAR EQUATIONS 69 8.3.4 INDEXED EQUATIONS 69 8.3.5 USING LABELS EXPLICITLY IN EQUATIONS 69 8.4 EXPRESSIONS IN EQUATION DEFINITIONS 70 8.4.1 ARITHMETIC OPERATORS IN EQUATION DEFINITIONS 70 8.4.2 FUNCTIONS IN EQUATION DEFINITIONS 70 8.4.3 PREVENTING UNDEFINED OPERATIONS IN EQUATIONS 71 8.5 DATA HANDLING ASPECTS OF EQUATIONS 72 8.6 SUMMARY 72 Model And Solve Statements 73 9.1 INTRODUCTION 73 9.2 THE MODEL STATEMENT 73 9.2.1 THE SYNTAX 73 9.2.2 CLASSIFICATION OF MODELS 74 9.2.3 MODEL ATTRIBUTES 74 9.3 THE SOLVE STATEMENT 76 9.3.1 THE SYNTAX 76 9.3.2 REQUIREMENTS FOR A VALID SOLVE STATEMENT 77 9.3.3 ACTIONS TRIGGERED BY THE SOLVE STATEMENT 77 9.4 PROGRAMS WITH SEVERAL SOLVE STATEMENTS 77 9.4.1 SEVERAL MODELS 77 9.4.2 SENSITIVITY OR SCENARIO ANALYSIS 78 9.4.3 ITERATIVE IMPLEMENTATION OF NON-STANDARD ALGORITHMS 79 9.5 MAKING NEW SOLVERS AVAILABLE WITH GAMS 80 Gams Output 81 10.1 INTRODUCTION 81 10.2 THE ILLUSTRATIVE MODEL 81 10.3. COMPILATION OUTPUT 82 10.3.1 ECHO PRINT OF THE INPUT FILE 82 10.3.2 THE SYMBOL REFERENCE MAP 83 10.3.3 THE SYMBOL LISTING MAP 85 10.3.4 THE UNIQUE ELEMENT LISTING - MAP 85 10.3.5 USEFUL DOLLAR CONTROL DIRECTIVES 86 10.4 EXECUTION OUTPUT 87 10.5 OUTPUT PRODUCED BY A SOLVE STATEMENT 87 10.5.1 THE EQUATION LISTING 87 10.5.2 THE COLUMN LISTING 89 10.5.3 THE MODEL STATISTICS 89 10.5.4 THE SOLVE SUMMARY 90 10.5.5 SOLVER REPORT 93 10.5.6 THE SOLUTION LISTING 94 10.5.7 REPORT SUMMARY 95 10.5.8 FILE SUMMARY 96

vii

 10.6.1 COMPILATION ERRORS 97 10.6.2 COMPILATION TIME ERRORS 98 10.6.3 EXECUTION ERRORS 99 10.6.4 SOLVE ERRORS 99 10.7 SUMMARY 100 Conditional Expressions, Assignments And Equations 101 11.1 INTRODUCTION 101 11.2 LOGICAL CONDITIONS 101 11.2.1 NUMERICAL EXPRESSIONS AS LOGICAL CONDITIONS 101 11.2.2 NUMERICAL RELATIONSHIP OPERATORS 102 11.2.3 LOGICAL OPERATORS 102 11.2.4 SET MEMBERSHIP 102 11.2.5 LOGICAL CONDITIONS INVOLVING ACRONYMS 103 11.2.6 NUMERICAL VALUES OF LOGICAL CONDITIONS 103 11.2.7 MIXED LOGICAL CONDITIONS - OPERATOR PRECEDENCE 103 11.2.8 MIXED LOGICAL CONDITIONS - EXAMPLES 104 11.3 THE DOLLAR CONDITION 104 11.3.1 AN EXAMPLE 105 11.3.2 NESTED DOLLAR CONDITIONS 105 11.4 CONDITIONAL ASSIGNMENTS 105 11.4.1 DOLLAR ON THE LEFT 106 11.4.2 DOLLAR ON THE RIGHT 106 11.4.3 FILTERING CONTROLLING INDICES IN INDEXED OPERATIONS 107 11.4.4 FILTERING SETS IN ASSIGNMENTS 107 11.5 CONDITIONAL INDEXED OPERATIONS 109 11.5.1 FILTERING CONTROLLING INDICES IN INDEXED OPERATIONS 109 11.6 CONDITIONAL EQUATIONS 110 11.6.1 DOLLAR OPERATORS WITHIN THE ALGEBRA 110 11.6.2 DOLLAR CONTROL OVER THE DOMAIN OF DEFINITION 110 11.6.3 FILTERING THE DOMAIN OF DEFINITION 111 Dynamic Sets 113 12.1 INTRODUCTION 113 12.2 ASSIGNING MEMBERSHIP TO DYNAMIC SETS 113 12.2.1 THE SYNTAX 113 12.2.2 ILLUSTRATIVE EXAMPLE 113 12.2.3 DYNAMIC SETS WITH MULTIPLE INDICES 114 12.2.4 ASSIGNMENTS OVER THE DOMAIN OF DYNAMIC SETS 114 12.2.5 EQUATIONS DEFINED OVER THE DOMAIN OF DYNAMIC SETS 115 12.3 USING DOLLAR CONTROLS WITH DYNAMIC SETS 115 12.3.1 ASSIGNMENTS 115 12.3.2 INDEXED OPERATIONS 116 12.3.3 EQUATIONS 116 12.3.4 FILTERING THROUGH DYNAMIC SETS 116 12.4 SET OPERATIONS 117 12.4.1 SET UNION 117

viii

 12.4.3 SET COMPLEMENT 117 12.4.4 SET DIFFERENCE 117 12.5 SUMMARY 118 Sets As Sequences: Ordered Sets 119 13.1 INTRODUCTION 119 13.2 ORDERED AND UNORDERED SETS 119 13.3 ORD AND CARD 120 13.3.1 THE ORD OPERATOR 120 13.3.2 THE CARD OPERATOR 121 13.4 LAG AND LEAD OPERATORS 121 13.5 LAGS AND LEADS IN ASSIGNMENTS 122 13.5.1 LINEAR LAG AND LEAD OPERATORS - REFERENCE 122 13.5.2 LNEAR LAG AND LEAD OPERATORS - ASSIGNMENT 123 13.5.3 CIRCULAR LAG AND LEAD OPERATORS 123 13.6 LAGS AND LEADS IN EQUATIONS 124 13.6.1 LINEAR LAG AND LEAD OPERATORS - DOMAIN CONTROL 124 13.6.2 LINEAR LAG AND LEAD OPERATORS - REFERENCE 125 13.6.3 CIRCULAR LAG AND LEAD OPERATORS 125 13.7 SUMMARY 126 The Display Statement 127 14.1 INTRODUCTION 127 14.2 THE SYNTAX 127 14.3 AN EXAMPLE 127 14.4 THE LABEL ORDER IN DISPLAYS 128 14.4.1 EXAMPLE 129 14.5 DISPLAY CONTROLS 129 14.5.1 GLOBAL DISPLAY CONTROLS 130 14.5.2 LOCAL DISPLAY CONTROL 130 14.5.3 DISPLAY STATEMENT TO GENERATE DATA IN LIST FORMAT 131 The Put Writing Facility 133 15.1 INTRODUCTION 133 15.2 THE SYNTAX 133 15.3 AN EXAMPLE 134 15.4 OUTPUT FILES 135 15.4.1 DEFINING FILES 135 15.4.2 ASSIGNING FILES 136 15.4.3 CLOSING A FILE 136 15.4.4 APPENDING TO A FILE 137 15.5 PAGE FORMAT 137 15.6 PAGE SECTIONS 138 15.6.1 ACCESSING VARIOUS PAGE SECTIONS 139 15.6.2 PAGING 139 15.7 POSITIONING THE CURSOR ON A PAGE 140

ix

 15.9 OUTPUT ITEMS 141 15.9.1 TEXT ITEMS 141 15.9.2 NUMERIC ITEMS 142 15.9.3 SET VALUE ITEMS 142 15.10 GLOBAL ITEM FORMATTING 143 15.10.1 FIELD JUSTIFICATION 143 15.10.2 FIELD WIDTH 143 15.11 LOCAL ITEM FORMATTING 144 15.12 ADDITIONAL NUMERIC DISPLAY CONTROL 144 15.12.1 ILLUSTRATIVE EXAMPLE 145 15.13 CURSOR CONTROL 146 15.13.1 CURRENT CURSOR COLUMN 146 15.13.2 CURRENT CURSOR ROW 147 15.13.3 LAST LINE CONTROL 147 15.14 PAGING CONTROL 148 15.15 EXCEPTION HANDLING 148 15.16 SOURCE OF ERRORS ASSOCIATED WITH THE PUT STATEMENT 149 15.16.1 SYNTAX ERRORS 149 15.16.2 PUT ERRORS 149 15.17 SIMPLE SPREADSHEET/DATABASE APPLICATION 150 15.17.1 AN EXAMPLE 150 Programming Flow Control Features 151 16.1 INTRODUCTION 151 16.2 THE LOOP STATEMENT 151 16.2.1 THE SYNTAX 151 16.2.2 EXAMPLES 152 16.3 THE IF-ELSEIF-ELSE STATEMENT 152 16.3.1 THE SYNTAX 153 16.3.2 EXAMPLES 153 16.4 THE WHILE STATEMENT 154 16.4.1 THE SYNTAX 154 16.4.2 EXAMPLES 154 16.5 THE FOR STATEMENT 155 16.5.1 THE SYNTAX 155 16.5.2 EXAMPLES 155 Special Language Features 157 17.1 INTRODUCTION 157 17.2 SPECIAL MIP FEATURES 157 17.2.1 TYPES OF DISCRETE VARIABLES 157 17.2.2 SPECIAL ORDER SETS OF TYPE 1 (SOS1) 157 17.2.3 SPECIAL ORDER SETS OF TYPE 2 (SOS2) 159 17.2.4 SEMI-CONTINUOUS VARIABLES 159 17.2.5 SEMI-INTEGER VARIABLES 160 17.2.6 SETTING PRIORITIES FOR BRANCHING 160 17.3 MODEL SCALING - THE SCALE OPTION 161

x

 17.3.2 VARIABLE SCALING 161 17.3.3 EQUATION SCALING 162 17.3.4 SCALING OF DERIVATIVES 163 Glossary 165 Appendix A: The Gams Model Library 171 A.1 INTRODUCTION 171 A.2 USING THE MODEL LIBRARY THE MODELS 171 A.3 172 Appendix B: The Gams Call 177 B.1 INTRODUCTION 177 2.1.1 THE GENERIC GAMS CALL 177 2.1.2 SPECIFYING OPTIONS THROUGH THE COMMAND LINE 177 B.2 LIST OF COMMAND LINE PARAMETERS B.2.1 PARAMETERS CONTROLLING THE SPECIFIC GAMS RUN B.2.2 PARAMETERS CONTROLLING SYSTEM SETTINGS 178 178 178 B.2.3 PARAMETERS AFFECTING INPUT FILE PROCESSING PARAMETERS AFFECTING OUTPUT IN LISTING FILE PARAMETERS AFFECTING OTHER FILES 179 B.2.4 179 B.2.5 179 B.3 DETAILED DESCRIPTION OF COMMAND LINE PARAMETERS 180 B.3.1 ACTION 180 B.3.2 APPENDLOG B.3.3 APPENDOUT B.3.4 BOTMARGIN 180 181 181 B.3.5 CASE 181 B.3.6 CERR 181 B.3.7 CTRLM 182 B.3.8 CTRLZ 182 B.3.9 CURDIR 182 B.3.10 DFORMAT 182 B.3.11 DUMPOPT 183 B.3.12 DUMPPARMS 185 B.3.13 EOLONLY 185 B.3.14 ERROR 185 B.3.15 ERRMSG 186 B.3.16 EXPAND 187 B.3.17 FERR 187 B.3.18 FORCEWORK 188 B.3.19 G205 188 B.3.20 INPUT 189 B.3.21 INPUTDIR 189 B.3.22 INPUTDIR1 TO INPUTDIR18 190 B.3.23 LEFTMARGIN B.3.24 LIBINCDIR 190 191

xi

 B.3.26 LOGFILE 191 B.3.27 LOGLINE 192 B.3.28 LOGOPTION B.3.29 MULTIPASS 193 193 B.3.30 NOCHECK 194 B.3.31 OPTFILE 195 B.3.32 OUTPUT 195 B.3.33 PAGECONTR 196 B.3.34 PAGESIZE 196 B.3.35 PAGEWIDTH 196 B.3.36 PROFILE 197 B.3.37 PUTDIR 198 B.3.38 REFERENCE 198 B.3.39 RELPATH 198 B.3.40 RESTART 199 B.3.41 SAVE 199 B.3.42 SCRDIR 199 B.3.43 STEPSUM 200 B.3.44 STRINGCHK 200 B.3.45 SUBSYS 201 B.3.46 SUPPRESS 201 B.3.47 SYSDIR 201 B.3.48 SYSINCDIR 201 B.3.49 TABIN 202 B.3.50 TFORMAT 202 B.3.51 TOPMARGIN 202 B.3.52 WORKDIR 203 Appendix C: Dollar Control Options 205 C.1 INTRODUCTION 205 C.1.1 SYNTAX 205 C.1.2 EXAMPLE 205 C.2 LIST OF DOLLAR CONTROL OPTIONS 206 C.2.1 OPTIONS AFFECTING INPUT COMMENT FORMAT OPTIONS AFFECTING INPUT DATA FORMAT OPTIONS AFFECTING OUTPUT FORMAT OPTIONS AFFECTING LISTING OF REFERENCE MAPS OPTIONS AFFECTING PROGRAM CONTROL 206 C.2.2 206 C.2.3 207 C.2.4 207 C.2.5 208 C.3 DETAILED DESCRIPTION OF DOLLAR CONTROL OPTIONS 208 C.3.1 ABORT ‘TEXT’ BATINCLUDE FILE ARG1 ARG2 … CALL COMMAND 208 C.3.2 209 C.3.3 210 C.3.4 CLEAR ID1 ID2 … 211 C.3.5 COMMENT C 212 C.3.6 DOLLAR C 212 C.3.7 DOUBLE 213 C.3.8 ECHO ‘TEXT’ > FILE OR ECHO ‘TEXT’ >> FILE 213 C.3.9 EJECT C.3.10 EOLCOM C 213 214

xii

 C.3.11 ERROR 'TEXT' EXIT 'TEXT' GOTO ID HIDDEN 'TEXT' IF [NOT] CONDITION NEW_INPUT_LINE INCLUDE 'FILE' 214 C.3.12 214 C.3.13 215 C.3.14 215 C.3.15 215 C.3.16 218 C.3.17 INLINECOM C 219 C.3.18 KILL ID1 ID2 219 C.3.19 LABEL ID 219 C.3.20 LIBINCLUDE FILE ARG1 ARG2 . 220 C.3.21 LINES N 220 C.3.22 LOG TEXT 221 C.3.23 MAXCOL N 221 C.3.24 MINCOL N 222 C.3.25 [ON|OFF]DIGIT C.3.26 [ON|OFF]DOLLAR C.3.27 [ON|OFF]EMPTY C.3.28 [ON|OFF]END C.3.29 [ON|OFF]EOLCOM C.3.30 [ON|OFF]EPS C.3.31 [ON|OFF]GLOBAL C.3.32 [ON|OFF]INCLUDE C.3.33 [ON|OFF]INLINE C.3.34 [ON|OFF]LISTING C.3.35 [ON|OFF]MARGIN C.3.36 [ON|OFF]MULTI C.3.37 [ON|OFF]NESTCOM C.3.38 [ON|OFF]SYMLIST C.3.39 [ON|OFF]SYMXREF C.3.40 [ON|OFF]TEXT C.3.41 [ON|OFF]UELLIST C.3.42 [ON|OFF]UELXREF C.3.43 [ON|OFF]UPPER C.3.44 [ON|OFF]WARNING C.3.45 PHANTOM ID C.C.46 SHIFT C.3.47 SINGLE C.3.48 STARS CCCC C.3.49 STITLE ‘TEXT’ 222 223 223 224 225 225 226 226 227 227 228 228 229 230 230 231 232 232 232 233 234 234 235 235 236 C.3.50 SYSINCLUDE FILE ARG1 ARG2 236 C.3.51 TITLE ‘TEXT’ 236 C.3.52 USE205 237 C.3.53 USE225 237 C.3.54 USE999 237 Appendix D: The Option Statement 239 D.1 INTRODUCTION 239

xiii

 D.1.2 EXAMPLE 240 D.2 LIST OF OPTIONS 240 D.2.1 OPTIONS CONTROLLING OUTPUT DETAIL OPTIONS CONTROLLING SOLVER SPECIFIC PARAMETERS OPTIONS CONTROLLING CHOICE OF SOLVER OPTIONS AFFECTING INPUT PROGRAM CONTROL 240 D.2.2 241 D.2.3 241 D.2.4 241 D.3 DETAILED DESCRIPTION OF OPTIONS 241 Appendix E: The Save And Restart Feature 247 E.1 INTRODUCTION 247 E.2 E.2.1 THE SAVE AND RESTART FEATURES SAVING WORK FILES RESTARTING F