遗传算法原理以及MATLAB实现

1.遗传算法流程图

遗传算法

2.matlab代码

2.1 main.m 主函数

%% 遗传算法,不使用工具包,自行编写函数完成
clear;
close all;
clc;

%% 参数
% 计算x1 ^ 2 - 4 * x2 ^ 2 + x3 - x4 ^ 3;的最小值, x1, x2属于{1, 2, 3, 4 ,5 6, 7}
nVar = 12; % 自变量长度
nPop = 300; % 种群规模
maxIt = 30; % 最大迭代次数
nPc = 0.8; % 子代比例
nC = round(nPop * nPc / 2) * 2; % 子代规模
nCmu = 0.5; % 交叉概率
nMu = 0.01; % 变异概率

%% Part1. 初始化种群
Parent.x = randi([0, 1], nPop, nVar); % 自变量

%% Part2. 适应度计算
Parent.y = fun(Parent.x); % 因变量

%% 迭代
for It = 1 : maxIt
    %% Part3. 选择
    Offspring = selectPop(Parent, nC);

    %% Part4. 交叉
    Offspring.x = crossPop(Offspring.x, nCmu);
    
    %% Part5. 变异
    Offspring.x = mutatePop(Offspring.x, nMu);
    Offspring.y = fun(Offspring.x);
    
    %% Part6. 筛选
    newPop.x = [Parent.x; Offspring.x]; % 合并种群x
    newPop.y = [Parent.y; Offspring.y]; % 合并种群y
    [~, so] = sort(newPop.y, 'descend');% 排序
    newPop.x = newPop.x(so, :);
    newPop.y = newPop.y(so, :);
    Parent.x = newPop.x(1 : nPop, :);% 筛选x
    Parent.y = newPop.y(1 : nPop);% 筛选y
    
    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 主函数

%% 遗传算法,使用GA工具箱
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]);
%变量范围上下界;适应度函数;适应度函数的参数;初始种群;精度和显示方式;终止函数的名称;终止函数的参数;
% 选择函数的名称;选择函数的参数;交叉函数的名称;交叉函数的参数;变异函数的名称;变异函数的参数
%  x: 最优个体     endpop: 优化终止的最优种群    bpop: 最优种群的进化轨迹   trace: 进化迭代过程中
%最优的适应度函数值和适应度函数值矩阵

%% 输出最优解并绘制最优点
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 运行结果

适应度
效果

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是易安吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值