Home > matpower5.1 > @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-2015 by Power System Engineering Research Center (PSERC)
0017 %   by Ray Zimmerman, PSERC Cornell
0018 %
0019 %   $Id: build_cost_params.m 2644 2015-03-11 19:34:22Z ray $
0020 %
0021 %   This file is part of MATPOWER.
0022 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0023 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0024 
0025 if nargin > 1 || ~isfield(om.cost.params, 'N')
0026     %% initialize parameters
0027     nw = om.cost.N;
0028     nnzN = 0;
0029     nnzH = 0;
0030     s = struct('type', {'.', '{}'}, 'subs', {'', 1});
0031     for k = 1:om.cost.NS
0032         name = om.cost.order(k).name;
0033         idx  = om.cost.order(k).idx;
0034         if isempty(idx)
0035             nnzN = nnzN + nnz(om.cost.data.N.(name));
0036             if isfield(om.cost.data.H, name)
0037                 nnzH = nnzH + nnz(om.cost.data.H.(name));
0038             end
0039         else
0040             % (calls to substruct() are relatively expensive ...
0041             % s = substruct('.', name, '{}', idx);
0042             % ... so replace it with these more efficient lines)
0043             s(1).subs = name;
0044             s(2).subs = idx;
0045             nnzN = nnzN + nnz(subsref(om.cost.data.N, s));
0046             if isfield(om.cost.data.H, name)
0047                 nnzH = nnzH + nnz(subsref(om.cost.data.H, s));
0048             end
0049         end
0050     end
0051     NNt = sparse([], [], [], om.var.N, nw, nnzN);   %% use NN transpose for speed
0052     Cw = zeros(nw, 1);
0053     H = sparse([], [], [], nw, nw, nnzH);   %% default => no quadratic term
0054     dd = ones(nw, 1);                       %% default => linear
0055     rh = Cw;                                %% default => no shift
0056     kk = Cw;                                %% default => no dead zone
0057     mm = dd;                                %% default => no scaling
0058     
0059     %% fill in each piece
0060     s2 = s;
0061     s(2).type = '()';
0062     s1 = s;
0063     for k = 1:om.cost.NS
0064         name = om.cost.order(k).name;
0065         idx  = om.cost.order(k).idx;
0066         if isempty(idx)
0067             N = om.cost.idx.N.(name);       %% number of rows to add
0068         else
0069             % (calls to substruct() are relatively expensive ...
0070             % s1 = substruct('.', name, '()', idx);
0071             % s2 = substruct('.', name, '{}', idx);
0072             % ... so replace them with these more efficient lines)
0073             s1(1).subs = name;
0074             s1(2).subs = idx;
0075             s2(1).subs = name;
0076             s2(2).subs = idx;
0077             N = subsref(om.cost.idx.N, s1); %% number of rows to add
0078         end
0079         if N                                %% non-zero number of rows to add
0080             if isempty(idx)
0081                 Nk = om.cost.data.N.(name);         %% N for kth cost set
0082                 i1 = om.cost.idx.i1.(name);         %% starting row index
0083                 iN = om.cost.idx.iN.(name);         %% ending row index
0084                 vsl = om.cost.data.vs.(name);       %% var set list
0085             else
0086                 Nk = subsref(om.cost.data.N, s2);   %% N for kth cost set
0087                 i1 = subsref(om.cost.idx.i1, s1);   %% starting row index
0088                 iN = subsref(om.cost.idx.iN, s1);   %% ending row index
0089                 vsl = subsref(om.cost.data.vs, s2); %% var set list
0090             end
0091             if isempty(vsl)         %% full rows
0092                 if size(Nk,2) == om.var.N
0093                     NNt(:, i1:iN) = Nk';     %% assign as columns in transpose for speed
0094                 else                %% must have added vars since adding
0095                                     %% this cost set
0096                     NNt(1:size(Nk,2), i1:iN) = Nk';  %% assign as columns in transpose for speed
0097                 end
0098             else                    %% selected columns
0099                 kN = 0;                             %% initialize last col of Nk used
0100                 Ni = sparse(N, om.var.N);
0101                 for v = 1:length(vsl)
0102                     % (calls to substruct() are relatively expensive ...
0103                     % s = substruct('.', vsl(v).name, '()', vsl(v).idx);
0104                     % ... so replace it with these more efficient lines)
0105                     s(1).subs = vsl(v).name;
0106                     s(2).subs = vsl(v).idx;
0107                     j1 = subsref(om.var.idx.i1, s); %% starting column in N
0108                     jN = subsref(om.var.idx.iN, s); %% ending column in N
0109                     k1 = kN + 1;                    %% starting column in Nk
0110                     kN = kN + subsref(om.var.idx.N, s);%% ending column in Nk
0111                     Ni(:, j1:jN) = Nk(:, k1:kN);
0112                 end
0113                 NNt(:, i1:iN) = Ni';    %% assign as columns in transpose for speed
0114             end
0115 
0116             if isempty(idx)
0117                 Cw(i1:iN) = om.cost.data.Cw.(name);
0118                 if isfield(om.cost.data.H, name)
0119                     H(i1:iN, i1:iN) = om.cost.data.H.(name);
0120                 end
0121                 if isfield(om.cost.data.dd, name)
0122                     dd(i1:iN) = om.cost.data.dd.(name);
0123                 end
0124                 if isfield(om.cost.data.rh, name)
0125                     rh(i1:iN) = om.cost.data.rh.(name);
0126                 end
0127                 if isfield(om.cost.data.kk, name)
0128                     kk(i1:iN) = om.cost.data.kk.(name);
0129                 end
0130                 if isfield(om.cost.data.mm, name)
0131                     mm(i1:iN) = om.cost.data.mm.(name);
0132                 end
0133             else
0134                 Cw(i1:iN) = subsref(om.cost.data.Cw, s2);
0135                 if isfield(om.cost.data.H, name) && ~isempty(subsref(om.cost.data.H, s2))
0136                     H(i1:iN, i1:iN) = subsref(om.cost.data.H, s2);
0137                 end
0138                 if isfield(om.cost.data.dd, name) && ~isempty(subsref(om.cost.data.dd, s2))
0139                     dd(i1:iN) = subsref(om.cost.data.dd, s2);
0140                 end
0141                 if isfield(om.cost.data.rh, name) && ~isempty(subsref(om.cost.data.rh, s2))
0142                     rh(i1:iN) = subsref(om.cost.data.rh, s2);
0143                 end
0144                 if isfield(om.cost.data.kk, name) && ~isempty(subsref(om.cost.data.kk, s2))
0145                     kk(i1:iN) = subsref(om.cost.data.kk, s2);
0146                 end
0147                 if isfield(om.cost.data.mm, name) && ~isempty(subsref(om.cost.data.mm, s2))
0148                     mm(i1:iN) = subsref(om.cost.data.mm, s2);
0149                 end
0150             end
0151         end
0152     end
0153 
0154     %% save in object
0155     om.cost.params = struct( ...
0156         'N', NNt', 'Cw', Cw, 'H', H, 'dd', dd, 'rh', rh, 'kk', kk, 'mm', mm );
0157 end

Generated on Fri 20-Mar-2015 18:23:34 by m2html © 2005