Home > matpower7.0 > lib > @opt_model > params_quad_cost.m

params_quad_cost

PURPOSE ^

PARAMS_QUAD_COST Returns the cost parameters for quadratic costs.

SYNOPSIS ^

function [Q, c, k, vs] = params_quad_cost(om, name, idx)

DESCRIPTION ^

PARAMS_QUAD_COST  Returns the cost parameters for quadratic costs.
   [Q, C] = OM.PARAMS_QUAD_COST()
   [Q, C] = OM.PARAMS_QUAD_COST(NAME)
   [Q, C] = OM.PARAMS_QUAD_COST(NAME, IDX)
   [Q, C, K] = OM.PARAMS_QUAD_COST(...)
   [Q, C, K, VS] = OM.PARAMS_QUAD_COST(...)

   With no input parameters, it assembles and returns the parameters
   for the aggregate quadratic cost from all quadratic cost sets added
   using ADD_QUAD_COST. The values of these parameters are cached
   for subsequent calls. The parameters are Q, C, and optionally K,
   where the quadratic cost is of the form
       F(X) = 1/2 * X'*Q*X + C'*X + K

   If a NAME is provided then it simply returns the parameters for the
   corresponding named set. Likewise for indexed named sets specified
   by NAME and IDX. In this case, Q and K may be vectors, corresponding
   to a cost function of the form
       F(X) = 1/2 * Q .* X.^2 + C .* X + K

   An optional 4th output argument VS indicates the variable sets used by
   this cost set. The size of Q and C will be consistent with VS.

   See also OPT_MODEL, ADD_QUAD_COST.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [Q, c, k, vs] = params_quad_cost(om, name, idx)
