Continued Equality for Condition

Hi all,

Long time no see! I have one question about the implementation of continued equality for a break condition of my algorithm. I want to achieve the following pseudocode:

If: a(1) = a(2) = a(3) = … = a(n), then Break;
else: Continue;

In a GAMS code manner:

loop(n,
         ...
         break$(a(1)=a(2) and a(1)=a(3) and ... a(1)=a(n));
         continue;
);

Does anyone have idea for that? Thanks in advance!

Cheers!
Gabriel

Sorry but I found the solution myself :smiley: And I apologize for the error in the post that I use ‘n’ twice for index.

I share my solution with you.

loop(i,
         ...

         loop(n,
                 s(n) = a('1') - a(n);
         );

         if(sum(n, s(n)) = 0,
                 k = 1;
         );

         break$(k = 1);
         continue;
);

If you have better idea please share with me too! Thanks!

Gabriel

A few observations:

  • The sequence a = 2,1,3 will also result in sum(n,s(n))=0. You probably should use abs to calculate sn.
  • GAMS likes parallel assignment statements (no need to loop over n) and you can even shove this all into the break $-condition
  • Depending how a got calculated (e.g. by a solve) they numbers might not be truly equal, so instead for =0 you probably should check for close to 0.
loop(i,
         ...
         break$(sum(n, abs(a('1') - a(n)) < 1e-6);
);

-Michael

Thanks for your kind reply! That should be the best solution!

Gabriel