-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.m
More file actions
282 lines (235 loc) · 7.11 KB
/
Copy pathMain.m
File metadata and controls
282 lines (235 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
% Flujo de carga optimo AC mediante fmincon
% En este flujo de carga los flujos de lineas se establecen como
% variables
clc, clear all;
% [BUSDATA, LINEDATA, GENDATA] = LoadData('DATOS_Wollenberg.xlsx', 'BUS', 'RAMAS', 'GEN');
% [BUSDATA, LINEDATA, GENDATA] = LoadData('DATOS_MarioPereira.xlsx', 'BUS', 'RAMAS', 'GEN');
[BUSDATA, LINEDATA, GENDATA] = LoadData('DATOS_3b_3g.xlsx', 'BUS', 'RAMAS', 'GEN');
nb = size(BUSDATA, 1);
ng = size(GENDATA, 1);
nl = size(LINEDATA, 1);
% contemos los shunts
ns = 0;
for i = 1:nl
if LINEDATA(i, 1) == LINEDATA(i, 2) % si i == k significa que es un shunt
ns = ns + 1;
end
end
% Ya que no queremos contar los shunts como lineas, debemos restar este
% valor al numero total de lineas
nl = nl - ns;
[Ybus, Gik, Bik, gi0, bi0] = CreateYbus(LINEDATA, nb, nl);
Pload = BUSDATA(:, 6);
Qload = BUSDATA(:, 7);
Pgen = BUSDATA(:, 8);
Qgen = BUSDATA(:, 9);
ci = GENDATA(:, 2);
bi = GENDATA(:, 3); %Cmg
ai = GENDATA(:, 4);
Pgmin = GENDATA(:, 5);
Pgmax = GENDATA(:, 6);
Qgmin = GENDATA(:, 7);
Qgmax = GENDATA(:, 8);
for i = 1:nl
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from ~= to
Pflowmax(i, 1) = LINEDATA(i, 7);
end
end
% Pflowmax = LINEDATA(:, 7);
for i = 1:nl
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from ~= to
Qflowmax(i, 1) = LINEDATA(i, 8);
end
end
% Qflowmax = LINEDATA(:, 8);
if ns > 0
v = 1;
for i = 1:nl+ns
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from == to % es shunt
Qshuntmax(v, 1) = LINEDATA(i, 8);
v = v + 1;
end
end
end
Pflowmax(Pflowmax >= 1e10) = Inf; % Si se declara un maximo >= 1e10, se asigna como infinito
Qflowmax(Qflowmax >= 1e10) = Inf; % Si se declara un maximo >= 1e10, se asigna como infinito
if ns > 0
Qshuntmax(Qshuntmax >= 1e10) = Inf; % Si se declara un maximo >= 1e10, se asigna como infinito
end
refang = BUSDATA(:, 3);
Vmin = BUSDATA(:, 18);
Vmax = BUSDATA(:, 19);
%%
% Las matrices como vectores tendran (ng + ng + 2*nl + 2*nl + ns + nb - 1 + nb) columnas
% Esto corresponde a 'ng + ng Potencias generadas', '4 variables por
% cada linea 2*nl + 2*nl (4 flujos por linea (Pik Pki Qik Qki)', 'ns' corresponde a cuantos shunts
% hay en el sistema. 'nb - 1 variables por barra (nb - 1 angulos como incognitas de angulos) y nb voltajes
% La matriz Aeq tendra nb filas y (ng + ng + 2*nl + 2*nl + nb - 1 + nb) columnas
% Pues sera una ecuacion por cada barra con generador (ecuaciones
% lineales)
% Por ahora no se toma en cuenta ecuaciones de potencia reactiva
% Aeq = zeros(2*nb, (ng + ng + 2*nl + 2*nl + ns + nb - 1 + nb));
beq = zeros(2*nb, 1);
Amq = zeros(2*nb, 2*ng);
v = 1;
for i = 1:ng % correspondiente a los terminos Pgi
Amq(v,v) = 1;
v = v + 1;
end
v = 1;
for i = 1:ng % correspondiente a los terminos Qgi
Amq(nb + v, ng + v) = 1;
v = v + 1;
end
Bmq = zeros(2*nb, 4*nl);
Cmq = zeros(2*nb, nb - 1);
Dmq = zeros(2*nb, nb);
% Llenamos la matriz Bmq con los terminos correspondientes a las
% variables de flujos de lineas correspondientes a cada ecuacion de
% potencia generada
% Pgi = P12 + P12 + ... Pik + Ploadi
% Pgi - P12 - P13 - ... Pik = Ploadi
% Qgi = Q12 + Q12 + ... Qik + Qloadi
% Qgi - Q12 - Q13 - ... Qik = Qloadi
v = 1;
for i = 1:nl
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from ~= to
Bmq(from, v) = -1;
v = v + 1;
end
end
for i = 1:nl
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from ~= to
Bmq(to, v) = -1;
v = v + 1;
end
end
v = 2*nl + 1;
for i = 1:nl
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from ~= to
Bmq(nb + from, v) = -1;
v = v + 1;
end
end
for i = 1:nl
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from ~= to
Bmq(nb + to, v) = -1;
v = v + 1;
end
end
% Vector correspondiente a las variables de los shunts
% Si existen compensadores en el sistema, se agrega una columna adicional a
% la matriz Bmq
for i = 1:nl+ns
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from == to %% corresponde a un shunt
Bmq(nb + from, 2*nl + 2*nl + 1) = -1;
end
end
Aeq = [Amq Bmq Cmq Dmq];
beq(1:nb) = Pload(1:nb);
beq(nb+1:2*nb) = Qload(1:nb);
A = [];
b = [];
% Se establecen los limites inferiores y superiores (lb y ub)
lb = Pgmin;
lb = vertcat(lb, Qgmin);
lb = vertcat(lb, -Pflowmax);
lb = vertcat(lb, -Pflowmax);
lb = vertcat(lb, -Qflowmax);
lb = vertcat(lb, -Qflowmax);
if ns > 0
lb = vertcat(lb, -Qshuntmax);
end
lb = vertcat(lb, -ones(nb - 1, 1).*Inf);
lb = vertcat(lb, Vmin);
ub = Pgmax;
ub = vertcat(ub, Qgmax);
ub = vertcat(ub, Pflowmax);
ub = vertcat(ub, Pflowmax);
ub = vertcat(ub, Qflowmax);
ub = vertcat(ub, Qflowmax);
if ns > 0
ub = vertcat(ub, Qshuntmax);
end
ub = vertcat(ub, ones(nb - 1, 1).*Inf);
ub = vertcat(ub, Vmax);
% Vector de valores iniciales
x0 = zeros(1, (ng + ng + 2*nl + 2*nl + ns + nb - 1 + nb));
x0((ng + ng + 2*nl + 2*nl + ns + nb - 1 + 1):(ng + ng + 2*nl + 2*nl + ns + nb - 1 + nb)) = BUSDATA(1:nb, 4);
options = optimset('display', 'on', 'algorithm', 'interior-point');
[x,fval,exitflag,~,lambda] = fmincon('FuncObjetivo', x0, A, b, Aeq, beq, lb, ub, 'NoLineales', options, LINEDATA, refang, ci, bi, ai, Gik, gi0, Bik, bi0, nb, ng, nl, ns);
exitflag
Pg = x(1:ng);
Pgen = Pg;
Pgen(ng+1:nb) = 0;
Qg = x((ng + 1):(ng + ng));
Qgen = Qg;
Qgen(ng+1:nb) = 0;
Pik = x((ng + ng + 1):(ng + ng + nl));
Pki = x((ng + ng + nl + 1):(ng + ng + 2*nl));
Qik = x((ng + ng + 2*nl + 1):(ng + ng + 2*nl + nl));
Qki = x((ng + ng + 2*nl + nl + 1):(ng + ng + 2*nl + 2*nl));
theta = zeros(nb, 1);
v = 1;
for i = 1:nb
if refang(i) == 0
theta(i) = x((ng + ng + 2*nl + 2*nl + ns) + v);
v = v + 1;
end
end
V = x((ng + ng + 2*nl + 2*nl + ns + nb - 1 + 1):(ng + ng + 2*nl + 2*nl + ns + nb - 1 + nb));
Ploss = sum(Pik) + sum(Pki);
Qloss = sum(Qik) + sum(Qki);
% Sshunt = zeros(nb, 1);
% for l = 1:nl
% i = LINEDATA(l, 1);
% k = LINEDATA(l, 2);
% if i == k% es shunt
% Zshunt = LINEDATA(l, 3) + 1i*LINEDATA(l, 4);
% Sshunt(i) = V(i)^2 /Zshunt;
% end
% end
Sshunt = zeros(nb, 1);
if ns > 0
v = 1;
for i = 1:nl+ns
from = LINEDATA(i, 1);
to = LINEDATA(i, 2);
if from == to
Sshunt(from) = x(ng + ng + 2*nl + 2*nl + v);
v = v + 1;
end
end
end
Pneta = Pgen' - Pload;
Qneta = Qgen' - Qload - Sshunt;
lamb = abs(lambda.eqlin);
%Kuhn-Tucker
eta = abs(lambda.lower);
miu = abs(lambda.upper);
Costo_generacion = sum(ci + bi.*Pg' + ai.*Pg'.^2);
Cobro_transmision = sum(eta((ng + ng + 1):(ng + ng + nl)).*Pik') + ...
sum(eta((ng + ng + nl + 1):(ng + ng +nl + nl)).*Pki') + ...
sum(miu((ng + ng + 1):(ng + ng + nl)).*Pik') + ...
sum(miu((ng + ng + nl + 1):(ng + ng +nl + nl)).*Pki');
PrintFCO(V, theta, Pgen, Qgen, Pload, Qload, Ploss, Qloss, Pneta, Qneta, Sshunt, Pik, Pki, Pflowmax, Costo_generacion, LINEDATA, nb, nl);
% lambda.eqnonlin
% lambda.ineqlin
% lambda.upper
% lambda.lower