⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ga_genetic.m

📁 书籍代码:遗传演算法原理与应用_活用MATLAB(Source Code)
💻 M
字号:
function [popu, popu_real, fcn_value, upper, average,...
 lower, BEST_popu, popuSize, gen_no, para, best_pi]=...
 GA_genetic(obj_fcn, range, IC, elite, gen_no, popuSize,...
 bit_n, xover_rate, mutate_rate);
% *********************************************************
% GA_genetic.m file. Modification of go_ga.m file
% ==========================================================
%function [popu, popu_real, fcn_value, upper, average,...
% lower, BEST_popu, popuSize, gen_no, para, best_pi]=...
% GA_genetic(obj_fcn, range, IC, elite, gen_no, popuSize,...
% bit_n, xover_rate, mutate_rate);
% ==========================================================
%  global variables are: MIN_offset LOCUS x_data y_data
%  (1). All global variables must be defined in MAIN program
%  (2). LOCUS=1 means to plot locus of optimization on the 
%       3-dim contour plot if the user wants to. otherwise,
%       set it to 0
%  (3). Let x_data=[]; y_data=[]; in the MAIN program
%  (4). User must determine the value of MIN_offset for a
%       minimization problem before calling GA_genetic function
%  (5). MIN_offset must be defined in your fitness evaluation
%       function file. (see below)
%=============================================================
% Calling notes:
%
% INPUTS:
% (1). obj_fcn must be a string whose function is to calculate
%      the fitness function 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 first row and max on the
%      second row, such as,
%        [-3 -3  5     <=== LOWER bounds
%          4  5  8]    <=== UPPER bounds
%      represents three variables in this example. var_n used
%      in this function is determined from range.
% (3). IC [appended later]
% (4). elite is the keeping of the best fit for every next 
%         generation (default=1. Use 1 or 0 only)
% (5). gen_no is the size of generations(default=70).
% (6). popuSize is the size of populations(default=70). IF 
%      popuSize <=10, BINARY BITS OF EACH POPU WILL BE PRINTED
%      FOR EXAMINATION USE.   //UPDATED 3/16/2001
% (7). bit_n is the bit length of one varaible (default=40).
% (8). xover_rate is the probability of single crossover rate 
%      (default=0.7).
% (9). mutate_rate is the probability of mutation (default=0.02).
%
% 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
% .........
%============================================================
% FOR EXAMPLE:
% The following is a complete and executed OK MAIN program
% Listing of the MAIN program called GA_ex21.m file
%---------------------------------------------------------
%% GA_ex21.m file
%% Fin min of a function of 2 varaibles
%
%% PenChen Chou, 7-1-2001
%
%% --------------------------------------------------------
%%       User must define the follows
%%---------------------------------------------------------
% global MIN_offset LOCUS x_data y_data
% MIN_offset=25; LOCUS=1; x_data=[]; y_data=[]; 
% obj_fcn = 'GA_f21';	% Objective function
% range = [0 0
%         10 10];	% Range of the input variables
%%---------------------------------------------------------
%%       Options, user can define or use defaults
%%---------------------------------------------------------
% elite=1; 
% gen_no=1100;
% popuSize=60;
% bit_n=50;
%%---------------------------------------------------------
%%       Options
%%---------------------------------------------------------
%% This is for 3-dim contour plot use, it can be ommitted
% x=0:0.1:10; y=x; [X,Y]=meshgrid(x,y);
% Z=X.*sin(4*X)+1.1*Y.*sin(2*Y);
% figure(2);contour(X,Y,Z)
%%---------------------------------------------------------
%% call GA 
% tic     % tic, toc for getting the execution time
% [popu, popu_real, fcn_value, upper, average, lower, ...
%        popuSize, gen_no] = GA_genetic(obj_fcn, range,...
%        IC, elite, gen_no, popuSize, bit_n);
% t=toc/60;
% fprintf('==>  Computation time is (%.2f) minutes.\n\n',t);   
%%-----------------------------------------------------------
%% You can stop your program writing here   <======LOOK HERE         
%%-----------------------------------------------------------
%%       OPTIONS
%%-----------------------------------------------------------
%% For 3-dim contour plot only.
%% Plot locus of optimization process
% LAST=length(x_data);
% figure(2); hold on
% plot(x_data(1),y_data(1),'x');
% plot(x_data,y_data);
% plot(x_data(LAST),y_data(LAST),'o');
%% plot(0,0,'*');   % The [x,y] coordinates of the best fit
%%                  % you put if you know. i.e. [0,0].
% hold off
% title('*=BEST, x=Starting point, o=Ending point')
%%-------------------------------------------------------------
%% End of the MAIN program
%%=============================================================
%% List the calling fitness function evaluation
%%-------------------------------------------------------------
% function PI=GA_f21(chro)
% global MIN_offset
%% Fitness function evaluation of Example 2-1
%
%% PenChen Chou, 6-30-2001
%
% x=chro(1); y=chro(2);
% z=x*sin(4*x)+1.1*y*sin(2*y);
%% z is the smaller the better
%% Convert it to a maximum value. x=[0 10], y=[0 10];
%% the worst negative value is -10-11=-21,
%% we assume a positive value of 25 to convert the min.
%% problem to a max. problem.
% PI=MIN_offset-z;
%% End of fitness evaluation function
%%=============================================================
%% Future revision
%%=============================================================
%% NEED TO APPEND (max,min), linear fitness scaling, Gray code, 
%% real parameters or CGA
%%=============================================================
%%  END of all DESCRIPTIONs
%%*************************************************************

