Set function tolerance patternsearch

6 views (last 30 days)
Hello,
I'm trying to do a minimization using patternsearch, here's a portion of my code.
lb=[Pmin Tmin];
ub=[Pmax Tmax];
objective=@(PT)objective_meemum(PT,bulk,pos,an);
options=optimoptions('patternsearch','Display','iter',...
'MeshTolerance',1e-10, 'ScaleMesh', false,'PlotFcn',@psplotbestf,...
'UseCompletePoll',true);
[PTopt(i,:), distance(i)]=patternsearch(objective,PT0,[],[],[],[],lb,ub,[], options);
In fact, I would want my the optimization to stop when the objective function gives a value <7e-3 (in this case the value is the distance variable).
Is there a way to do this? It seems like the FcnTolerance option does not work with patternsearch.
Thank you for the help,
Guillaume
  1 Comment
Guillaume Bonnet
Guillaume Bonnet on 8 Jul 2020
Edited: Guillaume Bonnet on 8 Jul 2020
Ok, so I figured I could use the Output Function to create a threshold and stop the optimization:
function stop = stopfunction(optimValues)
stop=false;
if optimValues.fval <= 7e-3
stop = true;
end
end
and then use this function in my main code by changing my options to:
options=optimoptions('patternsearch','Display','iter',...
'MeshTolerance',1e-10, 'ScaleMesh', false,...
'PlotFcn',@psplotbestf,...
'UseCompletePoll',true,'ConstraintTolerance',0.1,'OutputFcn',@stopfunction);
This seems to be the way to solve my problem.
However, when running the code, I get an error message:
"Error using stopfunction
Too many output arguments."
Any idea where the problem might be?

Sign in to comment.

Accepted Answer

Guillaume Bonnet
Guillaume Bonnet on 8 Jul 2020
Ok, after multiple tests, I actually found the answer to my question, here it is:
function [stop,options,optchanged] = stopfn(optimvalues,options,flag)
stop=false;
optchanged=false;
if optimvalues.fval <= 7e-3
stop=true;
end
end
and then:
options=optimoptions('patternsearch','Display','iter',...
'MeshTolerance',1e-10, 'ScaleMesh', false,...
'PlotFcn',@psplotbestf,...
'UseCompletePoll',true,'ConstraintTolerance',0.1,'OutputFcn',@stopfn);

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!