Page 1 of 1

Continued Equality for Condition

Posted: Tue Sep 25, 2018 4:55 pm
by GabrielYin
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:

Code: Select all

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

Re: Continued Equality for Condition

Posted: Tue Sep 25, 2018 5:02 pm
by GabrielYin
Sorry but I found the solution myself :D And I apologize for the error in the post that I use 'n' twice for index.

I share my solution with you.

Code: Select all

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

Re: Continued Equality for Condition

Posted: Wed Sep 26, 2018 5:46 am
by bussieck
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.

Code: Select all

loop(i,
         ...
         break$(sum(n, abs(a('1') - a(n)) < 1e-6);
);
-Michael

Re: Continued Equality for Condition

Posted: Wed Sep 26, 2018 4:39 pm
by GabrielYin
Thanks for your kind reply! That should be the best solution!

Gabriel