EVAL_NLN_CONSTRAINT_HESS Builds and returns Hessian of nonlinear constraints. D2G = OM.EVAL_NLN_CONSTRAINT_HESS(X, LAM, ISEQ) Builds the Hessian of the full set of nonlinear equality or inequality constraints for given values of the optimization vector X and dual variables LAM, based on constraints added by ADD_NLN_CONSTRAINT. g(X) = 0 h(X) <= 0 Example: d2G = om.eval_nln_constraint_hess(x, lam, 1) d2H = om.eval_nln_constraint_hess(x, lam, 0) See also OPT_MODEL, ADD_NLN_CONSTRAINT, EVAL_NLN_CONSTRAINT.
0001 function d2G = eval_nln_constraint_hess(om, x, lam, iseq) 0002 %EVAL_NLN_CONSTRAINT_HESS Builds and returns Hessian of nonlinear constraints. 0003 % D2G = OM.EVAL_NLN_CONSTRAINT_HESS(X, LAM, ISEQ) 0004 % Builds the Hessian of the full set of nonlinear equality or inequality 0005 % constraints for given values of the optimization vector X and dual 0006 % variables LAM, based on constraints added by ADD_NLN_CONSTRAINT. 0007 % 0008 % g(X) = 0 0009 % h(X) <= 0 0010 % 0011 % Example: 0012 % d2G = om.eval_nln_constraint_hess(x, lam, 1) 0013 % d2H = om.eval_nln_constraint_hess(x, lam, 0) 0014 % 0015 % See also OPT_MODEL, ADD_NLN_CONSTRAINT, EVAL_NLN_CONSTRAINT. 0016 0017 % MATPOWER 0018 % Copyright (c) 2008-2017, Power Systems Engineering Research Center (PSERC) 0019 % by Ray Zimmerman, PSERC Cornell 0020 % 0021 % This file is part of MATPOWER. 0022 % Covered by the 3-clause BSD License (see LICENSE file for details). 0023 % See https://matpower.org for more info. 0024 0025 %% get constraint type 0026 if iseq %% equality constraints 0027 om_nlx = om.nle; 0028 else %% inequality constraints 0029 om_nlx = om.nli; 0030 end 0031 0032 %% initialize d2G (use transpose for speed on older versions of MATLAB) 0033 d2Gt = sparse(om.var.N, om.var.N); 0034 0035 %% calls to substruct() are relatively expensive, so we pre-build the 0036 %% structs for addressing cell and numeric array fields, updating only 0037 %% the subscripts before use 0038 sc = struct('type', {'.', '{}'}, 'subs', {'', 1}); %% cell array field 0039 sn = sc; sn(2).type = '()'; %% num array field 0040 0041 %% fill in each piece 0042 for k = 1:om_nlx.NS 0043 name = om_nlx.order(k).name; 0044 idx = om_nlx.order(k).idx; 0045 if isempty(idx) 0046 if ~isfield(om_nlx.data.hess, name) 0047 continue; %% skip, there is no function handle stored here, 0048 %% the function value for this named set was included 0049 %% in the value computed by a previous named set 0050 end 0051 N = om_nlx.idx.N.(name); %% number of constraint functions 0052 %% evaluated for this named set 0053 if isfield(om_nlx.data.include, name) 0054 N = N + sum(om_nlx.data.include.(name).N); 0055 end 0056 else 0057 % (calls to substruct() are relatively expensive ... 0058 % sn = substruct('.', name, '()', idx); 0059 % sc = substruct('.', name, '{}', idx); 0060 % ... so replace them with these more efficient lines) 0061 sn(1).subs = name; 0062 sn(2).subs = idx; 0063 sc(1).subs = name; 0064 sc(2).subs = idx; 0065 N = subsref(om_nlx.idx.N, sn); 0066 end 0067 if N %% non-zero number of rows 0068 if isempty(idx) 0069 d2G_fcn = om_nlx.data.hess.(name); %% Hessian fcn for kth constraint set 0070 i1 = om_nlx.idx.i1.(name); %% starting row index 0071 iN = i1 + N - 1; %% ending row index 0072 vs = om_nlx.data.vs.(name); %% var sets 0073 else 0074 d2G_fcn = subsref(om_nlx.data.hess, sc); %% Hessian fcn for kth constraint set 0075 i1 = subsref(om_nlx.idx.i1, sn); %% starting row index 0076 iN = subsref(om_nlx.idx.iN, sn); %% ending row index 0077 vs = subsref(om_nlx.data.vs, sc); %% var sets 0078 end 0079 xx = om.varsets_x(x, vs); 0080 d2Gk = d2G_fcn(xx, lam(i1:iN)); %% evaluate kth Hessian 0081 0082 nk = size(d2Gk, 2); 0083 if isempty(vs) %% all rows of x 0084 if nk == om.var.N 0085 d2Gkt_full = d2Gk'; 0086 else %% must have added vars since adding 0087 %% this constraint set 0088 d2Gk_all_cols = sparse(nk, om.var.N); 0089 d2Gk_all_cols(:, 1:nk) = d2Gk; 0090 d2Gkt_full = sparse(om.var.N, om.var.N); 0091 d2Gkt_full(:, 1:nk) = d2Gk_all_cols'; 0092 end 0093 else %% selected rows of x 0094 jj = om.varsets_idx(vs); %% indices for var set 0095 d2Gk_all_cols = sparse(nk, om.var.N); 0096 d2Gk_all_cols(:, jj) = d2Gk; 0097 d2Gkt_full = sparse(om.var.N, om.var.N); 0098 d2Gkt_full(:, jj) = d2Gk_all_cols'; 0099 end 0100 d2Gt = d2Gt + d2Gkt_full; 0101 end 0102 end 0103 d2G = d2Gt';