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
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
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
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.
Ebru Angun
Ebru Angun 2022년 9월 6일
I see, thanks for the info. I will check interior-point since this is the algorithm I am planning to use.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by