How to loop the matrix

Dear All.
About a few days ago, I asked a similar question.
I used to ask the scalar loop(Thanks Mr. Michael solve this problem), now I have problems when it becomes a matrix.
This is a scalar solution.
Untitled_8.gms (5.16 KB)
The key code is this paragraph

scalar minc1 /inf/, bestns1 /1/,n1;

while(bestns1 < 10,
bestns1=bestns1+1;
ns1.fx=bestns1;


option optcr=0.0001;
  solve exchanger1 using minlp minimizing c1;
* Optimal or feasible solution
  if ((exchanger1.modelstat=1 or exchanger1.modelstat=8)and(c1.l<minc1),
    minc1 = c1.l;
    n1=bestns1;
  );
);

display minc1,n1;

Now I want to ask how to make a code when it comes to solving seven kinds of data at the same time.(7 kinds of data brought to the above file can have 7 answers)
Untitled_9.gms (3.66 KB)
Would not describe the part of the code. (I don’t know how to turn “” ns1.fx(heatx)“” into scalar)
Meaning is probably like this.

parameters
minc1(heatx) /ex1*ex7 inf/
bestns1(heatx)/ex1*ex7 1/
;
bestns1(heatx)$(r(heatx) ne 1)=1;

parameter n1,bestp121,bestft1,besta1;

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

option decimals = 4;
display minc1,n1,bestp121,bestft1,besta1;

Hi

You have to be clear about your stopping condition: does the while loop have to stop as soon as one of the bestns1(heatx) is above 10? Or if all bestns1 are equal to 9?

Let us assume it is the first stopping criterion, then you probably would change the code like this:

while(smax(heatx, bestns1(heatx)) < 10,

(smax gives you the maximum of a vector).

If it is the last option:

while(smin(heatx, bestns1(heatx)) < 10,

In both cases, I assume that bestns1 will reach 10 finally for all heatx (otherwise your model will run forever).
If this could happen, you have to add another while condition to stop the iterations (perhaps the sum over all bestns1 doesn’t change anymore:

parameter 
sumbestns  /1/, 
submbestnsprevious /0/;

while(sumbestns ne submbestnsprevious, 
   submbestnsprevious = sumbestns;
solve...
  sumbestns = sum(heatx, bestns(heatx));
  ...
  );

I hope this answers your question.

Cheers
Renger

My problem is the first one, let it stop the loop before 10, and find the best answer among them.
Untitled_8.gms (5.23 KB)
I use this code. but the model is run forever.

parameters
minc1
bestns1
mc1
cc1
;
bestns1(heatx)$(r(heatx) ne 1)=1;
minc1(heatx)$(r(heatx) ne 1)=inf;
mc1=sum(heatx,minc1(heatx));
cc1=sum(heatx,c1.l(heatx));

parameter n1,bestp121,bestft1,besta1;

while((smax(heatx, bestns1(heatx)) < 10),
ns1.fx(heatx)=bestns1(heatx)+1;

solve exchanger1 using minlp minimizing z1;
if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (cc1 < mc1),
minc1(heatx)  = c1.l(heatx);
n1(heatx)=bestns1(heatx);
bestp121(heatx)=p121.l(heatx);
bestft1(heatx)=ft1.l(heatx);
besta1(heatx)=a1.l(heatx);
  );
);

option decimals = 4;
display minc1,n1,bestp121,bestft1,besta1;

****Sorry, Renger. this code, I don’t know which side to enter.

parameter 
sumbestns  /1/, 
submbestnsprevious /0/;

while(sumbestns ne submbestnsprevious, 
   submbestnsprevious = sumbestns;
solve...
  sumbestns = sum(heatx, bestns(heatx));
  ...
  );

Thank you.

Hi
You should send me a model version that actually solves, or has no solution, but doesn’t stop because of having negative numbers in the log function by initializing the model properly.
Or send the model that runs indefinitely
Cheers
Renger

Renger, Thank you again.
I am missing a small piece of code and it can converge.

****If I want to find the best value in these loop ranges, how should I modify my IF sentence? Because it is showing the answer is the maximum value of the loop 10.

the full file.
Untitled_8.gms (5.12 KB)
This is sets code

parameters
minc1
bestns1
sumz1
sumc1
;
bestns1(heatx)$(r(heatx) ne 1)=1;
minc1(heatx)$(r(heatx) ne 1)=inf;
summz1=sum(heatx,minc1(heatx));
sumc1=sum(heatx,c1.l(heatx));

parameter n1;

while((smax(heatx, bestns1(heatx)) < 10),
bestns1(heatx)$(r(heatx) ne 1)=bestns1(heatx)+1;
ns1.fx(heatx)=bestns1(heatx);

solve exchanger1 using minlp minimizing z1;

if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (sumc1 < sumz1),
minc1(heatx)  = c1.l(heatx);
n1(heatx)=bestns1(heatx);

  );
);

display minc1,n1;



---- 154 PARAMETER minc1

ex1 112371.675, ex3 260081.845, ex4 333833.750, ex5 291600.978
ex6 275433.284, ex7 412156.453


---- 154 PARAMETER n1

ex1 10.000, ex3 10.000, ex4 10.000, ex5 10.000, ex6 10.000
ex7 10.000

this is scalar code ( ****but it can find out the best value from 1 to 10 in the loop)
Untitled_9.gms (3.67 KB)

scalar minc1 /inf/, bestns1 /1/,n1;

while(bestns1 < 10,
bestns1=bestns1+1;
ns1.fx=bestns1;

  solve exchanger1 using minlp minimizing c1;
* Optimal or feasible solution
  if ((exchanger1.modelstat=1 or exchanger1.modelstat=8)and(c1.l<minc1),
    minc1 = c1.l;
    n1=bestns1;
  );
);

display minc1,n1;



132 PARAMETER minc1 = 109064.251
PARAMETER n1 = 6.000

Hi
As you don’t loop over a set, it is difficult to write a parameter with all the results directly.
I would do it like this:

set loopnr Loop number /1*100/;;

parameter loopcount  /1/;

parameter results(loopnr, *,*);

while(...
	solve model
	loopcount = loopcount + 1;
	results(loopnr, "Mincl")$(loopnr.val = loopcount) = cl.l;
	results(loopnr, "nsl")$(loopnr.val = loopcount) = bestns1;
	...

This would give you all the results in a nice parameter and you can look for the best solution.

Hope this helps
Cheers
Renger

I am very sorry, my degree is not good.

****Is it convenient to enter the complete code?

Thank you very much for guiding me very patiently, Renger.
Untitled_8.gms (5.13 KB)

parameters
minc1
bestns1
sumz1
sumc1
;
bestns1(heatx)$(r(heatx) ne 1)=1;
minc1(heatx)$(r(heatx) ne 1)=inf;
sumz1=sum(heatx,minc1(heatx));
sumc1=sum(heatx,c1.l(heatx));

parameter n1;

while((smax(heatx, bestns1(heatx)) < 10),
bestns1(heatx)$(r(heatx) ne 1)=bestns1(heatx)+1;
ns1.fx(heatx)=bestns1(heatx);

solve exchanger1 using minlp minimizing z1;

if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (sumc1 < sumz1),
minc1(heatx)  = c1.l(heatx);
n1(heatx)=bestns1(heatx);
  );
);

display minc1,n1;
set loopnr Loop number /1*100/;;

parameter loopcount  /1/;

parameter results(loopnr, *,*);

while((smax(heatx, bestns1(heatx)) < 10),
bestns1(heatx)$(r(heatx) ne 1)=bestns1(heatx)+1;
ns1.fx(heatx)=bestns1(heatx);

solve exchanger1 using minlp minimizing z1;
    loopcount = loopcount + 1;
    results(loopnr, "Mincl")$(loopnr.val = loopcount) = cl.l;
    results(loopnr, "nsl")$(loopnr.val = loopcount) = bestns1;

if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (sumc1 < sumz1),
minc1(heatx)  = c1.l(heatx);
n1(heatx)=bestns1(heatx);
  );
);

display minc1,n1, results;

Sorry, It says Different dimensions.
The following two codes:


