indexing over subset elements and read data

49 views
Skip to first unread message

Rupsana Parveen

unread,
Mar 21, 2018, 10:51:59 AM3/21/18
to AMPL Modeling Language
Hi Bob,

I have few questions : 

#.mod file

param nos := 3;

param limit1 := 150;

param limit2 :=16;

set S ordered;

set S1{i in S} within {1..nos};

set S2{i in S, S[i]} within {1..limit1, 1..limit1, 1..limit1};

set C{i in K} within {1..limit2,1..limit2}; 


param P{k in K, i in C[k], t in S2[a,b]};

var x{k in K};

var y{t in S2[a,b], a in S1[1],b in S1[2]};


#.dat file

set K := 1 2;

set S := 1 2;

set S1[1] := 1 2 3;

set S1[2] := 1 2 3;

set S2[1,1] = (70,52,10);

set S2[1,2] = (10,45,29);

set S2[1,3] = (80,85,23);

set S2[2,1] = (82,71,43);

set S2[2,2] = (83,72,41);

set S2[2,3] = (89,15,12);

set C[1] = (1,1),..,(1,16),..(16,1),..,(16,16);

set C[2] = (1,1),..(1,16),..(16,1),..,(16,16);


My current data file (abc.txt) for parameter P is in the following format: 


1 1 1 70 52 10 0.01

1 3 2 10 45 29 0.02

1 4 6 80 85 23 0.03

1 5 9 70 52 10 0.04

1 5 10 82 71 43 0.05

1 10 12 83 72 41 0.06

2 10 12 10 45 29 0.07

2 12 10 83 72 41 0.08

2 12 14 82 71 43 0.09

2 13 14 80 85 23 0.1

2 13 15 89 15 12 0.11

2 15 16 89 15 12 0.12

Where 1st column (black) is for k, 2nd and 3rd columns (blue) are for i, 4th 5th and 6th columns (red) are for t in P[k,i,t], and last column (purple) is the value of P . 


I have a constraint like bellow:


subject to constraint1 {a in S1[1], b in S1[2], t in  S2[a,b] }: y[t,a,b]=sum{k in K} x{k}* (sum{i in C[k]} P[k,i,t]);


In summary, say I have 2 S, When S= 1, I have 3 subsets, which is defined in S1[1]. Similarly for S=2. Now , when S=1, S1[1]=1, then the elements are defined in set S2[1,1], and elements are always in this format (a,b,c). 


For example, when S=1, S1[1]=1, and S2[1,1]= (70,52,10) , then constraint will be      y[70,52,10,1,1]= x[1] *(P[1,1,1,70,52,10]+P[1,5,9,70,52,10]);

Similarly, when S=1, S1[2]=3, and S2[1,3]= (80,85,23), then constraint will be              y[80,85,23,1,3] = x[1]*P[1,4,6, 80,85,23]+x[2]* P[2,13,14, 80,85,23];


Not sure if I defined “{ a in S1[1], b in S1[2], t in  S2[a,b] }: y[t,a,b]” correctly in constraint1 based on what I explained above for y[70,52,10,1,1] and y[80,85,23,1,3].



 My questions are- 

I know the elements for S2[1,1],.…S2[2,3] , and the number of elements in these subsets are huge. In some cases, it’s close to 1 million. So, is it possible to define these elements in a different text file? If yes, how do we call it in data file? Say, we define elements of S2[1,1] in one text file, S2[1,2] in another text file, and so on. 



Is there any easy way to tell AMPL that when t= (70,52,10), find corresponding i values (in this case (1,1) and (5,9)), and corresponding k values (in this case 1)  from data file (abc.txt), and then use the corresponding P value in constraint? Similarly for all other t’s.  Or any other suggestions how to input P values in the constraint from above data format? 



One thing-  (70,52,10) can appear to only one of the S2 , as in this case it is in S2[1,1]. It cannot appear to any other S2’s. Similarly (89,15,12) can appear only in S2[2,3]. My C[1] and C[2] might not in correct format. I will fix it later. I just showed only one element for all S2 to explain my problem. The input data file and set elements are huge in my original case. 


I hope I made my questions clear. Please let me know if you have any confusions.   


Thanks in advance.




AMPL Google Group

