Save loop results including Solve

Problems with syntax of GAMS
Post Reply
mjavadm
User
User
Posts: 6
Joined: 3 years ago

Save loop results including Solve

Post by mjavadm »

Hello everyone. I'm writing a code in GAMS considering optimal power flow for a power plant. In this code, I import a set of data as a table with 24 rows and 100 columns as a parameter (sample), and then I need to solve the model and get the optimal solution for each column (it is called with j and shows 100 samples). The code is shown as follows, but when I run the project the output table (include the power of the power plant PVPP) has 100 columns with the same values. I have two questions: first, Is it correct to write the table directly code, not in the loop? Why the results are equal and Second, How I can save the result of the loop as a parameter? I would be so thankful to read your useful responses.

SETs
i Index for controllable units /1*6/
t Index for time periods /1*14/
j Number of samples /1*100/

VARIABLE
dtup(t,j) 'Upward Load'
dtdo(t,j) 'downward Load'
p(t,i) 'Active power of controllable unit i in time t'
PES(t) 'Output of energy storage'
PEStotal(t) 'Total output of energy for 80 battery storage'
E0 'initial storage capacity of energy storage elements'
C 'objective function'
D(t,j) 'load value after implementation of TOU pricing'
*CIL(t) 'cost of intruptable load'
PVPP(t,j) 'output of the VPP'
PG(t) 'total output of controllable unites'
RVPP(t,j) 'reserved capacity'
PVPPmax(t,j) 'Maximum active output'
FES 'cost of energy storage'
FEStotal 'total cost for 80 storag unites'
FG 'controllable unite generation cost'
FGtotal 'total cost for 6 microturbine'
In(j) 'income of VPP'
v(j) 'variance'
B(j) 'profit of VPP'

scalar
e 'operating cost of energy storage' /6.4/
Btup 'upper limit of upward demand' /0.08/
Btdo 'upper limit of downward demand' /0.07/
eup ' load elasticity coefficients which define the lower limit of load transfer' /0.04/
edo ' load elasticity coefficients which define the lower limit of load transfer' /0.03/
Pref 'traded tariff' /0.56/
r1 'coefficients of load' /0.05/
r2 'coefficients of load' /0.2/
r3 'coefficients of load' /0.15/
DT 'dispatch period(h)' /1/
Emin 'minimum storage capacity of energy storage devices(kw)' /0/
Emax 'maximum storage capacity of energy storage devices(kw)' /1.2/
Pcharmax 'maximum charge power of energy storage elements(kw)' /-15/
Pdischarmax 'maximum discharge power of energy storage elements(kw)' /15/
B0 'expected maximum profit based on the forecast prices' /33531.604/

parameters
sample(t,j) 'sample generated matrix'
dl(t) 'predict load value in time t'
/1 200
2 212
3 247.5
4 282.5
5 300
6 282.5
7 253
8 230
9 222
10 235.5
11 251
12 282.5
13 300
14 311/
Pr(t) 'prices of time t'
/ 1 0.3819
2 0.6171
3 0.6171
4 0.8789
5 0.8789
6 0.8789
7 0.6171
8 0.6171
9 0.6171
10 0.6171
11 0.6171
12 0.8789
13 0.8789
14 0.8789/
PWT(t)'predict WT output value in time t'
/
1 101
2 82
3 70
4 62.5
5 74
6 85.5
7 99
8 105
9 105
10 101
11 99.5
12 93.5
13 93.5
14 101/


report(t,j) 'collect data'

$call GDXXRW.exe GSample.xlsx trace=1 par=sample rng=sheet1!A1:o10001
$GDXIN GSample.gdx
$load sample
$gdxin

EQUATIONs
DD 'load value after implementation of TOU pricing'
LB 'load balance'
L1 'load adjustment cons1'
L2 'load adjustment cons2'
L3 'load adjustment cons3'
L4 'load adjustment cons4'
L5 'load adjustment cons5'
L6 'load adjustment cons6'
L7 'load adjustment cons7'
P1 'power balance cons 1'
P2 'power balance cons 2'
P3 'power balance cons 3'
P4 'power balance cons 4'
P5 'power balance cons 5'
P6 'power balance cons 6'
C1 'controllable unite const 1'
C2 'controllable unite const 2'
ES1 'energy storage const 1'
ES2 'energy storage const 2'
ES3 'energy storage const 3'
ES4 'energy storage const 4'
ES5 'energy storage const 5'
ES6 'energy storage const 6'
FES1 'cost of energy storage'
FES2
FG1 'controllable unite generation cost'
FG2
Cost 'objective function';

