high dimensional parameters with different levels

100 views
Skip to first unread message

Rupsana Parveen

unread,
Aug 16, 2016, 8:16:14 PM8/16/16
to AMPL Modeling Language

Hi AMPL experts,


I need to input 4-dimensional parameter weight in my optimization problem.


My .dat file is --


data;

set p1 := 1 2 3;

set p2 := 1 2 3;

set l1 := {(1,2,3,4) , (1,2,3) , (1,2)}; # It means if  p1 =1 then l1 will be 1,2,3,4,; if p1 =2 then l1 will be 1,2,3; if p1= 3 then l1 will be 1,2

set l2 := {(1,2,3,4,) , (1,2,3) , (1,2)}; # It means if  p2 =1, then  l2 will be 1,2,3,4,; if p2 =2 then l2 will be 1,2,3; if  p2= 3 then l2 will be 1,2


param weight :=

[:,*,*,*]: 1 2 3 :=

1 1 2 1 1/5 1/7

1 2 2 1 1/7 1/9

1 3 2 7 1 1

1 4 2 7 7 1

1 1 3 1 1

1 2 3 1/3 1/5

1 3 3 1/5 1

1 4 3 1/9 1

2 1 3 1 1

2 2 3 5 1

2 3 3 7 5;


In model file following variables are defined.

param weight {p1,l1,p2,l2}  >0;

var u {1..p1,1..l1} ;

var t {1..p1,1..l1, 1..p2, 1..l2} ;


The corresponding constraint is :


subject to constraint1 {i in p1, j in l1, s in p2, t  in l2: s>i } weight [i,j,s,t] * u[i,j] – u[s,t] = t[i,j,s,t];



My questions are-


The way I defined set l1 and l2 - is it correct?


In data table, l2 has 3 levels (1,2,3; marked in red) for first 4 weights , and l2 has 2 levels (1,2; marked in blue) for next 7 weights. The way I defined param weight table – is it correct? If not, then what is the best approach to input this type of parameter?

 

Thanks in advance. 


Robert Fourer

unread,
Aug 17, 2016, 2:09:52 PM8/17/16
to am...@googlegroups.com
You cannot create a set in AMPL like {(1,2,3,4),(1,2,3),(1,2)} that has members of different dimensions. As an example of why this isn't workable, to reference a variable x indexed over a set of two-dimensional members you write something like x[i,j], while to reference a variable x indexed over a set of three-dimensional members you write something like x[i,j,k]. This doesn't readily generalize to the case of a variable indexed over a set of mixed two-dimensional and three-dimensional members; a substantial generalization of the syntax and semantics of indexing would be necessary to handle the mixed case.

If the members in each tuple (1,2,3,4), (1,2,3), (1,2) will always be different, then you can instead consider defining indexed collections of ordered sets:

param p1;
set l1 {1..p1} ordered;

This requires you to specify the data as

set l1[1] := 1 2 3 4 ;
set l1[2] := 1 2 3 ;
set l1[3] := 1 2 ;

and similarly for p2, l2. Then in the model you can declare, for example,

param weight {i in 1..p1, l1[i], j in 1..p2, l2[j]};
var u {i in 1..p1, l1[i]};