unread,
Mar 21, 2018, 1:56:02 PM3/21/18
to am...@googlegroups.com
I know the elements for S2[1,1],.…S2[2,3] , and the number of elements in these subsets are huge. In some cases, it's close to 1 million. So, is it possible to define these elements in a different text file? If yes, how do we call it in data file? Say, we define elements of S2[1,1] in one text file, S2[1,2] in another text file, and so on.

Yes, you can. You need to put data in a file and use AMPL include command. For example: set S2[1,1] := include dt.txt; where the file dt.txt has the values of set S2.

Is there any easy way to tell AMPL that when t= (70,52,10), find corresponding i values (in this case (1,1) and (5,9)), and corresponding k values (in this case 1) from data file (abc.txt), and then use the corresponding P value in constraint? Similarly for all other t's. Or any other suggestions how to input P values in the constraint from above data format?

I don't think you have correctly specified 'P' in your example. If you just want to explain then it's fine, otherwise you have more than three indices when you define data for P. Regarding your question, when you restrict indices of 't', you will apply constraints for those indices only. For example, when you define the constraint as follows:

s.t c {k in K,i in C[k], t in S2[1,1]}: P[k,i,t]...

AMPL will have the constraint defined for the only valid indices of 't'. You can use expand c; to view the constraints that AMPL will define for your indices.


--
Paras Tiwari
am...@googlegroups.com
{#HS:545721264-3295#}
--
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.



Rupsana Parveen

unread,
Mar 23, 2018, 11:07:10 AM3/23/18
to AMPL Modeling Language
Hi Paras,
Thanks for your reply. 

Yes, you are right. My P was specified correctly. I tried with following:
param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S2[1,1]};

It was not giving me any error. But I have several S2 (S2[1,1],S2[1,2], S2[1,3], S2[2,1],S2[2,2],S2[2,3]) as I mentioned before.

When I try this -  param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S2[i in S,S1[i]]}; 
I get the following error : 
"i is not defined
context:  param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S2[i  >>> in  <<< S,S1[i]]};"

Can you please tell me how to fix this P so that it can read all S2's ? 

Thanks

AMPL Google Group

unread,
Mar 23, 2018, 5:54:55 PM3/23/18
to am...@googlegroups.com
I think we could be clear about the indexing once we properly define the variables. When I send your model and data to AMPL, I am getting several errors. Below is one declaration in your model file:


set S2{i in S, S[i]} within {1..limit1, 1..limit1, 1..limit1};

You have declared S as an set and you can't access the members of S as S[i]. If you want to define S2 over the S X S then you could do set S2{S,S} with in{1..limit1,1..limit1,1..limit1}.

You need to define i. One way to declare P is as follows:
param P{k in K,C[k],i in S,S2[i,i]};

--
Paras Tiwari
am...@googlegroups.com
{#HS:545721264-3295#}
On Fri, Mar 23, 2018 at 3:07 PM UTC, <am...@googlegroups.com> wrote:
Hi Paras,
Thanks for your reply.

Yes, you are right. My P was specified correctly. I tried with following:
param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S2[1,1]};

It was not giving me any error. But I have several S2 (S2[1,1],S2[1,2], S2[1,3], S2[2,1],S2[2,2],S2[2,3]) as I mentioned before.

When I try this - param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S2[i in S,S1]};

I get the following error :
"i is not defined
context: param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S2[i >>> in <<< S,S1]};"


Can you please tell me how to fix this P so that it can read all S2's ?

Thanks
On Wed, Mar 21, 2018 at 5:55 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
I know the elements for S2[1,1],.…S2[2,3] , and the number of elements in these subsets are huge. In some cases, it's close to 1 million. So, is it possible to define these elements in a different text file? If yes, how do we call it in data file? Say, we define elements of S2[1,1] in one text file, S2[1,2] in another text file, and so on.

Yes, you can. You need to put data in a file and use AMPL include command. For example: set S2[1,1] := include dt.txt; where the file dt.txt has the values of set S2.

Is there any easy way to tell AMPL that when t= (70,52,10), find corresponding i values (in this case (1,1) and (5,9)), and corresponding k values (in this case 1) from data file (abc.txt), and then use the corresponding P value in constraint? Similarly for all other t's. Or any other suggestions how to input P values in the constraint from above data format?

I don't think you have correctly specified 'P' in your example. If you just want to explain then it's fine, otherwise you have more than three indices when you define data for P. Regarding your question, when you restrict indices of 't', you will apply constraints for those indices only. For example, when you define the constraint as follows:

s.t c {k in K,i in C[k], t in S2[1,1]}: P[k,i,t]...

