Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
output function figure not updating after each iteration

Subject: output function figure not updating after each iteration

From: Dave Brackett

Date: 6 Sep, 2008 16:21:02

Message: 1 of 3

I have a ga which is hybridised with fminunc:

hybrid_options=optimset('OutputFcn',@fminuncOut,'Display','iter','MaxFunEvals',1000,'MaxIter',1000,'TolFun',1e-6,'TolX',1e-6);

options=gaoptimset('HybridFcn',{@fminunc,hybrid_options},'UseParallel','always','PopInitRange',variable_range,'PopulationSize',25,'CrossoverFraction',0.5,'Generations',10,'StallGenLimit',50,'StallTimeLimit',40,'SelectionFcn',@selectionstochunif,'MutationFcn',{@mutationuniform,0.7},'CrossoverFcn',@crossoverscattered,'PlotFcns',@gaplotbestf},'PlotInterval',1);

[x,fval,exitflag,output,final_pop]=ga(fitness_fcn,no_variables,[],[],[],[],lower_bound,upper_bound,[],options);


I have used fminuncOut.m as an output function from the matlab ga toolbox webinar with this intention of getting this:
http://www.mathworks.com/company/newsletters/digest/sept04/images/results5b_wl.jpg

This then produces a plot of the GA history followed by the fminunc history. However, it only updates the figure once the optimisation has ended whereas I want it to update after each iteration. Does anyone know how I can make it do so? Thanks.


fminuncOut.m consists of the following code (with the annotation part removed):

function stop = fminuncOut(x,optimvalues,state)

persistent fig gaIter
stop = false;

switch state

    case 'init'
        fig = findobj(0,'type','figure','name','Genetic Algorithm');
        limits = get(gca,'XLim');
        gaIter = limits(2);
        hold on;
    case 'iter'
        set(gca,'Xlim', [1 optimvalues.iteration + gaIter]);
        fval = optimvalues.fval;
        iter = gaIter + optimvalues.iteration;
        plot(iter,fval,'dr')
        title(['Best function value: ',num2str(fval)],'interp','none')
    case 'done'
        fval = optimvalues.fval;
        iter = gaIter + optimvalues.iteration;
        hold off
end

Subject: output function figure not updating after each iteration

From: Donn Shull

Date: 6 Sep, 2008 16:31:02

Message: 2 of 3

Try following your plot statement with a drawnow statement.

Donn

"Dave Brackett" <davebrackett@hotmail.com> wrote in message <g9uald$ekt$1@fred.mathworks.com>...
> I have a ga which is hybridised with fminunc:
>
> hybrid_options=optimset('OutputFcn',@fminuncOut,'Display','iter','MaxFunEvals',1000,'MaxIter',1000,'TolFun',1e-6,'TolX',1e-6);
>
> options=gaoptimset('HybridFcn',{@fminunc,hybrid_options},'UseParallel','always','PopInitRange',variable_range,'PopulationSize',25,'CrossoverFraction',0.5,'Generations',10,'StallGenLimit',50,'StallTimeLimit',40,'SelectionFcn',@selectionstochunif,'MutationFcn',{@mutationuniform,0.7},'CrossoverFcn',@crossoverscattered,'PlotFcns',@gaplotbestf},'PlotInterval',1);
>
> [x,fval,exitflag,output,final_pop]=ga(fitness_fcn,no_variables,[],[],[],[],lower_bound,upper_bound,[],options);
>
>
> I have used fminuncOut.m as an output function from the matlab ga toolbox webinar with this intention of getting this:
> http://www.mathworks.com/company/newsletters/digest/sept04/images/results5b_wl.jpg
>
> This then produces a plot of the GA history followed by the fminunc history. However, it only updates the figure once the optimisation has ended whereas I want it to update after each iteration. Does anyone know how I can make it do so? Thanks.
>
>
> fminuncOut.m consists of the following code (with the annotation part removed):
>
> function stop = fminuncOut(x,optimvalues,state)
>
> persistent fig gaIter
> stop = false;
>
> switch state
>
> case 'init'
> fig = findobj(0,'type','figure','name','Genetic Algorithm');
> limits = get(gca,'XLim');
> gaIter = limits(2);
> hold on;
> case 'iter'
> set(gca,'Xlim', [1 optimvalues.iteration + gaIter]);
> fval = optimvalues.fval;
> iter = gaIter + optimvalues.iteration;
> plot(iter,fval,'dr')
> title(['Best function value: ',num2str(fval)],'interp','none')
> case 'done'
> fval = optimvalues.fval;
> iter = gaIter + optimvalues.iteration;
> hold off
> end
>
>

Subject: output function figure not updating after each iteration

From: Dave Brackett

Date: 6 Sep, 2008 21:10:19

Message: 3 of 3

ah great thanks.

"Donn Shull" <donn.shull.no_spam@aetoolbox.com> wrote in message <g9ub86$kjp$1@fred.mathworks.com>...
> Try following your plot statement with a drawnow statement.
>
> Donn
>
> "Dave Brackett" <davebrackett@hotmail.com> wrote in message <g9uald$ekt$1@fred.mathworks.com>...
> > I have a ga which is hybridised with fminunc:
> >
> > hybrid_options=optimset('OutputFcn',@fminuncOut,'Display','iter','MaxFunEvals',1000,'MaxIter',1000,'TolFun',1e-6,'TolX',1e-6);
> >
> > options=gaoptimset('HybridFcn',{@fminunc,hybrid_options},'UseParallel','always','PopInitRange',variable_range,'PopulationSize',25,'CrossoverFraction',0.5,'Generations',10,'StallGenLimit',50,'StallTimeLimit',40,'SelectionFcn',@selectionstochunif,'MutationFcn',{@mutationuniform,0.7},'CrossoverFcn',@crossoverscattered,'PlotFcns',@gaplotbestf},'PlotInterval',1);
> >
> > [x,fval,exitflag,output,final_pop]=ga(fitness_fcn,no_variables,[],[],[],[],lower_bound,upper_bound,[],options);
> >
> >
> > I have used fminuncOut.m as an output function from the matlab ga toolbox webinar with this intention of getting this:
> > http://www.mathworks.com/company/newsletters/digest/sept04/images/results5b_wl.jpg
> >
> > This then produces a plot of the GA history followed by the fminunc history. However, it only updates the figure once the optimisation has ended whereas I want it to update after each iteration. Does anyone know how I can make it do so? Thanks.
> >
> >
> > fminuncOut.m consists of the following code (with the annotation part removed):
> >
> > function stop = fminuncOut(x,optimvalues,state)
> >
> > persistent fig gaIter
> > stop = false;
> >
> > switch state
> >
> > case 'init'
> > fig = findobj(0,'type','figure','name','Genetic Algorithm');
> > limits = get(gca,'XLim');
> > gaIter = limits(2);
> > hold on;
> > case 'iter'
> > set(gca,'Xlim', [1 optimvalues.iteration + gaIter]);
> > fval = optimvalues.fval;
> > iter = gaIter + optimvalues.iteration;
> > plot(iter,fval,'dr')
> > title(['Best function value: ',num2str(fval)],'interp','none')
> > case 'done'
> > fval = optimvalues.fval;
> > iter = gaIter + optimvalues.iteration;
> > hold off
> > end
> >
> >
>

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us