# Genetic Algorithm: Plot ONLY best fitness value, not mean.

26 views (last 30 days)
Ayman Al-Sukhon on 4 Feb 2020
Commented: Veector on 25 May 2022
Hi, I want to plot ONLY the best function value for each generation in MATLAB GA and not the mean, as the two values are on different orders and it ruins the presentation. How can I do this? I tried using the following output function:
function [state,options,optchanged] = OutputFunction(options,state,flag)
persistent h1 history r
optchanged = false;
switch flag
case 'init'
h1 = figure;
ax = gca;
ax.XLim = [0 200];
ax.YLim = [0 500];
case 'iter'
% Find the best objective function, and stop if it is low.
ibest = state.Best(end);
ibest = find(state.Score == ibest,1,'last');
bestx = state.Population(ibest,:);
bestf = CostFunctionV2(bestx);
% Update the plot.
figure(h1)
plot(state.Generation,bestf)
pause(0.1)
% Update the fraction of mutation and crossover after 25 generations.
if state.Generation == 25
options.CrossoverFraction = 0.8;
optchanged = true;
end
case 'done'
% Include the final population in the history.
ss = size(history,3);
history(:,:,ss+1) = state.Population;
assignin('base','gapopulationhistory',history);
end
It does not seem to be working as it does not actually plot any values and just changes axes.
Peter Kuetzing on 17 Mar 2021
When running 'ga', you may set options using optimoptions . The option for plotting the best function is: 'PlotFcns',@gaplotbestfun. The handle @gaplotbestfun results in Best & Mean, while @gaplotbestf results in just the Best. Hope this helps!

Alan Weiss on 18 Mar 2021
You could try using this slightly modified plot function, gaplotbestf2: (I modified it slightly from gaplotbestf)
function state = gaplotbestf2(options,state,flag)
%GAPLOTBESTF Plots the best score and the mean score.
% STATE = 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 = optimoptions('ga','PlotFcn',@gaplotbestf);
% Copyright 2003-2016 The MathWorks, Inc.
if size(state.Score,2) > 1
msg = getString(message('globaloptim:gaplotcommon:PlotFcnUnavailable','gaplotbestf'));
title(msg,'interp','none');
return;
end
switch flag
case 'init'
hold on;
set(gca,'xlim',[0,options.MaxGenerations]);
xlabel('Generation','interp','none');
ylabel('Fitness value','interp','none');
plotBest = plot(state.Generation,min(state.Score),'.k');
set(plotBest,'Tag','gaplotbestf');
% plotMean = plot(state.Generation,meanf(state.Score),'.b');
% set(plotMean,'Tag','gaplotmean');
title('Best: ','interp','none')
case 'iter'
best = min(state.Score);
% m = meanf(state.Score);
plotBest = findobj(get(gca,'Children'),'Tag','gaplotbestf');
% plotMean = findobj(get(gca,'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(gca,'Title'),'String',sprintf('Best: %g',best));
case 'done'
LegnD = legend('Best fitness');
set(LegnD,'FontSize',8);
hold off;
end
Set your plot function option to @gaplotbestf2.
Alan Weiss
MATLAB mathematical toolbox documentation
Veector on 25 May 2022
thanks