Filter löschen
Filter löschen

Plot gaplotdistance in one plot for multiple runs of genetic algorithm

10 Ansichten (letzte 30 Tage)
Dear all,
With the code below I managed to run the genetic algorithm multiple times.
gprMdl2 = fitrgp(X,Y1,'KernelFunction','squaredexponential','OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',struct('AcquisitionFunctionName','expected-improvement-plus'));
for i = 1:3
options = optimoptions('ga','CrossoverFrac',0.9,'PopulationSize',50,'StallGen',50,'Generations',70,'PlotFcn', {'gaplotbestf','gaplotdistance'});
fun = @(X) [abs(((predict(gprMdl2,X)-MFR_exp)/MFR_exp))];
[x_opt, Obj, exitflag,output] = ga(fun,2,[],[],[],[],[0.1 0.1], [0.9 0.9],[],[],options);
end
With PlotFcn I will get the plot of the fitness value vs genration and the average distance vs generation (as shown below). How can I combine the plots of each run into one plot?
  7 Kommentare
Tessa Kol
Tessa Kol am 31 Okt. 2020
Bearbeitet: Tessa Kol am 31 Okt. 2020
I changed the following line of code
testdist(end+1,:) = distance
After that I devided every value of the testdist by the number of samples, which is 20. That is how you get the values of the average distance. Than I filterd out the average distance values of every optimization run and put each run in a seperate column. Every 20th value corresponds to the value in the column corresponds to the average distance value in the plot.
A1 = testdis(:,1)/20;
A2 = zeros(1281,3);
A2(1:1281,1)=A1(1:1281,1);
A2(2:1081,2)=A1(1282:2361,1);
A2(2:1221,3)=A1(2362:3581,1);
A3 = A2(41:20:1221,3);
A4 = A2(41:20:1081,2);
A5 = A2(41:20:1281,1);
The resulting plot is shown below
I want to thank you for your help. Can you post your comment as an answer so I can accept it.
Mario Malic
Mario Malic am 31 Okt. 2020
Bearbeitet: Mario Malic am 31 Okt. 2020
Great to hear it works! Actually, I put the distance first, and then I thought, but it doesn't show every single distance out there, so I set it back to d.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mario Malic
Mario Malic am 31 Okt. 2020
Bearbeitet: Mario Malic am 31 Okt. 2020
Edited according to the change of testdist. More details in comments.
function state = customgaplotdistance(options,state,flag)
%GAPLOTDISTANCE Averages several samples of distances between individuals.
% STATE = GAPLOTDISTANCE(OPTIONS,STATE,FLAG) plots an averaged distance
% between individuals.
%
% Example:
% Create an options structure that uses GAPLOTDISTANCE
% as the plot function
% options = optimoptions('ga','PlotFcn',@gaplotdistance);
%
% (Note: If calling gamultiobj, replace 'ga' with 'gamultiobj')
% Copyright 2003-2015 The MathWorks, Inc.
persistent testdist % change number 1
testdist(1,:) = [0 0]; % initialising the value
samples = 20;
choices = ceil(sum(options.PopulationSize) * rand(samples,2));
switch flag
case 'init'
population = state.Population;
distance = 0;
for i = 1:samples
d = population(choices(i,1),:) - population(choices(i,2),:);
distance = distance + sqrt( sum ( d.* d));
testdist(end+1,:) = distance; % change number 2
end
plotDist = plot(state.Generation,distance/samples,'.');
set(gca,'xlimmode','manual','zlimmode','manual', ...
'alimmode','manual')
set(gca,'xlim',[1,options.MaxGenerations]);
set(plotDist,'Tag','gaplotdistance');
xlabel('Generation','interp','none');
ylabel('Average Distance');
title('Average Distance Between Individuals','interp','none')
case 'iter'
population = state.Population;
distance = 0;
for i = 1:samples
d = population(choices(i,1),:) - population(choices(i,2),:);
distance = distance + sqrt( sum ( d.* d));
testdist(end+1,:) = distance; % change number 3
assignin('base', 'testdist', testdist) % it might be better to assign it and save it from the main file
% save('testdist.mat', 'testdist') % as it will save the file 3000+ times
end
plotDist = findobj(get(gca,'Children'),'Tag','gaplotdistance');
newX = [get(plotDist,'Xdata') state.Generation];
newY = [get(plotDist,'Ydata') distance/samples];
set(plotDist,'Xdata',newX,'Ydata',newY);
end
Filtering out each ga run is done by lines below
A1 = testdis(:,1)/20;
A2 = zeros(1281,3);
A2(1:1281,1)=A1(1:1281,1);
A2(2:1081,2)=A1(1282:2361,1);
A2(2:1221,3)=A1(2362:3581,1);
A3 = A2(41:20:1221,3);
A4 = A2(41:20:1081,2);
A5 = A2(41:20:1281,1);

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by