Dear all,
I am a beginner to gams; I am not a member of the English-speaking country. The grammar is not used well.
Currently encountering questions about the loop.
The key syntax is as follows, but still need to see the full file. Untitled_1.gms (3.32 KB)

repeat(
ns1.fx=ns1.l+1;
solve exchanger1 using minlp minimizing c1;
until ft1.l >= 0.8
);

ns is integer variable. c1 and ft are free variable.

I want to use ns to gradually +1 to find out that ft>0.8. Get the minimum c1.
How can I express this code?
Currently, know the correct answer ns1 is 6 can find the minimum c1.
I am using Baron silver.

Not sure I understand. Especially the logic of the repeat loop:

you set bounds on ft1: ft1.lo = 0.8; ft1.up = 1;

you terminate the repeat if ft1.l>=0.8

If the solve in the loop body is optimal/feasible the stopping criterion is immediately met and the loop stops. I understand that you struggle with GAMS (and English) and appreciate that you make a good effort to follow the rules of the forum. If I change the lower bound of ft1 to 0 (and set an initial point away from 0, e.g. ft1.l=0.5) then the first 3 iterations ns1=1…3 are infeasible and ns1=4 gives:

---- VAR ft1 . 0.7594 1.0000 .
---- VAR c1 -INF 117232.3952 +INF .
---- VAR ns1 4.0000 4.0000 4.0000 -14058.7633

with ns1=5 we get

---- VAR ft1 . 0.8599 1.0000 .
---- VAR c1 -INF 110374.9973 +INF .
---- VAR ns1 5.0000 5.0000 5.0000 -2842.6226

and the loop terminates because ft1>=0.8. If we solve with ns1=6 we get

---- VAR ft1 . 0.9066 1.0000 .
---- VAR c1 -INF 109064.2508 +INF .
---- VAR ns1 6.0000 6.0000 6.0000 -268.9820

c1 is indeed smaller, but if you just look for a solution with ft1>=0.8 and c minimal, why to do a loop at all? Just let BARON optimize. So without any fixation of ns1 (and ft1.lo back at 0.8) we get the optimal solution

---- VAR ft1 0.8000 0.9066 1.0000 .
---- VAR c1 -INF 109064.2507 +INF .
---- VAR ns1 1.0000 6.0000 100.0000 -268.9820

Thanks for your reply.
The original file repeat’s code I explain badly.
In fact, I have two solutions, one is to find the minimum value directly with optcr = 0, and the other is gradually +1 with ns.
At the moment, I want to try the second method, which means finding the minimum c1 that matches ft>=0.8.
In without knowing the answer.

If from ns=1 to ns=100, I want to it use ns=1,2,3,4…99,100.
Find the minimum c1 between these ns=1~100 data
Condition is ft>=0.8.

Thank you for your help!
I really want to talk privately, but the system doesn’t seem to allow it.

This method is used because I have a study that needs to use this optimization method, so use this very cumbersome but necessary to do method.

Sorry. I have two paragraphs of code that I don’t understand.
Or can you explain the entire code (the best)? (Some logics don’t understand)

1.----->>>scalar minc1 /inf/, bestns1 /-1/;
why use bestns1/-1/? And the /-1/ doesn’t understand.

2.---->>>if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (c1.l < minc1) and (ft1.l>=0.8),
What is the significance(exchanger1.modelstat=1 or exchanger1.modelstat=8) and (c1.l < minc1)?

Thank you again

scalar minc1 /inf/, bestns1 /-1/;
scalar ns1cnt;
ft1.lo=0;
ft1.up=1;
ft1.l = 0.5;
option optcr=0;
for (ns1cnt=1 to 100,
ns1.fx = ns1cnt;
solve exchanger1 using minlp minimizing c1;
* Optimal or feasible solution
if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (c1.l < minc1) and (ft1.l>=0.8),
minc1 = c1.l;
bestns1 = ns1cnt;
);
);
display minc1, bestns1;

The -1 is just for initialization. Bestns1 records the ns1 where the optimum has been found. The logical expression with modelstat ensures that the solver only updates the best found if we have a solution and as minc1 holds the best solution so far, we only want to update if the newly found solution is better. This is very standard logic.

Thank you, Michael.
I try to change your command slightly and use the while command.
Like this:

scalar minc1 /inf/;
scalar ns1cnt;
ft1.lo=0;
ft1.up=1;
ft1.l = 0.5;
while(ns1.l<10,
ns1.fx=ns1.l+1;
solve exchanger1 using minlp minimizing c1;
if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (c1.l < minc1) ,
minc1 = c1.l;
ns1cnt = ns1.l;
);
);
display ns1cnt,c1.l,minc1,ns1.l;

But I don’t know why (ns1cnt and ns1.l) or (minc1 and c1.l) are the difference.

My understanding is that all data in ns1=1 to 10 when the IF condition is reached, the best data will be displayed. (But this explanation is actually very abstract for me.)

Sorry, Can I ask another question further? I think this is the last question.
About turn scalar into a matrix.

This is the result of my direct optimization.(The first set of data answers are the same as the original) Untitled_6.gms (4.87 KB)
If I want to use the same way of the loop, how to change to the same result. (Dimension problem)
Means to use the same algorithm, want to calculate six kinds of data in parallel. Untitled_8.gms (5.54 KB)
I will not modify this part, sorry for this part of the concept is not good.