Home > matpower6.0 > @opt_model > build_cost_params.m

build_cost_params

PURPOSE ^

BUILD_COST_PARAMS Builds and saves the full generalized cost parameters.

SYNOPSIS ^

function om = build_cost_params(om, force)

DESCRIPTION ^

BUILD_COST_PARAMS  Builds and saves the full generalized cost parameters.
   OM = BUILD_COST_PARAMS(OM)
   OM = BUILD_COST_PARAMS(OM, 'force')

   Builds the full set of cost parameters from the individual named
   sub-sets added via ADD_COSTS. Skips the building process if it has
   already been done, unless a second input argument is present.

   These cost parameters can be retrieved by calling GET_COST_PARAMS
   and the user-defined costs evaluated by calling COMPUTE_COST.

   See also OPT_MODEL, ADD_COSTS, GET_COST_PARAMS, COMPUTE_COST.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function om = build_cost_params(om, force)
0002 %BUILD_COST_PARAMS  Builds and saves the full generalized cost parameters.
0003 %   OM = BUILD_COST_PARAMS(OM)
0004 %   OM = BUILD_COST_PARAMS(OM, 'force')
0005 %
0006 %   Builds the full set of cost parameters from the individual named
0007 %   sub-sets added via ADD_COSTS. Skips the building process if it has
0008 %   already been done, unless a second input argument is present.
0009 %
0010 %   These cost parameters can be retrieved by calling GET_COST_PARAMS
0011 %   and the user-defined costs evaluated by calling COMPUTE_COST.
0012 %
0013 %   See also OPT_MODEL, ADD_COSTS, GET_COST_PARAMS, COMPUTE_COST.
0014 
0015 %   MATPOWER
0016 %   Copyright (c) 2008-2016, Power Systems Engineering Research Center (PSERC)
0017 %   by Ray Zimmerman, PSERC Cornell
0018 %
0019 %   This file is part of MATPOWER.
0020 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0021 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0022 
0023 if nargin > 1 || ~isfield(om.cost.params, 'N')
0024     %% initialize parameters
0025     nw = om.cost.N;
0026     nnzN = 0;
0027     nnzH = 0;
0028     s = struct('type', {'.', '{}'}, 'subs', {'', 1});
0029     for k = 1:om.cost.NS
0030         name = om.cost.order(k).name;
0031         idx  = om.cost.order(k).idx;
0032         if isempty(idx)
0033             nnzN = nnzN + nnz(om.cost.data.N.(name));
0034             if isfield(om.cost.data.H, name)
0035                 nnzH = nnzH + nnz(om.cost.data.H.(name));
0036             end
0037         else
0038             % (calls to substruct() are relatively expensive ...
0039             % s = substruct('.', name, '{}', idx);
0040             % ... so replace it with these more efficient lines)
0041             s(1).subs = name;
0042             s(2).subs = idx;
0043             nnzN = nnzN + nnz(subsref(om.cost.data.N, s));
0044             if isfield(om.cost.data.H, name)
0045                 nnzH = nnzH + nnz(subsref(om.cost.data.H, s));
0046             end
0047         end
0048     end
0049     NNt = sparse([], [], [], om.var.N, nw, nnzN);   %% use NN transpose for speed
0050     Cw = zeros(nw, 1);
0051     H = sparse([], [], [], nw, nw, nnzH);   %% default => no quadratic term
0052     dd = ones(nw, 1);                       %% default => linear
0053     rh = Cw;                                %% default => no shift
0054     kk = Cw;                                %% default => no dead zone
0055     mm = dd;                                %% default => no scaling
0056     
0057     %% fill in each piece
0058     s2 = s;
0059     s(2).type = '()';
0060     s1 = s;
0061     for k = 1:om.cost.NS
0062         name = om.cost.order(k).name;
0063         idx  = om.cost.order(k).idx;
0064         if isempty(idx)
0065             N = om.cost.idx.N.(name);       %% number of rows to add
0066         else
0067             % (calls to substruct() are relatively expensive ...
0068             % s1 = substruct('.', name, '()', idx);
0069             % s2 = substruct('.', name, '{}', idx);
0070             % ... so replace them with these more efficient lines)
0071             s1(1).subs = name;
0072             s1(2).subs = idx;
0073             s2(1).subs = name;
0074             s2(2).subs = idx;
0075             N = subsref(om.cost.idx.N, s1); %% number of rows to add
0076         end
0077         if N                                %% non-zero number of rows to add
0078             if isempty(idx)
0079                 Nk = om.cost.data.N.(name);         %% N for kth cost set
0080                 i1 = om.cost.idx.i1.(name);         %% starting row index
0081                 iN = om.cost.idx.iN.(name);         %% ending row index
0082                 vsl = om.cost.data.vs.(name);       %% var set list
0083             else
0084                 Nk = subsref(om.cost.data.N, s2);   %% N for kth cost set
0085                 i1 = subsref(om.cost.idx.i1, s1);   %% starting row index
0086                 iN = subsref(om.cost.idx.iN, s1);   %% ending row index
0087                 vsl = subsref(om.cost.data.vs, s2); %% var set list
0088             end
0089             if isempty(vsl)         %% full rows
0090                 if size(Nk,2) == om.var.N
0091                     NNt(:, i1:iN) = Nk';     %% assign as columns in transpose for speed
0092                 else                %% must have added vars since adding
0093                                     %% this cost set
0094                     NNt(1:size(Nk,2), i1:iN) = Nk';  %% assign as columns in transpose for speed
0095                 end
0096             else                    %% selected columns
0097                 kN = 0;                             %% initialize last col of Nk used
0098                 Ni = sparse(N, om.var.N);
0099                 for v = 1:length(vsl)
0100                     % (calls to substruct() are relatively expensive ...
0101                     % s = substruct('.', vsl(v).name, '()', vsl(v).idx);
0102                     % ... so replace it with these more efficient lines)
0103                     s(1).subs = vsl(v).name;
0104                     s(2).subs = vsl(v).idx;
0105                     j1 = subsref(om.var.idx.i1, s); %% starting column in N
0106                     jN = subsref(om.var.idx.iN, s); %% ending column in N
0107                     k1 = kN + 1;                    %% starting column in Nk
0108                     kN = kN + subsref(om.var.idx.N, s);%% ending column in Nk
0109                     Ni(:, j1:jN) = Nk(:, k1:kN);
0110                 end
0111                 NNt(:, i1:iN) = Ni';    %% assign as columns in transpose for speed
0112             end
0113 
0114             if isempty(idx)
0115                 Cw(i1:iN) = om.cost.data.Cw.(name);
0116                 if isfield(om.cost.data.H, name)
0117                     H(i1:iN, i1:iN) = om.cost.data.H.(name);
0118                 end
0119                 if isfield(om.cost.data.dd, name)
0120                     dd(i1:iN) = om.cost.data.dd.(name);
0121                 end
0122                 if isfield(om.cost.data.rh, name)
0123                     rh(i1:iN) = om.cost.data.rh.(name);
0124                 end
0125                 if isfield(om.cost.data.kk, name)
0126                     kk(i1:iN) = om.cost.data.kk.(name);
0127                 end
0128                 if isfield(om.cost.data.mm, name)
0129                     mm(i1:iN) = om.cost.data.mm.(name);
0130                 end
0131             else
0132                 Cw(i1:iN) = subsref(om.cost.data.Cw, s2);
0133                 if isfield(om.cost.data.H, name) && ~isempty(subsref(om.cost.data.H, s2))
0134                     H(i1:iN, i1:iN) = subsref(om.cost.data.H, s2);
0135                 end
0136                 if isfield(om.cost.data.dd, name) && ~isempty(subsref(om.cost.data.dd, s2))
0137                     dd(i1:iN) = subsref(om.cost.data.dd, s2);
0138                 end
0139                 if isfield(om.cost.data.rh, name) && ~isempty(subsref(om.cost.data.rh, s2))
0140                     rh(i1:iN) = subsref(om.cost.data.rh, s2);
0141                 end
0142                 if isfield(om.cost.data.kk, name) && ~isempty(subsref(om.cost.data.kk, s2))
0143                     kk(i1:iN) = subsref(om.cost.data.kk, s2);
0144                 end
0145                 if isfield(om.cost.data.mm, name) && ~isempty(subsref(om.cost.data.mm, s2))
0146                     mm(i1:iN) = subsref(om.cost.data.mm, s2);
0147                 end
0148             end
0149         end
0150     end
0151 
0152     %% save in object
0153     om.cost.params = struct( ...
0154         'N', NNt', 'Cw', Cw, 'H', H, 'dd', dd, 'rh', rh, 'kk', kk, 'mm', mm );
0155 end

Generated on Fri 16-Dec-2016 12:45:37 by m2html © 2005