MATLAB遗传算法求最大值

  最近疯了,光做这种作业!代码是COPY过来的,然后稍加修改就完工了!本想找找代码的出处的,发现网上各种转来转去,所以也没找到。

  唯一的收获就是花了好几个小时把Matlab的画图函数看了看,看了N遍,终于画了个自己满意的图,就此,感觉Matlab还是非常强大的。

  本事求一个方程的最大值,感觉需要用微分手算,可惜都忘了,不会算。但说回来,还是感觉这个结果又问题,求0-2PI的最大值,方程为targetfun这个中间的。

遗传算法求最大值

最后面附源代码:

文件:IfCroIfMut.m

%子程序:判断遗传运算是否需要进行交叉或变异
function pcc=IfCroIfMut(mutORcro)
test(1:100)=0;
l=round(100*mutORcro);
test(1:l)=1;
n=round(rand*99)+1;
pcc=test(n);

文件:crossover.m

%子程序:新物种交叉操作
function scro=crossover(population,seln,pc)
BitLength=size(population,2);
pcc=IfCroIfMut(pc);%根据交叉概率决定是否进行交叉操作,1则是,0则否
if pcc==1
    chb=round(rand*(BitLength-2))+1;%在[1,BitLength-1]范围内随机产生一个交叉位
    scro(1,:)=[population(seln(1),1:chb) population(seln(2),chb+1:BitLength)];
    scro(2,:)=[population(seln(2),1:chb) population(seln(1),chb+1:BitLength)];
else
    scro(1,:)=population(seln(1),:);
    scro(2,:)=population(seln(2),:);
end

文件:fitnessfun.m

%子程序:计算适应度函数
function [Fitvalue,cumsump]=fitnessfun(population)
global BitLength
global boundsbegin
global boundsend
popsize=size(population,1);%有popsize个个体
for i=1:popsize
    x=transform2to10(population(i,:));%将二进制转换为十进制
    %转化为[0,2]区间的实数
    xx=boundsbegin+x*(boundsend-boundsbegin)/(power(2,BitLength)-1);
    Fitvalue(i)=targetfun(xx);%计算函数值,即适应度
end
%给适应度函数加上一个大小合理的数以便保证种群适应度值为正数
Fitvalue=Fitvalue'+10;
%计算选择概率
fsum=sum(Fitvalue);
Pperpopulation=Fitvalue/fsum;
%计算累计概率
cumsump(1)=Pperpopulation(1);
for i=2:popsize
    cumsump(i)=cumsump(i-1)+Pperpopulation(i);
end
cumsump=cumsump';

文件:mutation.m

%子程序:新种群变异操作
function snnew=mutation(snew,pmutation)
BitLength=size(snew,2);
snnew=snew;
pmm=IfCroIfMut(pmutation);%根据变异概率决定是否进行变异操作,1则是,0则否
if pmm==1
    chb=round(rand*(BitLength-1))+1;%在[1,BitLength]范围内随机产生一个变异位
    snnew(chb)=abs(snew(chb)-1);
end

文件:selection.m

%子程序:新种群选择操作
function seln=selection(population,cumsump)
%从种群中选择两个个体
for i=1:2
    r=rand;%产生一个随机数
    prand=cumsump-r;
    j=1;
    while prand(j)<0
        j=j+1;
    end
    seln(i)=j;%选中个体的序号
end

文件:targetfun.m

function y = targetfun(x)
y = 11*sin(7*x*pi)+7*cos(5*x*pi);

文件:transform2to10.m

%子程序:将二进制数转换为十进制数
function x=transform2to10(Population)
BitLength=size(Population,2);
x=Population(BitLength);
for i=1:BitLength-1
    x=x+Population(BitLength-i)*power(2,i);
end

文件:main.m

function main(popsize,Generationmax,pcrossover,pmutation)
clc;
close all;
global BitLength
global boundsbegin
global boundsend
bounds=[0 2];%一维自变量的取值范围
precision=0.000001;%运算精度
boundsbegin=bounds(:,1);
boundsend=bounds(:,2);
%计算如果满足求解精度至少需要多长的染色体
BitLength=ceil(log2((boundsend-boundsbegin)'./precision));
%popsize=50;%初始种群大小
%Generationmax=50;%最大代数
%pcrossover=0.90;%交配概率
%pmutation=0.09;%变异概率
%产生初始种群
population=round(rand(popsize,BitLength));
%计算适应度值,返回Fitvalue和累计概率cumsump
[Fitvalue,cumsump]=fitnessfun(population);
Generation=1;
while Generation<Generationmax+1
    for j=1:2:popsize
        %选择操作
        seln=selection(population,cumsump);
        %交叉操作
        scro=crossover(population,seln,pcrossover);
        scnew(j,:)=scro(1,:);
        scnew(j+1,:)=scro(2,:);
        %变异操作
        smnew(j,:)=mutation(scnew(j,:),pmutation);
        smnew(j+1,:)=mutation(scnew(j+1,:),pmutation);
    end%产生了新种群
    population=smnew;
    %计算新种群的适应度
    [Fitvalue,cumsump]=fitnessfun(population);
    %记录当前代最好的适应度和平均适应度
    [fmax,nmax]=max(Fitvalue);
    fmean=mean(Fitvalue);
    ymax(Generation)=fmax;
    ymean(Generation)=fmean;
    %记录当前代的最佳染色体个体
    x=transform2to10(population(nmax,:));
    %自变量取值范围是[0 2],需要把经过遗传运算的最佳染色体整合到[0 2]区间
    xx=boundsbegin+x*(boundsend-boundsbegin)/(power(2,BitLength)-1);
    xmax(Generation)=xx;
    yvalue(Generation)=targetfun(xx);
    Generation=Generation+1;
end
Generation=Generation-1;
Bestpopuation=xx;
Besttargetfunvalue=targetfun(xx);
%绘制经过遗传运算后的适应度曲线。一般地,如果进化过程中种群的平均适应度与最大适
%应度在曲线上有相互趋同的形态,表示算法收敛进行得很顺利,没有出现震荡;在这种前
%提下,最大适应度个体连续若干代都没有发生进化表明种群已经成熟
figure(1);
x=1:Generation;
[AX,h1,h2] = plotyy(x,ymean,x,yvalue);
set(get(AX(1),'Ylabel'),'String','适应度');
set(get(AX(2),'Ylabel'),'String','最大值');
set(h1,'linestyle',':','marker','x','color','b');
hold on;
set(h2,'linestyle','-','marker','o','color','r');
hold on;
h3=plot(x,ymax,'-g*');
set(h3);
xlabel('进化代数');
legend([h1,h3,h2],'平均适应度','最大适应度','函数最大值');
title(['初始种群:',num2str(popsize),',最大值:',num2str(max(yvalue)),', 当前最优:',num2str(Besttargetfunvalue)]);
box off;hold off;

23条评论在“MATLAB遗传算法求最大值”

回复 郑州SEO优化   取消