How to Stop Fmincon from GUI

3 次查看(过去 30 天)
How to stop fmincon from a pushbutton in a Matlab Gui?
The explaination posted here at the botton does not work, and can not work, as hObject never is passed into the outputfunction
function stop = outfun(x,optimValues,state)
stop = false;
% Check if user has requested to stop the optimization.
stop = getappdata(hObject,'optimstop');
Is there a way to stop the execution of the fmincon algorithm from a Pushbutton? Thanks a lot. Ravi

采纳的回答

Geoff Hayes
Geoff Hayes 2015-12-10
Ravi - I suspect that the example is only meant to provide an idea of how to do this given that you could have created your GUI either using GUIDE or using the programmatic approach. If you are using GUIDE, then you can always pass in hObject as an additional input to the outfun. For example, suppose that you have a start push button to start the optimization where you create the options (for fmincon) and start the optimization. For example,
function start_pushbutton(hObject, eventdata, handles)
% do some stuff
% create the options
opts = optimoptions(@fmincon,'OutputFcn','{@outfun,hObject});
% start the optimization
Then, you would change the function signature of your outfun to be
function stop = outfun(x,optimValues,state,hObject)
Here, it is assumed that you have created the outfun in the same m-file as for your GUI. Now rather than using getappdata (which forces you to have called setappdata to update hObject with the state of stop), I would use guidata to get the handles structure as
function stop = outfun(x,optimValues,state,hObject)
stop = false;
handles = guidata(hObject);
if isfield(handles,'optimstop')
stop = handles.optimstop;
end
And so your stop button callback would look something like
function stop_pushbutton(hObject, eventdata, handles)
handles.optimstop = true;
guidata(hObject,handles);
In the above we set the optimstop field and then save that change to the handles structure.
I haven't tried the above (since I don't have the Optimization Toolbox) but I think that it can be made to work for your implementation.
  6 个评论
Pappu Murthy
Pappu Murthy 2017-2-4
This solution didn't work at all for me. Pushing the stop button is not able to interupt the process and update the stop value at all. so the optimization process keeps continuing to the end.
Kevin Thümmler
Kevin Thümmler 2021-3-17
There are intermediate execution during the optimization process. You can visualize it when you plot the number of times calling the output function and the number of times calling the objective function or you find it in the doc as well. I had the same problem. My solution was to set the error to 0 directly in the objective function, so that the optimizer thinks the job is done and stops the process

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by