The Data Section of a Model

The data section allows you to isolate data from the rest of your model. This is a useful practice in that it facilitates model maintenance and scaling of a model’s dimensions.

The data section begins with the keyword DATA: (including the colon) and ends with the keyword ENDDATA. In the data section, you can have statements to initialize set members and/or the attributes of the sets you declared in a previous sets section. These expressions have the syntax:

object_list = value_list;

The object_list contains the names of the attributes and/or a set whose members you want to initialize, optionally separated by commas. There can be no more than one set name in object_list, while there may be any number of attributes. If there is more than one attribute name in object_list, then the attributes must be defined on the same set. If there is a set name in object_list, then all attributes in object_list must be defined on this set.

The value_list contains the values you want to assign to the members of object_list, optionally separated by commas. As an example, consider the following model:

SETS:

  SET1 /A, B, C/: X, Y;

ENDSETS

DATA:

  X = 1, 2, 3;

  Y = 4, 5, 6;

ENDDATA

We have two attributes, X and Y, defined on the SET1 set. The three values of X are set to 1, 2, and 3, while Y is set to 4, 5, and 6. We could have also used the following compound data statement to the same end:

SETS:

  SET1 /A, B, C/: X, Y;

ENDSETS

DATA:

  X, Y =

  1, 4,

  2, 5,

  3, 6;

ENDDATA

Looking at this example, you might imagine X would be assigned the values 1, 4, and 2 because they are first in the values list, rather than the true values of 1, 2, and 3. When LINGO reads a data statement's value list, it assigns the first n values to the first position of each of the n attributes in the attribute list, the second n values to the second position of each of the n attributes, and so on. In other words, LINGO is expecting the input data in column form rather than row form.

As mentioned, we can also initialize the set members in the data section. Modifying our sample model to use this approach by moving the set members from the sets section to the data section we get:

SETS:

  SET1: X, Y;

ENDSETS

DATA:

  SET1, X, Y =

    A   1  4

    B   2  5

    C   3  6;

ENDDATA

This method is, perhaps, the most elegant of the three methods presented above, in that all model data (attribute values and set members) are isolated within the data section.  Given that a model's data is subject to frequent change, having all data isolated to one area should make model maintenance simpler.  We also refer to this concept of isolating data as data independence.

One additional method for inputting two-dimensional tables is through the use of a data frames.  Consider the following sets section for a transportation model where we are minimizing the cost of shipping product from warehouses to customers:

! A 3 Warehouse, 4 Customer

  Transportation Problem;

SETS:

  WAREHOUSE    : CAPACITY;

  CUSTOMER     : DEMAND;

  ROUTES( WAREHOUSE, CUSTOMER) : COST, VOLUME;

ENDSETS

We could enter the data for a four-customer, three-warehouse model with the data section:

DATA:

  CUSTOMER = C1 C2 C3 C4;

  WAREHOUSE = WH1 WH2 WH3;

  COST =

   6  2  6  7

   4  9  5  3

   8  8  1  5

  ;

  CAPACITY =   30, 25, 21;

  DEMAND   = 15 17 22 12;

ENDDATA

On the other hand, the data frame style of input allows us to enter the first dimension of a two-dimensional set along with an attribute of the same two-dimensional set, meaning we could use the alternative data section:

DATA:

  CUSTOMER = C1 C2 C3 C4;

  WAREHOUSE, COST =

         WH1   6  2  6  7

         WH2   4  9  5  3

         WH3   8  8  1  5

  ;

  CAPACITY =   30, 25, 21;

  DEMAND   = 15 17 22 12;

ENDDATA

Note that the WAREHOUSE set and COST attribute are now both entered in the same data statement.  The data frame method of input can be useful when reading in preexisting data sets from external files (e.g., linear regression data sets).  The complete version of this model can be found in the LINGO sample models set under the name TRANFRAME