any trick to stop fminsearch early?

14 次查看(过去 30 天)
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.

采纳的回答

Thiago Henrique Gomes Lobato
编辑:Thiago Henrique Gomes Lobato 2019-11-17
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 个评论
Jeff Miller
Jeff Miller 2019-11-17
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);

请先登录,再进行评论。

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Solver-Based Nonlinear Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by