Continued Equality for Condition Topic is solved

Problems with syntax of GAMS
Post Reply
GabrielYin
User
User
Posts: 72
Joined: 6 years ago
Location: Dallas, TX, USA
Contact:

Continued Equality for Condition

Post 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
GabrielYin
User
User
Posts: 72
Joined: 6 years ago
Location: Dallas, TX, USA
Contact:

Re: Continued Equality for Condition

Post 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
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Continued Equality for Condition

Post 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
GabrielYin
User
User
Posts: 72
Joined: 6 years ago
Location: Dallas, TX, USA
Contact:

Re: Continued Equality for Condition

Post by GabrielYin »

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

Gabriel
Post Reply