This kind of indexing is described in Section 6.5 of the AMPL book (http://ampl.com/BOOK/CHAPTERS/09-sets2.pdf#page=10). Data for weight is specified as for any 4-dimensional parameter (see the sections on "higher dimensional" lists and tables in Chapter 9).

Bob Fourer
am...@googlegroups.com

=======

Victor Zverovich

unread,
Aug 17, 2016, 2:17:05 PM8/17/16
to am...@googlegroups.com
I1 and I2 are not defined correctly because you can't have tuples of different dimensions in a single set in AMPL. I1 can be replaced with the following expression though

  1 .. 5 - p1

and the same can be done with I2.

The weight data is not correct either because you can't use expressions such as 1/5 in AMPL data. See also http://ampl.com/faqs/why-does-set-s-50-70-give-me-a-set-of-only-3-members/ 

HTH,
Victor

--
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,
Aug 18, 2016, 3:55:37 PM8/18/16
to AMPL Modeling Language, 4...@ampl.com

Dr. Robert Fourer; 

Thanks for your reply. Based on your suggestions I modified the code. Unfortunately the program still giving me errors. Probably my data input is not in right format. 

The following is a short description of my problem.

Suppose I have 3 indexes and each index has different levels. First index has 4 levels, Second index  has 3 levels, and Third index has 2 levels.

P1 = 1 2 3; For index p1 =1 , level l1=1 2 3 4; for p1 = 2 , level  l2=1 2 3; for p1 =3 then l3=1 2

My variables are combinations of indexes with their levels. For example in this case I have 9 variables, u11,u12,u13,u14,u21,u22,u23,u31,u32.


weight*u11-u21=t1121; weight*u11-u31=t1131;  …………(pairwise comparison of all levels of index 1 with all levels of index 2 and 3) {#constraint4}

weight*u21-u31=t2131; …………. (pairwise comparison of all levels of index 2 with all levels of index3) {#constraint4}

 


Weight is the 4-dimensional parameter for combination of all indexes and levels. In this case I will have 26 weight’s. 

I have added some parts of  .mod, .dat, .run file and output log. Could you please take a look and let me know where the problem is?

#.mod 

param p1 := 6;

set l1 {1..p1} ordered;

param p2 :=6;

set l2 {1..p2} ordered;

 param sigma :=3;

param w{i in 1..p1, l1[i], j in 1..p2, l2[j]}; 

var u{i in 1..p1, l1[i]}; 

var t {i in 1..p1, l1[i], j in 1..p2, l2[j]};

minimize obj: ……..+ sum{i in 1..p1, a in l1[i], j in 2..p2, b in l2[j] :j>i} t[i,a,j,b]^2; 

subject to constraint4{i in 1..p1, a in l1[i], j in 2..p2, b in l2[j] :j>i}: w[i,a,j,b]*u[i,a] - u[j,b]= t[i,a,j,b];

 #.dat 

data;

set l1[1]=1 2 3 4 5;

set l1[2]=1 2 3 4 5;

set l1[3]=1 2 3 4 5;

set l1[4]=1 2;

set l1[5]=1 2;

set l1[6]=1 2;

set l2[1]=1 2 3 4 5;

set l2[2]=1 2 3 4 5;

set l2[3]=1 2 3 4 5;

set l2[4]=1 2;

set l2[5]=1 2;

set l2[6]=1 2;

 

 

param w :=

[:,*,*,*]: 1 2 3 4 5 :=

1          1          2          1          1          0.2        0.142857143      0.111111111

.

.

.

.

1          1          6          1          1 (line 50 of .dat file)

1          2          6          1          1(line 51 of .dat file)

.

.

1          5          6          9          1 (line 54 of .dat file)

.

.

2          3          3          .           .           .           .           . (line 57 of .dat file)

 

.

.

5          2          6          5          1;

The red colors are the values of parameter w for different levels of 3rd column index. Fro example:

1 1 2 1 1 0.2 0.142857143 0.111111 shows the w value’s for level 1,2,3,4,5 of index 2 with compare to first level of first index are 1 1 0.2 0.142857143 0.111111   

 I used dot for those cases where I don’t have w’values.

#.run

reset;

model inputdata.mod;

data inputdata1.dat;

option solver couenne;

solve;

display {i in 1..p1} u1[i,l1[i]] > inputdata.out;

 

 w[3,1,1,4] is showing in output file. But it can never be like this. The third indices should be greater than 1st indices. It has to be w[3,1,4,1], which is the value w for 1st level of 3rd index compare to 1st level of 4th index. 



Again thank you so much for your help. 

error.PNG

Victor Zverovich

unread,
Aug 22, 2016, 6:23:51 PM8/22/16
to am...@googlegroups.com
The problem is that you have inconsistent number of elements in the rows of your data table. Each row should consist of 8 elements: 3 keys and 5 data values while some rows have 5 elements:

  1          1          6          1          1

and some just one (.).

Please see Chapter 9. Specifying Data for information on the AMPL data format.

HTH,
Victor

Message has been deleted

Robert Fourer

unread,
Aug 23, 2016, 1:21:56 AM8/23/16
to am...@googlegroups.com
You were correct to use . when there are no data values. Given the l2 data that you have, if the 3rd entry in the row is 1, 2, or 3, there should be 5 more numerical entries in the row; if the 3rd entry in the row is 4, 5, or 6 then there should be 2 more numerical entries and 3 . entries. In every row there should be 8 entries of some kind.

When you try using . in this way, what error do you get? The most likely cause of an error is that your data does not correspond properly to the indexing {i in 1..q1, a in l1[i], j in 2..p2, b in l2[j]} defined for the param w.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Rupsana Parveen
Sent: Monday, August 22, 2016 11:55 PM
To: AMPL Modeling Language
Subject: Re: [AMPL 12514] high dimensional parameters with different levels

Hi Victor,
Thanks for the reply. Actually I don’t have 5 data values for each 3rd key and in some cases no data values for 3rd key. For example :
1 1 4 . . . . .
The above row has no data values for 3rd key. If I use the above line then I get the following errors:
“Error at _cmdno 2 executing "solve" command
(file inputdata.run, line 5, offset 76):
error processing constraint constraint4[1,1,4,1]:
no value for w[1,1,4,1]”
But When I use the following format
1 1 4 0 0 0 0 0
Then I am getting results. In my case it has no values, but that doesn’t mean that it is 0. Actually it is not zero.
Similar for
1 1 6 1 1 0 0 0
Which means 3rd key 6 has two levels and has two data values which are 1, 1.
Could you please tell me how to handle this type of input when I don’t have the data values for all 3rd key?
Section 9.2 of AMPL book used dot(.) for no data values. But it didn’t work for me.
I have attached .mod file and .dat file.


Rupsana Parveen

unread,
Aug 24, 2016, 10:36:58 PM8/24/16
to AMPL Modeling Language, 4...@ampl.com
I figured out the error. It happened because I missed 1 numerical entry.

Thanks Dr. Fourer for your help. You made it clear to me.
Reply all
Reply to author
Forward
0 new messages