Why DerivativeCheck would be allowed to be on while there is no GradObj?

1 view (last 30 days)
When GradObj is off, and DerivativeCheck is on, fmincon starts to run without any error or warning. Why would this be the case? That is, if there is no gradient supplied, there cannot be derivattive check. So why would MATLAB allow the DerivativeCheck to be on if there is no gradient available?
options = optimset(...
'MaxFunEvals',10000,...
'TolFun',1e-4,...
'TolX',1e-4,...
'Algorithm','sqp',...
'GradObj','off',...
'DerivativeCheck','on',...
'Display','iter',...
'FunValCheck','off');

Accepted Answer

Matt J
Matt J on 29 Jan 2023
Here's a reason why you might want to allow DerivativeCheck to be on even when the analytical gradient computations are all off. Suppose the user has coded 6 different implementations of the gradient calculation and wants to compare them all for speed. Suppose also that she wants to run the optimization with finite difference gradient computations as a baseline for the comparison.
In this situation, you would want to have DerivativeCheck='on' for 6 different runs. To run the finite differencing baseline, however, your proposal would force the user to set both SpecifyObjectiveGradient=false and DerivativeCheck='off'. Some people would prefer just to set SpecifyObjectiveGradient=false and not to have to fuss with additional settings for just that one case.
  1 Comment
Snoopy
Snoopy on 29 Jan 2023
Edited: Snoopy on 29 Jan 2023
There are many suppositions in this example. I also do not see how it negates my point really. I consider that no message is harmful in the third example above where settings are simply nonsense. That is period for me. Thanks for all the replies.

Sign in to comment.

More Answers (3)

John D'Errico
John D'Errico on 27 Jan 2023
Moved: Walter Roberson on 27 Jan 2023
You can set other options that will be ignored if they are not applicable. Would you rather have the code fail for no reason on a problem that has a solution?
  3 Comments
Snoopy
Snoopy on 29 Jan 2023
We do not seem to disagree. I do not mean that there is a calculation issue and it is clear that there is none. All I mean is that MATLAB could guide the setting of the options and throw a message when settings by the user are nonsense. Why do I propose this? Because MATLAB, unlike other crap alternatives, do guide the user in many aspects of coding. So why not here too? That is one of the many things that makes MATLAB user frinedly and professional.

Sign in to comment.


Matt J
Matt J on 28 Jan 2023
Edited: Matt J on 28 Jan 2023
I would say that no warning is strictly necessary because one of two cases are possible:
(1) DerivativeCheck='on' was intentional, but GradObj='off' was unintentional. If so, you will be warned that something is wrong by the fact that no derivative check output appears.
(2) GradObj='off' was intentional, but DerivativeCheck='on' was unintentional. If so, it would be your wish that the optimization proceed without a derivative check and, indeed, that is what happens.
That said, I do agree that an explicit warning message in case (1) would be more aesthetic.
  6 Comments
Matt J
Matt J on 29 Jan 2023
Edited: Matt J on 29 Jan 2023
The user sees that things still run fine with nonsense settings.
It might be overstating things slightly to say that the settings are "nonsense". The main purpose of DerivativeCheck is to prevent the optimization from using analytical gradient calculations that are faulty. However, if SpecifyObjectiveGradient is turned off, the user is not supplying the gradient, and so there is no danger this will occur. So, arguably, the purpose of DerivativeCheck has been fulfilled and the optimization should just proceed care-free.
As I mention in my other answer, it would be nice if Matlab could warn you when you shut off analytical gradient calculations accidentally, or forget to turn them back on. But that is simply not always something Matlab can detect, and in any case is not something DerivativeCheck was primarily designed to protect against.

Sign in to comment.


Matt J
Matt J on 28 Jan 2023
Edited: Matt J on 28 Jan 2023
So why would MATLAB allow the DerivativeCheck to be on if there is no gradient available?
Because maybe SpecifyConstraintGradient=true, even if SpecyObjectiveGradient=false. In your example, this is not the case. In general however, there are 4 possible combinations of settings for SpecifyObjectiveGradient and SpecifyConstraintGradient and only one of these combinations is suspicious when DerivativeCheck='on'. Therefore, there is very limited protection a warning message could provide against users accidentally turning off analytical gradient computation.
  3 Comments
Snoopy
Snoopy on 29 Jan 2023
I would lke to emphasize that I do read the very elaborate responses and I can only thank for that. The discussion has added value I believe. It is only that I sometimes found it difficult how I should interpret some responses but now they are all clear to me. Your last post explains it all and I only agree. My whole or only point is about the last example. In that case we both seem to agree that a warning would not hurt. It would be in line with the overall ecosystem of MATLAB where MATLAB nicely gives warnings for many different suspicous user mistakes while other crap alternatives do not.

Sign in to comment.

Categories

Find more on Startup and Shutdown 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!