Home > matpower5.0 > @opt_model > compute_cost.m

compute_cost

PURPOSE ^

COMPUTE_COST Computes a user-defined cost.

SYNOPSIS ^

function f = compute_cost(om, x, name, idx)

DESCRIPTION ^

COMPUTE_COST  Computes a user-defined cost.
   F_U = COMPUTE_COST(OM, X)
   F_U = COMPUTE_COST(OM, X, NAME)
   F_U = COMPUTE_COST(OM, X, NAME, IDX)

   Computes the value of a user defined cost, either for all user
   defined costs or for a named set of costs. Requires calling
   BUILD_COST_PARAMS first to build the full set of parameters.

   Let X be the full set of optimization variables and F_U(X, CP) be the
   user-defined cost at X, corresponding to the set of cost parameters in
   the CP struct returned by GET_COST_PARAMS, where CP is a struct with the
   following fields:
       N      - nw x nx sparse matrix
       Cw     - nw x 1 vector
       H      - nw x nw sparse matrix (optional, all zeros by default)
       dd, mm - nw x 1 vectors (optional, all ones by default)
       rh, kk - nw x 1 vectors (optional, all zeros by default)

   These parameters are used as follows to compute F_U(X, CP)

       R  = N*x - rh

               /  kk(i),  R(i) < -kk(i)
       K(i) = <   0,     -kk(i) <= R(i) <= kk(i)
               \ -kk(i),  R(i) > kk(i)

       RR = R + K

       U(i) =  /  0, -kk(i) <= R(i) <= kk(i)
               \  1, otherwise

       DDL(i) = /  1, dd(i) = 1
                \  0, otherwise

       DDQ(i) = /  1, dd(i) = 2
                \  0, otherwise

       Dl = diag(mm) * diag(U) * diag(DDL)
       Dq = diag(mm) * diag(U) * diag(DDQ)

       w = (Dl + Dq * diag(RR)) * RR

       F_U(X, CP) = 1/2 * w'*H*w + Cw'*w

   See also OPT_MODEL, ADD_COSTS, BUILD_COST_PARAMS, GET_COST_PARAMS.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function f = compute_cost(om, x, name, idx)
0002 %COMPUTE_COST  Computes a user-defined cost.
0003 %   F_U = COMPUTE_COST(OM, X)
0004 %   F_U = COMPUTE_COST(OM, X, NAME)
0005 %   F_U = COMPUTE_COST(OM, X, NAME, IDX)
0006 %
0007 %   Computes the value of a user defined cost, either for all user
0008 %   defined costs or for a named set of costs. Requires calling
0009 %   BUILD_COST_PARAMS first to build the full set of parameters.
0010 %
0011 %   Let X be the full set of optimization variables and F_U(X, CP) be the
0012 %   user-defined cost at X, corresponding to the set of cost parameters in
0013 %   the CP struct returned by GET_COST_PARAMS, where CP is a struct with the
0014 %   following fields:
0015 %       N      - nw x nx sparse matrix
0016 %       Cw     - nw x 1 vector
0017 %       H      - nw x nw sparse matrix (optional, all zeros by default)
0018 %       dd, mm - nw x 1 vectors (optional, all ones by default)
0019 %       rh, kk - nw x 1 vectors (optional, all zeros by default)
0020 %
0021 %   These parameters are used as follows to compute F_U(X, CP)
0022 %
0023 %       R  = N*x - rh
0024 %
0025 %               /  kk(i),  R(i) < -kk(i)
0026 %       K(i) = <   0,     -kk(i) <= R(i) <= kk(i)
0027 %               \ -kk(i),  R(i) > kk(i)
0028 %
0029 %       RR = R + K
0030 %
0031 %       U(i) =  /  0, -kk(i) <= R(i) <= kk(i)
0032 %               \  1, otherwise
0033 %
0034 %       DDL(i) = /  1, dd(i) = 1
0035 %                \  0, otherwise
0036 %
0037 %       DDQ(i) = /  1, dd(i) = 2
0038 %                \  0, otherwise
0039 %
0040 %       Dl = diag(mm) * diag(U) * diag(DDL)
0041 %       Dq = diag(mm) * diag(U) * diag(DDQ)
0042 %
0043 %       w = (Dl + Dq * diag(RR)) * RR
0044 %
0045 %       F_U(X, CP) = 1/2 * w'*H*w + Cw'*w
0046 %
0047 %   See also OPT_MODEL, ADD_COSTS, BUILD_COST_PARAMS, GET_COST_PARAMS.
0048 
0049 %   MATPOWER
0050 %   $Id: compute_cost.m 2048 2012-05-03 12:59:07Z cvs $
0051 %   by Ray Zimmerman, PSERC Cornell
0052 %   Copyright (c) 2008-2012 by Power System Engineering Research Center (PSERC)
0053 %
0054 %   This file is part of MATPOWER.
0055 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0056 %
0057 %   MATPOWER is free software: you can redistribute it and/or modify
0058 %   it under the terms of the GNU General Public License as published
0059 %   by the Free Software Foundation, either version 3 of the License,
0060 %   or (at your option) any later version.
0061 %
0062 %   MATPOWER is distributed in the hope that it will be useful,
0063 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0064 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0065 %   GNU General Public License for more details.
0066 %
0067 %   You should have received a copy of the GNU General Public License
0068 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0069 %
0070 %   Additional permission under GNU GPL version 3 section 7
0071 %
0072 %   If you modify MATPOWER, or any covered work, to interface with
0073 %   other modules (such as MATLAB code and MEX-files) available in a
0074 %   MATLAB(R) or comparable environment containing parts covered
0075 %   under other licensing terms, the licensors of MATPOWER grant
0076 %   you additional permission to convey the resulting work.
0077 
0078 if nargin < 3
0079     cp = get_cost_params(om);
0080 elseif nargin < 4
0081     cp = get_cost_params(om, name);
0082 else
0083     cp = get_cost_params(om, name, idx);
0084 end
0085 
0086 [N, Cw, H, dd, rh, kk, mm] = deal(cp.N, cp.Cw, cp.H, cp.dd, ...
0087                                     cp.rh, cp.kk, cp.mm);
0088 nw = size(N, 1);
0089 r = N * x - rh;                 %% Nx - rhat
0090 iLT = find(r < -kk);            %% below dead zone
0091 iEQ = find(r == 0 & kk == 0);   %% dead zone doesn't exist
0092 iGT = find(r > kk);             %% above dead zone
0093 iND = [iLT; iEQ; iGT];          %% rows that are Not in the Dead region
0094 iL = find(dd == 1);             %% rows using linear function
0095 iQ = find(dd == 2);             %% rows using quadratic function
0096 LL = sparse(iL, iL, 1, nw, nw);
0097 QQ = sparse(iQ, iQ, 1, nw, nw);
0098 kbar = sparse(iND, iND, [   ones(length(iLT), 1);
0099                             zeros(length(iEQ), 1);
0100                             -ones(length(iGT), 1)], nw, nw) * kk;
0101 rr = r + kbar;                  %% apply non-dead zone shift
0102 M = sparse(iND, iND, mm(iND), nw, nw);  %% dead zone or scale
0103 diagrr = sparse(1:nw, 1:nw, rr, nw, nw);
0104 
0105 %% linear rows multiplied by rr(i), quadratic rows by rr(i)^2
0106 w = M * (LL + QQ * diagrr) * rr;
0107 
0108 f = full((w' * H * w) / 2 + Cw' * w);

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