fmincon and Getting "Solution" Before Convergence Tolerance is Hit

7 views (last 30 days)
I'm running a relatively large and slow fitting routine through fmincon – there are 12 bounded parameters. As a result, on the available machines we have, the fit has taken several days, so far. I also set the tolerance to be way lower than we need, for our purposes. As a result, when the function output is plotted (to the Optimization PlotFcns window), the function output has effectively converged (each iteration does not change, to within what I need). Is there a way to stop the script and get the current "solution" (the current values fmincon has), without re-running the script?
I'm looking to save several days' worth of time here.

Accepted Answer

Adam Danz
Adam Danz on 9 Sep 2019
Edited: Adam Danz on 10 Sep 2019
From the Editor, press the pause button (first panel below). Execution will eventually pause but it may not be within the function that contains your call to fmincon() so you'll need to step through using the 'step' button in the 2nd panel below until you get to the fmincon outputs. You can save all of the existing variables so you can later reload them and start where you left off.
[addendum]
If the editor is not already open, you can press the "New Script" button from the Home tab to open the editor and then follow the instructions above.
To determine the current file and line number, run dbstack() from the command window.
  4 Comments
Blenndrman
Blenndrman on 10 Sep 2019
Good to know about various tricks to store variables along the way. I'm certainly not a programmer by trade, and have never built such an unwieldy code before. Thanks!
Adam Danz
Adam Danz on 10 Sep 2019
Edited: Adam Danz on 10 Sep 2019
For these kinds of long running processes it's a good idea to occasionally save the data from within the program. For example, every 'n' iterations save all variables so that if the program breaks 10 days from now you can start off at the most recently saved state.
Another good idea is to set the "pause on errors" option (from the Editor in r2019a, click the down arror under 'Run" and select Pause on errors). That way if there is an error the code will pause at the error before breaking. All of the internal variables will hold their values which gives you a chance to do a postmortem in order to debug and fix the problem (and again, save the variables).
A guy in my lab had to run an analysis that took 2 years on a cluster of computers just because a reviewer asked for an additional figure. You can be sure that program backed up the progress occasionally.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 9 Sep 2019
When you use fmincon(), you can designate an output function separately from plot functions.
The outputfcn will get the current state for each iteration. You can save copies of the best solution you found so far (or all solutions, if you prefer). Then when you click STOP in the plotfcn, the outputfcn should in theory execute once with 'done' state, which you can then use to save the values to somewhere the user can get to.
  1 Comment
Blenndrman
Blenndrman on 10 Sep 2019
So this will be for the next time I run the code, certainly – I'll be able to set this up. I guess I was wondering about a currently-still-running script. Thanks, though, It's a useful tool that I didn't realize existed.

Sign in to comment.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!