AMPL will have the constraint defined for the only valid indices of 't'. You can use expand c; to view the constraints that AMPL will define for your indices.


--
Paras Tiwari
am...@googlegroups.com


Rupsana Parveen

unread,
Mar 25, 2018, 1:58:44 PM3/25/18
to AMPL Modeling Language

Hi, I think I was unable to make my problem clear. All I am having difficulties to define P as param in .mod file. Let me try to explain it to you again.  My data for P is in the following format: 


1 1 70 52 10 0.01

3 2 10 45 29 0.02

4 6 80 85 23 0.03

5 9 70 52 10 0.04

5 10 82 71 43 0.05

10 12 83 72 41 0.06

10 12 10 45 29 0.07

12 10 83 72 41 0.08

12 14 82 71 43 0.09

13 14 80 85 23 0.1

13 15 89 15 12 0.11

15 16 89 15 12 0.12

 

Where 1st column (black) is for k, 2nd and 3rd columns (blue) are for (i1,i2), 4th 5th and 6th columns (red) are for (j1,j2,j3) in P[k,i1,i2,j1,j2,j3], and last column (purple) is the value of P .


Here is the thing- the red columns(j1,j2,j3) in the data are from set S1[1], S1[2],S1[3],S1[4],S1[5], and S1[6]. 


If I define P individually for any of the S1 as like : param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[1]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[2]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[3]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[4]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[5]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[6]}; then it is absolutely fine. But as I said, (j1,j2,j3) will come from all of S1[1] to S1[6]. 


Now, if I define as P{k in K, (i1,i2) in C[k], i in S, (j1,j2,j3) in S1[i]}; to include all the S1, then it means I need 7 subscripts (k,i1,i2,i,j1.j2,j3) of  parameter P in constraint1. But as you see from the data, I have 6 (k, i1, i2, j1,j2, j3) and the last one is the value of P. If I define as  P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[S]}; then I get the "syntax error context:  param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in  >>> S1[S] <<< };"  Because as you mentioned in your last email that  we cannot the member of S in S1[S]. 


All I want to know how to define this P so that it can take all the values from set S1[1] to S1[6] , and will have 6 subscript. More specifically, just the S1 part in param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[?]};

Just for your convenience, I am adding the updated .mod and .dat file here again:


param limit1 := 150;

param limit2 :=16;

set S ordered;

set K ordered;

set S1{i in S} within {1..limit1, 1..limit1, 1..limit1};

set C{i in K} within {1..limit2,1..limit2};

var y{i in S,(j1,j2,j3) in S1[i]}; #that means my y-variables will be y[1, 70,52,10] , y[2,10,45,29], y[3, 80,85,23], y[4, 82,71,43], y[5, 83,72,41], y[6, 89,15,12]

var x{k in K}; #that means x-variables will be y[1] and y[2]

minimize obj: sum{k in K} x[k];

subject to constraint1 {i in S,(j1,j2,j3) in S1[i]}: y[i,j1,j2,j3]=sum{k in K} x[k]* (sum{(i1,i2) in C[k]} P[k,i1,i2,j1,j2,j3]);

 

set K := 1 2;

set S := 1 2 3 4 5 6;

set S1[1] :=  (70,52,10);

set S1[2] := (10,45,29);

set S1[3] := (80,85,23);

set S1[4] := (82,71,43);

set S1[5] := (83,72,41);

set S1[6] := (89,15,12);

set C[1] := (1,1), (3,2), (4,6), (5,9),(5,10), (10,12);

set C[2] := (10,12);(12,10),(12,14),(13,14),(13,15),(15,16);


I hope this time I was able to explain what I need to know. Thanks in advance. 

AMPL Google Group

unread,
Mar 25, 2018, 2:34:07 PM3/25/18
to am...@googlegroups.com
Since you defined S1 as an indexed collection of sets,


set S1 {i in S} within {1..limit1, 1..limit1, 1..limit1};

the references to {i in S, (j1,j2,j3) in S1} in your model will be in error. You need to instead write the indexing with a subscript on S1: {i in S, (j1,j2,j3) in S1[i]}. As for P, perhaps what you're looking for is to define

P {k in K, (i1,i2) in C[k], (j1,j2,j3) in union {i in S} S1[i]};

Then P will have 6 subscripts and you can use an AMPL data statement like the following in your data:

param P :=
1 1 1 70 52 10 0.01
1 3 2 10 45 29 0.02
1 4 6 80 85 23 0.03 . . .

--
Robert Fourer
am...@googlegroups.com
{#HS:545721264-3295#}
On Sun, Mar 25, 2018 at 5:58 PM UTC, <am...@googlegroups.com> wrote:
Hi, I think I was unable to make my problem clear. All I am having difficulties to define P as param in .mod file. Let me try to explain it to you again. My data for P is in the following format:

1 1 1 70 52 10 0.01
1 3 2 10 45 29 0.02
1 4 6 80 85 23 0.03
1 5 9 70 52 10 0.04
1 5 10 82 71 43 0.05
1 10 12 83 72 41 0.06
2 10 12 10 45 29 0.07
2 12 10 83 72 41 0.08
2 12 14 82 71 43 0.09
2 13 14 80 85 23 0.1
2 13 15 89 15 12 0.11
2 15 16 89 15 12 0.12

Where 1st column (black) is for k, 2nd and 3rd columns (blue) are for (i1,i2), 4th 5th and 6th columns (red) are for (j1,j2,j3) in P[k,i1,i2,j1,j2,j3], and last column (purple) is the value of P.

Here is the thing- the red columns(j1,j2,j3) in the data are from set S1[1], S1[2],S1[3],S1[4],S1[5], and S1[6].

If I define P individually for any of the S1 as like : param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[1]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[2]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[3]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[4]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[5]}; or P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[6]}; then it is absolutely fine. But as I said, (j1,j2,j3) will come from all of S1[1] to S1[6].

Now, if I define as P{k in K, (i1,i2) in C[k], i in S, (j1,j2,j3) in S1}; to include all the S1, then it means I need 7 subscripts (k,i1,i2,i,j1.j2,j3) of parameter P in constraint1. But as you see from the data, I have 6 (k, i1, i2, j1,j2, j3) and the last one is the value of P. If I define as P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[S]}; then I get the "syntax error context: param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in >>> S1[S] <<< };" Because as you mentioned in your last email that we cannot the member of S in S1[S].


All I want to know how to define this P so that it can take all the values from set S1[1] to S1[6] , and will have 6 subscript. More specifically, just the S1 part in param P{k in K, (i1,i2) in C[k], (j1,j2,j3) in S1[?]};

Just for your convenience, I am adding the updated .mod and .dat file here again:

param limit1 := 150;
param limit2 :=16;
set S ordered;
set K ordered;
set S1{i in S} within {1..limit1, 1..limit1, 1..limit1};
set C{i in K} within {1..limit2,1..limit2};

var y{i in S,(j1,j2,j3) in S1}; #that means my y-variables will be y[1, 70,52,10] , y[2,10,45,29], y[3,80,85,23], y[4, 82,71,43], y[5, 83,72,41], y[6, 89,15,12]


var x{k in K}; #that means x-variables will be y[1] and y[2]

minimize obj: sum{k in K} x[k];

subject to constraint1 {i in S,(j1,j2,j3) in S1}: y[i,j1,j2,j3]=sum{k in K} x[k]* (sum{(i1,i2) in C[k]} P[k,i1,i2,j1,j2,j3]);


set K := 1 2;
set S := 1 2 3 4 5 6;
set S1[1] := (70,52,10);
set S1[2] := (10,45,29);
set S1[3] := (80,85,23);
set S1[4] := (82,71,43);
set S1[5] := (83,72,41);
set S1[6] := (89,15,12);
set C[1] := (1,1), (3,2), (4,6), (5,9),(5,10), (10,12);
set C[2] := (10,12);(12,10),(12,14),(13,14),(13,15),(15,16);

I hope this time I was able to explain what I need to know. Thanks in advance.
On Fri, Mar 23, 2018 at 9:54 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
I think we could be clear about the indexing once we properly define the variables. When I send your model and data to AMPL, I am getting several errors. Below is one declaration in your model file:

set S2{i in S, S[i]} within {1..limit1, 1..limit1, 1..limit1};

You have declared S as an set and you can't access the members of S as S[i]. If you want to define S2 over the S X S then you could do set S2{S,S} with in{1..limit1,1..limit1,1..limit1}.

You need to define i. One way to declare P is as follows:
param P{k in K,C[k],i in S,S2[i,i]};

--
Paras Tiwari
am...@googlegroups.com


Reply all
Reply to author
Forward
0 new messages