any trick to stop fminsearch early?

14 views (last 30 days)
I am using fminsearch to minimize an error score in fitting a model to many, many data sets.
Often, the error score gets close enough to zero for my purposes, and at that point I'd like fminsearch to stop and go on to the next data set.
Unfortunately, I can't find choices for TolX and TolFun (nor maximum function evaluations, etc) that will reliably stop if and only if the total error score is low enough.
So, my question is whether there is some other way to get fminsearch to stop when my objective function detects that a low enough overall error has been achieved?
Thanks for any suggestions.

Accepted Answer

Thiago Henrique Gomes Lobato
Edited: Thiago Henrique Gomes Lobato on 17 Nov 2019
Yes, there is, you can pass an output function and change the optimization state as soon as your error gets the threshold that you want. Here is a very simple example adapated from the one at mathworks (https://de.mathworks.com/help/matlab/math/output-functions.html) that does what you want and it is also very useful to visualize the stop criteria working:
function [x fval historyT] = myproblem(x0)
historyT = [];
options = optimset('OutputFcn', @myoutput);
[x fval] = fminsearch(@objfun, x0,options);
function stop = myoutput(x,optimvalues,state);
stop = false;
% This block here
AcceptableError = 0.02;
if optimvalues.fval<AcceptableError
state = 'done';
end
if isequal(state,'iter')
historyT = [historyT; x];
end
end
function z = objfun(x)
z = exp(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2));
end
end
If I then call the funciton, I get:
[x fval history] = myproblem([-1 1])
size(history,1)
ans =
15
While if I comment the stop criteria:
% This block here
% AcceptableError = 0.02;
% if optimvalues.fval<AcceptableError
% state = 'done';
% end
[x fval history] = myproblem([-1 1])
size(history,1)
ans =
48
  1 Comment
Jeff Miller
Jeff Miller on 17 Nov 2019
Thanks very much for this detailed answer. For my case, it was sufficient to use:
StopIfErrorSmall = @(x,optimvalues,state) optimvalues.fval<.01;
thisDist.SearchOptions = optimset('OutputFcn',StopIfErrorSmall);

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 17 Nov 2019
fminsearch permits options that include Outputfcn and that function can signal to terminate.

Community Treasure Hunt

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

Start Hunting!