Updating set elements using dynamic sets and loops Topic is solved

Problems with modeling
Post Reply
EconZ
User
User
Posts: 5
Joined: 2 years ago

Updating set elements using dynamic sets and loops

Post by EconZ »

Hi all,

I have a set kk in my model. I am trying to write a loop for kk and do some calculations for each elements of kk. For example, first kk=3 and I have equations for kk={1,2,3}. Then I need to have kk=4 and have the same equations for kk={1,2,3,4}. I need to increase kk=20 and run the model for kk={1,...,20}. I tried to use a dynamic set to update the set kk and then use the dynamic set in the loop, something like this:

Code: Select all

set kk  /1*20/ ; 
.
.
.
objective...
.
.
.
constraint1(kk,t)..  sp(t,t+ord(kk)) + slack(t,t+ord(kk),sow,f) =e=  acr1* yield;  
.
.
.

alias (kk,ka);
alias (kk,ka1);

$onempty
set  k(kk) // ;

loop(ka1, k(ka1) = yes ;
loop(ka$k(ka) , display k;
solve mymodel using mip maximizing objective;
.
.
.


));

But this does not what I want to do. It does not update the value of objective and variables in the model for different kks.
Does anyone have any idea how to fix it?

Thanks in advance,
Sirin
Fred
Posts: 373
Joined: 7 years ago

Re: Updating set elements using dynamic sets and loops

Post by Fred »

Sirin,

The following should do the trick.

Code: Select all

set kk  /1*20/
    k(kk); 
.
.
.
objective...
.
.
.
constraint1(k(kk),t)..  sp(t,t+ord(kk)) + slack(t,t+ord(kk),sow,f) =e=  acr1* yield;  


loop(ka1, k(ka1) = yes ;
  solve mymodel using mip maximizing objective;
.
.
.
);
The key is to defined the equation over the dynamic subset k(kk) instead of defining over the super set kk.
With every iteration of the loop, you then add one more element to set k.

I hope this helps!

Fred
Post Reply