# A GAMS Example

This toy problem is presented only to illustrate how GAMS lets you model in a natural way. GAMS can handle much larger and highly complex problems. Only a few of the basic features of GAMS can be highlighted here.

## Algebraic Description

Here is a standard algebraic description of the problem, which is to minimize the cost of shipping goods from 2 plants to 3 markets, subject to supply and demand constraints.

Indices:

$$i$$ = plants
$$j$$ = markets

Given Data:

$$a_i$$ = supply of commodity of plant $$i$$ (cases)
$$b_j$$ = demand for commodity at market $$j$$ (cases)
$$d_{ij}$$ = distance between plant i and market j (thousand miles)
$$c_{ij}$$ = $$F\cdot d_{ij}$$ shipping cost per unit shipment between plant $$i$$ and market $$j$$ ($/case/thousand miles)  Distances Markets Plants New York Chicago Topeka Supply Seattle 2.5 1.7 1.8 350 San Diego 2.5 1.8 1.4 600 Demand 325 300 275 $$F$$ =$ per thousand miles

Decision Variables:

$$x_{ij}$$ = amount of commodity to ship from plant $$i$$ to market $$j$$ (cases)
where $$x_{ij} \geq 0$$, for all $$i$$, $$j$$

Constraints:

Observe supply limit at plant $$i$$: $$\sum_j x_{ij} \leq a_i$$ for all $$i$$ (cases)
Satisfy demand at market $$j$$: $$\sum_i x_{ij} \geq b_j$$ for all $$j$$ (cases)

Objective Function:

Minimize $$\sum_i \sum_j c_{ij} x_{ij}$$ (\$K)

## The GAMS Model

The same model modeled in GAMS. The use of concise algebraic descriptions makes the model highly compact, with a logical structure. Internal documentation, such as explanation of parameters and units of measurement, makes the model easy to read.

