📄 flexga.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% flexga flexga flexga
% =========================================================================
% =========================================================================
% Genetic Algorithms Tools for MathTools
% =========================================================================
% Copyright (c) 1999, 2000
% Flexible Intelligence Group (FIG)
% P. O. Box 861477, Tuscaloosa, AL 35486, USA.
% =========================================================================
% PURPOSE
% Command line execution of GA module
% .........................................................................
% GIVEN
%
% fname = A fname.m file that contains the definition of the performance measure
% The format should be PI=fname(P),
% where
% PI= the scalar value of performance
% P = vector of parameters.
%
% P_type(1:Noofp) = A vector that describes the type of parameter used.
% The options are:
% 1=Integer type
% 2=Real type that takes discrete values
% > 2 Real type that takes continuous values (this number also defines
% the number of bits that are used to represent each parameter
% using stochastic GA).
%
% P_min(1:Noofp) = A vector of minimum values for the parameter vector.
% If P_type (i) <= 2, P_min(i) represents the minimum value for P(i).
% If P_type (i) > 2, P_min(i) represents an approximate value for the
% minimum of P(i).
%
% P_max(1:Noofp) = A vector of maximum values for the parameter vector.
% If P_type (i) <= 2, P_max(i) represents the maximum value for P(i).
% If P_type(i) > 2, P_max(i) represents an approximate value for the
% maximum of P(i).
%
% P_res(1:Noofp) = A vector of resolution values for the parameter vector.
% If P_type (i) <= 2, P_res(i) represents the resolution value for P(i).
% If P_type(i) > 2, P_res(i) is ignored.
%
% gap(1) = Type of GA
% 1= Regular GA
% 2= Micro-GA
% 3=Steady State GA
% gap(2) = # of generations for evolution
% gap(3) = population size
% gap(4) = No of peaks;
% gap(5) = Type of selection desired
% 1= Roulette Wheel Selection
% 2= Tournament Selection
% 3= Ranking Selection
% gap(6) = Tournament size (> 1 if Tournament selection is chosen).
% gap(7) = Steady State population size (>1 if Steady State GA is chosen)
% gap(8) = Probability of Crossover (0 - 1)
% gap(9) = Number of crossover points ( > 1)
% gap(10) = Probability of Mutation (0 - 1)
% gap(11) = Probability of Gaussian Mutation (0 - 1) [OPTIONAL]
% gap(12) = Micro-GA inner loop # of Generations [OPTIONAL]
% gap(13) = # of asexual children for stochastic coding [OPTIONAL]
%
% G_disp = Gap Display options [OPTIONAL]
% = 0 (show no plots, no display)
% = 1 (Default, show plots of evolution)
% = 2 (show only Generation count)
% = 3 (show data on screen and no plots. Data includes Generation #,
% Max, Min, Avg Values)
% ...........................................................................
% RETURNS
% Jmax(1:Ngen) = A vector of maximum performance index values
% Jmin(1:Ngen) = A vector of minimum performance index values
% Javg(1:Ngen) = A vector of average performance index values
% bestP(1:Noofpeaks,1:Noofp) = A matrix of best Parameter sets
% PI(1:popsize) = A vector of fitness functions for the last generation
%
% where
% Ngen = # of generations
% Noofp = # of parameters
% Noofpeaks = # of desired sub-optimal solutions
% popsize = population Size
%
%.............................................................................
% USES
% fmcclust, fminit.m, fminipop.m, fmdecode.m, fmnich.m, fmstats.m, fmgen.m,
% fmscreen,
% rand, if, load, figure, hold, for, feval, while, sort,
% subplot, plot, xlabel, ylabel, axis, title, pause, save
% =========================================================================
% SYNTAX / USAGE
% [Jmax,Jmin,Javg,opt_P,PI]=flexga('JPERF',P_min,P_max,P_res,P_type,gap,G_disp)
% ========================================================================
% CODE
function [max_PI,min_PI,avg_PI,bestP,PI]=flexga(fname,P_min,P_max,P_res,P_type,gap,G_disp)
if size(P_min,1) ~= size(P_max,1)
disp(['Size mismatch in P_min and P_max vectors.']);
return;
end
if size(P_min,1) ~= size(P_res,1)
disp(['Size mismatch in P_min and P_res vectors.']);
return;
end
if size(P_min,1) ~= size(P_type,1)
disp(['Size mismatch in P_min and P_res vectors.']);
return;
end
if (P_max < P_min ~= 0)
disp(['P_max is not greater than P_min.']);
return;
end
%------------------------------------------------
% Initialize ga_parameters (call fminit)
%------------------------------------------------
[gap,fpd,fud,mean_table,sigma_table,pot_table]=fminit(P_min,P_max,P_res,P_type,gap);
if nargin < 7, G_disp=1; end
if (2*round(gap(3)/2)-gap(3)) == 0, gap(3)=gap(3)+1; end
Nga_choice = gap(1);
if Nga_choice==3, gap(7)=fix(gap(3)/2); end
Ngen = gap(2);
Npop = gap(3);
Noofpeaks = gap(4);
Nselect = gap(5);
Tsize = gap(6);
Nsspop = gap(7);
pcross = gap(8);
Ncross = gap(9);
pmutate = gap(10);
pgmutate = gap(11);
Nregen = gap(12);
Lchrom = fud(97);
Noofp = fud(98);
Noofobj = fud(99);
mm = fud(100:(100+Noofobj-1));
gno=1;
genn(gno)=gno;
nooffeval(gno)=gno*(Npop-Nsspop);
%-----------------------------------------------
% Initialize Population (call fminipop)
%-----------------------------------------------
oldpop=fminipop(gap,fud);
%-------------------------------
% Random PI Initialization
%-------------------------------
PI=rand(Npop,Noofobj);
%load ga_warm
%Ngen=Ngen+Ngen;
%--------
% figure
%--------
if G_disp==1
FigurePos_GAplot=[70 25 490 350];
fig_fmga=figure( ...
'NumberTitle','off', ...
'Name','flexga Evolution Screen', ...
'Position',FigurePos_GAplot,...
'Resize','on', ...
'Pointer','watch');
hold off;
end
%--------------------------------------------------------------------------------
% decode parameters; compute PI by calling the objective function
%------------------------------------------------------------------------------------------------
for i=1:Npop
[p,PI(i,:),mean_table,sigma_table]=...
fmdecode(oldpop(i,:),gap,fud,fpd,mean_table,sigma_table,pot_table,fname); % call fmdecode
p_p(i,:)=p;
PI(i,:)=PI(i,:).*mm;
end % i for loop
savePI=PI;
savep_p=p_p;
if Noofpeaks > 1
nichPI=fmnich(fud,Npop,Noofp,PI(:,1),p_p',P_max',P_min',Noofpeaks); % call fmnich
else
nichPI=PI;
end % if
%-------------------------------------------
% compute statistics (call fmstats)
%-------------------------------------------
[maxi(gno),mini(gno),avg(gno),sd(gno),Nmax,Nmin]=fmstats(PI(:,1));
%-------------------------
% For each generation
%-------------------------
%....................................................................
%for ijkl=1:Ngen
while gno < Ngen
%--------------------
% ga or micro-ga loop
%--------------------
% -------------------------------------------------------
for k=1:Nregen
[sort_y,sort_i]=sort(PI);
%-------------------------------------------------
% CALL fmgen.m (generate new population)
%-------------------------------------------------
[newpop]=fmgen(gap,fud,oldpop,nichPI);
for i=1:Nsspop
newpop((Npop-Nsspop+i),:)=oldpop(sort_i(i),:);
PI((Npop-Nsspop+i),:)=savePI(sort_i(i),:);
p_p((Npop-Nsspop+i),:)=savep_p(sort_i(i),:);
end % i for loop
%-----------------
% increment gno
%------------------
gno=gno+1;
%-------------------
% decode newpop
%-------------------
for j=1:(Npop-Nsspop)
[p,PI(j,:),mean_table,sigma_table]=...
fmdecode(newpop(j,:),gap,fud,fpd,mean_table,sigma_table,pot_table,fname); % call fmdecode
p_p(j,:)=p;
PI(j,:)=PI(j,:).*mm;
end % j for loop
savePI=PI;
savep_p=p_p;
if Noofpeaks > 1
nichPI=fmnich(fud,Npop,Noofp,PI(:,1),p_p',P_max',P_min',Noofpeaks); % call fmnich
else
nichPI=PI;
end % if
genn(gno)=gno;
nooffeval(gno)=gno*(Npop-Nsspop);
oldpop=newpop;
%------------------------------------------
% Compute statistics (call fmstats)
%------------------------------------------
[maxi(gno),mini(gno),avg(gno),sd(gno),Nmax,Nmin]=fmstats(PI(:,1));
%------------------------
% get best parameters
%------------------------
bestp = p_p(Nmin,:); % call fmdecode.m
savepop(1,:)=oldpop(Nmin,:);
if gno > 5, bgno=gno-5; else bgno=1; end
genp=genn(bgno:gno);
nfnp=nooffeval(bgno:gno);
minp=mini(bgno:gno);
maxp=maxi(bgno:gno);
avgp=avg(bgno:gno);
%-------------------------------------------------
% PLOT GRAPH & DISPLAY OPTIONS
%-------------------------------------------------
if G_disp==1
subplot(2,1,1)
plot(genp,(maxp*mm(1)),'r', genp,(minp*mm(1)),'g',genp,(avgp*mm(1)),'w');
title('white = average, green = best, red = worst');
xlabel('# of Gens');
ylabel('Fitness Value');
subplot(2,1,2)
plot(nfnp,(minp*mm(1)),'g');
xlabel('Number of Function Evaluations');
ylabel('Fitness Value');
drawnow
end
if G_disp==2
disp(['#of Gens= ' num2str(gno)]);
end
if G_disp==3
pp1=num2str(gno);
pp2= num2str(maxi(gno) );
pp3= num2str(mini(gno) );
pp4= num2str(avg(gno) );
disp(['#of Gens = ' pp1 ' Max = ' pp2 ' Min = ' pp3 ' Avg = ' pp4]);
end
end % k for loop (Nregen)
%------------------------------------------------------------
%----------
% Micro GA
%----------
if Nga_choice == 2
oldpop=fminipop(gap,fud); %call fminipop.m
oldpop(Npop,:)=savepop(1,:);
PI(Npop,:)=PI(Nmin,:);
p_p(Npop,:)=p_p(Nmin,:);
for j=1:(Npop-1)
[p,PI(j,:),mean_table,sigma_table]=...
fmdecode(oldpop(j,:),gap,fud,fpd,mean_table,sigma_table,pot_table,fname); % call fmdecode
p_p(j,:)=p;
PI(j,:)=PI(j,:).*mm;
end % j for loop (Npop)
savePI=PI;
nichPI=PI;
%------------------------------------------
% compute statistics (call fmstats)
%------------------------------------------
[maxi(gno),mini(gno),avg(gno),sd(gno),Nmax,Nmin]=fmstats(PI);
end % if
%---------
% SAVE
%---------
if Noofpeaks > 1 & gno==Ngen
[Ysort,Isort]=sort(PI);
for i=1:Npop
bestP(i,:)=p_p(Isort(i),:);
end
else
bestP=bestp;
end; % if
% save ga_warm.mat;
end % Ngen for loop
%.............................................................
%-----------------------------------------------
% modify values based on max. or min.
%-----------------------------------------------
min_PI=mini*mm(1);
max_PI=maxi*mm(1);
avg_PI=avg*mm(1);
for i=1:Npop
PI(i,:)=PI(i,:).*mm;
end
%-----------------------------------
% clear old variables if existed
%-----------------------------------
clear oldpop newpop p_p savepop savePI nichPI maxi mini avg sd
clear fname
% =========================================================================
% (c) Flexible Intelligence Group (FIG)
% END flexga.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -