How to create STOP button in GUI to stop my GA optimization?

I have a GUI with "Optimize" and "Stop" pushbuttons. Pressing "Optimize" will call "ga" command to run an optimization process with iteration display being on. Now, I'd like for the user to be able to stop the GA process whenever "Stop" button is clicked. Could you please help me how to invoke GA from GUI to stop and then retrieve the best set of parameters GA has found so far? Thanks!

 Accepted Answer

Have your GUI call ga with an output function. As explained in the documentation,
"The output function returns the following arguments to ga:
  • state — Structure containing information about the current generation. The State Structure describes the fields of state. To stop the iterations, set state.StopFlag to a nonempty string."
Alan Weiss
MATLAB mathematical toolbox documentation

3 Comments

Thanks, Alan!
I have a followup question regarding GUI callbacks: I created an output function to check whenever the "STOP" button is clicked or not, and if so to set state.StopFlag to true. Now, how can I make sure that the GUI updates and send out the STOP button click callback right away (compared to waiting for previous task to finish and then sending out the callback request)?
You'll need to add a drawnow to flush the event queue (I.e. capture the mouse click event).
The optimizer will still finish the iteration it's on either way.
Thank you, Sean! it's now working by putting the "drawnow" at the begining of my OutputFcn to flush event queue as you mentioned.

Sign in to comment.

More Answers (0)

Asked:

on 4 Mar 2015

Commented:

on 5 Mar 2015

Community Treasure Hunt

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

Start Hunting!