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