Compile time imports from GDX files

Top  Previous  Next

A set of dollar commands can be used to cause GAMS to read data from a GDX file at compile time.  The data read from the GDX file will be the data present in it at the time that the compile job is begun.

The basic syntax involves a three-part sequence

 

$Gdxin filename

$Load itemname

$Gdxin

 

The first part is an initial $Gdxin command which also specifies the filename to be used.  A file with this filename and the extension .GDX is looked for in the current working directory.  In turn this command opens the GDX file and prepares it for reading.

The second part of the sequence is one or more $Load commands or the alternative Loaddc (which eploys domain checking).  These commands specify the items to be read from the GDX file.  Several commands may be used and each line can read more than one item.  For example, one could load several items with the command structure

 

$Gdxin filename

$load itemname1

$loaddc itemname2

$load itemname3

$load itemname4

$Gdxin

 

or could use the structure

 

$Gdxin filename

$load itemname1 itemname2 itemname3  itemname4

$loaddc itemname5 itemname6 itemname7  itemname8

$Gdxin

 

It is also possible to have different names for parameters in the GDX file and the GAMS program.  In such a case the syntax is

 

$load internalname1=gdxfileitemname1 i2=gf2

 

Any parameter data can be loaded as can set data defining domains and variable/equation data.

The third part of the sequence simply consists of another $Gdxin command which closes the GDX file.  Actually the statements can be intermixed with GAMS calculations solves etc. but must eventually be closed with a $Gdxin.

Example:

 In the model gdxintrnsport.gms we introduce the sequence

 

 $GDXin tran2

   Sets

       i   canning plants

       j   markets          ;

 $load i j

   Parameters

        a(i)  capacity of plant i in cases

        b(j)  demand at market j in cases;

 $load a=sup

 $loaddc b=dem

  Parameter d(i,j) distance in thousands of miles;

 $load d

   Scalar f  freight in dollars per case;

 $load f

 $GDXin

 

This loads data from the GDX file named tran2.gdx that was saved by the example gdxexectrnsport.gms.

Notes:

Items must be declared with Set, Parameter, Scalar, Variable or Equation statements before the Load appears.
When using $load  GAMS does not domain check ignoring items that are resident in GDX files for named set dependent parameters, variables, equations and sets that do not match current set elements.  GAMS will ignore these items and will not create errors or cause generation of any messages.
When using $loaddc GAMS does domain checking generating errors for items that are resident in the GDX files  that do not match internal sets.
One can import items for set positions that are not in existing sets where the set specified for that position is equivalent to the universal set (i.e. when an * is used or a terms equivalenced to the universal set or the set is a subset of the universal set).
When the $Load is not followed by arguments this causes a listing of the GDX file contents to be generated.