📄 fmincopf.m
字号:
ubpqh = ubpqh / baseMVA; lbpqh = -1e10*ones(npqh,1);else Apqh = []; ubpqh = []; lbpqh = [];end% similarly Apqlif npql > 0 Apqldata = [gen(ipql,QC2MIN)-gen(ipql,QC1MIN), gen(ipql,PC1)-gen(ipql,PC2)]; ubpql= (gen(ipql,QC2MIN)-gen(ipql,QC1MIN)) .* gen(ipql,PC1) ... - (gen(ipql,PC2)-gen(ipql,PC1)) .* gen(ipql,QC1MIN) ; for i=1:npql, tmp = norm(Apqldata(i, : )); Apqldata(i, :) = Apqldata(i, :) / tmp; ubpql(i) = ubpql(i) / tmp; end Apql = sparse([1:npql, 1:npql]', [(pgbas-1)+ipql;(qgbas-1)+ipql], ... Apqldata(:), npql, nxyz); ubpql = ubpql / baseMVA; lbpql = -1e10*ones(npql,1);else Apql = []; ubpql = []; lbpql = [];end% Insert y columns in Au and N as necessaryif ny > 0 if nz > 0 Au = [ Au(:,1:qgend) sparse(nusr, ny) Au(:, qgend+(1:nz)) ]; if ~isempty(N) N = [ N(:,1:qgend) sparse(size(N,1), ny) N(:, qgend+(1:nz)) ]; end else Au = [ Au sparse(nusr, ny) ]; if ~isempty(N) N = [ N sparse(size(N,1), ny) ]; end endend% Now form the overall linear restriction matrix;% note the order of the constraints.if (ncony > 0 ) A = [ Au; Apqh; Apql; Avl; Ay; sparse(ones(1,ny), ybas:yend, ones(1,ny), 1, nxyz ) ]; % "linear" cost l = [ lbu; lbpqh; lbpql; lvl; -1e10*ones(ncony+1, 1) ]; u = [ ubu; ubpqh; ubpql; uvl; by; 1e10];else A = [ Au; Apqh; Apql; Avl ]; l = [ lbu; lbpqh; lbpql; lvl ]; u = [ ubu; ubpqh; ubpql; uvl ];end% So, can we do anything good about lambda initialization?if all(bus(:, LAM_P) == 0) bus(:, LAM_P) = (10)*ones(nb, 1);end% --------------------------------------------------------------% Up to this point, the setup is MINOS-like. We now adapt% things for fmincon.j = sqrt(-1);% Form a vector with basic info to pass on as a parameterparms = [ ... nb ;% 1 ng ;% 2 nl ;% 3 ny ;% 4 nx ;% 5 nvl ;% 6 nz ;% 7 nxyz;% 8 thbas;% 9 thend;% 10 vbas;% 11 vend;% 12 pgbas;% 13 pgend;% 14 qgbas;% 15 qgend;% 16 ybas;% 17 yend;% 18 zbas;% 19 zend;% 20 pmsmbas;% 21 pmsmend;% 22 qmsmbas;% 23 qmsmend;% 24 sfbas;% 25 sfend;% 26 stbas;% 27 stend;% 28];% If there are y variables the last row of A is a linear cost vector% of length nxyz. Let us excise it from A explicitly if it exists;% otherwise it is zero.if ny > 0 nn = size(A,1); ccost = full(A(nn, :)); A(nn, :) = []; l(nn) = []; u(nn) = [];else ccost = zeros(1, nxyz);end% Divide l <= A*x <= u into less than, equal to, greater than, doubly-bounded% sets.ieq = find( abs(u-l) <= eps);igt = find( u >= 1e10); % unlimited ceilingilt = find( l <= -1e10); % unlimited bottomibx = find( (abs(u-l) > eps) & (u < 1e10) & (l > -1e10));Af = [ A(ilt, :); -A(igt, :); A(ibx, :); -A(ibx, :) ];bf = [ u(ilt); -l(igt); u(ibx); -l(ibx)];Afeq = A(ieq, :);bfeq = u(ieq);% bounds on optimization vars; y and z vars unbounded at box bounds;% if needed, user must do this via the Ax < l mechanism if needed and hope that% fmincon handles singleton rows elegantly.UB = Inf * ones(nxyz, 1);LB = -UB;LB(thbas+ref-1) = bus(ref, VA)*pi/180; UB(thbas+ref-1) = bus(ref, VA)*pi/180;LB(vbas:vend) = bus(:, VMIN); UB(vbas:vend) = bus(:, VMAX);LB(pgbas:pgend) = gen(:, PMIN)/baseMVA; UB(pgbas:pgend) = gen(:, PMAX)/baseMVA;LB(qgbas:qgend) = gen(:, QMIN)/baseMVA; UB(qgbas:qgend) = gen(:, QMAX)/baseMVA;% Compute initial vectorx0 = zeros(nxyz, 1);x0(thbas:thend) = bus(:, VA) * pi/180;x0(vbas:vend) = bus(:, VM);x0(vbas+gen(:,GEN_BUS)-1) = gen(:, VG); % buses w. gens init V from gen datax0(pgbas:pgend) = gen(:, PG) / baseMVA;x0(qgbas:qgend) = gen(:, QG) / baseMVA;% no ideas to initialize z, y variables, though, and no mechanism yet% to ask for user-provided initial z, y.% build admittance matrices[Ybus, Yf, Yt] = makeYbus(baseMVA, bus, branch);% Tolerancesif mpopt(19) == 0 % # iterations mpopt(19) = 150 + 2*nb;end% basic optimset options needed for fmincon% fmoptions = optimset('GradObj', 'on', 'Hessian', 'on', 'LargeScale', 'on', ...% 'GradConstr', 'on');fmoptions = optimset('GradObj', 'on', 'LargeScale', 'off', 'GradConstr', 'on');fmoptions = optimset(fmoptions, 'MaxIter', mpopt(19), 'TolCon', mpopt(16) );fmoptions = optimset(fmoptions, 'TolX', mpopt(17), 'TolFun', mpopt(18) );fmoptions.MaxFunEvals = 4 * fmoptions.MaxIter;if mpopt(31) == 0, fmoptions.Display = 'off';else fmoptions.Display = 'iter';endAf = full(Af);Afeq = full(Afeq);[x, f, info, Output, Lambda, Jac] = ... fmincon('costfmin', x0, Af, bf, Afeq, bfeq, LB, UB, 'consfmin', fmoptions, ... baseMVA, bus, gen, gencost, branch, areas, Ybus, Yf, Yt, mpopt, ... parms, ccost, N, fparm, H, Cw);success = (info > 0);% Unpack optimal xbus(:, VA) = x(thbas:thend)*180/pi;bus(:, VM) = x(vbas:vend);gen(:, PG) = baseMVA * x(pgbas:pgend);gen(:, QG) = baseMVA * x(qgbas:qgend);gen(:, VG) = bus(gen(:, GEN_BUS), VM);% reconstruct voltagesVa = x(thbas:thend);Vm = x(vbas:vend);V = Vm .* exp(j*Va);%% compute branch injectionsSf = V(branch(:, F_BUS)) .* conj(Yf * V); %% cplx pwr at "from" bus, p.u.St = V(branch(:, T_BUS)) .* conj(Yt * V); %% cplx pwr at "to" bus, p.u.branch(:, PF) = real(Sf) * baseMVA;branch(:, QF) = imag(Sf) * baseMVA;branch(:, PT) = real(St) * baseMVA;branch(:, QT) = imag(St) * baseMVA;% Put in Lagrange multipliersgen(:, MU_PMAX) = Lambda.upper(pgbas:pgend) / baseMVA;gen(:, MU_PMIN) = Lambda.lower(pgbas:pgend) / baseMVA;gen(:, MU_QMAX) = Lambda.upper(qgbas:qgend) / baseMVA;gen(:, MU_QMIN) = Lambda.lower(qgbas:qgend) / baseMVA;bus(:, LAM_P) = Lambda.eqnonlin(1:nb) / baseMVA;bus(:, LAM_Q) = Lambda.eqnonlin(nb+1:2*nb) / baseMVA;bus(:, MU_VMAX) = Lambda.upper(vbas:vend);bus(:, MU_VMIN) = Lambda.lower(vbas:vend);branch(:, MU_SF) = Lambda.ineqnonlin(1:nl) / baseMVA; branch(:, MU_ST) = Lambda.ineqnonlin(nl+1:2*nl) / baseMVA;% extract lambdas from linear constraintsnlt = length(ilt);ngt = length(igt);nbx = length(ibx);lam = zeros(size(u));lam(ieq) = Lambda.eqlin;lam(ilt) = Lambda.ineqlin(1:nlt);lam(igt) = Lambda.ineqlin(nlt+[1:ngt]);lam(ibx) = Lambda.ineqlin(nlt+ngt+[1:nbx]) + Lambda.ineqlin(nlt+ngt+nbx+[1:nbx]);% stick in non-linear constraints too, so we can use the indexing variables% we've defined, and negate so it looks like the pimul from MINOSpimul = [ zeros(stend,1); -lam ];% If we succeeded and there were generators with general pq curve% characteristics, this is the time to re-compute the multipliers,% splitting any nonzero multiplier on one of the linear bounds among the% Pmax, Pmin, Qmax or Qmin limits, producing one multiplier for a P limit and% another for a Q limit. For upper Q limit, if we are neither at Pmin nor at % Pmax, the limit is taken at Pmin if the Qmax line's normal has a negative P% component, Pmax if it has a positive P component. Messy but there really% are many cases.if success & (npqh > 0) k = 1; for i = ipqh' if gen(i, MU_PMAX) > 0 gen(i,MU_PMAX)=gen(i,MU_PMAX)-pimul(pqhbas+k-1)*Apqhdata(k,1)/baseMVA; gen(i,MU_QMAX)=gen(i,MU_QMAX)-pimul(pqhbas+k-1)*Apqhdata(k,2)/baseMVA; elseif gen(i, MU_PMIN) < 0 gen(i,MU_PMIN)=gen(i,MU_PMIN)+pimul(pqhbas+k-1)*Apqhdata(k,1)/baseMVA; gen(i,MU_QMAX)=gen(i,MU_QMAX)-pimul(pqhbas+k-1)*Apqhdata(k,2)/baseMVA; else if Apqhdata(k, 1) >= 0 gen(i, MU_PMAX) = -pimul(pqhbas+k-1)*Apqhdata(k,1)/baseMVA; else gen(i, MU_PMIN) = pimul(pqhbas+k-1)*Apqhdata(k,1)/baseMVA; end gen(i, MU_QMAX)= gen(i,MU_QMAX)-pimul(pqhbas+k-1)*Apqhdata(k,2)/baseMVA; end k = k + 1; endendif success & (npql > 0) k = 1; for i = ipql' if gen(i, MU_PMAX) > 0 gen(i,MU_PMAX)=gen(i,MU_PMAX)-pimul(pqlbas+k-1)*Apqldata(k,1)/baseMVA; gen(i,MU_QMIN)=gen(i,MU_QMIN)-pimul(pqlbas+k-1)*Apqldata(k,2)/baseMVA; elseif gen(i, MU_PMIN) > 0 gen(i,MU_PMIN)=gen(i,MU_PMIN)-pimul(pqlbas+k-1)*Apqldata(k,1)/baseMVA; gen(i,MU_QMIN)=gen(i,MU_QMIN)+pimul(pqlbas+k-1)*Apqldata(k,2)/baseMVA; else if Apqldata(k,1) >= 0 gen(i,MU_PMAX)= -pimul(pqlbas+k-1)*Apqldata(k,1)/baseMVA; else gen(i,MU_PMIN)= pimul(pqlbas+k-1)*Apqldata(k,1)/baseMVA; end gen(i,MU_QMIN)=gen(i,MU_QMIN)+pimul(pqlbas+k-1)*Apqldata(k,2)/baseMVA; end k = k + 1; endend% With these modifications, printpf must then look for multipliers% if available in order to determine if a limit has been hit.% We are done with standard opf but we may need to provide the% constraints and their Jacobian also.if nargout > 7 [g, geq, dg, dgeq] = consfmin(x, baseMVA, bus, gen, gencost, branch, areas,... Ybus, Yf, Yt, mpopt, parms, ccost); g = [ geq; g]; jac = [ dgeq'; dg']; % true Jacobian organizationend% Go back to original data.% reorder generatorsgen = gen(inv_gen_ord, :);% convert to original external bus ordering[bus, gen, branch, areas] = int2ext(i2e, bus, gen, branch, areas);% Now create output matrices with all lines, all generators, committed and% non-committedgenout = genorg;branchout = branchorg;genout(comgen, : ) = gen;branchout(onbranch, :) = branch;% And zero out appropriate fields of non-comitted generators and linesif ~isempty(offgen) tmp = zeros(length(offgen), 1); genout(offgen, PG) = tmp; genout(offgen, QG) = tmp; genout(offgen, MU_PMAX) = tmp; genout(offgen, MU_PMIN) = tmp;endif ~isempty(offbranch) tmp = zeros(length(offbranch), 1); branchout(offbranch, PF) = tmp; branchout(offbranch, QF) = tmp; branchout(offbranch, PT) = tmp; branchout(offbranch, QT) = tmp; branchout(offbranch, MU_SF) = tmp; branchout(offbranch, MU_ST) = tmp;endet = etime(clock,t1);if (nargout == 0) & ( success ) printpf(baseMVA, bus, genout, branchout, f, info, et, 1, mpopt);endif nargout, busout = bus; endreturn;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -