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

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
output function Dave Brackett 6 Sep, 2008 12:25:04
hybrid Dave Brackett 6 Sep, 2008 12:25:04
optimization Dave Brackett 6 Sep, 2008 12:25:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com