Plot gaplotdistance in one plot for multiple runs of genetic algorithm
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tessa Kol
le 28 Oct 2020
Modifié(e) : Mario Malic
le 31 Oct 2020
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 commentaires
Mario Malic
le 31 Oct 2020
Modifié(e) : Mario Malic
le 31 Oct 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.
Réponse acceptée
Mario Malic
le 31 Oct 2020
Modifié(e) : Mario Malic
le 31 Oct 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);
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Genetic Algorithm dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!