📄 xgenetic.m
字号:
function [popu, popu_real, fcn_value, upper, average, lower, popuSize]=xgenetic(...
obj_fcn, range, mode, IC, mutate_mode, elite, generation_n , popuSize, ...
bit_n, xover_rate, mutate_rate);
% xgenetic.m file. Modification of go_ga.m file
%********************************************************************
%function [popu, popu_real, fcn_value, upper, average, lower, popuSize]=xgenetic(...
% obj_fcn, range, mode, IC, mutate_mode, elite, generation_n , popuSize, ...
% bit_n, xover_rate, mutate_rate);
%********************************************************************
% NEED TO APPEND (max,min), linear fitness scaling, Gray code, real parameters
% later by Chou, Penchen
%
%============================================================
% Calling notes:
%
% INPUTS:
% (1). obj_fcn must be a string whose function is to calculate the fitness fuction values. for
% example, 'xpeaksfcn'. In general xpeaksfcn.m is function m-file with a form of
% output=xpeaksfcn(input).
% (2). range is matrix with min on the forst row and max on the second row, such as,
% [-3 -3 5
% 4 5 8]
% represents three variables in this example. var_n used in this function is
% determined from range.
% (2a). mode=0 for BINARY GA, =1 (DEFAULT) for floating point GA.
% (3). elite is the keep of the best fit for every next generation (default=1. Use 1 or 0 only)
% (4). generation_n is the size of generations(default=30).
% (5). popuSize is the size of populations(default=20). IF popuSize <=10, BINARY BITS OF
% EACH POPU WILL BE PRINTED FOR EXAMINATION USE. //UPDATED 3/16/2001
% (6). bit_n is the bit length of one varaible (default=20).
% (7). xover_rate is the probability of single crossover rate (default=1.0).
% (8). mutate_rate is the probability of mutation (default=0.01).
%
% OUTPUTS:
% (1). popu --- populations of the last generation
% (2). fcn_value --- fitness values for the last populations
% (3). upper --- max fitness of each generation
% (4). average --- average fitness of each generation
% (5). lower --- min fitness of each generation
%============================================================
% ORIGINAL CONSTRUCTOR: DR. CHANG
% MODIFIER: PENCHEN CHOU
% DATE: 10/16/2000
%====================================================
% GA starts here
%====================================================
% CHECK ERRORS AND SET DEFAULTS
NARG=nargin;
if NARG>11 | NARG<2
error('===>Too many or too little input arguments');
else
if NARG<= 11
;
end;
if NARG<= 10
mutate_rate=0.01;
end;
if NARG<= 9
xover_rate=1;
end;
if NARG<= 8
bit_n=20;
end;
if NARG<= 7
popuSize=20;
end;
if NARG<= 6
generation_n=30;
end;
if NARG<= 5
elite=1;
end;
if NARG<= 4
mutate_mode=0; % Normal mutation
end;
if NARG<= 3
IC=[ ];
end;
if NARG<= 2
mode=1;
end;
end;
%----------------------------------------------------------------------
% CHECK ERRORS AND FIND NEEDED CONSTANTS
% If improper popuSize. set it at least to 6.
popuSize=max([6, popuSize]);
% If range is improper, return with error messgae
LOW=range(1,:); HI=range(2,:); delta=HI-LOW;
AHA=find(delta<0);
if ~isempty(AHA), error('****> Sorry! Your range data is incorrect, please check!'); end;
% Find var_n from range.
var_n=size(range,2);
gen23=fix(generation_n*2/3);
if mode==0,
SWITCH=0;
else
SWITCH=1;
end;
%----------------------------------------------------------------------
% INITIAL POPULATION
if mode==1, % CGA case. Continuous GA algorithm
LL=zeros(popuSize,var_n);
HH=zeros(popuSize,var_n);
for i=1:popuSize
LL(i,:)=LOW; HH(i,:)=HI; % Match with popu in size
end;
DD=HH-LL;
popu = DD.*rand(popuSize,var_n)+LL; % Give numbers in ranges
else
% BGA case. BINARY GA algorithm
popu = rand(popuSize, bit_n*var_n) > 0.5; % Binary [x, y] if two variables
if mode==0 & popuSize<=10
fprintf('<Initial population>\n');
[popu]
end;
end;
% Load one of previous chromosomes together with present population
len_IC=length(IC);
len_code=size(popu,2);
if len_code==len_IC
if ~isempty(IC),
popu(1,:)=IC; fprintf('>>>Use IC value <<<\n'); pause(2);
end % IC in binary for BGA or real for CGA
end
upper = zeros(generation_n, 1); % Record max fitness of each generation
average = zeros(generation_n, 1);
lower = zeros(generation_n, 1);
COUNT=0; LAST=0; Mx=1;
%=====================================================
% MAIN LOOP FOR GA
for i = 1:generation_n;
% Evaluate objective function for each individual
[fcn_value, popu_real] = xevalpopu(mode, popu, bit_n, range, obj_fcn);
if SWITCH==0,
mode=0;
else
mode=1;
end; %End of use mode 0, back to mode 1
if mod(i,100)==0 & var_n<=2,
FCN=fcn_value; POP=popu_real;
[FCN, II]=sort(FCN);
POP=POP(II,:);
fprintf('***Generation #=%i\n',i);
ww=length(fcn_value);
for w=1:ww
if var_n==2
fprintf('f[%10.6f, %10.6f]=%12.9f\n',POP(w,1),POP(w,2),FCN(w));
else
fprintf('f[%10.6f]=%12.9f\n',POP(w,1),FCN(w));
end;
end;
%[fcn_value/1000 popu_real]
%pause
end
%[junk,index]=max(fcn_value);
%popu_real(index,:), pause
%
% if elite==1
% fcn_value=(1+0.2*i)*fcn_value; % This is for Robustness test
% end;
% if (i==1),
% fprintf('Initial population.\n');
% fprintf('Press any key to continue...\n');
% pause;
% end
% Fill objective function matrices
upper(i) = max(fcn_value); % i is the generation #
average(i) = mean(fcn_value);
lower(i) = min(fcn_value);
% display current best
[best, index] = max(fcn_value);
if SWITCH==0,
fprintf( '\n(==>B)');
else
fprintf( '\n(R<**)');
end;
fprintf(' Generation %i: ', i);
if mode==0
if var_n==2,
fprintf('f(%f, %f)=%.9f\n', ...
xbit2num(popu(index, 1:bit_n), range(:,1)), ...
xbit2num(popu(index, bit_n+1:2*bit_n), range(:,2)), ...
best);
else
fprintf('==>Max fitness=%f, average fitness=%f\n',best,average(i));
%popu(index,:)
end;
if mode==0 & popuSize<=10
popu_real
popu
end;
else
if var_n==2,
fprintf('f(%f, %f)=%.9f\n', popu(index,1),popu(index,2),best);
else
fprintf('==>Max fitness=%f, average fitness=%f\n',best,average(i));
end;
end;
if best~=LAST
LAST=best; COUNT=0; Mx=max([1 Mx-1]);
else
COUNT=COUNT+1;
if mode==0,
% ENDCOUNT=60;
ENDCOUNT=60;
else
% ENDCOUNT=50;
ENDCOUNT=50;
end;
if COUNT>ENDCOUNT,
Mx=Mx+0.1; COUNT=0; SWITCH=xor(1,SWITCH);
end;
end;
if SWITCH==1 & mode==0,
mode=1;
end;
% generate next population via selection, crossover and mutation
if mode==0
popu = xnextpopu(popu, fcn_value, xover_rate, mutate_rate, elite);
else
if SWITCH==1 % i>=gen23
if COUNT==0,
mode=1;
popu=popu_real;
% fprintf(' *** In real-number mode\n'); pause(2)
end;
popu = cnextpopu(popu, fcn_value, xover_rate, mutate_rate, elite, ...
range, mutate_mode, Mx, generation_n, i);
else % Switch to BGA for fine tunning
% if i==gen23, % Convert real popu to binary popu
if COUNT==0 % Convert real popu to binary popu
mode=0;
popu = xencode(popu_real,range(1,:),range(2,:),bit_n*ones(1,size(range(:,2))));
% fprintf(' *** In binary-number mode\n'); pause(2)
end; % Use BGAfor fine tunning
popu = xnextpopu(popu, fcn_value, xover_rate, mutate_rate, elite);
end;
end;
% if (i==5),
% fprintf('Population after the 5th generation.\n');
% fprintf('Press any key to continue...\n');
% pause;
% end
% if (i==10),
% fprintf('Population after the 10th generation.\n');
% fprintf('Press any key to continue...\n');
% pause;
% end
end % End of for loop
% Print message
if SWITCH==0,
fprintf('==>Use Bianry GA algorithm\n');
else
fprintf('==>Use Floating Point or Real GA algorithm\n');
if mutate_mode==1, fprintf('==>Use adjustable mutation rate during the process.\n'); end;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -