User Tools

Site Tools


This is an old revision of the document!

How do I split up my model in a definition and into a data part?

Consider the transport example. Please find below two part of the transport model. The first one (first.gms with the equations and (empty) set/parameter definition) must be run first and saved in a restart file.

It may contain data assignments (line 16 in first.gms) and therefore we cannot execute the GAMS code (impossible without data). Hence we have to run gams first a=c s=sfirst

The a=c says 'compile only'. Another specialty is that we have to provide the compiler with the dimension of the sets (we don't have to do that if we fill the sets with data, but without data we have to do that). Hence the basic set definition (i and j) have to have the '*' as an index.

In the second model (second.gms) we do the set and parameter definitions again. Usually, GAMS doesn't allow that so we have to do the $on/offmulti. Run gams second r=sfirst

GAMS takes the restart file, populates the sets and parameters according to the data definition in second.gms, knows that the code in the restart file hasn't be executed and does the execution now. Then it executes the code in second.gms, which triggers the solve.


     i(*)   canning plants   /  /
     j(*)   markets          /  / ;

     a(i)    capacity of plant i in cases / /
     b(j)    demand at market j in cases / /
     d(i,j)  distance in thousands of miles / /;


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 ;

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

     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/ ;


     i   canning plants   / seattle, san-diego /
     j   markets          / new-york, chicago, topeka / ;

     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/ ;

Solve transport using lp minimizing z ;
Display x.l, x.m ;
IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/split_up_my_model_in_a_definition_and_into_a_data_part.1620331583.txt.gz ยท Last modified: 2021/05/06 22:06 by Atharv Bhosekar