你想要的的遗传算法代码

Genetic Algorithm

标签(空格分隔): 遗传算法 改进的遗传算法 GA 实数编码 matlab算法


与经典遗传算法的区别:精英替换机制,直接复制父代最优的k(ex:k=10)个个体到子代种群,替换子代中适应度值差的k个个体。
****目的
防止搜索到的最优个体在变异交叉以后丢失。
****测试问题
:**y = x*(3πx)在[-1,2]上的最大值。函数

clear;
clc;
close all;

%设置初始参数
popSize = 100; %种群大小
Vnum = 1; %每一个解的变量数
iterations = 10000; % 迭代次数
crossoverRate = 0.95; %交叉几率
mutationRate = 0.01; %变异几率
uBound = 2 ;%x上界
lBound =  -1;%x下界
Bound = [uBound,lBound];

%初始化种群
pop = initializePop(popSize,Vnum,Bound);
Fitness = cal_Fitness(pop);%计算适应度
[best_ind,best_fitness] = bestOpt(pop,Fitness);   %选择最好个体

%画出初始种群分布
figure
x=linspace(-1,2,1000);
y=x.*sin(3*pi.*x);
plot(x,y);
hold on
plot(pop,pop.*sin(3*pi.*pop),'*r');
hold off

for i = 1:iterations
    pop = selectionOpt(pop,Fitness);%选择
    pop = crossoverOpt(pop,crossoverRate,Bound);%交叉
    pop = mutationOpt(pop,mutationRate,Bound);%变异
    Fitness = cal_Fitness(pop);%计算适应度
    [pop,Fitness] = Newparents(pop,Fitness,best_ind,best_fitness); %构成新父代   
end

%末代种群分布
figure
x=linspace(-1,2,1000);
y=x.*sin(3*pi.*x);
plot(x,y);
hold on
plot(pop,pop.*sin(3*pi.*pop),'*r');
hold off

[best_ind,~] = bestOpt(pop,Fitness);
disp(['获得最优解(取得最大值的点):',num2str(best_ind(1))]);
X = best_ind;

%在图中标注最大值
figure
x=linspace(-1,2,1000);
y=x.*sin(3*pi.*x);
plot(x,y);
hold on
plot(X,X.*sin(3*pi.*X),'*r');
hold off

function pop = initializePop(popSize,Vnum,Bound)
%   初始化种群函数
   pop = rand(popSize,Vnum)*(Bound(1)-Bound(2))+Bound(2); 
end

function [Fitness] = cal_Fitness(pop)
%本函数用于计算适应度值,返回各个体的适应度值
[popsize,~] = size(pop);
Fitness = zeros(popsize,1);
for i=1:popsize
    x = pop(i,:);
    Fitness(i) =1/(2-x.*sin(3*pi.*x));
end
end

function [best_ind,best_fitness] = bestOpt(pop,Fitness)
%BESTOPT 计算最优k个个体
[~,index] = sort(Fitness,'descend');
best_ind = pop(index(1:size(pop,1)/10),:);
best_fitness = Fitness(index(1:size(pop,1)/10));
end

function [newpop] = crossoverOpt(pop,crossoverRate,Bound)
%   实现交叉操做 策略:模拟二进制
[popSize,Vnum] = size(pop);
ubound = ones(1,Vnum)*Bound(1);
lbound = ones(1,Vnum)*Bound(2);
newpop = zeros(popSize,Vnum);
miu = 1;

for i = 1:2:popSize-1
        x1 = pop(i);
        x2 = pop(i+1);
        y1 = x1;
        y2 = x2;
    if(rand(1)<crossoverRate)
         for j = 1:Vnum
            randnum = rand(1);
            if randnum<=0.5
                beta = (randnum*2)^(1/(1+miu));
            else
                beta = 1/((2*(1-randnum))^(1/(1+miu)));
            end
            y1(j) = 0.5*((1+beta)*x1(j)+(1-beta)*x2(j));
            y2(j) = 0.5*((1-beta)*x1(j)+(1+beta)*x2(j));
        end
        %溢出检查
        y1(y1>ubound) = ubound;
        y1(y1<lbound) = lbound;
        y2(y2>ubound) = ubound;
        y2(y2<lbound) = lbound;
        newpop(i,:) = y1;
        newpop(i+1,:) = y2;
    else
        newpop(i,:) = y1;
        newpop(i+1,:) = y2;
    end
end
end

function [newpop] = mutationOpt(pop,mutationRate,Bound)
%   变异操做,多项式变异
[popSize,Vnum] = size(pop);
newpop = zeros(popSize,Vnum);
ubound = ones(1,Vnum)*Bound(1);
lbound = ones(1,Vnum)*Bound(2);

miu2 = 5;%变异参数
for i = 1:popSize
    x = pop(i,:);
    if(rand(1)<mutationRate)
        for j = 1:Vnum
            y = x;
            randnum = rand(1);
            if(randnum<0.5)
                delta = (2*randnum)^(1/(miu2+1))-1;
            else
                delta = 1-(2*(1-randnum))^(1/(miu2+1));
            end
                y(j)=y(j)+delta;
        end
        %溢出检查
        y(y>ubound) = ubound;
        y(y<lbound) = lbound;
        newpop(i,:) = y;
    else
          newpop(i,:) = x;
    end
end
end

function [pop,Fitness] = Newparents(pop,Fitness,best_ind,best_fitness)
%NEWPARENTS 构成新父代
    [~,index] = sort(Fitness);
    pop(index(1:size(best_ind,1)),:) = best_ind;
    Fitness(index(1:size(best_ind,1))) = best_fitness;
end


相关文章
相关标签/搜索