any trick to stop fminsearch early?

조회 수: 19 (최근 30일)
Jeff Miller
Jeff Miller 2019년 11월 17일
댓글: Jeff Miller 2019년 11월 17일
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일
편집: 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.
  댓글 수: 1
Jeff Miller
Jeff Miller 2019년 11월 17일
Thanks. Yes, OutputFcn lets me do what I want.

댓글을 달려면 로그인하십시오.

카테고리

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