ADD_LEGACY_COST Adds a set of user costs to the model. ----- DEPRECATED - Please use one of the following instead: ----- ----- ADD_QUAD_COST, ADD_NLN_COST, INIT_INDEXED_NAME ----- OM.ADD_LEGACY_COST(NAME, CP); OM.ADD_LEGACY_COST(NAME, CP, VARSETS); OM.ADD_LEGACY_COST(NAME, IDX_LIST, CP); OM.ADD_LEGACY_COST(NAME, IDX_LIST, CP, VARSETS); Adds a named block of user-defined costs to the model. Each set is defined by the CP struct described below. All user-defined sets of costs are combined together into a single set of cost parameters in a single CP struct by BULD_COST_PARAMS. This full aggregate set of cost parameters can be retreived from the model by GET_COST_PARAMS. Examples: cp1 = struct('N', N1, 'Cw', Cw1); cp2 = struct('N', N2, 'Cw', Cw2, 'H', H, 'dd', dd, ... 'rh', rh, 'kk', kk, 'mm', mm); om.add_legacy_cost('usr1', cp1, {'Pg', 'Qg', 'z'}); om.add_legacy_cost('usr2', cp2, {'Vm', 'Pg', 'Qg', 'z'}); om.init_indexed_name('c', {2, 3}); for i = 1:2 for j = 1:3 om.add_legacy_cost('c', {i, j}, cp(i,j), ...); end end Let x refer to the vector formed by combining the specified VARSETS, and f_u(x, CP) be the cost at x corresponding to the cost parameters contained in CP, where CP is a struct with the following fields: N - nw x nx sparse matrix (optional, identity matrix by default) 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, PARAMS_LEGACY_COST, EVAL_LEGACY_COST.
0001 function om = add_legacy_cost(om, name, idx, varargin) 0002 %ADD_LEGACY_COST Adds a set of user costs to the model. 0003 % 0004 % ----- DEPRECATED - Please use one of the following instead: ----- 0005 % ----- ADD_QUAD_COST, ADD_NLN_COST, INIT_INDEXED_NAME ----- 0006 % 0007 % OM.ADD_LEGACY_COST(NAME, CP); 0008 % OM.ADD_LEGACY_COST(NAME, CP, VARSETS); 0009 % OM.ADD_LEGACY_COST(NAME, IDX_LIST, CP); 0010 % OM.ADD_LEGACY_COST(NAME, IDX_LIST, CP, VARSETS); 0011 % 0012 % Adds a named block of user-defined costs to the model. Each set is 0013 % defined by the CP struct described below. All user-defined sets of 0014 % costs are combined together into a single set of cost parameters in 0015 % a single CP struct by BULD_COST_PARAMS. This full aggregate set of 0016 % cost parameters can be retreived from the model by GET_COST_PARAMS. 0017 % 0018 % Examples: 0019 % cp1 = struct('N', N1, 'Cw', Cw1); 0020 % cp2 = struct('N', N2, 'Cw', Cw2, 'H', H, 'dd', dd, ... 0021 % 'rh', rh, 'kk', kk, 'mm', mm); 0022 % om.add_legacy_cost('usr1', cp1, {'Pg', 'Qg', 'z'}); 0023 % om.add_legacy_cost('usr2', cp2, {'Vm', 'Pg', 'Qg', 'z'}); 0024 % 0025 % om.init_indexed_name('c', {2, 3}); 0026 % for i = 1:2 0027 % for j = 1:3 0028 % om.add_legacy_cost('c', {i, j}, cp(i,j), ...); 0029 % end 0030 % end 0031 % 0032 % Let x refer to the vector formed by combining the specified VARSETS, 0033 % and f_u(x, CP) be the cost at x corresponding to the cost parameters 0034 % contained in CP, where CP is a struct with the following fields: 0035 % N - nw x nx sparse matrix (optional, identity matrix by default) 0036 % Cw - nw x 1 vector 0037 % H - nw x nw sparse matrix (optional, all zeros by default) 0038 % dd, mm - nw x 1 vectors (optional, all ones by default) 0039 % rh, kk - nw x 1 vectors (optional, all zeros by default) 0040 % 0041 % These parameters are used as follows to compute f_u(x, CP) 0042 % 0043 % R = N*x - rh 0044 % 0045 % / kk(i), R(i) < -kk(i) 0046 % K(i) = < 0, -kk(i) <= R(i) <= kk(i) 0047 % \ -kk(i), R(i) > kk(i) 0048 % 0049 % RR = R + K 0050 % 0051 % U(i) = / 0, -kk(i) <= R(i) <= kk(i) 0052 % \ 1, otherwise 0053 % 0054 % DDL(i) = / 1, dd(i) = 1 0055 % \ 0, otherwise 0056 % 0057 % DDQ(i) = / 1, dd(i) = 2 0058 % \ 0, otherwise 0059 % 0060 % Dl = diag(mm) * diag(U) * diag(DDL) 0061 % Dq = diag(mm) * diag(U) * diag(DDQ) 0062 % 0063 % w = (Dl + Dq * diag(RR)) * RR 0064 % 0065 % f_u(x, CP) = 1/2 * w'*H*w + Cw'*w 0066 % 0067 % See also OPT_MODEL, PARAMS_LEGACY_COST, EVAL_LEGACY_COST. 0068 0069 % MATPOWER 0070 % Copyright (c) 2008-2017, Power Systems Engineering Research Center (PSERC) 0071 % by Ray Zimmerman, PSERC Cornell 0072 % 0073 % This file is part of MATPOWER. 0074 % Covered by the 3-clause BSD License (see LICENSE file for details). 0075 % See https://matpower.org for more info. 0076 0077 if iscell(idx) 0078 cp = varargin{1}; 0079 args = varargin(2:end); 0080 else %% simple named set 0081 cp = idx; 0082 args = varargin; 0083 idx = {}; 0084 end 0085 0086 if isempty(args) 0087 varsets = {}; 0088 else 0089 varsets = args{1}; 0090 end 0091 0092 %% convert varsets from cell to struct array if necessary 0093 varsets = om.varsets_cell2struct(varsets); 0094 nv = om.varsets_len(varsets); %% number of variables 0095 0096 if isfield(cp, 'N') 0097 [nw, nx] = size(cp.N); 0098 else 0099 nw = length(cp.Cw); 0100 nx = nw; 0101 cp.N = speye(nw, nx); 0102 end 0103 0104 %% check sizes 0105 if nx ~= nv 0106 if nw == 0 0107 cp.N = sparse(nw, nx); 0108 else 0109 error('@opt_model/add_legacy_cost: number of columns in N (%d x %d) does not match\nnumber of variables (%d)\n', nw, nx, nv); 0110 end 0111 end 0112 if size(cp.Cw, 1) ~= nw 0113 error('@opt_model/add_legacy_cost: number of rows of Cw (%d x %d) and N (%d x %d) must match\n', size(cp.Cw), nw, nx); 0114 end 0115 if isfield(cp, 'H') && (size(cp.H, 1) ~= nw || size(cp.H, 2) ~= nw) 0116 error('@opt_model/add_legacy_cost: both dimensions of H (%d x %d) must match the number of rows in N (%d x %d)\n', size(cp.H), nw, nx); 0117 end 0118 if isfield(cp, 'dd') && size(cp.dd, 1) ~= nw 0119 error('@opt_model/add_legacy_cost: number of rows of dd (%d x %d) and N (%d x %d) must match\n', size(cp.dd), nw, nx); 0120 end 0121 if isfield(cp, 'rh') && size(cp.rh, 1) ~= nw 0122 error('@opt_model/add_legacy_cost: number of rows of rh (%d x %d) and N (%d x %d) must match\n', size(cp.rh), nw, nx); 0123 end 0124 if isfield(cp, 'kk') && size(cp.kk, 1) ~= nw 0125 error('@opt_model/add_legacy_cost: number of rows of kk (%d x %d) and N (%d x %d) must match\n', size(cp.kk), nw, nx); 0126 end 0127 if isfield(cp, 'mm') && size(cp.mm, 1) ~= nw 0128 error('@opt_model/add_legacy_cost: number of rows of mm (%d x %d) and N (%d x %d) must match\n', size(cp.mm), nw, nx); 0129 end 0130 0131 %% add the legacy cost set 0132 om.add_named_set('cost', name, idx, nw, cp, varsets);