How to pass user-defined gradients for anonymous non-linear constraints in fmincon
4 ビュー (過去 30 日間)
古いコメントを表示
Hi to all,
I have a non-linear optimization problem where both non-linear constraints and objective functions have to be defined anonymously. I want to use fmincon as efficiently as possible by providing user-supplied gradients for both gradients and objective, and user-supplied Hessian for objective.
How can I pass these gradients for anonymously defined functions?
Thanks in advance
Ebru
6 件のコメント
Matt J
2022 年 9 月 6 日
You should just modify non_linear_constraints to (optionally) return the gradients:
function [c,ceq,gc,gceq]=non_linear_constraints(x,model_disservice,alpha,q,n,k,X_design);
c=zeros(q-1,1);
ceq=[];
z_a = norminv(1-(alpha/2));
B_predict=1;
[y_hat_disservice,mspe_disservice] = SKpredict_new(model_disservice,x,B_predict);
c(1)= y_hat_disservice-z_a * sqrt(mspe_disservice);
if nargout>3 %check if fmincon really asked for the gradients
gc=...
gceq=[];
end
end
採用された回答
Matt J
2022 年 9 月 6 日
編集済み: Matt J
2022 年 9 月 6 日
I suspect you've misled yourself into believing the functions must be defined anonymously. I don't know of any situation where that truly is the case. Nevertheless, you can accomplish what you ask as in the example below. The convergence of fmincon will benefit from providing the gradients, however, you will never be able to get the same efficiency as a non-anonymous function implemention. This is because an anonymous function must always compute the function gradients whenever it is invoked, even at steps where fmincon requires only the function values.
nonlcon=@(x)f(nargout,{x^2-1,tan(x)+3,2*x,sec(x)^2});
[c,ceq]=nonlcon(1)
c =
0
ceq =
4.5574
[c,ceq,gc,gceq]=nonlcon(1)
c =
0
ceq =
4.5574
gc =
2
gceq =
3.4255
function varargout=f(n,c)
varargout=c(1:n);
end
3 件のコメント
Matt J
2022 年 9 月 6 日
For example, some of the fmincon algorithms involve line search steps (e.g. SQP and, with certain settings, interior-point). Line searches will involve repeated evaluation of the function, but not its derivatives.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!