1.遗传算法流程图
2.matlab代码
2.1 main.m 主函数
clear;
close all;
clc;
nVar = 12;
nPop = 300;
maxIt = 30;
nPc = 0.8;
nC = round(nPop * nPc / 2) * 2;
nCmu = 0.5;
nMu = 0.01;
Parent.x = randi([0, 1], nPop, nVar);
Parent.y = fun(Parent.x);
for It = 1 : maxIt
Offspring = selectPop(Parent, nC);
Offspring.x = crossPop(Offspring.x, nCmu);
Offspring.x = mutatePop(Offspring.x, nMu);
Offspring.y = fun(Offspring.x);
newPop.x = [Parent.x; Offspring.x];
newPop.y = [Parent.y; Offspring.y];
[~, so] = sort(newPop.y, 'descend');
newPop.x = newPop.x(so, :);
newPop.y = newPop.y(so, :);
Parent.x = newPop.x(1 : nPop, :);
Parent.y = newPop.y(1 : nPop);
disp(['迭代次数:', num2str(It), ', 最大值为:', num2str(Parent.y(1))])
end
2.2 适应度函数
function y = fun(x)
[m, ~] = size(x);
y = zeros(m, 1);
x1 = x(:, 1 : 3);
x2 = x(:, 4 : 6);
x3 = x(:, 7 : 9);
x4 = x(:, 10 : 12);
out = [];
for t = 1 : m
xx1 = bin2dec(num2str(x1(t, :)));
xx2 = bin2dec(num2str(x2(t, :)));
xx3 = bin2dec(num2str(x3(t, :)));
xx4 = bin2dec(num2str(x4(t, :)));
if xx1 == 0 || xx2 == 0 || xx3 == 0 || xx4 == 0
out = [out, t];
end
y(t) = xx1 ^ 2 - 4 * xx2 ^ 2 + xx3 - xx4 ^ 3;
end
y(out) = min(y) - 1;
end
2.3 选择
function y = fun(x)
[m, ~] = size(x);
y = zeros(m, 1);
x1 = x(:, 1 : 3);
x2 = x(:, 4 : 6);
x3 = x(:, 7 : 9);
x4 = x(:, 10 : 12);
out = [];
for t = 1 : m
xx1 = bin2dec(num2str(x1(t, :)));
xx2 = bin2dec(num2str(x2(t, :)));
xx3 = bin2dec(num2str(x3(t, :)));
xx4 = bin2dec(num2str(x4(t, :)));
if xx1 == 0 || xx2 == 0 || xx3 == 0 || xx4 == 0
out = [out, t];
end
y(t) = xx1 ^ 2 - 4 * xx2 ^ 2 + xx3 - xx4 ^ 3;
end
y(out) = min(y) - 1;
end
2.4 交叉
function p = crossPop(pop, pc)
[px, py] = size(pop);
p = zeros(size(pop));
for i = 1:2:px-1
if(rand < pc)
cpoint = round(rand * py);
p(i, :) = [pop(i, 1 : cpoint), pop(i + 1, cpoint + 1 : end)];
p(i + 1, :) = [pop(i + 1, 1 : cpoint), pop(i, cpoint + 1 : end)];
else
p(i, :) = pop(i, :);
p(i + 1, : ) = pop(i + 1, : );
end
end
end
2.5 变异
function p = crossPop(pop, pc)
[px, py] = size(pop);
p = zeros(size(pop));
for i = 1:2:px-1
if(rand < pc)
cpoint = round(rand * py);
p(i, :) = [pop(i, 1 : cpoint), pop(i + 1, cpoint + 1 : end)];
p(i + 1, :) = [pop(i + 1, 1 : cpoint), pop(i, cpoint + 1 : end)];
else
p(i, :) = pop(i, :);
p(i + 1, : ) = pop(i + 1, : );
end
end
end
3. 使用Gaot工具箱
3.1 main.m 主函数
clear;
close all;
clc;
t = 1:0.01:7;
x = repmat(t, length(t), 1);
y = x';
z = x .^ 2 - y .^ 2;
figure;
plot3(x, y, z);
xlabel('自变量x');
ylabel('自变量y')
zlabel('自变量z');
title('z = x^2 + y^2');
grid
initPop = initializega(500, [1, 7; 1, 7], 'fitness');
[x, endPop, bpop, trace] = ga([1, 7; 1, 7], 'fitness', [], initPop, [1e-6, 1, 1], 'maxGenTerm', 200, ...
'normGeomSelect', 0.1, 'arithXover', 3, 'nonUnifMutation', [2, 25, 3]);
disp("最优解");
disp(x);
hold on;
plot3(x(1), x(2), x(3), 'r*')
figure;
plot(trace(:, 1), trace(:, 3), 'b:');
hold on;
plot(trace(:, 1), trace(:, 2), 'r-');
xlabel('Generation');
ylabel('Fittness');
legend('Mean Fitness', 'Best Fitness');
3.2 适应度函数 fitness.m
function [sol, fitnessVal] = fitness(sol, ~)
x = sol(1);
y = sol(2);
fitnessVal = x ^ 2 - y ^ 2;
end
3.3 运行结果