Read Sets from txt file

1,036 views
Skip to first unread message

Zheng

unread,
May 15, 2011, 11:42:39 PM5/15/11
to am...@googlegroups.com
Hi, is there any way to read Sets from txt files rather than tab or spreadsheet? Iike reading parameters using read command. Here's briefly my problem:

ampl: set a = 1..8;
ampl: set b within {a,a};
ampl: read b < test.txt;

syntax error
context:  read  >>> b  <<< < test.txt;


in my test.txt:

in out
1 2
2 3
2 4
3 5
3 6
4 6
5 7
6 8

I really want to read sets from that txt file. So thank you very much if anyone could help figure out it.

Zheng

unread,
May 16, 2011, 12:19:16 AM5/16/11
to am...@googlegroups.com
Actually my problem is my txt file contains:

1
2
3
28
...

1 2  
2 3  
2 4  
3 5  
3 6  
4 6  
5 7  
6 8  

and I really want to read the first several lines for some other parameters1 2   1
2 3   1
2 4   1
3 5   1
3 6   1
4 6   1
5 7   1
6 8   1
(unrelated with this set) and the rest into a set; So I don't want to split the file up and read parameters and sets seperately.

Robert Fourer

unread,
May 16, 2011, 12:00:34 PM5/16/11
to am...@googlegroups.com

The "read" statement syntax doesn't work to read members into a set.  What you can do instead is to read them into parameters and then create the set.  For example,

 

   param ns = 8;

   param nb integer >= 1;

   param b1 {1..nb} in 1..ns;

   param b2 {1..nb} in 1..ns;

   read nb, {j in 1..nb} (b1[j],b2[j]) <test.txt;

   set b = setof {j in 1..nb} (b1[j],b2[j]);     

 

Note that you need to put one more number into your input, giving the number of members of b to be read from test.txt -- unless this number is always equal to ns, in which case you can just use ns instead of nb.

 

Bob Fourer

4...@ampl.com

 

 

From: am...@googlegroups.com [mailto:am...@googlegroups.com]

On Behalf Of Zheng
Sent: Sunday, May 15, 2011 10:43 PM
To: am...@googlegroups.com
Subject: [AMPL 4634] Read Sets from txt file

Reply all
Reply to author
Forward
0 new messages