Interrupting GUI during optimization

Hi, I'm designing an app with appdesigner that runs an optimization process. I'm trying to put in a push button that would interrupt the optimization because it can sometimes be quite lengthy. Here is my code:
% Button pushed function: SolveButton
function Solve(app, event)
Objective= @(x) MultiObjectiveFun(x,Ar,BODmin,BODmax,Ecoli_min,Ecoli_max,Ecoli_objective,BOD_objective,Depth1,Depth2,Depth3,Depth4);
A = [];
B = [];
Aeq = [];
Beq = [];
LB= [20 20 20 ];
UB= [60 60 60];
[a]=gamultiobj(Objective,3,A,B,Aeq,Beq,LB,UB);
end
This function would be the callback to stop the optimization: % Button pushed function: StopButton
function Stop(app, event)
end
I've tried a few things but can't seem to get it to interrupt the optimization. I would like it to end the optimization completely. Allowing me to change the inputs and then press solve again to restart. I don't know where to write in my pause or how to write in my stop button.

Answers (1)

You need to add an output function option to your gamultiobj call, and the callback should look something like this:
function [state,options,optchanged] = gaoutfun(options,state,flag)
optchanged = false;
if stopbutton % put your test for the stop button here
state.StopFlag = 'y'
end
Alan Weiss
MATLAB mathematical toolbox documentation

3 Comments

Hi Alan,
I did what you told me to doand my ga now looks like this:
stopbutton=0;% My solve button gives it an initial value of 0
A = [];
B = [];
Aeq = [];
Beq = [];
LB= [20 20 20 ];
UB= [60 60 60];
options = optimoptions('gamultiobj','OutputFcn',@gaoutfun);
Objective= @(x) MultiObjectiveFun(x,Ar,Depth,prct);
[a]=gamultiobj(Objective,3,A,B,Aeq,Beq,LB,UB,[],options);
I added a private function that is the following:
function [state,options,optchanged] = gaoutfun(options,state,flag)
optchanged = false;
if stopbutton==1
state.StopFlag = 'y'
end
And I have my stop button callback function:
function Stop(app, event)
stopbutton=1
end
I get the following error:
"Undefined function 'gaoutfun' for input arguments of type 'optim.options.GamultiobjOptions'.
Error in gaoutput (line 39)
[state,optnew,changed] = feval(functions{i},options.OutputPlotFcnOptions,state,flag,args{i}{:});
Error in gamultiobjsolve (line 13)
[state,options] = gaoutput(FitnessFcn,options,state,currentState);
Error in gamultiobj (line 303)
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars, ..."
Jan
Jan on 18 Jun 2018
Edited: Jan on 18 Jun 2018
@Francois Daudelin: Please read Tutorial: How to format code in the forum
What does "I added a private function" exactly mean? The error message means, that it cannot be found in the path.
Of course "if stopbutton==1" was pseudo-code. You have to implement this by your own. We cannot guess how you have implemented the button for stopping. But you have to insert the check of the state of the button here. Maybe the button sets a flag in the ApplicationData of the figure (see: setappdata). Defining the variable stopbutton is not sufficient, because it is not visible from the outside.
It cannot find the function gaoutfun for some reason.
Are you using nested functions with stopbutton being a shared variable?

Sign in to comment.

Asked:

on 18 Jun 2018

Edited:

Jan
on 18 Jun 2018

Community Treasure Hunt

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

Start Hunting!