0001 function [co, cb, r, dispatch, success] = ...
0002 smartmkt(mpc, offers, bids, mkt, mpopt)
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 if nargin < 5
0039 mpopt = mpoption;
0040 end
0041
0042
0043 G = find( ~isload(mpc.gen) );
0044 L = find( isload(mpc.gen) );
0045 nL = length(L);
0046 if isfield(offers, 'Q') || isfield(bids, 'Q')
0047 haveQ = 1;
0048 else
0049 haveQ = 0;
0050 end
0051
0052 if haveQ && mkt.auction_type ~= 0 && mkt.auction_type ~= 5
0053 error(['smartmkt: Combined active/reactive power markets ', ...
0054 'are only implemented for auction types 0 and 5']);
0055 end
0056
0057
0058 mpopt = mpoption(mpopt, 'model', upper(mkt.OPF));
0059
0060
0061 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0062 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0063 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0064 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0065 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0066 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0067 [QUANTITY, PRICE, FCOST, VCOST, SCOST, PENALTY] = idx_disp;
0068
0069
0070 mkt.lim = pricelimits(mkt.lim, isfield(offers, 'Q') || isfield(bids, 'Q'));
0071 [gen, genoffer] = off2case(mpc.gen, mpc.gencost, offers, bids, mkt.lim);
0072
0073
0074
0075
0076 if any(find(genoffer(:, MODEL) == PW_LINEAR))
0077 gg = find( ~isload(gen) );
0078 gen(gg, PMIN) = gen(gg, PMIN) - 100 * mpopt.opf.violation * ones(size(gg));
0079 gen(gg, PMAX) = gen(gg, PMAX) + 100 * mpopt.opf.violation * ones(size(gg));
0080 end
0081
0082
0083
0084 mpc2 = mpc;
0085 mpc2.gen = gen;
0086 mpc2.gencost = genoffer;
0087 [r, success] = uopf(mpc2, mpopt);
0088 r.genoffer = r.gencost;
0089 r.gencost = mpc.gencost;
0090 [bus, gen] = deal(r.bus, r.gen);
0091 if mpopt.verbose && ~success
0092 fprintf('\nSMARTMARKET: non-convergent UOPF');
0093 end
0094
0095
0096
0097 ng = size(gen, 1);
0098 if success
0099
0100 i2e = bus(:, BUS_I);
0101 e2i = sparse(max(i2e), 1);
0102 e2i(i2e) = (1:size(bus, 1))';
0103
0104
0105 gbus = e2i(gen(:, GEN_BUS));
0106 nPo = size(offers.P.qty, 2);
0107 nPb = size(bids.P.qty, 2);
0108 nP = max([ nPo nPb ]);
0109 lamP = sparse(1:ng, 1:ng, bus(gbus, LAM_P), ng, ng) * ones(ng, nP);
0110 lamQ = sparse(1:ng, 1:ng, bus(gbus, LAM_Q), ng, ng) * ones(ng, nP);
0111
0112
0113 pf = zeros(length(L), 1);
0114 Qlim = (gen(L, QMIN) == 0) .* gen(L, QMAX) + ...
0115 (gen(L, QMAX) == 0) .* gen(L, QMIN);
0116 pf = Qlim ./ gen(L, PMIN);
0117
0118 gtee_prc.offer = 1;
0119 Poffer = offers.P;
0120 Poffer.lam = lamP(G,1:nPo);
0121 Poffer.total_qty = gen(G, PG);
0122
0123 Pbid = bids.P;
0124 Pbid.total_qty = -gen(L, PG);
0125 if haveQ
0126 Pbid.lam = lamP(L,1:nPb);
0127 gtee_prc.bid = 0;
0128 else
0129 Pbid.lam = lamP(L,1:nPb) + sparse(1:nL, 1:nL, pf, nL, nL) * lamQ(L,1:nPb);
0130 gtee_prc.bid = 1;
0131 end
0132
0133 [co.P, cb.P] = auction(Poffer, Pbid, mkt.auction_type, mkt.lim.P, gtee_prc);
0134
0135 if haveQ
0136 nQo = size(offers.Q.qty, 2);
0137 nQb = size(bids.Q.qty, 2);
0138 nQ = max([ nQo nQb ]);
0139
0140
0141 lamQ = sparse(1:ng, 1:ng, bus(gbus, LAM_Q), ng, ng) * ones(ng, nQ);
0142
0143 Qoffer = offers.Q;
0144 Qoffer.lam = lamQ(:,1:nQo);
0145 Qoffer.total_qty = (gen(:, QG) > 0) .* gen(:, QG);
0146
0147 Qbid = bids.Q;
0148 Qbid.lam = lamQ(:,1:nQb);
0149 Qbid.total_qty = (gen(:, QG) < 0) .* -gen(:, QG);
0150
0151
0152
0153 [co.Q, cb.Q] = auction(Qoffer, Qbid, mkt.auction_type, mkt.lim.Q, gtee_prc);
0154 end
0155
0156 quantity = gen(:, PG);
0157 quantityQ = gen(:, QG);
0158 price = zeros(ng, 1);
0159 price(G) = co.P.prc(:, 1);
0160 price(L) = cb.P.prc(:, 1);
0161 if nP == 1
0162 k = find( co.P.qty );
0163 price(G(k)) = co.P.prc(k, :);
0164 k = find( cb.P.qty );
0165 price(L(k)) = cb.P.prc(k, :);
0166 else
0167 k = find( sum( co.P.qty' )' );
0168 price(G(k)) = sum( co.P.qty(k, :)' .* co.P.prc(k, :)' )' ./ sum( co.P.qty(k, :)' )';
0169 k = find( sum( cb.P.qty' )' );
0170 price(L(k)) = sum( cb.P.qty(k, :)' .* cb.P.prc(k, :)' )' ./ sum( cb.P.qty(k, :)' )';
0171 end
0172 else
0173 quantity = zeros(ng, 1);
0174 quantityQ = zeros(ng, 1);
0175 if isempty(mkt.lim.P.max_offer)
0176 price = NaN(ng, 1);
0177 else
0178 price = mkt.lim.P.max_offer * ones(ng, 1);
0179 end
0180 co.P.qty = zeros(size(offers.P.qty));
0181 co.P.prc = zeros(size(offers.P.prc));
0182 cb.P.qty = zeros(size(bids.P.qty));
0183 cb.P.prc = zeros(size(bids.P.prc));
0184 if haveQ
0185 co.Q.qty = zeros(size(offers.Q.qty));
0186 co.Q.prc = zeros(size(offers.Q.prc));
0187 cb.Q.qty = zeros(size(bids.Q.qty));
0188 cb.Q.prc = zeros(size(bids.Q.prc));
0189 end
0190 end
0191
0192
0193
0194 if size(mpc.gencost, 1) == ng
0195 pgcost = mpc.gencost;
0196 fcost = mkt.t * totcost(pgcost, zeros(ng, 1));
0197 vcost = mkt.t * totcost(pgcost, quantity ) - fcost;
0198 scost = (~mkt.u0 & gen(:, GEN_STATUS) > 0) .* ...
0199 pgcost(:, STARTUP) + ...
0200 ( mkt.u0 & gen(:, GEN_STATUS) <= 0) .* ...
0201 pgcost(:, SHUTDOWN);
0202 else
0203 pgcost = mpc.gencost(1:ng, :);
0204 qgcost = mpc.gencost(ng+(1:ng), :);
0205 fcost = mkt.t * ( totcost(pgcost, zeros(ng, 1)) + ...
0206 totcost(qgcost, zeros(ng, 1)) );
0207 vcost = mkt.t * ( totcost(pgcost, quantity) + ...
0208 totcost(qgcost, quantityQ) ) - fcost;
0209 scost = (~mkt.u0 & gen(:, GEN_STATUS) > 0) .* ...
0210 (pgcost(:, STARTUP) + qgcost(:, STARTUP)) + ...
0211 ( mkt.u0 & gen(:, GEN_STATUS) <= 0) .* ...
0212 (pgcost(:, SHUTDOWN) + qgcost(:, SHUTDOWN));
0213 end
0214
0215
0216 dispatch = zeros(ng, PENALTY);
0217 dispatch(:, [QUANTITY PRICE FCOST VCOST SCOST]) = [quantity price fcost vcost scost];