results(loopnr, “Mincl”)(loopnr.val = loopcount) = c1.l; results(loopnr, "nsl")(loopnr.val = loopcount) = bestns1;




set loopnr Loop number /1*100/;;

parameter loopcount  /1/;

parameter results(loopnr, *,*);

parameters
minc1
bestns1
sumz1
sumc1
;
bestns1(heatx)$(r(heatx) ne 1)=1;
minc1(heatx)$(r(heatx) ne 1)=inf;
sumz1=sum(heatx,minc1(heatx));
sumc1=sum(heatx,c1.l(heatx));

parameter n1;

while((smax(heatx, bestns1(heatx)) < 10),
bestns1(heatx)$(r(heatx) ne 1)=bestns1(heatx)+1;
ns1.fx(heatx)=bestns1(heatx);

solve exchanger1 using minlp minimizing z1;
    loopcount = loopcount + 1;
    results(loopnr, "Mincl")$(loopnr.val = loopcount) = c1.l;
    results(loopnr, "nsl")$(loopnr.val = loopcount) = bestns1;

if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (sumc1 < sumz1),
minc1(heatx)  = c1.l(heatx);
n1(heatx)=bestns1(heatx);
  );
);

display minc1,n1, results;

Sorry, it should have been results(loopnr, *) as definition.
R

****Sorry, Still Different dimensions at two codes. Are c1.l and bestns1 problem? I don’t know. :cry:


results(loopnr, “Mincl”)(loopnr.val = loopcount) = c1.l; results(loopnr, "nsl")(loopnr.val = loopcount) = bestns1;


Untitled_7.gms (5.39 KB)

set loopnr Loop number /1*100/;

parameter loopcount  /1/;

parameter results(loopnr, *);

parameters
minc1
bestns1
sumz1
sumc1
;
bestns1(heatx)$(r(heatx) ne 1)=1;
minc1(heatx)$(r(heatx) ne 1)=inf;
sumz1=sum(heatx,minc1(heatx));
sumc1=sum(heatx,c1.l(heatx));

parameter n1;


while((smax(heatx, bestns1(heatx)) < 10),
bestns1(heatx)$(r(heatx) ne 1)=bestns1(heatx)+1;
ns1.fx(heatx)=bestns1(heatx);

solve exchanger1 using minlp minimizing z1;
    loopcount = loopcount + 1;
    results(loopnr, "Mincl")$(loopnr.val = loopcount) = cl.l;
    results(loopnr, "nsl")$(loopnr.val = loopcount) = bestns1;

if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (sumc1 < sumz1),
minc1(heatx)  = c1.l(heatx);
n1(heatx)=bestns1(heatx);
  );
);

display minc1,n1, results;

Here you go:

parameter results(loopnr, *,*);

parameters
minc1
bestns1
sumz1
sumc1
;
bestns1(heatx)$(r(heatx) ne 1)=1;
minc1(heatx)$(r(heatx) ne 1)=inf;
sumz1=sum(heatx,minc1(heatx));
sumc1=sum(heatx,c1.l(heatx));

parameter n1;


while((smax(heatx, bestns1(heatx)) < 10),
bestns1(heatx)$(r(heatx) ne 1)=bestns1(heatx)+1;
ns1.fx(heatx)=bestns1(heatx);

solve exchanger1 using minlp minimizing z1;
    loopcount = loopcount + 1;
    results(loopnr, "Mincl",heatx)$(loopnr.val = loopcount) = c1.l(heatx);
    results(loopnr, "nsl",heatx)$(loopnr.val = loopcount) = bestns1(heatx);

if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (sumc1 < sumz1),
minc1(heatx)  = c1.l(heatx);
n1(heatx)=bestns1(heatx);
  );
);

display minc1,n1, results;

Sorry, Renger. Bad things.
Still Different dimensions at two codes. :cry:


results(loopnr, “minc1”,heatx)(loopnr.val = loopcount) = c1.l(heatx); results(loopnr, "nsl",heatx)(loopnr.val = loopcount) = bestns1(heatx);


Untitled_7.gms (5.41 KB)

set loopnr Loop number /1*100/;

