Filter löschen
Filter löschen

Interrupting an optimization with a GUI

4 Ansichten (letzte 30 Tage)
Matthew
Matthew am 31 Jul. 2012
So this is a bit of a two part question. Part of my program involves using fminsearch to calculate values such that the RMS residual using those values is as small as possible for my system. Since the models can become very complex, the optimizations in some cases can be up to 15 minutes long. In the past, the values being used in the current iteration of fminsearch were displayed in the command window so the user could terminate the optimization early if he/she noticed something going wrong by pressing control-c. However, since I am compiling this program into an executable, I have neither the use of the command window to display the current values nor the ability to terminate the optimization with control-c. Instead, I am displaying the current values to a text box in the GUI.
This brings me to my first question: right now the code to set the string of the textbox is placed inside the output function of fminsearch so that the text box is updated every iteration (well, every 10 iterations since every iteration would be excessive). I could not figure out how to pass the handles structure to the output function without causing an error in the optimization so I have currently settled for storing the handles structure to a global variable and then invoking the global variable inside the output function. This works and the text box displays what I want it too but I am wondering if there is a better method to pass the handles structure to the output function without using a global variable or if there is another method to display the values used mid-optimization in the GUI.
My second question involves actually interrupting the optimization. After searching in the Matlab documentation I found this:
---
Stopping an Optimization Based on GUI Input. If you design a GUI to perform optimizations, you can make the output function stop an optimization when a user clicks a Stop button on the GUI. The following code shows how to do this, assuming that the Stop button callback stores the value true in the optimstop field of a handles structure called hObject:
function stop = outfun(x,optimValues,state)
stop = false;
% Check if user has requested to stop the optimization.
stop = getappdata(hObject,'optimstop');
---
As I understand this, I need to create a push button in the GUI. Then, under the button's callback, I add the code
setappdata(handles.optimizestop,'optimstop',true)
and then in the output function I add
stop=getappdata(handles.optimizestop,'optimstop')
However, I have tried this and the optimization just continues to completion.
I am still somewhat new to Matlab so I apologize if I am missing something obvious but any help would be greatly appreciated.
  2 Kommentare
Walter Roberson
Walter Roberson am 31 Jul. 2012
You might want to add a disp() or the like inside the stop function, in order to determine whether the stop function is being called.
Matthew
Matthew am 1 Aug. 2012
For what I can tell, it's getting called. I added a pause statement to the end of the output function so I can see what happens at each iteration of the optimization. Before pressing the stop button, the value of both the stop flag and the appdata are 0. Then, when I press the stop button, a statement is displayed in the command window, and the value of the appdata in the stop button callback is 1 but then on the subsequent iteration of the optimization, the value of the appdata is back to 0.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by