Search code examples
ampl

AMPL, selecting data from two sets


I have two sets which are:

set s1 := 0 1 5 6 ;
set s2 := 3 4 8 9 ;

I need to have a constraint which selects the data from these sets like the following line:

subject to sym1{i in 0..3 , j in 0..3 : i=j } : x[0,s1[i],0] = x[1,s1[j],0];

It means selecting data from s1 and s2 should be like: x[0,0,0] = x[1,3,0]; x[0,1,0] = x[1,4,0]; x[0,5,0] = x[1,8,0]; x[0,6,0] = x[1,9,0];

But the code I wrote has a syntax error. Would you please help me

Thanks


Solution

  • One way to do this is by declaring sets as ordered and using the member function to an element of a set by its index:

    set s1 ordered := {0, 1, 5, 6};
    set s2 ordered := {3, 4, 8, 9};
    
    subject to sym1{i in 1..4}: x[0, member(i, s1), 0] = x[1, member(i, s2), 0];
    

    Alternatively, you can replace sets with parameters:

    param s1{1..4};
    param s2{1..4};
    
    subject to sym1{i in 1..4}: x[0, s1[i], 0] = x[1, s2[i], 0];
    
    data;
    
    param s1 :=
     1 0
     2 1
     3 5
     4 6;
    
    param s2 :=
     1 3
     2 4
     3 8
     4 9;