How to know the current input for fminsearch?

I'm using fminsearch for an optimization problem.
I use 100 random inputs and run fminsearch hoping that it doesn't get trapped in local minimas.
Sometimes, fminsearch takes more than usual. I suspect this is because of some bad behavior of some function in my problem that I haven't overseen earlier. I am wondering if I can somehow display the x (input) that fminsearch is working on right now (in real time). This will help me to find out why my problem takes longer around this particular point and if there is some issue with how I defined my problem, correct it (with some if,else,etc).
Thank you!

 Accepted Answer

Matt J
Matt J on 25 Jul 2020
Edited: Matt J on 25 Jul 2020
You can use an OutputFcn,
or simply add a line of code to your objective function to display x, or pause under certain conditions. You could also possibly set conditional or non-conditional breakpoints,
However, I feel I should mention that fminsearch uses the Nelder-Mead algorithm, which is not designed for problems with a large number unknowns. In theory, it is only guaranteed to converge for 1 unknown, and in practice its performance starts to degrade significantly beyond about 6 unknowns. In your case, with 100 unknowns, I am suprised you ever see good fminsearch behavior.

3 Comments

I can't believe this didn't occur to me. Thank you!
Well, I may have not said it clearly. My problem doesn't have 100 unknowns. It has 6 or 7 at most. The 100 that I mentioned was an arbitrary number of times that I run the code (from different initial points) to escape potential local minimas.
I tried other optimization methods of Matlab and fminsearch was the most efficient one. Do you have a suggestion on which method you think may work better?
Do you have a suggestion on which method you think may work better?
Not without seeing your cost function. Presumably you've already tried fminunc?
Yes.
My cost function is a long code and includes some numerical integrations and for loops.

Sign in to comment.

More Answers (1)

For optimizing a simulation or ODE, you may want to try patternsearch and surrogateopt. For surrogateopt you will need to bound all of the variables. These solvers have informative plot functions available.
Alan Weiss
MATLAB mathematical toolbox documentation

6 Comments

Thank you. I gave them a try. patternsearch didn't give me good results. For surrogateopt, I got this error and couldn't find out what I am doing wrong. Do you know what is the problem here? Apparently, the error is not stemmed from my code whatsoever and it is something totally related to plotting results. I tried one of surrogateopt examples (from documentation) and that worked okay.
Index exceeds the number of array elements (0).
Error in bar (line 287)
if strcmp(h(1).BarLayout,'grouped')
Error in optimplotfval>plotvector (line 61)
plotfval = bar(fval);
Error in optimplotfval (line 26)
plotvector(optimValues.iteration,optimValues.fval);
Error in callAllOptimPlotFcns (line 153)
state(i) = feval(plotNames{i},x,optimvalues,flag,varargin{:});
Error in globaloptim.bmo.BlackboxModelOptimizer/outputAndPlots (line 378)
stop = callAllOptimPlotFcns(fcn,xCurrent,optimValues,self.state.displayState)
|| stop;
Error in globaloptim.bmo.BlackboxModelOptimizer/iterativeUpdate (line 297)
stop = self.outputAndPlots();
Error in globaloptim.bmo.BlackboxModelOptimizer/optimize (line 164)
self.interrupt = self.iterativeUpdate(); % Iterative updates
Error in surrogateopt (line 168)
controller = controller.optimize();
Sounds like you might have found a bug. Sorry. Would you be able to provide reproductiion steps or code that causes this error? And let us know what version of MATLAB you are using?
Alan Weiss
MATLAB mathematical toolbox documentation
Sure.
lb = [0.1,0.1,0.1,0.1,0.1];
ub = [1e6,1e5,1e4,1e8,1e8];
objconstr = @my_fun;
[x,fval] = ...
surrogateopt(objconstr,lb,ub);
Are you interested in knowing my function code, too?
Cheers,
Siavash
Yes, I want to reproduce the error, and need working code to do so.
Also, what version of MATLAB are you using?
Alan Weiss
MATLAB mathematical toolbox documentation
Where should I send it to?
An email address or something...?
You can attach it to this discussion or send it to me at aweiss@mathworks.com .
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Products

Release

R2019a

Asked:

on 25 Jul 2020

Commented:

on 7 Aug 2020

Community Treasure Hunt

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

Start Hunting!