Saving population individauls for each generation of GA Multiobjective optimisation.

40 views (last 30 days)
Hi all
I have used GA multiobjective optimisation of Matlab. I would like to save the population individuals for each generation in one file for a generation. I can do that for the fitness values for each individual in a generation.
Anybody have done that before?
Thanks

Answers (1)

Star Strider
Star Strider on 2 Oct 2022
I created a function for ga (not the multiobjective version) that will do that. See if the approach in How to save data from Genetic Algorithm in case MATLAB crashes? - MATLAB Answers - MATLAB Central will work in your application.
  2 Comments
Shahrbanoo Shamekhi-Amiri
Thank you for your response.
When I try to use your code, it does not recognize the 'Best' file. How should I resolve this problem?
Star Strider
Star Strider on 2 Oct 2022
Edited: Star Strider on 2 Oct 2022
The state structure is different in gamultiobj. I need to run the example in the documentation and experiment with it. The conversion will likely involve using Rank to select from the Population. I need to experiment with that to be certain, and that could take at least a few minutes.
EDIT — (2 Oct 2022 at 17:16)
The state structure in gamulitobj is not the same as for ga, (that I understand much more thoroughly than I do gamulitobj) so this required some experimentation. (In this example from the documentation, there are only two parameters.) I have not used gamultiobj much, and not recently, so I do not have much experience with it. There may be two sets of parameters — one for each objective — and that would be reflected in the ‘Var’ result, in this instance in different lines sequentially for each objective.
I need to work with gamultiobj a bit more to fully understand it, however this version of ‘SaveOut’ appears to work effectively with it.
opts = optimoptions('gamultiobj', 'OutputFcn',@SaveOut);
fitnessfcn = @(x)[norm(x)^2,0.5*norm(x(:)-[2;-1])^2+2];
% Find the Pareto front for this objective function.
rng default % For reproducibility
x = gamultiobj(fitnessfcn,2, [],[],[],[],[],[],[],[], opts);
Optimization terminated: average change in the spread of Pareto solutions less than options.FunctionTolerance.
% Optimization terminated: average change in the spread of Pareto solutions less than options.FunctionTolerance.
%
% Plot the solution points.
plot(x(:,1),x(:,2),'ko')
xlabel('x(1)')
ylabel('x(2)')
title('Pareto Points in Parameter Space')
BestInGen = load('SaveBest.mat')
BestInGen = struct with fields:
Var: [318×2 double]
LastFive = BestInGen.Var(end-4:end,:)
LastFive = 5×2
2.0003 -1.0004 0.0000 0.0008 2.0003 -1.0004 0.0000 0.0008 2.0003 -1.0004
function [state,options,changed,str] = SaveOut(options,state,flag)
file_name = 'SaveBest.mat'; % Name File
if strcmp(flag,'init')
Var = state.Population;
save(file_name, 'Var') % Write ‘Best Individual’ To File
elseif strcmp(flag,'iter')
[minScore,idx] = min(state.Score);
bestx = state.Population(idx,:);
previous = load('SaveBest.mat');
Var = [previous.Var; bestx]; % Read Previous Results, Append New Value
save(file_name, 'Var') % Write ‘Best Individual’ To File
end
changed = true; % Necessary For Cide, Use App
end
.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!