1. 程式人生 > >Matlab遺傳演算法求函式最大值

Matlab遺傳演算法求函式最大值

用遺傳演算法求目標函式的最大值

主函式函式main.m

global Bitlength%定義3個全域性變數
global boundsbegin
global boundsend
boundsbegin=-2;
boundsend=2;
precision=0.0001;%運算精確度
Bitlength=ceil(log2((boundsend-boundsbegin)'./precision));%染色體長度
popsize=50;%初始種群大小
Generationmax=12;%最大進化代數
pcrossover=0.90;%交叉概率
pmutation=0.09;%變異概率
population=round(rand(popsize,Bitlength));%隨機生產初始化種群
[Fitvalue,cumsump]=fitness(population); %呼叫子函式1算個體適應度和選擇累積概率 Generation=1; while Generation<Generationmax+1%總共進化12for j=1:2:popsize%每一次生成2個個體,經過25次迴圈生產一個新種群,完成一代進化 seln=selection(population,cumsump);%呼叫子函式2選擇操作,返回選中的2個個體的序號 scro=crossover(population,seln,pcrossover);%呼叫子函式3交叉操作,返回2條染色體
scnew(j,:)=scro(1,:);%儲存交叉操作返回的染色體 scnew(j+1,:)=scro(2,:); smnew(j,:)=mutation(scnew(j,:),pmutation);%呼叫子函式4對交叉操作返回的染色體變異 smnew(j+1,:)=mutation(scnew(j+1,:),pmutation); end population=smnew;%更新種群 [Fitvalue,cumsump]=fitness(population);%呼叫子函式1計算新種群的適應度
[fmax,nmax]=max(Fitvalue);%記錄最佳適應度和其染色序號 fmean=mean(Fitvalue); ymax(Generation)=fmax;%記錄當代最佳適應度 ymean(Generation)=fmean;%記錄當代平均適應度 x=transform2to10(population(nmax,:));%呼叫子函式6轉化為10進位制數 xx=boundsbegin+x*(boundsend-boundsbegin)/(power(boundsend,Bitlength)-1);%將x整合到[-2,2]區間中 xmax(Generation)=xx;%儲存整合後最佳染色體10進位制的值 Generation=Generation+1;%依次迴圈進化12代,得到最佳種群個體 end Generation=Generation-1 Bestpopulation=xx%最佳種群個體,即x的值 Targetmax=targetfun(xx)%呼叫子函式7要求的函式的最大值

子函式1:計算適應度和累積選擇率fitness.m

function [Fitvalue,cumsump]=fitness(population)
global Bitlength
global boundsbegin
global boundsend
popsize=size(population,1);%總共計算多少個個體
for i=1:popsize
    x=transform2to10(population(i,:));%呼叫子函式6轉化為10進位制數
    xx=boundsbegin+x*(boundsend-boundsbegin)/(power((boundsend),Bitlength)-1);
    Fitvalue(i)=targetfun(xx);
end
Fitvalue=Fitvalue'+230;
fsum=sum(Fitvalue);
everypopulation=Fitvalue/fsum;
cumsump(1)=everypopulation(1);
for i=2:popsize
    cumsump(i)=cumsump(i-1)+everypopulation(i);
end
cumsump=cumsump';

子函式2:選擇操作函式selection.m

function seln=selection(population,cumsump)
%子函式選擇操作,從種群中隨機選擇2個個體
for i=1:2
    r=rand;
    j=1;
    prand=cumsump-r;
    while prand(j)<0
        j=j+1;
    end
    seln(i)=j;%選擇個體的序號
end

子函式3:交叉操作函式crossover.m

function scro=crossover(population,seln,pc)
global Bitlength
pcc=IfcroORmut(pc);%呼叫子函式5判斷是否進行交叉
if pcc==1%進行交叉
    c=round(rand*(Bitlength-2))+1;%在[1,Bitlength-1]範圍內隨機產生一個交叉位
    scro(1,:)=[population(seln(1),1:c) population(seln(2),c+1:Bitlength)];
    scro(2,:)=[population(seln(2),1:c) population(seln(1),c+1:Bitlength)];
else%不交叉,返回原值
    scro(1,:)=population(seln(1),:);
    scro(2,:)=population(seln(2),:);
end

子函式4:變異操作mutation.m

function smnew=mutation(scnew,pmutation)
global Bitlength
smnew=scnew;
paa=IfcroORmut(pmutation);呼叫子函式5,判斷是否進行變異
if paa==1
    v=round(rand*(Bitlength-1))+1;%在[1,Bitlength]中選擇一個變異位
    smnew(v)=abs(scnew(v)-1);%將smnew中第V個位置變異 
end

子函式5:判斷是否進行交叉或者變異IfcroORmut.m

function pcc=IfcroORmut(mutORcro)
judge(1:100)=0;
L=round(100*mutORcro);
judge(1:L)=1;
n=round(rand*99)+1;
pcc=judge(n);

子函式6:二進位制轉化為十進位制transform2to10.m

function x=transform2to10(Population)
global Bitlength
x=Population(Bitlength);
for i=1:Bitlength-1
    x=x+Population(Bitlength-i)*power(2,i);
end

子函式7:目標函式即為適應度函式targetfun.m

function y=targetfun(x)
%適應度函式,即原函式
y=200*exp(-0.05*x).*sin(x);

執行結果:
執行結果如圖
當進化了12代後,x約等於1.5時取到最大值185。