# any trick to stop fminsearch early?

35 views (last 30 days)
Jeff Miller on 17 Nov 2019
Commented: Jeff Miller on 17 Nov 2019
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.

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 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);

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

#### 1 Comment

Jeff Miller on 17 Nov 2019
Thanks. Yes, OutputFcn lets me do what I want.