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

5 ビュー (過去 30 日間)
DavidZ
DavidZ 2014 年 8 月 13 日
コメント済み: Rustem Devletov 2019 年 4 月 26 日
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 Center および File ExchangeSolver 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