image thumbnail

Image reconstruction

by

 

The purpose of this demo is to reconstruct a simple picture of several polygons.

mygaplotbestf(options,state,flag)
function [state,options,optchanged] = mygaplotbestf(options,state,flag)
%GAPLOTBESTF Plots the best score and the mean score.
%   [state,options,optchanged] = GAPLOTBESTF(OPTIONS,STATE,FLAG) plots the best score
%   as well as the mean of the scores.
%
%   Example:
%    Create an options structure that will use GAPLOTBESTF
%    as the plot function
%     options = gaoptimset('PlotFcns',@gaplotbestf);

%   Copyright 2003-2007 The MathWorks, Inc.
%   $Revision: 1.1.6.2 $  $Date: 2010/10/08 16:50:58 $

if size(state.Score,2) > 1
    title('Best Fitness Plot: not available','interp','none');
    return;
end
global ax3
switch flag
    case 'init'
        hold(ax3,'on');
        set(ax3,'xlim',[0,options.Generations]);
        xlabel(ax3,'Generation','interp','none');
        ylabel(ax3,'Fitness value','interp','none');
        plotBest = plot(ax3,state.Generation,min(state.Score),'.k');
        set(plotBest,'Tag','gaplotbestf');
        plotMean = plot(ax3,state.Generation,meanf(state.Score),'.b');
        set(plotMean,'Tag','gaplotmean');
        title(ax3,['Best: ',' Mean: '],'interp','none')
    case 'iter'
        best = min(state.Score);
        m    = meanf(state.Score);
        plotBest = findobj(get(ax3,'Children'),'Tag','gaplotbestf');
        plotMean = findobj(get(ax3,'Children'),'Tag','gaplotmean');
        newX = [get(plotBest,'Xdata') state.Generation];
        newY = [get(plotBest,'Ydata') best];
        set(plotBest,'Xdata',newX, 'Ydata',newY);
        newY = [get(plotMean,'Ydata') m];
        set(plotMean,'Xdata',newX, 'Ydata',newY);
        set(get(ax3,'Title'),'String',sprintf('Best: %g Mean: %g',best,m));
    case 'done'
        legend(ax3,'Best fitness','Mean fitness','FontSize',8);
        hold(ax3,'off');
end
optchanged=false;
%------------------------------------------------
function m = meanf(x)
nans = isnan(x);
x(nans) = 0;
n = sum(~nans);
n(n==0) = NaN; % prevent divideByZero warnings
% Sum up non-NaNs, and divide by the number of non-NaNs.
m = sum(x) ./ n;

Contact us