-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaplotbestcustom.m
62 lines (56 loc) · 2.22 KB
/
gaplotbestcustom.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
%---------------G.Etsias September-07-2018--------------------------------%
%Created as a variation of standard gaplotbestf---------------------------%
%In order to be used in a ga with a heurisitc objective function, like NNs%
%Alterations only in lines 16,32&38
function state = gaplotbestcustom(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);
load ('bestperformance')%my change
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');
state.Generation
bestperformance
plotBest = plot(state.Generation,bestperformance,'.k'); %my change
set(plotBest,'Tag','gaplotbestf');
plotMean = plot(state.Generation,meanf(state.Score),'.b');
set(plotMean,'Tag','gaplotmean');
title(['Best: ',' Mean: '],'interp','none')
case 'iter'
best = bestperformance; %my change
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 Mean: %g',best,m));
case 'done'
LegnD = legend('Best fitness','Mean fitness');
set(LegnD,'FontSize',8);
hold off;
end
%------------------------------------------------
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;