MPL supports reading in data from external text files. Text files are mainly used when the data for the model is stored locally, generated by other programs or by running SQL queries from a database. The data can be read either in a dense format, where all numbers are specified, or in a sparse column format, where only the numbers than have non-zero values are included. MPL can also read single scalar numbers for data constants. The data file can be any free-format text file, which gives the developer the ability to read the data from different sources without first requiring a conversion to a standard format. Reading data from text files is one of the fastest ways available to import data into MPL.
When reading data from a text file, use the keyword DATAFILE followed by a filename inside parentheses. If you use any characters in the filename, such as '-' and '#', that have special meaning to MPL, you can enter them by including the filename in double quotes (").
Demand[product,month] := 1000 DATAFILE("demand.dat");
In our example, the demand vector has 36 values, 12 months times 3 products, which are stored in the file demand.dat. Each number in the file is multiplied by 1000 as it is read. The following is an example of a data file.
{ Demand.dat } ! Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ! ------------------------------------------------------------ 50, 60, 70, 80, 90, 100, 110, 120, 120, 120, 110, 100 24, 30, 36, 42, 48, 52, 50, 48, 44, 40, 36, 32 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 13
The file is a free-format text file, in which numbers are read in the order they appear. Separate the numbers with commas or spaces and make sure there are enough numbers to satisfy the size of the vector. In order to use comments in the DATAFILE insert everything inside curly braces for multi-line comments and following an exclamation mark for single line comments. See Chapter 5.2: Basic Input Elements for more information on inserting comments.
MPL allows you to read multiple data vectors and simple data constants from the same datafile. Simply group them together in the data section and give the same filename inside the parentheses. MPL will then continue reading the values from where it left off from the previous definition.
Sometimes, the data you need to read is sparse, meaning that not all the index entries have values. In this case, to read the sparse data from an external file, use the keyword SPARSEFILE instead of DATAFILE. MPL then expects both the subscripts and the values for each entry in the given file. Here is an example that shows how you can read a sparse datafile into a data vector:
ProdCost[machine, product] := SPARSEFILE("prodcost.dat");The file "produce.dat" could look something like:
mach1, prod1, 73.30, mach1, prod2, 52.90, mach1, prod3, 65.40, mach1, prod4, 47.60, mach2, prod1, 79.00, mach2, prod3, 66.80, mach3, prod2, 52.00, mach3, prod3, 53.80
If the file you want to read the data elements from contains other columns of data, MPL allows you to specify which column is chosen, by adding a comma and a column number after the filename inside the parentheses.
ProdCost[machine, product] := SPARSEFILE("prodcost.dat", 2);
This will read the production cost values from the fourth column of the data file. Here is a sample of the datafile produce.dat:
mach1, prod1, 450, 73.30, mach1, prod2, 500, 52.90, mach1, prod3, 350, 65.40, mach1, prod4, 425, 47.60, mach2, prod1, 355, 79.00, mach2, prod3, 500, 66.80, mach3, prod2, 344, 52.00, mach3, prod3, 250, 53.80