## use loop about gradually plus one Topic is solved

Problems with syntax of GAMS
LiangYu
User
Posts: 13
Joined: 6 months ago

### use loop about gradually plus one

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
*******************************************
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.

Many thanks.

bussieck
Moderator
Posts: 337
Joined: 2 years ago

### Re: use loop about gradually plus one

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

1) you set bounds on ft1: ft1.lo = 0.8; ft1.up = 1;
2) 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:

Code: Select all

``````---- 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

Code: Select all

``````---- 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

Code: Select all

``````---- 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

Code: Select all

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

If you are interested in the true global optimum, you need to set optcr=0 (see https://www.gams.com/26/docs/UG_GamsCal ... AMSAOoptcr). Please find the modified model attached.

-Michael
Attachments
Untitled_1.gms

LiangYu
User
Posts: 13
Joined: 6 months ago

### Re: use loop about gradually plus one

Hi Michael,

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.

I don't know if this explanation is ok.

bussieck
Moderator
Posts: 337
Joined: 2 years ago

### Re: use loop about gradually plus one

I don't understand why you want to look for the optimum this way, but your could do:

Code: Select all

``````...
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;
``````
-Michael

LiangYu
User
Posts: 13
Joined: 6 months ago

### Re: use loop about gradually plus one

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

Code: Select all

``````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;
``````

bussieck
Moderator
Posts: 337
Joined: 2 years ago

### Re: use loop about gradually plus one

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.

-Michael

LiangYu
User
Posts: 13
Joined: 6 months ago

### Re: use loop about gradually plus one

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

Code: Select all

``````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.)

bussieck
Moderator
Posts: 337
Joined: 2 years ago

### Re: use loop about gradually plus one

You forgot the ft1.l>=0.8. -Michael

LiangYu
User
Posts: 13
Joined: 6 months ago

### Re: use loop about gradually plus one

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

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
I will not modify this part, sorry for this part of the concept is not good.
*************************************************
parameters
minc1(heatx) /ex1*ex7 inf/
minc2(heatx) /ex1*ex7 inf/
;
parameter bestns1(heatx),bestp121(heatx),bestft1(heatx),besta1(heatx);
parameter bestns2(heatx),bestp122(heatx),bestft2(heatx),besta2(heatx);
ft1.lo(heatx)=0;
ft1.up(heatx)=1;
ft1.l(heatx) = 0.5;

while(ns1.l(heatx)<10,
ns1.fx(heatx)=ns1.l(heatx)+1;
solve exchanger1 using minlp minimizing z1;
if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (c1.l(heatx) < minc1(heatx)) and (ft1.l(heatx)>=0.8),
minc1(heatx) = c1.l(heatx);
bestns1(heatx) = ns1.l(heatx);
bestp121(heatx)=p121.l(heatx);
bestft1(heatx)=ft1.l(heatx);
besta1(heatx)=a1.l(heatx);
);
);
*************************************************
Thank you again.