any trick to stop fminsearch early?

14 visualizaciones (últimos 30 días)
Jeff Miller
Jeff Miller el 17 de Nov. de 2019
Comentada: Jeff Miller el 17 de Nov. de 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.

Respuesta aceptada

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato el 17 de Nov. de 2019
Editada: Thiago Henrique Gomes Lobato el 17 de Nov. de 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 comentario
Jeff Miller
Jeff Miller el 17 de Nov. de 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);

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 17 de Nov. de 2019
fminsearch permits options that include Outputfcn and that function can signal to terminate.
  1 comentario
Jeff Miller
Jeff Miller el 17 de Nov. de 2019
Thanks. Yes, OutputFcn lets me do what I want.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by