ga_ex21.m

来自「书籍代码:遗传演算法原理与应用_活用MATLAB(Source Code)」· M 代码 · 共 46 行

M
46
字号
% GA_ex21.m file
% Fin min of a function of 2 varaibles

% PenChen Chou, 7-1-2001

global MIN_offset MUL_factor LOCUS x_data y_data
MIN_offset=250; LOCUS=1; x_data=[]; y_data=[];
MUL_factor=1;
%**********************************************************/
%       User can modify the following in blocks
%**********************************************************/
obj_fcn = 'GA_f21';	% Objective function
range = [0 0
        10 10];	% Range of the input variables
IC=[]; elite=1; 
gen_no=500;
popuSize=60;
bit_n=40;
xover_rate=0.0;
mutate_rate=0.02;

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 written by Chang
tic
%GA_genetic(obj_fcn, range, IC, elite,...
% gen_no, popuSize, bit_n, xover_rate, mutate_rate);
[popu, popu_real, fcn_value, upper, average, lower, ...
        popuSize, gen_no] = GA_genetic(obj_fcn, range,...
        IC, elite, gen_no, popuSize, bit_n,...
        xover_rate, mutate_rate);
t=toc/60;
fprintf('==>  Computation time is (%.2f) minutes.\n\n',t);            
            
% 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 best position
hold off
title('*=BEST, x=Starting point, o=Ending point')

⌨️ 快捷键说明

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