Genetic Algorithm: Plot ONLY best fitness value, not mean.
조회 수: 20 (최근 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!