Genetic algorithm plot diagram display

31 visualizaciones (últimos 30 días)
joshua Abam
joshua Abam el 21 de Feb. de 2020
Comentada: joshua Abam el 23 de Feb. de 2020
Dear all,
I am having a few challenges with regards to GA displaying the plots from the plot function and saving the output results from the output function, while using the m file, however when put in optimization toolbox, the plot are displayed and the output functions are saved respectively. That draws me to the questions, what do I have to do to make the plots displays and save the out put functions using an m file as displayed below.
Thanks
Kind regards
Joshua Abam
function [x,fval,existflag,output,population,scores] = scr_gafunction(x)
options = optimoptions('ga'); %Define Optimization the solver
options = optimoptions('ga','PopulationSize',50,'Generations',50,'OutputFcn',@gascroutputdata); %Define the Population Size
%Define the generation Size %Define Ouput Function
options.SelectionFcn = ('selectionstochunif'); %Define the selection pattern
options.MutationFcn = ('mutationgaussian'); %Define the mutation methods
options.CrossoverFcn = ('crossoverheuristic'); %Define the crossover methods
options = optimoptions('ga', 'Display','diagnose','MaxStallGenerations',60); %Define Display function
%Define at what Generation is GA stall
%Define the Plot Function
scr_gafunction.options.PlotFcns = optimoptions('ga','PlotFcn',{@gaplotbestf,@gaplotrange,@gaplotscores,...
@gaplotselection,@gaplotdistance,@gaplotexpectation,@gaplotstopping})
options = optimoptions('ga','UseParallel', true, 'UseVectorized', false);
ObjectiveFunction = @weight; %fitness function
nvars = 3; % Number of variables
A = [0 0 -1]; %Linear Inequality Constraint matrix
b = [-0.0254]; %Linear inequality constraint vector (t_1+t_corr+t_fab)
Aeq = [1 -0.03 0]; %Linear equality Constraint matrix
beq = [77]; %Linear equality constraint vector
lb = [167, 3000, 0.0127]; %Vector of Lower bounds
ub = [173, 3200, 0.0286]; %Vector of Upper bounds
% Initial design point could be determined in 2 ways: One is to leave it to
% GA automatically generate; or it can be specified by the users.
%x0 = []; %Initial Feasible Point
nonlcon = @nonlinearconst; %Nonlinear constraints
Intcon = []; %Vector indicating variables that take integer values
%Excuate GA with output
[x,fval,exitflag,output,population,scores] = ga(ObjectiveFunction,nvars,A,b,Aeq,beq,lb,ub,nonlcon,Intcon,options)
Final_time = toc(startTime);
fprintf('GA optimization takes %g seconds.\n',Final_time)
end
  4 comentarios
Walter Roberson
Walter Roberson el 22 de Feb. de 2020
The code you posted does not attempt to save() anything, so it is not clear why you would expect that anything would end up in a .mat file?
joshua Abam
joshua Abam el 22 de Feb. de 2020
Editada: joshua Abam el 22 de Feb. de 2020
Hi Walter
In the code above, the GA output function is called using options to save each iteration from the initial to the final as illustrated using GA options. The iterations are saved when the function '@gascroutputdata' is placed in the optimization tool, however, when run in the GA code as stated above, that is not the case as mat file seems to be genetrate as such meaning the the iterations are not saved. The second part is the plot diagram from GA generates, when the optimization toool is used, however, that is not the case when the code above is use.
here is the m code for the output function.
function [state,options,optchanged] = gascroutputdata(options,state,flag)
persistent pin pit pout history r
optchanged='false';
d = size(state.Population)
pin=zeros(d);
pit=zeros(d);
pout=zeros(d);
switch flag
case 'init' %Save the inital generations iteration results for the %population and the fitness value
disp('Starting the algorithm');
pin = [state.Population,state.Score]; %state.fval,
assignin('base','gapopulationhistory',history);
save scroptiminitiadata.mat;
case 'iter' %Save the subseaquent generations iteration results for the %population and the fitness value
disp('Iterating ...')
% if rem(state.Generation,30) == 0
% ss = size(pit,100000);
pit = [state.Population,state.Score]; %,state.fval
assignin('base','gapopulationhistory',history);
save scroptimiteratdata.mat;
% end
case 'done' %Save the final generations iteration results for the
%population and the fitness value
disp('Performing final task');
pout=[state.Population,state.Score];
assignin('base','gapopulationhistory',history);
save scroptimfinal.mat;
end

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Feb. de 2020
The variable that you create that has the same name as your function, is not used after it is assigned to, so the options you assign there are never used. The options you use are the ones you create about using parallel.
  1 comentario
joshua Abam
joshua Abam el 23 de Feb. de 2020
Hi Walter
Please How do I use the options created here in the code, Please can you help by iluustrating it using a part of the code.
thanks
Kind regards
Joshua Abam

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by