Home > matpower5.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 %   $Id: build_cost_params.m 2137 2013-03-29 18:52:50Z ray $
0017 %   by Ray Zimmerman, PSERC Cornell
0018 %   Copyright (c) 2008-2012 by Power System Engineering Research Center (PSERC)
0019 %
0020 %   This file is part of MATPOWER.
0021 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0022 %
0023 %   MATPOWER is free software: you can redistribute it and/or modify
0024 %   it under the terms of the GNU General Public License as published
0025 %   by the Free Software Foundation, either version 3 of the License,
0026 %   or (at your option) any later version.
0027 %
0028 %   MATPOWER is distributed in the hope that it will be useful,
0029 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0030 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0031 %   GNU General Public License for more details.
0032 %
0033 %   You should have received a copy of the GNU General Public License
0034 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0035 %
0036 %   Additional permission under GNU GPL version 3 section 7
0037 %
0038 %   If you modify MATPOWER, or any covered work, to interface with
0039 %   other modules (such as MATLAB code and MEX-files) available in a
0040 %   MATLAB(R) or comparable environment containing parts covered
0041 %   under other licensing terms, the licensors of MATPOWER grant
0042 %   you additional permission to convey the resulting work.
0043 
0044 if nargin > 1 || ~isfield(om.cost.params, 'N')
0045     %% initialize parameters
0046     nw = om.cost.N;
0047     nnzN = 0;
0048     nnzH = 0;
0049     for k = 1:om.cost.NS
0050         name = om.cost.order(k).name;
0051         idx  = om.cost.order(k).idx;
0052         if isempty(idx)
0053             nnzN = nnzN + nnz(om.cost.data.N.(name));
0054             if isfield(om.cost.data.H, name)
0055                 nnzH = nnzH + nnz(om.cost.data.H.(name));
0056             end
0057         else
0058             s = substruct('.', name, '{}', idx);
0059             nnzN = nnzN + nnz(subsref(om.cost.data.N, s));
0060             if isfield(om.cost.data.H, name)
0061                 nnzH = nnzH + nnz(subsref(om.cost.data.H, s));
0062             end
0063         end
0064     end
0065     NNt = sparse([], [], [], om.var.N, nw, nnzN);   %% use NN transpose for speed
0066     Cw = zeros(nw, 1);
0067     H = sparse([], [], [], nw, nw, nnzH);   %% default => no quadratic term
0068     dd = ones(nw, 1);                       %% default => linear
0069     rh = Cw;                                %% default => no shift
0070     kk = Cw;                                %% default => no dead zone
0071     mm = dd;                                %% default => no scaling
0072     
0073     %% fill in each piece
0074     for k = 1:om.cost.NS
0075         name = om.cost.order(k).name;
0076         idx  = om.cost.order(k).idx;
0077         if isempty(idx)
0078             N = om.cost.idx.N.(name);       %% number of rows to add
0079         else
0080             s1 = substruct('.', name, '()', idx);
0081             s2 = substruct('.', name, '{}', idx);
0082             N = subsref(om.cost.idx.N, s1); %% number of rows to add
0083         end
0084         if N                                %% non-zero number of rows to add
0085             if isempty(idx)
0086                 Nk = om.cost.data.N.(name);         %% N for kth cost set
0087                 i1 = om.cost.idx.i1.(name);         %% starting row index
0088                 iN = om.cost.idx.iN.(name);         %% ending row index
0089                 vsl = om.cost.data.vs.(name);       %% var set list
0090             else
0091                 Nk = subsref(om.cost.data.N, s2);   %% N for kth cost set
0092                 i1 = subsref(om.cost.idx.i1, s1);   %% starting row index
0093                 iN = subsref(om.cost.idx.iN, s1);   %% ending row index
0094                 vsl = subsref(om.cost.data.vs, s2); %% var set list
0095             end
0096             if isempty(vsl)         %% full rows
0097                 if size(Nk,2) == om.var.N
0098                     NNt(:, i1:iN) = Nk';     %% assign as columns in transpose for speed
0099                 else                %% must have added vars since adding
0100                                     %% this cost set
0101                     NNt(1:size(Nk,2), i1:iN) = Nk';  %% assign as columns in transpose for speed
0102                 end
0103             else                    %% selected columns
0104                 kN = 0;                             %% initialize last col of Nk used
0105                 Ni = sparse(N, om.var.N);
0106                 for v = 1:length(vsl)
0107                     s = substruct('.', vsl(v).name, '()', vsl(v).idx);
0108                     j1 = subsref(om.var.idx.i1, s); %% starting column in N
0109                     jN = subsref(om.var.idx.iN, s); %% ending column in N
0110                     k1 = kN + 1;                    %% starting column in Nk
0111                     kN = kN + subsref(om.var.idx.N, s);%% ending column in Nk
0112                     Ni(:, j1:jN) = Nk(:, k1:kN);
0113                 end
0114                 NNt(:, i1:iN) = Ni';    %% assign as columns in transpose for speed
0115             end
0116 
0117             if isempty(idx)
0118                 Cw(i1:iN) = om.cost.data.Cw.(name);
0119                 if isfield(om.cost.data.H, name)
0120                     H(i1:iN, i1:iN) = om.cost.data.H.(name);
0121                 end
0122                 if isfield(om.cost.data.dd, name)
0123                     dd(i1:iN) = om.cost.data.dd.(name);
0124                 end
0125                 if isfield(om.cost.data.rh, name)
0126                     rh(i1:iN) = om.cost.data.rh.(name);
0127                 end
0128                 if isfield(om.cost.data.kk, name)
0129                     kk(i1:iN) = om.cost.data.kk.(name);
0130                 end
0131                 if isfield(om.cost.data.mm, name)
0132                     mm(i1:iN) = om.cost.data.mm.(name);
0133                 end
0134             else
0135                 Cw(i1:iN) = subsref(om.cost.data.Cw, s2);
0136                 if isfield(om.cost.data.H, name) && ~isempty(subsref(om.cost.data.H, s2))
0137                     H(i1:iN, i1:iN) = subsref(om.cost.data.H, s2);
0138                 end
0139                 if isfield(om.cost.data.dd, name) && ~isempty(subsref(om.cost.data.dd, s2))
0140                     dd(i1:iN) = subsref(om.cost.data.dd, s2);
0141                 end
0142                 if isfield(om.cost.data.rh, name) && ~isempty(subsref(om.cost.data.rh, s2))
0143                     rh(i1:iN) = subsref(om.cost.data.rh, s2);
0144                 end
0145                 if isfield(om.cost.data.kk, name) && ~isempty(subsref(om.cost.data.kk, s2))
0146                     kk(i1:iN) = subsref(om.cost.data.kk, s2);
0147                 end
0148                 if isfield(om.cost.data.mm, name) && ~isempty(subsref(om.cost.data.mm, s2))
0149                     mm(i1:iN) = subsref(om.cost.data.mm, s2);
0150                 end
0151             end
0152         end
0153     end
0154 
0155     %% save in object
0156     om.cost.params = struct( ...
0157         'N', NNt', 'Cw', Cw, 'H', H, 'dd', dd, 'rh', rh, 'kk', kk, 'mm', mm );
0158 end

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