0002 %PARAMS_QUAD_COST  Returns the cost parameters for quadratic costs.
0003 %   [Q, C] = OM.PARAMS_QUAD_COST()
0004 %   [Q, C] = OM.PARAMS_QUAD_COST(NAME)
0005 %   [Q, C] = OM.PARAMS_QUAD_COST(NAME, IDX)
0006 %   [Q, C, K] = OM.PARAMS_QUAD_COST(...)
0007 %   [Q, C, K, VS] = OM.PARAMS_QUAD_COST(...)
0008 %
0009 %   With no input parameters, it assembles and returns the parameters
0010 %   for the aggregate quadratic cost from all quadratic cost sets added
0011 %   using ADD_QUAD_COST. The values of these parameters are cached
0012 %   for subsequent calls. The parameters are Q, C, and optionally K,
0013 %   where the quadratic cost is of the form
0014 %       F(X) = 1/2 * X'*Q*X + C'*X + K
0015 %
0016 %   If a NAME is provided then it simply returns the parameters for the
0017 %   corresponding named set. Likewise for indexed named sets specified
0018 %   by NAME and IDX. In this case, Q and K may be vectors, corresponding
0019 %   to a cost function of the form
0020 %       F(X) = 1/2 * Q .* X.^2 + C .* X + K
0021 %
0022 %   An optional 4th output argument VS indicates the variable sets used by
0023 %   this cost set. The size of Q and C will be consistent with VS.
0024 %
0025 %   See also OPT_MODEL, ADD_QUAD_COST.
0026 
0027 %   MATPOWER
0028 %   Copyright (c) 2017, Power Systems Engineering Research Center (PSERC)
0029 %   by Ray Zimmerman, PSERC Cornell
0030 %
0031 %   This file is part of MATPOWER.
0032 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0033 %   See https://matpower.org for more info.
0034 
0035 if nargin > 1       %% individual set
0036     if nargin < 3
0037         idx = {};
0038     end
0039     if isempty(idx)                 %% name, no index provided
0040         if prod(size(om.qdc.idx.i1.(name))) == 1    %% simple named set
0041             Q = om.qdc.data.Q.(name);
0042             c = om.qdc.data.c.(name);
0043             k = om.qdc.data.k.(name);
0044             if nargout > 3
0045                 vs = om.qdc.data.vs.(name);
0046             end
0047         else                                        %% indexing required
0048             error('@opt_model/params_quad_cost: quadratic cost set ''%s'' requires an IDX arg', name);
0049         end
0050     else                            %% indexed named set
0051         % (calls to substruct() are relatively expensive ...
0052         % s = substruct('.', name, '{}', idx);
0053         % ... so replace it with these more efficient lines)
0054         sc = struct('type', {'.', '{}'}, 'subs', {name, idx});
0055         Q = subsref(om.qdc.data.Q, sc);
0056         c = subsref(om.qdc.data.c, sc);
0057         k = subsref(om.qdc.data.k, sc);
0058         if nargout > 3
0059             vs = subsref(om.qdc.data.vs, sc);
0060         end
0061     end
0062 else                %% aggregate
0063     cache = om.qdc.params;
0064     if isempty(cache)       %% build the aggregate
0065         nx = om.var.N;          %% number of variables
0066         Qt = sparse(nx, nx);    %% transpose of quadratic coefficients
0067         c = zeros(nx, 1);       %% linear coefficients
0068         k = 0;                  %% constant term
0069         for i = 1:om.qdc.NS
0070             name = om.qdc.order(i).name;
0071             idx  = om.qdc.order(i).idx;
0072             [Qk, ck, kk, vs] = om.params_quad_cost(name, idx);
0073             haveQ = ~isempty(Qk);
0074             havec = ~isempty(ck);
0075             nk = max(size(Qk, 1), size(ck, 1));     %% size of Qk and/or ck
0076             if isempty(vs)
0077                 if nk == nx     %% full size
0078                     if size(Qk, 2) == 1     %% Qk is a column vector
0079                         Qkt_full = spdiags(Qk, 0, nx, nx);
0080                     elseif haveQ            %% Qk is a matrix
0081                         Qkt_full = Qk';
0082                     end
0083                     if havec
0084                         ck_full = ck;
0085                     end
0086                 else            %% vars added since adding this cost set
0087                     if size(Qk, 2) == 1     %% Qk is a column vector
0088                         Qkt_full = sparse(1:nk, 1:nk, Qk, nx, nx);
0089                     elseif haveQ            %% Qk is a matrix
0090                         Qk_all_cols = sparse(nk, nx);
0091                         Qk_all_cols(:, 1:nk) = Qk;
0092                         Qkt_full(:, 1:nk) = Qk_all_cols';
0093                     end
0094                     if havec
0095                         ck_full = zeros(nx, 1);
0096                         ck_full(1:nk) = ck;
0097                     end
0098                 end
0099             else
0100                 jj = om.varsets_idx(vs);    %% indices for var set
0101                 if size(Qk, 2) == 1     %% Qk is a column vector
0102                     Qkt_full = sparse(jj, jj, Qk, nx, nx);
0103                 elseif haveQ            %% Qk is a matrix
0104                     Qk_all_cols = sparse(nk, nx);
0105                     Qk_all_cols(:, jj) = Qk;
0106                     Qkt_full = sparse(nx, nx);
0107                     Qkt_full(:, jj) = Qk_all_cols';
0108                 end
0109                 if havec
0110                     ck_full = zeros(nx, 1);
0111                     ck_full(jj) = ck;
0112                 end
0113             end
0114             if haveQ
0115                 Qt = Qt + Qkt_full;
0116             end
0117             if havec
0118                 c = c + ck_full;
0119             end
0120             k = k + sum(kk);
0121         end
0122         Q = Qt';
0123 
0124         %% cache aggregated parameters
0125         om.qdc.params = struct('Q', Q, 'c', c, 'k', k);
0126     else                    %% return cached values
0127         Q = cache.Q;
0128         c = cache.c;
0129         k = cache.k;
0130     end
0131     if nargout > 3
0132         vs = {};
0133     end
0134 end

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005