GAMS Tutorial #2
← ChE 477: Process Optimization
GAMS TUTORIAL - #2 by Brooke A., Kendrick D., Meeraus A. "GAMS Release 2.25: A User's Guide" The Scientific Press, San Francisco, CA, USA, 1992 CHAPTER 2 by R.E. Rosenthal2.1 INTRODUCTION The introductory part of this book ends with a detailed example of the use of GAMS for formulating, solving, and analyzing a small and simple optimization problem. It was written by Richard E. Rosenthal of the Naval Postgraduate School in Monterey, California. The example is a quick but complete overview of GAMS and its features. Many references are made to other parts of the book, but they are only to tell you where to look for more details; the material here can be read profitably without reference to the rest of the book. The example is an instance of the transportation problem of linear programming, which has historically served as a "laboratory animal" in the development of optimization technology. (See, for example, Dantzig (1963).) It is a good choice for illustrating the power of algebraic modeling languages like GAMS because the transportation problem, no matter how large the instance at hand, possesses a simple, exploitable algebraic structure. You will see that almost all of the statements in the GAMS input file we are about to present would remain unchanged if a much larger transportation problem were considered. In the familiar transportation problem, we are given the supplies at several plants and the demands at several markets for a single commodity, and we are given the unit costs of shipping the commodity from plants to markets. The economic question is: how much shipment should there be between each plant and each market so as to minimize total transport cost? The algebraic representation of this problem is usually presented in a format similar to the following.Indices:*i* = plants*j* = marketsGiven Dataai= supply of commodity at plant *i* (in cases)bi= demand for commodity at market *j* (cases)cij= cost per unit shipment between plant *i* and market *j* ($/case)Decision Variables:xij= amount of commodity to ship from plant *i* to market *j* (cases),where *xij* > 0, for all *i*,*j*Constraints:Observe supply limit at plant *i*:Sj *xij* < *ai*, for all *i* (cases)Satisfy demand at market *j*:Si *xij* > *bj*, for all *j* (cases)Objective Function:MinimizeSij *cij* *x*i ($K)Note that this simple example reveals some modeling practices that we regard as good habits in general and that are consistent with GAMS's design. First, all the entities of the model are identified (and grouped) by type. Second, the ordering of entities is chosen so that no symbol is referred to before it is defined. Third, the units of all entities are specified, and, fourth, the units are chosen to a scale such that the numerical values to be encountered by the optimizer have relatively small absolute orders of magnitude. (The symbol $K here means thousands of dollars.) The names of the types of entities may differ among modelers. For example, economists use the terms "exogenous variable" and "endogenous variable" for "given data" and "decision variable", respectively. In GAMS, the terminology adopted is as follows: indices are calledSETS, given data are calledPARAMETERS, decision variables are calledVARIABLES, and constraints and the objective function are calledEQUATIONS. The GAMS representation of the transportation problem closely resembles the algebraic representation above. The most important difference, however, is that the GAMS version can be read and processed by a computer. As an instance of the transportation problem, suppose there are two canning plants and three markets, with the given data as follows. (This example is adapted from Dantzig,1963, Chapter 3.)**Markets**New York Chicago TopekaPlants*Shipping Distances* *Supplies*Seattle 2.5 1.7 1.8 350San Diego 2.5 1.8 1.4 600***Demands*** 325 300 275Shipping distances are in thousands of miles, and shipping costs are assumed to be $90.00 per case per thousand miles. The GAMS representation of this problem is as follows:SETSI canning plants /SEATTLE, SAN-DIEGO /J markets /NEW-YORK, CHICAGO, TOPEKA /;PARAMETERSA(I) capacity of plant i in cases/ SEATTLE 350SAN-DIEGO 600 /B(J) demand at market j in cases/ NEW-YORK 325CHICAGO 300TOPEKA 275 /;TABLE D(I,J) distance in thousands of milesNEW-YORK CHICAGO TOPEKASEATTLE 2.5 1.7 1.8SAN-DIEGO 2.5 1.8 1.4;SCALAR F freight in dollars per case per thousand miles /90/;PARAMETER C(I,J) transport cost in thousands of dollars per case;C(I,J) = F ∗ D(I,J) / 1000;VARIABLESX(I,J) shipment quantities in casesZ total transportation costs in thousands of dollars;POSITIVE VARIABLE X;EQUATIONSCOST define objective functionSUPPLY(I) observe supply limit at plant iDEMAND(J) satisfy demand at market j;COST .. Z =E= SUM((I,J), C(I,J)∗X(I,J));SUPPLY(I) .. SUM(J, X(I,J)) =L= A(I);DEMAND(J) .. SUM(I, X(I,J)) =G= B(J);MODEL TRANSPORT /ALL/;SOLVE TRANSPORT USING LP MINIMIZING Z;DISPLAY X.L, X.M;If you submit a file containing the statements above as input to the GAMS program, the transportation model will be formulated and solved. Appendix A gives detailed instructions on how to invoke GAMS on several kinds of computers, but the simplest ("no frills")way to call GAMS is to enter the word GAMS followed by the input file's name. You will see a number of terse lines describing the progress GAMS is making, including the name of the file onto which the output is being written. When GAMS has finished, examine this tile, and if all has gone well the optimal shipments will be displayed at the bottom as follows.NEW-YORK CHICAGO TOPEKASEATTLE 50.000 300.000SAN-DIEGO 275.000 275.000You will also receive the marginal costs (simplex multipliers) below.CHICAGO TOPEKASEATTLE 0.036SAN-DIEGO 0.009These results indicate, for example, that it is optimal to send nothing from Seattle to Topeka, but if you insist on sending one case it will add .036 $K (or $36.00) to the optimal cost. (Can you prove that this figure is correct from the optimal shipments and the given data?) 2.2 STRUCTURE OF A GAMS MODEL For the remainder of the tutorial, we will discuss the basic components of a GAMS model, with reference to the example above. The basic components areInputsSETSDeclarationAssignment of membersData (PARAMETERS, TABLES, SCALARS)DeclarationAssignment of valuesVARIABLESDeclarationAssignment of valuesOptional) Assignment of bounds and/or initial valuesEQUATIONSDeclarationDefinitionMODEL and SOLVE statements(Optional) DISPLAY statementsOutputsEcho PrintReference MapsEquation ListingsStatus ReportsResultsThere are optional input components, such as edit checks for bad data and requests for customized reports of results. Other optional advanced features include saving and restoring old models, and creating multiple models in a single run, but this tutorial will discuss only the basic components. Before treating the individual components, we give a few general remarks. A GAMS model is a collection of statements in the GAMS language. The only rule governing the ordering of statements is that an entity of the model cannot be referenced before it is declared to exist. GAMS statements may be laid out typographical in almost any style that is appealing to the user. Multiple lines per statement, embedded blank lines, and multiple statements per line are allowed. You will get a good idea of what is allowed from the examples in this tutorial, but precise rules of the road are given in Chapter 3. When you are a beginning GAMS user, you should terminate every statement with a semicolon, as in our examples. The rules for relaxed punctuation are given in Chapter 18. The GAMS compiler does not distinguish between upper- and lowercase letters, so you are free to use either. The style adopted and recommended here is to always use uppercase for any word or symbol that is part of the GAMS language or that is an entity declared to exist in a particular GAMS model. We recommend reserving lowercase only for words that appear in the GAMS input for documentation. Documentation is crucial to the usefulness of mathematical models. It is all the more useful (and most likely to be accurate) if it is embedded within the model itself rather than written up separately. There are at least two ways to insert documentation within a GAMS model. First, any line that starts with an asterisk in column 1 is disregarded as a comment line by the GAMS compiler. Second, perhaps more important, documentary text can be inserted within specific GAMS statements, All the lowercase words in the transportation model are examples of the second form of documentation. As you can see from the list of input components above, the creation of GAMS entities involves two steps: a declaration and an assignment or definition. "Declaration" means declaring the existence of something and giving it a name. "Assignment" or "definition" means giving something a specific value or form. In the case of equations, you must make the declaration and definition in separate GAMS statements. For all other GAMS entities, however, you have the option of making declarations and assignments in the same statement or separately. The names given to the entities of the model must start with a letter and can be followed by up to nine more letters or digits.