Professional Documents
Culture Documents
Ví dụ về thuật toán di truyên GA dùng matlab
Ví dụ về thuật toán di truyên GA dùng matlab
phần
mềm matlab. Cho biết số thế hệ tối đa là 50, số cá thể là 10, hệ số lai ghép CR=0.5, hệ số đột
biến MR=0.05. Với -5<=x,y<=5.
4 3 2
f ( x)
( x 2) ( y 2) 1 ( x 2) ( y 2) 1 ( x 2) ( y 2) 2 1
2 2 2 2 2
function example_GA
% Genetic Algorithms
%--------------------------------------------------------------
% Reference book "A Tutorial on Meta-Heuristics for Optimization"
% Authors: Shu-Chuan Chu, Chin-Shiuh Shieh, and John F. Roddick
%--------------------------------------------------------------
% --Initialize Parameters---
n = 10; % Population Size
cl = 32; % Number of bits in each chromosome
c = zeros(n,cl);
mg = 50; % %Maximal Number of Generations
best_f = -10^9; % Best Fitness Value
f = zeros(1,n);
best_c = zeros(1,cl); % Best Chromosome
p = zeros(1,n);
tmpc=zeros(n,cl);
CR = 0.5; % Crossover Rate
MR = 0.05; % Mutation Rate
Fvl=zeros(1,cl);
% Initialize Population
for i=1:1:n
for j=1:1:cl
if(rand(1)<0.5)
c(i,j)=0;
else
c(i,j)=1;
end
end
end
% Main Program
for t=1:1:mg
%--Decode Chromosome--
x = zeros(1,n);
y = zeros(1,n);
for i=1:1:n
for j=1:1:cl/2
x(i) = x(i)*2+c(i,j);
end
x(i)=10*x(i)/(2^16)-5;
for j = (cl/2+1):1:cl
y(i) = y(i)*2+c(i,j);
end
y(i)=10*y(i)/(2^16)-5;
%--Update best solution--
f(i) = 4/((x(i)-2)^2+(y(i)-2)^2 +1)+3/((x(i)-2)^2+(y(i)+2)^2
+1)+2/((x(i)+2)^2+(y(i)-2)^2 +1);
if(f(i) > best_f)
best_f = f(i);
for j=1:1:cl
best_c(j)=c(i,j);
end
end
end
end
% ---Export Graph---
disp(Fvl);
plot(Fvl);
hold on;
end