parameter loopcount  /1/;

parameter results(loopnr, *);

parameters
minc1
bestns1
sumz1
sumc1
;
bestns1(heatx)$(r(heatx) ne 1)=1;
minc1(heatx)$(r(heatx) ne 1)=inf;
sumz1=sum(heatx,minc1(heatx));
sumc1=sum(heatx,c1.l(heatx));

parameter n1;


while((smax(heatx, bestns1(heatx)) < 10),
bestns1(heatx)$(r(heatx) ne 1)=bestns1(heatx)+1;
ns1.fx(heatx)=bestns1(heatx);

solve exchanger1 using minlp minimizing z1;
    loopcount = loopcount + 1;
    results(loopnr, "minc1",heatx)$(loopnr.val = loopcount) = c1.l(heatx);
    results(loopnr, "nsl",heatx)$(loopnr.val = loopcount) = bestns1(heatx);

if ((exchanger1.modelstat=1 or exchanger1.modelstat=8) and (sumc1 < sumz1),
minc1(heatx)  = c1.l(heatx);
n1(heatx)=bestns1(heatx);
  );
);

display minc1,n1, results;

Compare your code and my suggestion carefully, and you will find the mistake in your code.
Cheers
Renger

Thank you very much, Renger.

There is a problem now, the result it displays will follow the EX1display(only ns1 to ns4 of ex1 is infeasible) , and the subsequent EX3 to EX7 will not be displayed.

ns1 to ns4 from ex3 to ex7 do not display minc1. (ex1 to ex7 are 7 different data that do not interfere with each other)

the file:
Untitled_3.gms (3.79 KB)

----    121 PARAMETER results  

                 ex1         ex3         ex4         ex5         ex6         ex7

1 .ns1         1.000       1.000       1.000       1.000       1.000       1.000
2 .ns1         2.000       2.000       2.000       2.000       2.000       2.000
3 .ns1         3.000       3.000       3.000       3.000       3.000       3.000
4 .ns1         4.000       4.000       4.000       4.000       4.000       4.000
5 .minc1  110374.997  204393.067  263294.489  229423.575  217114.173  414231.210
5 .ns1         5.000       5.000       5.000       5.000       5.000       5.000
6 .minc1  109064.251  217714.346  280041.543  244262.139  230976.843  413380.078
6 .ns1         6.000       6.000       6.000       6.000       6.000       6.000
7 .minc1  109312.056  229690.175  295186.282  257626.515  243501.099  412870.786
7 .ns1         7.000       7.000       7.000       7.000       7.000       7.000
8 .minc1  110142.506  240616.567  309053.233  269833.378  254962.138  412541.793
8 .ns1         8.000       8.000       8.000       8.000       8.000       8.000
9 .minc1  111210.801  250697.943  321877.534  281104.421  265557.550  412316.940
9 .ns1         9.000       9.000       9.000       9.000       9.000       9.000
10.minc1  112371.675  260081.845  333833.750  291600.978  275433.284  412156.453
10.ns1        10.000      10.000      10.000      10.000      10.000      10.000

code show as below

sets
loopnr Loop number /1*100/
;
parameter loopcount  /0/;

parameters
results(loopnr, *,heatx);

scalar bestns1 /0/;

while(bestns1 < 10 ,
bestns1=bestns1+1;
ns1.fx(heatx)=bestns1;

solve exchanger1 using minlp minimizing z1;
    loopcount = loopcount + 1;
    results(loopnr,"minc1",heatx)$(loopnr.val = loopcount)= c1.l(heatx);
    results(loopnr,"ns1",heatx)$[(loopnr.val = loopcount) and (r(heatx) ne 1)]= bestns1;
);

display results;

If use scalar solves ex4, the following results will be displayed.
the scalar file:
Untitled_4.gms (2.5 KB)

----     97 PARAMETER results  

         Mincl         nsl

1                    1.000
2   199050.374       2.000
3   223062.862       3.000
4   244493.148       4.000
5   263294.489       5.000
6   280041.543       6.000
7   295186.282       7.000
8   309053.233       8.000
9   321877.534       9.000
10  333833.750      10.000