Set Indexing

41 views
Skip to first unread message

cajimen0

unread,
Oct 14, 2016, 7:53:56 PM10/14/16
to AMPL Modeling Language
Hi

I am writing my first model in AMPL but I got a problem trying to create some kind of "set of sets" with a large data. 

Can somebody please give a clue about how write this example set of 10 sets:

model file:

param n := 10;
set BL = 1..n;
set PRE{BL};
param P1 {BL};
param P2 {BL};
param P3 {BL};
param P4 {BL};
param P5 {BL};
param P6 {BL};


data file:

table prec{P1,P2,P3,P4,P5,P6} IN "ODBC" "Data.xls" "prec": [BL], PREC;
read table prec;



excel file attached:

BL P1 P2 P3 P4 P5 P6
1    
2   2   2
3   2   3 10 
4   2   4 10 
5   1   5
6   2   0
7    
8   3   8 10 9
9    
10 5   3 7 10 4 2


Thanks in advance. 
Data.xls

Robert Fourer

unread,
Oct 17, 2016, 9:44:57 AM10/17/16
to am...@googlegroups.com
AMPL's table facility can only read spreadsheet tables that look like relational data tables. It cannot read the sort of irregular table that you have in your spreadsheet. Also it does not work with indexed collections of sets like "set PRE{BL}".

There are still ways to read this data, if you arrange it differently. However it is not clear to me how your data from the spreadsheet should be assigned to the AMPL sets and params. When you define "set PRE{BL}" you are creating ten sets PRE[1], PRE[2], ..., PRE[10]. What members would you like to put in each of these sets?

Bob Fourer
am...@googlegroups.com

=======

cajimen0

unread,
Oct 17, 2016, 3:13:36 PM10/17/16
to AMPL Modeling Language, 4...@ampl.com
Hi Bob

Thanks for you answer. 

I am expecting define set PRE{BL} in this way:

set PREC[1] :=   ;
set PREC[2] :=  2 2 9 ;
set PREC[3] :=  2 3 10 ;
set PREC[4] :=  2 4 10 ;
set PREC[5] :=  1 5 ;
set PREC[6] :=  2 0 6 ;
set PREC[7] :=   ;
set PREC[8] :=  3 8 10 9 ;
set PREC[9] :=   ;
set PREC[10] :=  5 3 7 10 4 2 ;

Using these 10 definitions in data is working to solve my model , but my real problem has thousand of this kind of sets, so reading those sets from a external file is important because I dont have the time to type every set on data.


Thanks in advance.

Carlos.

Robert Fourer

unread,
Oct 18, 2016, 3:52:15 PM10/18/16
to am...@googlegroups.com
You can't have

set PREC[2] := 2 2 9 ;

since it is not possible to have the same number appear twice in the same set. Is this a mistake, or do you want something like

param n;
param pmax {1..n};
param PREC {j in 1..n, k in 1..pmax[j]};

where PREC[j,k] is the kth value in the list for "set" j? In the latter case, PREC[2,1] and PREC[2,2] could both be 2, and all of the param could be read from a spreadsheet if you list the values for PREC in a table like

N P PREC
2 1 2
2 2 2
2 3 9
3 1 2
3 2 3
3 3 10
4 1 2
4 2 4
4 3 10
Message has been deleted

cajimen0

unread,
Oct 19, 2016, 11:09:52 PM10/19/16
to AMPL Modeling Language, 4...@ampl.com
Hi Bob

Thanks for your response.


You are right about set PREC[2] := 2 2 9 ; is not possible, was a typo mistake. The correct one is set PREC[2] := 2 9 ;

Unfortunately these data need to be a set because in the model they are declared as a set. I attached a working model and data file testing my problem. I write every set PREC{setBL} in data just to test the model and it works, but my big concern is learn how read these sets from a spreadsheet because my real problems have 100.000 or more of those sets and I can not write every set by hand.

Thanks for your interest.

Carlos.
Newman.xls
cpitorig2.mod
newman2.dat

Robert Fourer

unread,
Oct 21, 2016, 4:11:44 PM10/21/16
to am...@googlegroups.com
In your data file, the number of members in each PREC set is included as a member of the set. This does not seem right, and it explains why you got 2 2 9 as the members of one of the sets. To include just the members shown in the spreadsheet, you could use the following approach:

param pmax = 5;
param Pval {setBL,1..pmax} default -1;

set PREC {j in setBL} = setof {pr in 1..pmax} Pval[j,pr] diff {-1};

table prec IN "ODBC" "Newman.xls" "prec": [j ~ setBL], {pr in 1..pmax} <Pval[j,pr] ~ ("P"&pr)>;
read table prec;

This table statement syntax is described in chapter 10 of the AMPL book, in the subsection "Indexed collections of data columns." Even for >1000 sets as in the spreadsheet you posted, it runs very fast.

Carlos Andrés Jiménez Builes

unread,
Oct 23, 2016, 9:52:05 AM10/23/16
to am...@googlegroups.com
Hi Bob

Your code works fantastic!!! 

Thanks for help me with that issue and share the source to learn more about indexing a set. 



Regards


Carlos

--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/PcpP72wH8CQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+unsubscribe@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