필터 지우기
필터 지우기

Can I supply a gradient to fmincon when my objective function is anonymous?

조회 수: 5 (최근 30일)
I am using fmincon and would like to supply a gradient for the objective function to speed up convergence. The trouble is I use an anonymous objective function so I can pass parameters to my real objective function. I believe the anonymous function only picks up the first argument of my real objective function, which is the function value and not the gradient. The basic structure of my code is:
if true
%%%%%%%%%%%%%%%%%%%
[f,gradf]=realobjective(parameters,x]
f=blah;
gradf=blah;
end
%%%%%%%%%%%%%%%%%%%
options=optimoptions(@fmincon,'Algorithm','active-set','MaxFunEvals',1E5,'MaxIter',1E5,'TolFun',1E-6,'TolX',1E-6,'GradConstr','on','GradObj','on');
xguess=0;
func=@(x)realobjective(parameters,x)
argmin=fmincon(func,xguess,[],[],[],[],0,1000,nonlconstr,options)
end
So I think fmincon is not using the gradient I have defined in the real objective function. I'd be incredibly grateful for any advice.
David
  댓글 수: 1
Rustem Devletov
Rustem Devletov 2019년 4월 26일
Hey, friend! I'm a fourth year student in Russia, currently writing my thesis. My supervisor asked me to provide gradient to fmincon
I have triend smth like this
function F = rosenbrockwithgrad(x)
F = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
if nargout > 1 % gradient required
g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
But he said that he wants it to be found in another file. How can I implement this? How do I find gradient in one file and pass it to another one?

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

채택된 답변

Matt J
Matt J 2014년 8월 13일
편집: Matt J 2014년 8월 13일
What you've posted looks fine. Call nargout inside realobjective (e.g., at a breakpoint), to test whether fmincon is calling it with two output arguments.
  댓글 수: 5
Matt J
Matt J 2014년 8월 13일
Did you try running with 'DerivativeCheck' set to 'on' to validate your gradient calculation? Sounds like your constraint gradient calculation is working okay, but the objective function gradient calculation might have errors.
Rustem Devletov
Rustem Devletov 2019년 4월 26일
Hey, friend! I'm a fourth year student in Russia, currently writing my thesis. My supervisor asked me to provide gradient to fmincon
I have triend smth like this
function F = rosenbrockwithgrad(x)
F = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
if nargout > 1 % gradient required
g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
But he said that he wants it to be found in another file. How can I implement this? How do I find gradient in one file and pass it to another one?

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

추가 답변 (1개)

DavidZ
DavidZ 2014년 8월 13일
Thanks both. It's very mysterious; when I turn the gradient calls off fmincon does converge (slowly) to the right answer (I know it's right as for certain parameter values I can work out an analytical solution to compare to the numerical solution fmincon gives) but when I turn the gradient calls on it never converges. I tried the debugging tip Matt helpfully suggested and fmincon is calling withing nargout 2 some of the time, but not all of it. Any thoughts? Thanks again for your help.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by