Problem reading data from text file

18 views
Skip to first unread message

Artur Zygadło

unread,
Mar 20, 2018, 10:58:30 AM3/20/18
to AMPL Modeling Language
Hello,

I have a text file (file.txt) in format:

M1 M2 M3
1 45.7256046374655 28.4062955882777 35.8474298744489
2 46.4275276498946 47.374679706601 32.9808478906229
3 46.1226020555142 22.4848278029251 43.1264515215413
4 45.1024313926292 34.8955821456699 40.7070988034582
5 44.6652808915011 31.1993941104196 41.4451205380671
6 45.514755530348 35.5824714998599 36.2780370857497
7 45.2545781813678 26.5073124093516 43.1065966659425
8 45.5026244770681 31.0676897525546 40.1115744611901
9 45.4537472012675 35.9457396335022 41.0270968689643
10 45.441471952754 21.0964683718102 40.2429333204404
...

There are 1000 rows and the first row with column names (these column names are elements of set S). I would like to read the data into a 1000x3 array in AMPL.

In my .dat file I have:
set S:= M1 M2 M3 ;

and in my .mod file:

param v {1..1000, S} >= 0;
read {i in 1..1000, j in S} v[i, j] <file.txt;

--------------------

I keep getting an error:
Error at _cmdno 1 executing "read" command
(file wdwr.mod, line 11, offset 201):
no data for set S

What is the proper syntax of the read command to read data from this file?

Best regards,
Artur

AMPL Google Group

unread,
Mar 20, 2018, 8:25:39 PM3/20/18
to am...@googlegroups.com
In order to load this data directly from the file using a single read command you should remove the first row and first column of the file so that only values would remain.

Alternatively, as the data in the file is already in the form of a table you can add ":=" to the end of the first line of the file and load the data as follows:

set S;

param v {1..1000, S} >= 0;
data;

set S := M1 M2 M3;
param v : include file.txt;
model;
display S;
display v;

The content of file.txt should look like this:


M1 M2 M3 :=

1 45.7256046374655 28.4062955882777 35.8474298744489
2 46.4275276498946 47.374679706601 32.9808478906229
3 46.1226020555142 22.4848278029251 43.1264515215413
4 45.1024313926292 34.8955821456699 40.7070988034582
5 44.6652808915011 31.1993941104196 41.4451205380671
6 45.514755530348 35.5824714998599 36.2780370857497
7 45.2545781813678 26.5073124093516 43.1065966659425
8 45.5026244770681 31.0676897525546 40.1115744611901
9 45.4537472012675 35.9457396335022 41.0270968689643
10 45.441471952754 21.0964683718102 40.2429333204404
...

Best regards,
Filipe

--
Filipe Brandão
am...@googlegroups.com
{#HS:544974216-3166#}
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages