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!