BaiTapLonGeneticAlgorithm 2019
BaiTapLonGeneticAlgorithm 2019
3.1 Dùng giải thuật di truyền, tìm đường đi ngắn nhất nối các điểm ABCDEFG.
% ga.m
clear all
a = 2;
b = 3;
d = 5;
n = 7;
locations(1,:) = [1 , 1 ]; %A
locations(2,:) = [1+a , 1 ]; %B
locations(3,:) = [1+a+b , 1 ]; %C
locations(4,:) = [1+a+b+a, 1 ]; %D
locations(5,:) = [1+a , 1-a ]; %E
locations(6,:) = [1+a+b , 1-a ]; %F
locations(7,:) = [1+a , 1+d]; %G
AA = meshgrid(1:n);
distances = reshape(sqrt(sum((locations(AA,:)-locations(AA',:)).^2,2)),n,n);
% distances =
% 0 2.0000 5.0000 7.0000 2.8284 5.3852 5.3852
% 2.0000 0 3.0000 5.0000 2.0000 3.6056 5.0000
% 5.0000 3.0000 0 2.0000 3.6056 2.0000 5.8310
% 7.0000 5.0000 2.0000 0 5.3852 2.8284 7.0711
% 2.8284 2.0000 3.6056 5.3852 0 3.0000 7.0000
% 5.3852 3.6056 2.0000 2.8284 3.0000 0 7.6158
% 5.3852 5.0000 5.8310 7.0711 7.0000 7.6158 0
% fitness function
FitnessFcn = @(x)open_traveling_salesman_fitness(x,distances);
% ga option
options = gaoptimset('PopulationType','custom','PopInitRange',[1;n]);
options = gaoptimset(options,'CreationFcn',@create_permutations,...
'CrossoverFcn', @crossover_permutation,'MutationFcn',...
@mutate_permutation, 'PlotFcn', [],'Generations',20000,...
'PopulationSize',60,'StallGenLimit',200,'Vectorized','on');
numberOfVariables = n;
[x,fval,reason,output] = ga(FitnessFcn, numberOfVariables, options);
x{1,1}
axis([0 9 -2 7]);
hold on
plot(locations(:,1),locations(:,2),'bo');
plot(locations(x{1,1},1),locations(x{1,1},2),'r.-');
fval
%-----------
function pop = create_permutations(NVARS,FitnessFcn,options)
n = NVARS;
totalPopulationSize = 60;
pop = cell(totalPopulationSize,1);
for i = 1:totalPopulationSize
pop{i}=randperm(n);
end
%-----------
function mutationChildren = mutate_permutation(parents, options, NVARS, ...
FitnessFcn, state, thisScore, thisPopulation, mutationRate)
mutationChildren = cell(length(parents),1);
for i = 1:length(parents)
parent = thisPopulation{parents(i)};
p = ceil(length(parent)*rand(1,2));
child = parent;
child(p(1)) = parent(p(2));
child(p(2)) = parent(p(1));
mutationChildren{i} = child;
end
%-----------
function scores = open_traveling_salesman_fitness(x,distances)
scores = zeros(size(x,1),1);
for j = 1:size(x,1)
p = x{j};
f = 0;
for i = 2:length(p)
f = f + distances(p(i-1),p(i));
end
scores(j) = f;
end
3.2 Dùng giải thuật di truyền, tìm thông số của bộ điều khiển PI cho đối tượng G(s) = sao cho J =
min
clear all
% Stop condition
max_generation = 200;
max_stall_generation = 50;
epsilon = 0.001;
% Parameter
pop_size = 20; % population size
npar = 2; % 2 parameters Kp, Ki
range = [ 0 0 % min value
100 100]; % max value
dec = [2 2]; % posiition of decimal point
sig = [5 5]; % 5 gen
cross_prob = 0.9; % probability of cross
mutate_prob = 0.1; % -------------- mutation
elitism = 1; % keep the best individual
% par = range*rand(pop_size, npar);
par = InitPop(pop_size, npar, range); %
stop_condition = 0;
generation = 0;
stall_generation = 0;
for pop_index = 1:pop_size,
Kp = par(pop_index,1);
Ki = par(pop_index,2);
sim('myGA_PID.mdl');
J = e'*e;
fitness(pop_index) = 1/(J+eps); % min
end
[bestfit0, bestchrom] = max(fitness);
while ~stop_condition,
generation = generation + 1;
pop = EncodeDecimal(par, sig, dec);
parent = SelectLinearRanking(pop, fitness, 0.5, elitism, bestchrom); %
child = CrossTwoPoint(parent, cross_prob, elitism, bestchrom); %
pop = MutateUniform(child, mutate_prob, elitism, bestchrom); %
par = DecodeDecimal(pop, sig, dec); %
if generation == max_generation
stop_condition = 1;
elseif generation > 1
if abs(bestfit(generation) - bestfit(generation - 1)) < epsilon
stall_generation = stall_generation + 1;
if stall_generation == max_stall_generation,
stop_condition = 1;
end
else
stall_generation = 0;
end
end
end
plot(1./bestfit);
Kp = par(bestchrom,1);
Ki = par(bestchrom,2);
sim('myGA_PID.mdl');
%---------------------------
function child = CrossTwoPoint(parent, cross_prob, elitism, bestchrom)
[N, L] = size(parent);
for p1 = 1:N
if (elitism == 1) & (p1 == bestchrom)
child(p1,:) = parent(p1,:);
else
if cross_prob > rand
p2 = p1;
while p2 == p1
p2 = rand*N;
p2 = p2 - rem(p2,1) + 1;
end
k1 = rand*(L-1);
k1 = k1 - rem(k1,1) + 1;
k2 = k1;
while k2 == k1
k2 = rand*(L-1);
k2 = k2 - rem(k2,1) + 1;
end
if k1 > k2,
t = k2;
k2 = k1;
k1 = t;
end
child(p1,1:k1) = parent(p1,1:k1);
child(p1,k1+1:k2) = parent(p2,k1+1:k2);
child(p1,k2+1:L) = parent(p1,k2+1:L);
else
child(p1,:) = parent(p1,:);
end
end
end
%---------------------------
function par = DecodeDecimal(pop, sig, dec)
[N, L] = size(pop);
d = length(sig);
par = zeros(N,d);
for pop_index = 1:N
start_gene = 1;
for par_index = 1:d
for count = 1:sig(par_index)
gene_index = start_gene + count;
weight = dec(par_index) - count;
par(pop_index, par_index) = par(pop_index, par_index) + (pop(pop_in-
dex, gene_index))*10^weight;
end
for k = 1:N
if elitism == 1 & order(k) == bestchrom
parent(order(k),:) = pop(order(k),:);
else
r = rand*s(N+1);
index = find(s<r);
j = index(end);
parent(order(k),:) = pop(order(j),:);
end
end
tt 1 2 3 4 5 6 7 8 9 10
a 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75
b 0.99 0.97 0.96 0.93 0.91 0.89 0. 87 0.86 0.84 0.83
d 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
tt 11 12 13 14 15 16 17 18 19 20
a 0.80 0.82 0.85 0.88 0.90 0.92 0.94 0.95 0.96 0.98
b 0.81 0.79 0.77 0.68 0.64 0.62 0.58 0.53 0.48 0.43
d 1.2 1.5 1.8 2 2.2 2.5 2.8 3 3.5 4
tt 21 22 23 24 25 26 27 28 29 30
a 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75
b 0.81 0.79 0.77 0.68 0.64 0.62 0.58 0.53 0.48 0.43
d 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9
tt 31 32 33 34 35 36 37 38 39 40
a 0.80 0.82 0.85 0.88 0.90 0.92 0.94 0.95 0.96 0.98
b 0.99 0.97 0.96 0.93 0.91 0.89 0. 87 0.86 0.84 0.83
d 10 12 15 18 20 25 30 35 40 45
tt 41 42 43 44 45 46 47 48 49 50
a 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75
b 0.89 0. 87 0.86 0.84 0.83 0.99 0.97 0.96 0.93 0.91
d -0.1 -0.2 -0.3 -0.4 -0.5 -0.6 -0.7 -0.8 -0.9 -1
tt 51 52 53 54 55 56 57 58 59 60
a 0.80 0.82 0.85 0.88 0.90 0.92 0.94 0.95 0.96 0.98
b 0.62 0.58 0.53 0.48 0.43 0.81 0.79 0.77 0.68 0.64
d -1.2 -1.5 -1.8 -2 -2.2 -2.5 -2.8 -3 -3.5 -4
tt 61 62 63 64 65 66 67 68 69 70
a 0.92 0.94 0.95 0.96 0.80 0.82 0.85 0.88 0.90 0.80
b 0.99 0.97 0.96 0.93 0.91 0.89 0. 87 0.86 0.84 0.83
d -4.5 -5 -5.5 -6 -6.5 -7 -7.5 -8 -8.5 -9
tt 71 72 73 74 75 76 77 78 79 80
a 0.55 0.60 0.65 0.70 0.75 0.30 0.35 0.40 0.45 0.50
b 0.81 0.79 0.77 0.68 0.64 0.62 0.58 0.53 0.48 0.43
d -10 -12 -15 -18 -20 -25 -30 -35 -40 -45
tt 81 82 83 84 85 86 87 88 89 90
a 0.55 0.60 0.65 0.70 0.75 0.30 0.35 0.40 0.45 0.50
b 0.79 0.68 0.62 0.53 0.81 0.77 0.64 0.43 0.43 0.48
d -15 -20 -35 -40 -12 -10 -45 -25 -18 -30