Page 1 of 1

Problem with loop

Posted: Tue Aug 03, 2021 12:10 pm
by Rofice
Dear all,

I am trying to perform few calculations using loop. However, the formulated syntax is not able to store the last value of parameter b(z) (722) to set u2(zz). Please see the attached model for more details. I will really appreciate it if someone helps me with correcting the codes.
syntax.gms
(459 Bytes) Downloaded 224 times
Best regards,
Rofice

Re: Problem with loop

Posted: Sat Aug 14, 2021 12:31 pm
by GFA
Hi,

B(z+1) is declared in the same loop as u2(zz), so in the last loop iteration B(z+1) will have a value (772) but u2+1 won't.
One way to solve this is to declare u2(zz+1) in the same loop, but keep in mind that now you will miss the first value of u2(zz), but just like the first b-value you can also initialize the first u2 value with the same value as b.

Cheers,
GFA

Code: Select all


 set z  hours in a year / 1*8760 /

 alias (z,zz);

 scalar init stopping criteria ; init = 0;

 parameter

 a(z) number of days in january ,

 b(z) add interval of 24 hours ;

 set u2(zz) feasible elements should equal to b(z) ;

 init = 0;  a('1') = 1 ; b('1') = 2  ;
 u2("2") = yes;

 loop(z $ (init ne 1) ,

 a(z+1) = a(z) + 1;

 b(z+1) = b(z) + 24;

 u2(zz+1) $ (ord(zz)+1=b(z+1))  = yes ;

 init $ {a(z+1) eq 31} = 1;

 ) ;


 display a,b,u2;



Re: Problem with loop

Posted: Wed Aug 18, 2021 5:17 pm
by Rofice
Thanks GFA!

Best regards,
Rofice