% ORIGINAL CONSTRUCTOR: Roger Jang
% MODIFIER: PENCHEN CHOU
% DATE:    4- 5-2001
% Revised: 6-30-2001
% Revised: 7- 8-2001

%====================================================
% GA starts here
%====================================================
% CHECK ERRORS AND SET DEFAULTS
NARG=nargin;
if NARG>9 | NARG<2
   error('===>Too many or too little input arguments');
else   
   if NARG<= 9
        ;
   end;   
   if NARG<= 8
      mutate_rate=0.02;   
   end;   
   if NARG<= 7
      xover_rate=0.7;
   end;   
   if NARG<= 6
        bit_n=40;
   end;   
   if NARG<= 5
        popuSize=70;
   end;   
   if NARG<= 4
        gen_no=70;
   end;   
   if NARG<= 3
        elite=1;
   end;   
   if NARG<= 2
        IC=[ ];
   end;   
end;   
%---------------------------------------------------------
% CHECK ERRORS AND FIND NEEDED CONSTANTS
var_n = GA_chk_range(popuSize,range);
%---------------------------------------------------------
% INITIAL POPULATION
% BGA case. BINARY GA algorithm
popu = GA_initpopu(popuSize, bit_n, var_n);

% Load IC into popu.   
popu=GA_IC(popu, IC);

% Set matrices for upper, average and lower 
upper   = zeros(gen_no, 1); % Record max fitness of each gen.
average = zeros(gen_no, 1);
lower   = zeros(gen_no, 1);
BEST_popu=zeros(gen_no, var_n);
%=====================================================
% MAIN LOOP FOR GA
for n_gen = 1:gen_no;
     % Evaluate objective function for each individual
     %size(popu)
     %pause
     [fcn_value, popu_real, popu] = GA_evalpopu(popu,...
                bit_n, range, obj_fcn);
     
     % Print out best fitness and parameters for
     %  every generation
     % FOR EXAMPLE, the following is a type of printout
     % [Generation #499], ===>Best_FIT= 100.00000000
     %      parameter( 1)=  -0.00000001
     %      parameter( 2)=   0.00000002
     GA_print(n_gen, gen_no, var_n, popu, popu_real,...
                  fcn_value, 1);
     %popu % Open it for printout
     % Print out partial data after each 100 generations
     %   and var_n<=2
     % GA_prt_100x2(i, var_n, fcn_value, popu_real);
     
     % Fill objective function matrices
	 [upper(n_gen), index] = max(fcn_value); 
	 average(n_gen) = mean(fcn_value);
	 lower(n_gen)   = min(fcn_value);
     BEST_popu(n_gen,1:var_n)=popu_real(index,:);
     
     % Generate next population via selection, crossover 
     %    and mutation
     popu = GA_newpopu(fcn_value, popu, bit_n, xover_rate,...
                       mutate_rate, elite);
end  % End of for loop

% Plot the final result, for example, print out the best
%   result (see below) and plot the transition locus of
%   best fit, average fitness and worst fitness in figure 1.
% The best generation occured at generation #371.
%     x1 =  -0.00000001
%     x2 =   0.00000002
%  ==>Best fitness for MAX. problem is (1.00000000)
[para, best_pi]=GA_plot(gen_no, upper, average, lower, BEST_popu);

% End of GA_genetic


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -