Continued Equality for Condition Topic is solved

Problems with syntax of GAMS
GabrielYin
User Posts: 54
Joined: 1 year ago
Location: Dallas, TX, USA

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:

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 Posts: 54
Joined: 1 year ago
Location: Dallas, TX, USA

Re: Continued Equality for Condition

Sorry but I found the solution myself 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

bussieck
Moderator Posts: 344
Joined: 3 years ago

Re: Continued Equality for Condition

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 Posts: 54
Joined: 1 year ago
Location: Dallas, TX, USA

Re: Continued Equality for Condition

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

Gabriel