Genetic Algorithm: Plot ONLY best fitness value, not mean.
22 ビュー (過去 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
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
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
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 Center および File Exchange で Genetic Algorithm についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!