Reading sparse data
Suppose we want to read in data of the form
i, j, valueij |
from an ASCII file, setting up a dynamic array A(range, range) with just the A(i,j)= valueij for the pairs (i,j) which exist in the file. Here is an example which shows three different ways of doing this. We read data from differently formatted files into three different arrays, and using writeln show that the arrays hold identical data.
Data input with initializations from
The first method, using the initializations block, has already been introduced (transport problem in Section A transport example).
model "Trio input (1)" declarations A1: dynamic array(range,range) of real end-declarations ! First method: use an initializations block initializations from 'data_1.dat' A1 as 'MYDATA' end-initializations ! Now let us see what we have writeln('A1 is: ', A1) end-model
The data file data_1.dat could be set up thus (every data item is preceded by its index-tuple):
MYDATA: [ (1 1) 12.5 (2 3) 5.6 (10 9) -7.1 (3 2) 1 ]
This model produces the following output:
A1 is: [(1,1,12.5),(2,3,5.6),(3,2,1),(10,9,-7.1)]
Data input with readln
The second way of setting up and accessing data demonstrates the immense flexibility of readln. The format of the data file may be freely defined by the user. After every call to read or readln the parameter nbread contains the number of items read. Its value should be tested to check whether the end of the data file has been reached or an error has occurred (e.g. unrecognized data items due to incorrect formating of a data line). Notice that read and readlninterpret spaces as separators between data items; strings containing spaces must therefore be quoted using either single or double quotes.
model "Trio input (2)" declarations A2: dynamic array(range,range) of real i, j: integer end-declarations ! Second method: use the built-in readln function fopen("data_2.dat",F_INPUT) repeat readln('Tut(', i, 'and', j, ')=', A2(i,j)) until getparam("nbread") < 6 fclose(F_INPUT) ! Now let us see what we have writeln('A2 is: ', A2) end-model
The data file data_2.dat could be set up thus:
File data_2.dat:
Tut(1 and 1)=12.5 Tut(2 and 3)=5.6 Tut(10 and 9)=-7.1 Tut(3 and 2)=1
When running this second model version we get the same output as before:
A2 is: [(1,1,12.5),(2,3,5.6),(3,2,1),(10,9,-7.1)]
Data input with diskdata
As a third possibility, one may use the diskdata I/O driver from module mmetc to read in comma separated value (CSV) files. With this driver the data file may contain single line comments preceded with !.
model "Trio input (3)" uses "mmetc" ! Required for diskdata declarations A3: dynamic array(range,range) of real end-declarations ! Third method: use diskdata driver initializations from 'mmetc.diskdata:' A3 as 'sparse,data_3.dat' end-initializations ! Now let us see what we have writeln('A3 is: ', A3) end-model
The data file data_3.dat is set up thus (one data item per line, preceded by its indices, all separated by commas; strings should be quoted using either single or double quotes):
1, 1, 12.5 2, 3, 5.6 10,9, -7.1 3, 2, 1
We obtain again the same output as before when running this model version:
A3 is: [(1,1,12.5),(2,3,5.6),(3,2,1),(10,9,-7.1)]
Note: the diskdata format is deprecated, it is provided to enable the use of data sets designed for mp-model and does not support certain new features introduced by Mosel.
© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.