Is it possible to have access to the information related to any generation that is less than maxgenerations?

4 visualizaciones (últimos 30 días)
Hi,
I was wondering if it is possible to have access to the information (e.g. x, Fval, etc.) related to any generation that is less than the max generations? For example below, where 'MaxGenerations' is specified to be 100, is it possible to check Fval after 5/45/90 generations?
options = optimoptions(@gamultiobj,'PlotFcn',@gaplotpareto, 'MaxGenerations',100, 'PopulationSize',...
population,'UseParallel',true);
% Run the optimization
[x,Fval,exitFlag,Output,population,scores] = gamultiobj(evalObjectives,numVariables,A, ...
b,Aeq,beq,boundsLo,boundsHi,constraint,options);
I could imagine there is a structure that contains information for each generation but just couldn't find it. As an alternative, I did something like this:
generations = [5 45 90];
for thisGen = 1:length(generations)
options = optimoptions(@gamultiobj,'PlotFcn',@gaplotpareto, 'MaxGenerations',generations(thisGen), 'PopulationSize',...
population,'UseParallel',true);
[x,Fval,exitFlag,Output,population,scores] = gamultiobj(evalObjectives,numVariables,A, ...
b,Aeq,beq,boundsLo,boundsHi,constraint,options);
if generations(thisGen) == 5
Fval_Gen5.O1 = Fval(:,1); % Function (Objective) 1's value after 5 generations
elseif generations(thisGen) == 45
Fval_Gen45.O1 = Fval(:,1); % Function (Objective) 1's value after 45 generations
elseif generations(thisGen) == 90
Fval_Gen90.O1 = Fval(:,1); % Function (Objective) 1's value after 90 generations
end
end
Apparently, it would be great if I can get all information in one shot - setting 'MaxGenerations' to 90, running gamultiobj once, then doing post-processing to get information for generation 5 and 45. Please let me know if this is feasible.
Thanks,

Respuestas (1)

Jasvin
Jasvin el 18 de Sept. de 2023
Hi Shuxuan,
So, the short answer is that you cannot access the “x” or “fval” values for “gamultiobj”. However, you can get a lot of similarly relevant information for each iteration via the “OutputFcn” option in “optimoptions which is the function that will be called after each iteration of the solver’s execution.
Here are two sections from the GA options page that go over the basic structure and the output you can obtain via the “OutputFcn” parameter:
The main variables of interest from the “state” structure seem to be “Population” and “Score”.
Here is a guide that documents how to write the “OutputFcn” for “ga”:
And here is my modified version of the above code that worked for a basic “gamultiobj” example:
function [state,options,optchanged] = gaoutfun(options,state,flag)
persistent h1 history r
optchanged = false;
switch flag
case 'init'
h1 = figure;
ax = gca;
ax.XLim = [0 21];
ax.YLim = [0 21];
l1 = min(state.Population(:,1));
m1 = max(state.Population(:,1));
l2 = min(state.Population(:,2));
m2 = max(state.Population(:,2));
r = rectangle(ax,'Position',[l1 l2 m1-l1 m2-l2]);
history(:,:,1) = state.Population;
assignin('base','gapopulationhistory',history);
case 'iter'
disp(state);
% Update the history every 10 generations.
if rem(state.Generation,10) == 0
ss = size(history,3);
history(:,:,ss+1) = state.Population;
assignin('base','gapopulationhistory',history);
end
% Update the plot.
figure(h1)
l1 = min(state.Population(:,1));
m1 = max(state.Population(:,1));
l2 = min(state.Population(:,2));
m2 = max(state.Population(:,2));
r.Position = [l1 l2 m1-l1 m2-l2];
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
end
Basically, the generation history data will be stored in the “gapopulationhistory” in the base workspace.
Another point of interest is that the Simulated Annealing optimization does provide “x” and “fval” as part of their “OutputFcn” "optimvaluesstructure. The following page lists the “OutputFcn” format for Simulated Annealing and you can modify the gaoutfunto work for Simulated Annealing which would give you the exact parameters in iteration-wise execution:

Categorías

Más información sobre Genetic Algorithm en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by