Okay, I guess I just figured out what going on. I just set the DerivativeCheck option to off then everything is fine now. I guess there is some DerivativeCheck if I didn't set it explicitly. And therefore, if the user-supplied gradient is slightly off with the result by finite difference, fmincon will use finite difference anyway.
fmincon not using user-supplied gradient to optimize
2 views (last 30 days)
Show older comments
Hi Everyone,
I am facing a problem using fmincon function. I am trying to use my user-supplied gradient instead of the one gained by finite difference method. But, the thing is fmincon is not using it even if I have supplied it as the second output in my objective function. And a portion of my code is below:
%
options = optimoptions('fmincon','Algorithm','sqp','UseParallel', true,'Display','iter','GradObj','on','TolX',1e-4,'TolFun',1e-4,'DerivativeCheck', 'on');
[r,fval,exitflag,output] = fmincon(@Objfunc,lb+0.01,[],[],Aeq,beq,lb,ub,[],options)
function [ F, gradF ] = Objfunc(r)
% Some complex function omitted here
end
I have tried several method to make it work:
1. When I turn on 'DerivativeCheck' option, the result showed that the difference is larger than 1e-6. Therfore, it won't pass the check.
2. So, I looked into my user-supplied gradient and sort it from largest to smallest and compare to the optimization result using finite differnce. I am pretty sure now that my gradient is correct (Though the scale is very small which is possibly the reason why it couldn't pass the DerivativeCheck)
3. Therefore, I turned to edit the validateFirstDerivatives.m file to enlarge my tolerance.This time the DerivativeCheck passed and therefore fmincon used my user-supplied gradient and the result perfectly matched what I got without supplying it.
So, the question is: Can I avoid DerivativeCheck to enforce fmincon using my user-supplied gradient? Because DerivativeCheck need a lot of time to pass and I have a need to speed up my optimization process. Since the objective function takes time to compute, using the user-supplied gradient is necessary for me.
If someone could kindly tell me the way to solve this issue? Thank you for your reply.
Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!