EVAL_LEGACY_COST Evaluates individual or full set of legacy user costs. F = OM.EVAL_LEGACY_COST(X ...) [F, DF] = OM.EVAL_LEGACY_COST(X ...) [F, DF, D2F] = OM.EVAL_LEGACY_COST(X ...) [F, DF, D2F] = OM.EVAL_LEGACY_COST(X, NAME) [F, DF, D2F] = OM.EVAL_LEGACY_COST(X, NAME, IDX_LIST) Evaluates an individual named set or the full set of legacy user costs and their derivatives for a given value of the optimization vector X, based on costs added by ADD_LEGACY_COST. Example: [f, df, d2f] = om.eval_legacy_cost(x) [f, df, d2f] = om.eval_legacy_cost(x, name) [f, df, d2f] = om.eval_legacy_cost(x, name, idx) See also OPT_MODEL, ADD_LEGACY_COST, PARAMS_LEGACY_COST.
0001 function [f, df, d2f] = eval_legacy_cost(om, x, name, idx) 0002 %EVAL_LEGACY_COST Evaluates individual or full set of legacy user costs. 0003 % F = OM.EVAL_LEGACY_COST(X ...) 0004 % [F, DF] = OM.EVAL_LEGACY_COST(X ...) 0005 % [F, DF, D2F] = OM.EVAL_LEGACY_COST(X ...) 0006 % [F, DF, D2F] = OM.EVAL_LEGACY_COST(X, NAME) 0007 % [F, DF, D2F] = OM.EVAL_LEGACY_COST(X, NAME, IDX_LIST) 0008 % Evaluates an individual named set or the full set of legacy user 0009 % costs and their derivatives for a given value of the optimization vector 0010 % X, based on costs added by ADD_LEGACY_COST. 0011 % 0012 % Example: 0013 % [f, df, d2f] = om.eval_legacy_cost(x) 0014 % [f, df, d2f] = om.eval_legacy_cost(x, name) 0015 % [f, df, d2f] = om.eval_legacy_cost(x, name, idx) 0016 % 0017 % See also OPT_MODEL, ADD_LEGACY_COST, PARAMS_LEGACY_COST. 0018 0019 % MATPOWER 0020 % Copyright (c) 2008-2017, Power Systems Engineering Research Center (PSERC) 0021 % by Ray Zimmerman, PSERC Cornell 0022 % 0023 % This file is part of MATPOWER. 0024 % Covered by the 3-clause BSD License (see LICENSE file for details). 0025 % See https://matpower.org for more info. 0026 0027 if om.cost.N 0028 done = 0; 0029 0030 %% collect cost parameters 0031 if nargin < 3 %% full set 0032 [cp, vs] = om.params_legacy_cost(); 0033 elseif nargin < 4 || isempty(idx) %% name, no idx provided 0034 dims = size(om.cost.idx.i1.(name)); 0035 if prod(dims) == 1 %% simple named set 0036 [cp, vs] = om.params_legacy_cost(name); 0037 elseif nargout == 1 %% indexing required, recurse 0038 f = 0; %% initialize cumulative cost 0039 idx = num2cell(ones(size(dims))); %% initialize idx 0040 while ~done %% call eval_legacy_cost() recursively 0041 f = f + om.eval_legacy_cost(x, name, idx); 0042 0043 %% increment idx 0044 D = length(dims); 0045 idx{D} = idx{D} + 1; %% increment last dimension 0046 for d = D:-1:2 %% increment next dimension, if necessary 0047 if idx{d} > dims(d) 0048 idx{d} = 1; 0049 idx{d-1} = idx{d-1} + 1; 0050 end 0051 end 0052 if idx{1} > dims(1) %% check if done 0053 done = 1; 0054 end 0055 end 0056 else 0057 error('@opt_model/eval_legacy_cost: legacy cost set ''%s'' requires an IDX_LIST arg when requesting DF output', name) 0058 end 0059 else %% indexed named set 0060 [cp, vs] = om.params_legacy_cost(name, idx); 0061 end 0062 0063 if ~done 0064 %% assemble appropriately-sized x vector 0065 xx = om.varsets_x(x, vs, 'vector'); 0066 0067 %% compute function & derivatives 0068 if nargout == 1 0069 f = opf_legacy_user_cost_fcn(xx, cp); 0070 elseif nargout == 2 0071 [f, df] = opf_legacy_user_cost_fcn(xx, cp); 0072 else %% nargout == 3 0073 [f, df, d2f] = opf_legacy_user_cost_fcn(xx, cp); 0074 end 0075 end 0076 else 0077 f = 0; 0078 if nargout > 1 0079 df = []; 0080 if nargout > 2 0081 d2f = []; 0082 end 0083 end 0084 end