Home > matpower5.0 > extras > smartmarket > smartmkt.m

smartmkt

PURPOSE ^

SMARTMKT Runs the PowerWeb smart market.

SYNOPSIS ^

function [co, cb, r, dispatch, success] =smartmkt(mpc, offers, bids, mkt, mpopt)

DESCRIPTION ^

SMARTMKT  Runs the PowerWeb smart market.
   [CO, CB, RESULTS, DISPATCH, SUCCESS] = SMARTMKT(MPC, ...
       OFFERS, BIDS, MKT, MPOPT) runs the ISO smart market.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [co, cb, r, dispatch, success] = ...
0002             smartmkt(mpc, offers, bids, mkt, mpopt)
0003 %SMARTMKT  Runs the PowerWeb smart market.
0004 %   [CO, CB, RESULTS, DISPATCH, SUCCESS] = SMARTMKT(MPC, ...
0005 %       OFFERS, BIDS, MKT, MPOPT) runs the ISO smart market.
0006 
0007 %   MATPOWER
0008 %   $Id: smartmkt.m 2229 2013-12-11 01:28:09Z ray $
0009 %   by Ray Zimmerman, PSERC Cornell
0010 %   Copyright (c) 1996-2010 by Power System Engineering Research Center (PSERC)
0011 %
0012 %   This file is part of MATPOWER.
0013 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0014 %
0015 %   MATPOWER is free software: you can redistribute it and/or modify
0016 %   it under the terms of the GNU General Public License as published
0017 %   by the Free Software Foundation, either version 3 of the License,
0018 %   or (at your option) any later version.
0019 %
0020 %   MATPOWER is distributed in the hope that it will be useful,
0021 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0022 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0023 %   GNU General Public License for more details.
0024 %
0025 %   You should have received a copy of the GNU General Public License
0026 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0027 %
0028 %   Additional permission under GNU GPL version 3 section 7
0029 %
0030 %   If you modify MATPOWER, or any covered work, to interface with
0031 %   other modules (such as MATLAB code and MEX-files) available in a
0032 %   MATLAB(R) or comparable environment containing parts covered
0033 %   under other licensing terms, the licensors of MATPOWER grant
0034 %   you additional permission to convey the resulting work.
0035 
0036 %%-----  initialization  -----
0037 %% default arguments
0038 if nargin < 5
0039     mpopt = mpoption;       %% use default options
0040 end
0041 
0042 %% initialize some stuff
0043 G = find( ~isload(mpc.gen) );       %% real generators
0044 L = find(  isload(mpc.gen) );       %% dispatchable loads
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 %% set power flow formulation based on market
0058 mpopt = mpoption(mpopt, 'model', upper(mkt.OPF));
0059 
0060 %% define named indices into data matrices
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 %% set up cost info & generator limits
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 %% move Pmin and Pmax limits out slightly to avoid problems
0074 %% with lambdas caused by rounding errors when corner point
0075 %% of cost function lies at exactly Pmin or Pmax
0076 if any(find(genoffer(:, MODEL) == PW_LINEAR))
0077     gg = find( ~isload(gen) );      %% skip dispatchable loads
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 %%-----  solve the optimization problem  -----
0083 %% attempt OPF
0084 mpc2 = mpc;
0085 mpc2.gen = gen;
0086 mpc2.gencost = genoffer;
0087 [r, success] = uopf(mpc2, mpopt);
0088 r.genoffer = r.gencost;     %% save the gencost used to run the OPF
0089 r.gencost  = mpc.gencost;   %% and restore the original gencost
0090 [bus, gen] = deal(r.bus, r.gen);
0091 if mpopt.verbose && ~success
0092     fprintf('\nSMARTMARKET: non-convergent UOPF');
0093 end
0094 
0095 %%-----  compute quantities, prices & costs  -----
0096 %% compute quantities & prices
0097 ng = size(gen, 1);
0098 if success      %% OPF solved case fine
0099     %% create map of external bus numbers to bus indices
0100     i2e = bus(:, BUS_I);
0101     e2i = sparse(max(i2e), 1);
0102     e2i(i2e) = (1:size(bus, 1))';
0103 
0104     %% get nodal marginal prices from OPF
0105     gbus    = e2i(gen(:, GEN_BUS));                 %% indices of buses w/gens
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);  %% real power prices
0110     lamQ    = sparse(1:ng, 1:ng, bus(gbus, LAM_Q), ng, ng) * ones(ng, nP);  %% reactive power prices
0111     
0112     %% compute fudge factor for lamP to include price of bundled reactive power
0113     pf   = zeros(length(L), 1);                 %% for loads Q = pf * P
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;         %% guarantee that cleared offers are >= offers
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);   %% use unbundled lambdas
0127         gtee_prc.bid = 0;       %% allow cleared bids to be above bid price
0128     else
0129         Pbid.lam = lamP(L,1:nPb) + sparse(1:nL, 1:nL, pf, nL, nL) * lamQ(L,1:nPb);  %% bundled lambdas
0130         gtee_prc.bid = 1;       %% guarantee that cleared bids are <= bids
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         %% get nodal marginal prices from OPF
0141         lamQ    = sparse(1:ng, 1:ng, bus(gbus, LAM_Q), ng, ng) * ones(ng, nQ);  %% reactive power prices
0142 
0143         Qoffer = offers.Q;
0144         Qoffer.lam = lamQ(:,1:nQo);     %% use unbundled lambdas
0145         Qoffer.total_qty = (gen(:, QG) > 0) .* gen(:, QG);
0146         
0147         Qbid = bids.Q;
0148         Qbid.lam = lamQ(:,1:nQb);       %% use unbundled lambdas
0149         Qbid.total_qty = (gen(:, QG) < 0) .* -gen(:, QG);
0150 
0151         %% too complicated to scale with mixed bids/offers
0152         %% (only auction_types 0 and 5 allowed)
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);   %% need these for prices for
0160     price(L)    = cb.P.prc(:, 1);   %% gens that are shut down
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        %% did not converge even with imports
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 %% compute costs in $ (note, NOT $/hr)
0194 if size(mpc.gencost, 1) == ng                   %% no reactive costs
0195     pgcost = mpc.gencost;
0196     fcost = mkt.t * totcost(pgcost, zeros(ng, 1));          %% fixed costs
0197     vcost = mkt.t * totcost(pgcost, quantity    ) - fcost;  %% variable costs
0198     scost =   (~mkt.u0 & gen(:, GEN_STATUS) >  0) .* ...
0199                     pgcost(:, STARTUP) + ...                %% startup costs
0200                 ( mkt.u0 & gen(:, GEN_STATUS) <= 0) .* ...
0201                     pgcost(:, SHUTDOWN);                    %% shutdown costs
0202 else    %% size(mpc.gencost, 1) == 2 * ng       %% reactive costs included
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)) );      %% fixed costs
0207     vcost = mkt.t * ( totcost(pgcost, quantity) + ...
0208                       totcost(qgcost, quantityQ) ) - fcost; %% variable costs
0209     scost = (~mkt.u0 & gen(:, GEN_STATUS) >  0) .* ...
0210                 (pgcost(:, STARTUP) + qgcost(:, STARTUP)) + ... %% startup costs
0211             ( mkt.u0 & gen(:, GEN_STATUS) <= 0) .* ...
0212                 (pgcost(:, SHUTDOWN) + qgcost(:, SHUTDOWN));    %% shutdown costs
0213 end
0214 
0215 %% store in dispatch
0216 dispatch = zeros(ng, PENALTY);
0217 dispatch(:, [QUANTITY PRICE FCOST VCOST SCOST]) = [quantity price fcost vcost scost];

Generated on Mon 26-Jan-2015 15:21:31 by m2html © 2005