Define dynamic set using embedded code facility

Hello ,

I am new to gams and I am trying to define a dynamic set in a “for statement”. In fact, I want to solve my model using a different set each time and I am using the embedded code facility for python to define in each iteration my new dynamic set.

In my model I have declared my parameters and equations using the main set and then the definition using the subset.

Below is my code and this is the error I am getting, I don’t know what I am missing and if this is the right way to do it

Error:

Assignment to set used in ‘ord’ or lag
this statement changes the content of a set previously used with ‘ord’ or lag/lead

Code:

Sets i products / i0*i50 /
     j suppliers / j0*j50  /
     t time periods / t0*t50 /
     sub_j(j)   dynamic subset for j
     sub_t(t)   dynamic subset for t
     sub_i(i)   dynamic subset for i ;
     
for ( product = 1 to 5 by 2,
    for( supplier = 1 to 5 by 2,
       for ( time = 1 to 5 by 2,

           embeddedCode Python:

            product = list(gams.get("product"))
            supplier = list(gams.get("supplier"))
            time = list(gams.get("time"))

            sub_i = list(gams.get("sub_i"))
            sub_j = list(gams.get("sub_j"))
            sub_t = list(gams.get("sub_t"))

            "gams.set("sub_i",["i" + str(x) for x in list(range(0, int(product)))])"
            "gams.set("sub_j",["j" + str(x) for x in list(range(0, int(supplier)))])"
            "gams.set("sub_t",["t" + str(x) for x in list(range(0, int(time)))])"

          endEmbeddedCode sub_i  sub_j  sub_t

          Solve model minimizing cost using mip ;

       );
    );
);

Any help would be appreciated
Thanks !

Hi,

I am not quite sure whether this is really what you mean to do, but I guess using embeddedCode in this situation is a bit of an overkill. Here is some GAMS code that does what I think you want to achieve:

Sets i products / i0*i50 /
     j suppliers / j0*j50  /
     t time periods / t0*t50 /
     sub_j(j)   dynamic subset for j
     sub_t(t)   dynamic subset for t
     sub_i(i)   dynamic subset for i ;
  
for ( product = 1 to 5 by 2,
    for( supplier = 1 to 5 by 2,
       for ( time = 1 to 5 by 2,
           sub_i(i) = ord(i) <= product;
           sub_j(j) = ord(j) <= supplier;
           sub_t(t) = ord(t) <= time;

          Solve model minimizing cost using mip ;

       );
    );
);

Greetings,
Freddy

Thanks for your quick response
I included what you mentioned and I tested it and it is working pretty fine but in a way simpler than my code with the embedded facility code, however, I am still getting the same error with the dynamic set assignment for the objective function and the first constraint:

Error:

Assigning to set used in 'ord' or lag
       this statement changes the content of a set
       previously used with 'ord' or lag/lead.

This is my complete code:

Scalar product ;
Scalar supplier ;
Scalar time ;

Sets i products / i0*i50 /
     j suppliers / j0*j50  /
     t time periods / t0*t50 /
     sub_j(j)   dynamic subset for j
     sub_t(t)   dynamic subset for t
     sub_i(i)   dynamic subset for i ;

Alias ( k , t ) ;
Parameter D( i , t )  ;
Parameter P( i , j ) ;
Parameter H( i )  ;
Parameter O( j )  ;


Variables x ( i , j , t )
y ( j , t )
R( i , t )
Z( i , j , t )
cost ;
Positive Variables x ,R, Z;
Binary Variable y ;
Equations obj objective function
          con1 ( i , t ) ;

obj .. cost =E= sum ( ( sub_i , sub_j , sub_t ) ,P( sub_i , sub_j )* x ( sub_i , sub_j , sub_t ))+sum ( ( sub_j , sub_t ) ,O( sub_j )* y ( sub_j , sub_t ) )
+sum ( ( sub_i , sub_t ) ,H( sub_i ) * ( sum ( k$ ( ord ( sub_t ) GE ord ( k ) ) , sum ( sub_j , x ( sub_i , sub_j , k))-D( sub_i , k ) ) ) ) ;

con1 ( sub_i , sub_t ) .. R( sub_i , sub_t ) =E= sum ( k$ ( ord ( sub_t ) GE ord ( k ) ) , sum ( sub_j , x ( sub_i , sub_j , k))-D( sub_i , k ) ) ;


display "---------------------formulation ----------------------";

option optcr =0;
option limrow =0;
option limcol =0;

Model Regular /all/;

for ( product = 5 to 15 by 5,
    for( supplier = 5 to 15 by 5,
       for ( time = 5 to 15 by 5,

           sub_i(i) = ord(i) <= product;
           sub_j(j) = ord(j) <= supplier;
           sub_t(t) = ord(t) <= time;

            Loop ((sub_i,sub_j), D(sub_i,sub_t) = uniformint(10,20));
            Loop (sub_i, P(sub_i,sub_j) = uniformint(10,20));
            Loop (sub_i, H(sub_i) = uniformint(1,4))    ;
            Loop (sub_j, O(sub_j) = uniformint(10,20))    ;

          Solve Regular minimizing cost using mip ;

       );
    );
);

Thanks for your help !

So the problem here is that you are using the ord operator on the dynamic set ‘sub_t’. Please refer to: https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_OrdAndCard for more information about how to use the ord operator. I have changed your code, so it executes properly now. If this is not what you wanted to achieve or you have any further questions, please feel free to ask.

Scalar product ;
Scalar supplier ;
Scalar time ;

Sets i products / i0*i50 /
     j suppliers / j0*j50  /
     t time periods / t0*t50 /
     sub_j(j)   dynamic subset for j
     sub_t(t)   dynamic subset for t
     sub_i(i)   dynamic subset for i ;

Alias ( k , t ) ;
Parameter D( i , t )  ;
Parameter P( i , j ) ;
Parameter H( i )  ;
Parameter O( j )  ;


Variables x ( i , j , t )
y ( j , t )
R( i , t )
Z( i , j , t )
cost ;
Positive Variables x ,R, Z;
Binary Variable y ;
Equations obj objective function
          con1 ( i , t ) ;

obj ..
   cost =E= sum ( ( sub_i(i) , sub_j(j) , sub_t(t) ) ,P( i , j )* x ( i , j , t ))+sum ( ( sub_j(j) , sub_t(t) ) ,O( j )* y ( j , t ) )
            + sum ( ( sub_i(i) , sub_t(t) ) ,H( i ) * ( sum ( k$ ( ord ( t ) GE ord ( k ) ) , sum ( sub_j(j) , x ( i , j , k))-D( i , k ) ) ) ) ;

con1 ( sub_i(i) , sub_t(t) ) ..
   R( i , t ) =E= sum ( k$ ( ord ( t ) GE ord ( k ) ) , sum ( sub_j(j) , x ( i , j , k))-D( i , k ) ) ;


display "---------------------formulation ----------------------";

option optcr =0;
option limrow =0;
option limcol =0;

Model Regular /all/;

for ( product = 5 to 15 by 5,
    for( supplier = 5 to 15 by 5,
       for ( time = 5 to 15 by 5,

           sub_i(i) = ord(i) <= product;
           sub_j(j) = ord(j) <= supplier;
           sub_t(t) = ord(t) <= time;

           D(sub_i,sub_t) = uniformint(10,20);
           P(sub_i,sub_j) = uniformint(10,20);
           H(sub_i) = uniformint(1,4);
           O(sub_j) = uniformint(10,20);

          Solve Regular minimizing cost using mip ;

       );
    );
);

Thank you so much, that worked :smiley: