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

조회 수: 20 (최근 30일)
Ayman Al-Sukhon
Ayman Al-Sukhon 2020년 2월 4일
댓글: hemanth chaduvula 2023년 7월 30일
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.
  댓글 수: 1
Peter Kuetzing
Peter Kuetzing 2021년 3월 17일
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!

댓글을 달려면 로그인하십시오.

답변 (1개)

Alan Weiss
Alan Weiss 2021년 3월 18일
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
  댓글 수: 2
hemanth chaduvula
hemanth chaduvula 2023년 7월 30일
Dear sir/madam
I have a following query.
I have understood the plot for best fitness in ga. How to get the best fitness values?
Thank you

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by