Generating Hessian of the Lagrangian with dynamic number of nonlinear constraints in Fmincon

3 次查看(过去 30 天)
Hi,
(I posted this in stack exchange 2 days back but didn't get a response. Hope it works here).
I'm using interior point algorithm for solving a nonlinear optimization problem and want to provide Hessian of the Lagrangian as part of fmincon to speed up the process (running couple of thousand variations for different sets of parameters)
The workflow that I'm designing has the (nonlinear) objective function, form of linear and nonlinear constraints each in separate function files. I use a master script which has the fmincon. Since i'm running different variations of the problems, sometimes there may be zero nonlinear equality & zero nonlinear inequality constraints and in other scenarios, nonzero number of nonlinear equality/inequality constraints. In short the number of nonlinear constraints are dynamic and stored in the 2 variables "NumOfNonLinEqConstr" and "NumOfNonLinInEqConstr"
I have taken a look at Generating Hessian using Symbolic toolbox and few other web-pages but cannot see an example where the Hessian of the Lagrangian is constructed for dynamic number of constraints. In the referred matlab webpage example, like in one variation I tried replacing 10 with NumOfNonLinInEqConstr bu it doesn't work as matlabFunction does not work on cell data type. Can anybody provide a working example for constructing hessian of the lagrangian with dynamic number of nonlinear constraints
Relevant code lines from the matlab link provided below: My question is how could I avoid hard-coding 10 in the below code (and also avoid manual addition of the product of hessian of the constraint and lambda.ineqnonlin 10 times)?
hessc = cell(1, 10);
for i = 1:10
hessc{i} = jacobian(gradc(:,i),x);
end
for i = 1:10
ii = num2str(i);
thename = ['hessc',ii];
filename = [currdir,thename,'.m'];
matlabFunction(hessc{i},'file',filename,'vars',{x});
end
function H = hessfinal(X,lambda)
%
% Call the function hessenergy to start
H = hessenergy(X);
% Add the Lagrange multipliers * the constraint Hessians
H = H + hessc1(X) * lambda.ineqnonlin(1);
H = H + hessc2(X) * lambda.ineqnonlin(2);
H = H + hessc3(X) * lambda.ineqnonlin(3);
H = H + hessc4(X) * lambda.ineqnonlin(4);
H = H + hessc5(X) * lambda.ineqnonlin(5);
H = H + hessc6(X) * lambda.ineqnonlin(6);
H = H + hessc7(X) * lambda.ineqnonlin(7);
H = H + hessc8(X) * lambda.ineqnonlin(8);
H = H + hessc9(X) * lambda.ineqnonlin(9);
H = H + hessc10(X) * lambda.ineqnonlin(10);
end

采纳的回答

Matt J
Matt J 2014-7-21
编辑:Matt J 2014-7-21
The number of nonlinear equality and inequality constraints can be recovered from the lambda structure passed to hessfinal, and could look something like the following,
function H = hessfinal(X,lambda)
lam_ineq=[lambda.ineqnonlin].';
lam_eq=[lambda.eqnonlin].';
M_ineq=length(lam_ineq); %Same as "NumOfNonLinInEqConstr"
M_eq=length(lam_eq); %Same as "NumOfNonLinEqConstr"
for i=M_ineq:-1:1
H_ineq(:,:,i)=... %Hessians of all the inequality constraints
end
for i=M_eq:-1:1
H_eq(:,:,i)=... %Hessians of all the equality constraints
end
H= reshape(H_ineq,[],M_ineq)*lambda_ineq + ...
reshape(H_eq,[],M_eq)*lambda_eq;
H= reshape(H,numel(X),[]) +Hessian_objective(X);
end
  5 个评论
Hari
Hari 2014-7-23
One additional ques regarding usage of anonymous function for passing extra parameter.
My (limited) analysis indicated that for cases when I can create a malabFunction with the function saved in specific location using 'file' option and if for that same scenario if I use anonymous function, then the named matlabFunction is faster than anonymous func approach. If it is true, my question is, can the functionality of anonymous function used to pass extra parameters be replicated by non-anonymous function using matlabFunction 'file' approach? (I couldn't work out the syntax unless I use the subs function within matlabFunction definition)
Matt J
Matt J 2014-7-23
编辑:Matt J 2014-7-26
I'm not really sure I understand the comparison that was done. As best I can tell, you have 2 implementations of the same function(s):
  1. As a symbolic expression generated with the Symbolic Math Toolbox.
  2. As an mfile, generated from 1. using matlabFunction using optimization flags.
I would definitely expect the optimized mfile implementation to run faster than the symbolic implenetation.
If you were to then wrap either 1. or 2. in an anonymous function, for the purpose of passing extra parameters, I wouldn't expect either implementation's performance to change very much. That's assuming the computations done inside the function are considerably more time-consuming than the overhead needed to call the function itself. Otherwise, if your objective function and gradient calculations are trivially fast, you're at a point where pursuing speed optimization doesn't really make much sense.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Least Squares 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by