DD(t,j).. D(t,j)=e=dl(t)+dtup(t,j)-dtdo(t,j);
LB(j) .. sum(t,dtup(t,j))=e=sum(t,dtdo(t,j));
L1(t,j).. Btup*dl(t)=g=dtup(t,j);
L2(t,j).. Btdo*dl(t)=g=dtdo(t,j);
L3(t,j).. dtup(t,j)=g=0;
L4(t,j).. dtdo(t,j)=g=0;
L5(t,j).. dtup(t,j)=g=eup*dl(t)*(1-(Pr(t)/Pref));
L6(t,j).. dtdo(t,j)=g=edo*dl(t)*((Pr(t)/Pref)-1);
L7(t,j) .. dtup(t,j)*dtdo(t,j)=e=0;
ES1 .. sum(t,PES(t)*DT)=e=0;
ES2 .. Emin=l=(E0-sum(t,(PES(t))*DT));
ES3 .. E0-sum(t,(PES(t))*DT)=l=Emax;
ES4(t) .. Pcharmax=l=(PES(t));
ES5(t) .. (PES(t))=l=Pdischarmax;
ES6(t) .. PEStotal(t)=e=80*PES(t);
P1(t,j)..PVPP(t,j)=e=D(t,j);
P2(t,i) .. PG(t)=e=6*p(t,i);
P3(t,j) .. PVPP(t,j)=l=PG(t)+PWT(t)+sample(t,j)+PEStotal(t);
P4(t,j) .. RVPP(t,j)=e=(r1*D(t,j))+(r2*(PWT(t)))+(r3*sample(t,j));
P5(t,j) .. PVPPmax(t,j)=e=180+PWT(t)+sample(t,j)+PEStotal(t);
P6(t,j) .. PVPPmax(t,j)-(D(t,j))=g=RVPP(t,j);
C1(t,i) .. p(t,i)=g=0;
C2(t,i) .. p(t,i)=l=30;
FES1 .. FES=e=sum(t,0.5*e*abs((PES(t))));
FES2 .. FEStotal=e=80*FES;
FG1 .. FG=e=sum(t,sum(i,(0.0040787*p(t,i)*p(t,i))+1.475*p(t,i)+9.986));
FG2 .. FGtotal=e=6*FG;
Cost .. C=e=FEStotal+FGtotal;

model GaT4 /all/;

alias(j,j2);
loop(j2,
solve GaT4 using DNLP minimizing C;
W(t)= D(jj,t);
);

display PVPP.l,C.l;

execute_unload "GaT4.gdx" PVPP.l,C.l
image.png
execute 'gdxxrw.exe GaT4.gdx var=PVPP.l'
execute 'gdxxrw.exe GaT4.gdx var=C.l'
result.JPG
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Save loop results including Solve

Post by bussieck »

First a few remarks: Please attach a file instead of pasting the code if you communicate a larger model or at least put it in a code section. Make sure someone else can run your model. In your case the Excel file is missing and even with that the loop at the end would not compiler. W is unknown, and the .l suffix is missing on D and the jj is totally unclear (did you mean j2) and indeed part of the issue you describe.

In your model you loop over j2, but your model equations (e.g. L1) are indexed over t and j. The loop index j2 and the equation index j are not connected at all, so you are generating with each iteration a model for all your j columns and solve an identical model over and over, hence you get identical reports.

You want to solve a model slice, so restrict the generation of the model to j=j2. This can be done in many ways. Here are two:

1. Use a dynamic set for the j in the equation definition. Make a dynamic set jj(j), rewrite the algebra with jj instead of j, and fill jj in the loop with exactly the j2 from the loop index:

Code: Select all

set jj(j) 'single j driven by loop';
DD(t,jj).. D(t,jj)=e=dl(t)+dtup(t,jj)-dtdo(t,jj);
...
loop(j,
option clear=jj; jj(j) = yes;
solve GaT4 using DNLP minimizing C;
...
);
2. You can also work with a singleton set jj and don't even mention j in the equation index:

Code: Select all

singleton set jj(j) 'single j driven by loop';
DD(t).. D(t,jj)=e=dl(t)+dtup(t,jj)-dtdo(t,jj);
...
loop(j,
option clear=jj; jj(j) = yes;
solve GaT4 using DNLP minimizing C;
...
);
3. Add some $-control to the equation algebra so that you only pick up the j that is equal to j2:

Code: Select all

scalar ordj2;
DD(t,j)$(ord(j)=ordj2).. D(t,j)=e=dl(t)+dtup(t,j)-dtdo(t,j);
...
alias (j,j2);
loop(j2,
ordj2 = ord(j2);
solve GaT4 using DNLP minimizing C;
...
);
I prefer options 1 and 2 since they look much cleaner and execute faster.

-Michael
mjavadm
User
User
Posts: 6
Joined: 3 years ago

Re: Save loop results including Solve

Post by mjavadm »

Dear Michael
accept my apology for the mistakes, I have to mention some points about the code.
1- I wrote the W(t)= D(jj,t); to be able to save the results of the loop. but it did not work and I got the error of "suffix missing". I had to deactivate it but I forgot to insert * and sent the code with this line which was not correct!!! :(
2- about the loop: I linked two sets j and j2 by the "alias(j,j2);", without this, I receive the error of " Loop controlling index appears in model equation(s)". In addition, I think you are right!! Maybe it is better not to use j2 for this command because it seems that only I want to consider the second column of the table, although it is not correct. Actually, the goal is this: there are 14 values for PV power for a day and I made 100 samples of these 14 values. So the loop should consider each column for each iteration and solve the problem 100 times. the output "PVPP" must be a table with 14 rows and 100 columns.

point: with respect to this point, in the new code, I changed j2 :arrow: jj
Finally, I want to add this point that in the newly modified code, two cases are considered. In case1, the "sample" parameter is written directly into the equations. In this case, GAMS generates PVPP correctly but the problem is the identical values for each column (same as before)
Case2: here the line Ppv(t)=sample(t,j) is added inside the loop and I did not use jj niether in loop nor in equations. The result in this case is worese and is showns as pic "Case2 result".
Case1 Code.gms
Case1 GAMS file
(4.61 KiB) Downloaded 179 times
Case2 Code.gms
Case2 GAMS file
(4.54 KiB) Downloaded 180 times
Result of case1
Result of case1
Result of case2
Result of case2
GSample.xlsx
Input sample excel file
(28.65 KiB) Downloaded 174 times
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Save loop results including Solve

Post by bussieck »

Thanks for sharing the data. Nevertheless, I don't get it. I give you three solutions to your problem. Do don't take any of them and implement your own solution that still doesn't work and you ask again for help. Please try one of the solutions I gave in my first answer.

-Michael
Post Reply