Sets
i   canning plants   / Seattle, San-Diego /
j   markets          / New-York, Chicago, Topeka / ;
Parameters
a(i)  capacity of plant i in cases
/    Seattle     350
San-Diego   600  /
b(j)  demand at market j in cases
/    New-York    325
Chicago     300
Topeka      275  / ;
Table  d(i,j)  distance in thousands of miles
New-York       Chicago      Topeka
Seattle          2.5           1.7          1.8
San-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 ;
Variables
x(i,j)  shipment quantities in cases
z       total transportation costs in thousands of dollars ;
Positive variables x ;
Equations
cost        define objective function
supply(i)   observe supply limit at plant i
demand(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 ;


#### Sets

Sets
i   canning plants   / Seattle, San-Diego /
j   markets          / New-York, Chicago, Topeka / ;
GAMS lets you specify indices in a straightforward way: declare and name the set (here, I and J), and enumerate their elements.

#### Parameters

Parameters
a(i)  capacity of plant i in cases
/    Seattle     350
San-Diego   600  /
b(j)  demand at market j in cases
/    New-York    325
Chicago     300
Topeka      275  / ;
Here data are entered as indexed parameters A(I) and B(J), and values simply are listed.
GAMS lets you place explanatory text (shown in lower case) throughout your model, as you develop it. Your comments are automatically incorporated into the output report, at the appropriate places.

#### Table

Table  d(i,j)  distance in thousands of miles
New-York       Chicago      Topeka
Seattle          2.5           1.7          1.8
San-Diego        2.5           1.8          1.4  ;
Data can also be entered in convenient table form. GAMS lets you input data in their basic form - transformations are specified algebraically.

#### Scalar

Scalar f  freight in dollars per case per thousand miles  /90/ ;
A constant simply can be declared as a SCALAR, and its value specified.

#### Data manipulation

Parameter
c(i,j)  transport cost in thousands of dollars per case ;
c(i,j) = f * d(i,j) / 1000 ;
When data values are to be calculated, you first declare the parameter (i.e. give it a symbol and, optionally, index it), then give its algebraic formulation. GAMS will automatically make the calculations.

#### Variables

Variables
x(i,j)  shipment quantities in cases
z       total transportation costs in thousands of dollars ;
Positive variables x ;

Decision variables are expressed algebraically, with their indices specified. From this general form, GAMS generates each instance of the variable in the domain.
Variables are specified as to type: FREE, POSITIVE, NEGATIVE, BINARY, or INTEGER. The default is FREE.
The objective variable (z, here) is simply declared without an index.

#### Equations

Equations
cost        define objective function
supply(i)   observe supply limit at plant i
demand(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) ;

Objective function and constraint equations are first declared by giving them names. Then their general algebraic formulae are described. GAMS now has enough information (from data entered above and from the algebraic relationships specified in the equations) to automatically generate each individual constraint statement - as you can see in the output report below. An extensive set of tools enables you to model any expression that can be stated algebraically: arithmetic, indexing, functions and exception-handling log (e.g. if-then-else and such-that constructs).

=E= indicates 'equal to'
=L= indicates 'less than or equal to'
=G= indicates 'greater than or equal to'

#### Model Statement

Model transport /all/ ;

The model is given a unique name (here, TRANSPORT), and the modeler specifies which equations should be included in this particular formulation. In this case we specified ALL which indicates that all equations are part of the model. This would be equivalent to MODEL TRANSPORT /COST, SUPPLY, DEMAND/ . This equation selection enables you to formulate different models within a single GAMS input file, based on the same or different given data.

#### Solve Statement

Solve transport using LP minimizing z ;

The solve statement (1) tells GAMS which model to solve, (2) selects the solver to use (in this case an LP solver), (3) indicaties the direction of the optimization, either MINIMIZING or MAXIMIZING , and (4) specifies the objective variable.

## The GAMS Output Report (excerpts only)

The full GAMS output report is much more extensive than the small excerpts shown here, and contains many aids for interpreting and diagnosing your model. Moreover, you can modify the output format to suit your particular purposes.

#### Equations Listing

---- cost  =E=  define objective function

cost..  - 0.225*x(Seattle,New-York) - 0.153*x(Seattle,Chicago)

- 0.162*x(Seattle,Topeka) - 0.225*x(San-Diego,New-York)

- 0.162*x(San-Diego,Chicago) - 0.126*x(San-Diego,Topeka) + z =E= 0 ;

(LHS = 0)

---- supply  =L=  observe supply limit at plant i

supply(Seattle)..  x(Seattle,New-York) + x(Seattle,Chicago) + x(Seattle,Topeka)
=L= 350 ; (LHS = 0)

supply(San-Diego)..  x(San-Diego,New-York) + x(San-Diego,Chicago)

+ x(San-Diego,Topeka) =L= 600 ; (LHS = 0)

---- demand  =G=  satisfy demand at market j

demand(New-York)..  x(Seattle,New-York) + x(San-Diego,New-York) =G= 325 ;

(LHS = 0, INFES = 325 ****)

demand(Chicago)..  x(Seattle,Chicago) + x(San-Diego,Chicago) =G= 300 ;

(LHS = 0, INFES = 300 ****)

demand(Topeka)..  x(Seattle,Topeka) + x(San-Diego,Topeka) =G= 275 ;

(LHS = 0, INFES = 275 ****)

The equation listing shows the individual constraints that have been generated from the blocks specified in the GAMS input. In GAMS one can write down indexed equation blocks in a very compact form, that will generate a large amount of single equations. In our example we have specified three blocks of equations that generated six single equations.

#### Column Listing

---- x  shipment quantities in cases

x(Seattle,New-York)
(.LO, .L, .UP, .M = 0, 0, +INF, 0)
-0.225   cost
1       supply(Seattle)
1       demand(New-York)

x(Seattle,Chicago)
(.LO, .L, .UP, .M = 0, 0, +INF, 0)
-0.153   cost
1       supply(Seattle)
1       demand(Chicago)

x(Seattle,Topeka)
(.LO, .L, .UP, .M = 0, 0, +INF, 0)
-0.162   cost
1       supply(Seattle)
1       demand(Topeka)

REMAINING 3 ENTRIES SKIPPED

---- z  total transportation costs in thousands of dollars

z
(.LO, .L, .UP, .M = -INF, 0, +INF, 0)
1       cost

The column listing gives information on the individual variables that were generated. The variable X(I,J) expands to six single variables. When many variables are generated for one block the default listing shows only the first three (this can be changed by the user).

## Solve Messages

MODEL STATISTICS

BLOCKS OF EQUATIONS           3     SINGLE EQUATIONS            6
BLOCKS OF VARIABLES           2     SINGLE VARIABLES            7
NON ZERO ELEMENTS            19

GENERATION TIME      =        0.000 SECONDS      4 MB

EXECUTION TIME       =        0.000 SECONDS      4 MB

S O L V E      S U M M A R Y

MODEL   transport           OBJECTIVE  z
TYPE    LP                  DIRECTION  MINIMIZE
SOLVER  BDMLP               FROM LINE  32

**** SOLVER STATUS     1 Normal Completion
**** MODEL STATUS      1 Optimal
**** OBJECTIVE VALUE              153.6750

RESOURCE USAGE, LIMIT          0.047      1000.000
ITERATION COUNT, LIMIT         4    2000000000

BDMLP 1.3

Originally developed by
A. Brooke, A. Drud, and A. Meeraus,
World Bank, Washington, D.C., U.S.A.
A. Drud, ARKI Consult, Denmark
M. Bussieck, GAMS Dev. Corp. U.S.A.
Work space requested by solver --    0.03 Mb
EXIT -- OPTIMAL SOLUTION FOUND.

The solve statement will generate the model (creation of single equations and variables corresponding to the specified model). First some statistics about the generated model are printed: number of equations, variables and non-zero elements.

In the solve summary we see that BDMLP is called to solve this model. BDMLP found an optimal solution to this problem in 4 iterations and 0.184 seconds. The messages following the solve summary are coming from the solver.

#### Solution

                       LOWER     LEVEL     UPPER    MARGINAL

---- EQU cost            .         .         .        1.000

cost  define objective function

---- EQU supply  observe supply limit at plant i

LOWER     LEVEL     UPPER    MARGINAL

Seattle       -INF    350.000   350.000      EPS
San-Diego     -INF    550.000   600.000      .

---- EQU demand  satisfy demand at market j

LOWER     LEVEL     UPPER    MARGINAL

New-York   325.000   325.000     +INF      0.225
Chicago    300.000   300.000     +INF      0.153
Topeka     275.000   275.000     +INF      0.126

---- VAR x  shipment quantities in cases

LOWER     LEVEL     UPPER    MARGINAL

Seattle  .New-York      .       50.000     +INF       .
Seattle  .Chicago       .      300.000     +INF       .
Seattle  .Topeka        .         .        +INF      0.036
San-Diego.New-York      .      275.000     +INF       .
San-Diego.Chicago       .         .        +INF      0.009
San-Diego.Topeka        .      275.000     +INF       .

LOWER     LEVEL     UPPER    MARGINAL

---- VAR z              -INF    153.675     +INF       .

z  total transportation costs in thousands of dollars

**** REPORT SUMMARY :        0     NONOPT
0 INFEASIBLE
0  UNBOUNDED

The solution is printed here. The marginals correspond to the duals for the equations and to the reduced costs for the variables.

GAMS provides many more facilities to tailor the output to your needs and to generate management-style reports. In order to use the advanced report writing facilities of GAMS you don't need to learn yet another language. In GAMS both data manipulation, model specification and report writing is done in one single environment.

### Reference

Dantzig G. B., Linear Programming and Extensions, Princeton University Press, Princeton, New Jersey, 1963, Chapter 3-3.