## How to loop the matrix

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

### Re: How to loop the matrix

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

************************************************
results(loopnr, "Mincl")\$(loopnr.val = loopcount) = c1.l;
results(loopnr, "nsl")\$(loopnr.val = loopcount) = bestns1;
************************************************
Untitled_7.gms
(5.39 KiB) Downloaded 9 times

Code: Select all

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

Renger
Posts: 266
Joined: 2 years ago

### Re: How to loop the matrix

Here you go:

Code: Select all

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

LiangYu
User
Posts: 13
Joined: 3 months ago

### Re: How to loop the matrix

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

************************************************
results(loopnr, "minc1",heatx)\$(loopnr.val = loopcount) = c1.l(heatx);
results(loopnr, "nsl",heatx)\$(loopnr.val = loopcount) = bestns1(heatx);
************************************************
Untitled_7.gms
(5.41 KiB) Downloaded 6 times

Code: Select all

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

Renger
Posts: 266
Joined: 2 years ago

### Re: How to loop the matrix

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

LiangYu
User
Posts: 13
Joined: 3 months ago

### Re: How to loop the matrix

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 KiB) Downloaded 6 times

Code: Select all

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

Code: Select all

``````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 KiB) Downloaded 7 times

Code: Select all

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