matlab智能算法之遗传算法

时间:2022-12-13 08:55:05

智能算法之遗传算法

1.背景

2.算法

3.案例

3.1 案例求解二元函数的最大值

例1:计算二元函数 f ( x , y ) = 20 + x 2 + y 2 − 10 ∗ ( c o s ( 2 π x ) + c o s ( 2 π y ) ) f(x,y)=20+x^2+y^2-10*(cos(2\pi x)+cos(2\pi y)) f(x,y)=20+x2+y210(cos(2πx)+cos(2πy)) 的最大值,其中 x ∈ [ 0 , 10 ] , y ∈ [ 0 , 5 ] x\in[0,10],y\in [0,5] x[0,10],y[0,5]
目标函数:

% -------------------适应度函数--------------------目标函数
function y = fitnessfcn(x)
    y = 20+x(1)^2+x(2)^2-10*(cos(2*pi*x(1))+cos(2*pi*x(2)));
end

选择/复制:

% ----------------------------选择子函数selection--------------------------------
function population = selection(population, best, fitness, N)
    newpopulation = zeros(N, size(population, 2));           % 先预先分配内存   可以不要
    p = fitness./sum(fitness);
    q = cumsum(p);
    for i = 1:(N-1)
        r = rand;
        tmp = find(r <= q);
        newpopulation(i, :) = population(tmp(1), :);
    end
    newpopulation(N, :) = best;                              % 保留最优 
    population = newpopulation;
end

交叉:

% -------------------交叉子函数crossover-------------------
function population = crossover(population, best, pc, L, N)
    for i = 1:2:(N-1)
        cc = rand;
        if cc < pc
            point = 1 + ceil(rand*(L-2));                    % 取得一个2到L-1的整数
            ch = population(i, :);
            population(i, point+1:L) = population(i+1, point+1:L);
            population(i+1, point+1:L) = ch(1, point+1:L);
        end
    end
    population(N, :) = best;                                 % 保留最优
end

变异:

% -------------------------变异子函数mutation----------------------
function population = mutation(population, pm, L, N)
    mm = rand(N, L)<pm;                                      % 小于变异概率的赋值为1,其他为0;也可以取反变异,这里的mm是逻辑值
    mm(N, :) = zeros(1, L);                                  % 最优保留,不变异
    population(mm) = 1-population(mm);                       % 变异发生
end

二进制转换为十进制:

% ----------------解码子函数decode------------------
function [xx, fitness] = decoding(population, lb, ub, Li, N)
    for i = 1:N
        for k = 1:length(Li)
            s(k) = 0;
            for j = 1:Li(k)
                s(k) = s(k) + population(i, sum(Li(1:k))-j+1)*2^(j-1);    % 二进制转十进制
            end
            x(k) = (ub(k) - lb(k))*s(k)/(2^Li(k)-1)+lb(k);                % 映射到取值范围内
        end
        fitness(i) = fitnessfcn(x);
        xx(i, :) = x;
    end
end

主函数:

clc;clear;close all;
% ----------------初始化参数--------------------
e = 0.01;                         % 计算精度
lb = [0; 0];                      % 自变量下界
ub = [10; 5];                     % 自变量上界
Li = ceil(log2((ub-lb)./e));       
L = sum(Li);                      % 变量字串长度
N = 30;                           % 群体规模
T = 30;                           % 最大遗传代数
pc = 0.9;                         % 交叉概率
pm = 0.05;                        % 变异概率                       
% -----------------初始种群----------------------
population = round(rand(N, L));                           % 初始种群,二进制编码(0,1)之间随机数,然后四舍五入为0/1
[xx, fitness] = decoding(population, lb, ub, Li, N);      % 解码,计算适应度
[optfit, indmax] = max(fitness);                 % 初始种群最佳个体的适应度和索引
best = population(indmax, :);                    % 初始种群最优染色体  二进制的
optx = xx(indmax, :);                            % 初始种群最优变量值  十进制的
fitcurve = zeros(1, T+1);                        % 有多少代最优适应度
fitcurve(1) = optfit;                            % 第一代的最优适应度
% -------------------迭代求解----------------------
for ii = 1:T
    population = selection(population, best, fitness, N);        % 选择(复制)
    population = crossover(population, best, pc, L, N);          % 交叉
    population = mutation(population, pm, L, N);                 % 变异
    [xx, fitness] = decoding(population, lb, ub, Li, N);         % 解码,计算适应度
    [fmax, indmax] = max(fitness);                               % 当代最佳个体
    if fmax >= optfit
       best = population(indmax, :);                             % 到目前为止最优染色体
       optx = xx(indmax, :);                                     % 到目前为止最优变量值
       optfit = fmax;                                            % 到目前为止最优适应度
    end
    fitcurve(ii+1) = optfit;                                     % 存储每代的最优适应度
end
optx                                                             % 最优变量
optfit                                                           % 最优适应度

% ------------------------画图---------------------------
subplot(1,2,1);
plot(0:T, fitcurve);
title('最优适应度曲线');
xlabel('遗传代数'); ylabel('最优适应度');
subplot(1,2,2);
x = linspace(0, 10);
y = linspace(0, 5);
[x, y] = meshgrid(x, y);
f = 20+x.^2+y.^2-10*(cos(2*pi*x)+cos(2*pi*y));
mesh(x, y, f);
hold on;
scatter3(optx(1), optx(2), optfit, 'red', 'filled');  
xlabel('x'); ylabel('y'); zlabel('f');title('f(x,y)=20+x^2+y^2-10*(cos(2\pi x)+cos(2\pi y))')

得到最优解为: ( 9.5406 , 4.5303 ) (9.5406,4.5303) (9.5406,4.5303),最大值为 f ( 9.5406 , 4.5303 ) = 151.0422 f(9.5406,4.5303)=151.0422 f(9.5406,4.5303)=151.0422
matlab智能算法之遗传算法