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;
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:
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.
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;
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)