?? 遺傳算法程序 matlab.txt
字號:
遺傳算法程序:
說明: fga.m 為遺傳算法的主程序; 采用二進(jìn)制Gray編碼,采用基于輪盤賭法的非線性排名選擇, 均勻交叉,變異操作,而且還引入了倒位操作!
function [BestPop,Trace]=fga(FUN,LB,UB,eranum,popsize,pCross,pMutation,pInversion,options)
% [BestPop,Trace]=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation)
% Finds a maximum of a function of several variables.
% fmaxga solves problems of the form:
% max F(X) subject to: LB <= X <= UB
% BestPop - 最優(yōu)的群體即為最優(yōu)的染色體群
% Trace - 最佳染色體所對應(yīng)的目標(biāo)函數(shù)值
% FUN - 目標(biāo)函數(shù)
% LB - 自變量下限
% UB - 自變量上限
% eranum - 種群的代數(shù),取100--1000(默認(rèn)200)
% popsize - 每一代種群的規(guī)模;此可取50--200(默認(rèn)100)
% pcross - 交叉概率,一般取0.5--0.85之間較好(默認(rèn)0.8)
% pmutation - 初始變異概率,一般取0.05-0.2之間較好(默認(rèn)0.1)
% pInversion - 倒位概率,一般取0.05-0.3之間較好(默認(rèn)0.2)
% options - 1*2矩陣,options(1)=0二進(jìn)制編碼(默認(rèn)0),option(1)~=0十進(jìn)制編
%碼,option(2)設(shè)定求解精度(默認(rèn)1e-4)
%
% ------------------------------------------------------------------------
T1=clock;
if nargin<3, error('FMAXGA requires at least three input arguments'); end
if nargin==3, eranum=200;popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[0 1e-4];end
if nargin==4, popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[0 1e-4];end
if nargin==5, pCross=0.8;pMutation=0.1;pInversion=0.15;options=[0 1e-4];end
if nargin==6, pMutation=0.1;pInversion=0.15;options=[0 1e-4];end
if nargin==7, pInversion=0.15;options=[0 1e-4];end
if find((LB-UB)>0)
error('數(shù)據(jù)輸入錯(cuò)誤,請重新輸入(LB<UB):');
end
s=sprintf('程序運(yùn)行需要約%.4f 秒鐘時(shí)間,請稍等......',(eranum*popsize/1000));
disp(s);
global m n NewPop children1 children2 VarNum
bounds=[LB;UB]';bits=[];VarNum=size(bounds,1);
precision=options(2);%由求解精度確定二進(jìn)制編碼長度
bits=ceil(log2((bounds(:,2)-bounds(:,1))' ./ precision));%由設(shè)定精度劃分區(qū)間
[Pop]=InitPopGray(popsize,bits);%初始化種群
[m,n]=size(Pop);
NewPop=zeros(m,n);
children1=zeros(1,n);
children2=zeros(1,n);
pm0=pMutation;
BestPop=zeros(eranum,n);%分配初始解空間BestPop,Trace
Trace=zeros(eranum,length(bits)+1);
i=1;
while i<=eranum
for j=1:m
value(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%計(jì)算適應(yīng)度
end
[MaxValue,Index]=max(value);
BestPop(i,:)=Pop(Index,:);
Trace(i,1)=MaxValue;
Trace(i,(2:length(bits)+1))=b2f(BestPop(i,:),bounds,bits);
[selectpop]=NonlinearRankSelect(FUN,Pop,bounds,bits);%非線性排名選擇
[CrossOverPop]=CrossOver(selectpop,pCross,round(unidrnd(eranum-i)/eranum));
%采用多點(diǎn)交叉和均勻交叉,且逐步增大均勻交叉的概率
%round(unidrnd(eranum-i)/eranum)
[MutationPop]=Mutation(CrossOverPop,pMutation,VarNum);%變異
[InversionPop]=Inversion(MutationPop,pInversion);%倒位
Pop=InversionPop;%更新
pMutation=pm0+(i^4)*(pCross/3-pm0)/(eranum^4);
%隨著種群向前進(jìn)化,逐步增大變異率至1/2交叉率
p(i)=pMutation;
i=i+1;
end
t=1:eranum;
plot(t,Trace(:,1)');
title('函數(shù)優(yōu)化的遺傳算法');xlabel('進(jìn)化世代數(shù)(eranum)');ylabel('每一代最優(yōu)適應(yīng)度(maxfitness)');
[MaxFval,I]=max(Trace(:,1));
X=Trace(I,(2:length(bits)+1));
hold on; plot(I,MaxFval,'*');
text(I+5,MaxFval,['FMAX=' num2str(MaxFval)]);
str1=sprintf('進(jìn)化到 %d 代 ,自變量為 %s 時(shí),得本次求解的最優(yōu)值 %f\n對應(yīng)染色體是:%s',I,num2str(X),MaxFval,num2str(BestPop(I,:)));
disp(str1);
%figure(2);plot(t,p);%繪制變異值增大過程
T2=clock;
elapsed_time=T2-T1;
if elapsed_time(6)<0
elapsed_time(6)=elapsed_time(6)+60; elapsed_time(5)=elapsed_time(5)-1;
end
if elapsed_time(5)<0
elapsed_time(5)=elapsed_time(5)+60;elapsed_time(4)=elapsed_time(4)-1;
end %像這種程序當(dāng)然不考慮運(yùn)行上小時(shí)啦
str2=sprintf('程序運(yùn)行耗時(shí) %d 小時(shí) %d 分鐘 %.4f 秒',elapsed_time(4),elapsed_time(5),elapsed_time(6));
disp(str2);
%初始化種群
%采用二進(jìn)制Gray編碼,其目的是為了克服二進(jìn)制編碼的Hamming懸崖缺點(diǎn)
function [initpop]=InitPopGray(popsize,bits)
len=sum(bits);
initpop=zeros(popsize,len);%The whole zero encoding individual
for i=2:popsize-1
pop=round(rand(1,len));
pop=mod(([0 pop]+[pop 0]),2);
%i=1時(shí),b(1)=a(1);i>1時(shí),b(i)=mod(a(i-1)+a(i),2)
%其中原二進(jìn)制串:a(1)a(2)...a(n),Gray串:b(1)b(2)...b(n)
initpop(i,:)=pop(1:end-1);
end
initpop(popsize,:)=ones(1,len);%The whole one encoding individual
%解碼
function [fval] = b2f(bval,bounds,bits)
% fval - 表征各變量的十進(jìn)制數(shù)
% bval - 表征各變量的二進(jìn)制編碼串
% bounds - 各變量的取值范圍
% bits - 各變量的二進(jìn)制編碼長度
scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1); %The range of the variables
numV=size(bounds,1);
cs=[0 cumsum(bits)];
for i=1:numV
a=bval((cs(i)+1):cs(i+1));
fval(i)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(i)+bounds(i,1);
end
%選擇操作
%采用基于輪盤賭法的非線性排名選擇
%各個(gè)體成員按適應(yīng)值從大到小分配選擇概率:
%P(i)=(q/1-(1-q)^n)*(1-q)^i, 其中 P(0)>P(1)>...>P(n), sum(P(i))=1
function [selectpop]=NonlinearRankSelect(FUN,pop,bounds,bits)
global m n
selectpop=zeros(m,n);
fit=zeros(m,1);
for i=1:m
fit(i)=feval(FUN(1,:),(b2f(pop(i,:),bounds,bits)));%以函數(shù)值為適應(yīng)值做排名依據(jù)
end
selectprob=fit/sum(fit);%計(jì)算各個(gè)體相對適應(yīng)度(0,1)
q=max(selectprob);%選擇最優(yōu)的概率
x=zeros(m,2);
x(:,1)=[m:-1:1]';
[y x(:,2)]=sort(selectprob);
r=q/(1-(1-q)^m);%標(biāo)準(zhǔn)分布基值
newfit(x(:,2))=r*(1-q).^(x(:,1)-1);%生成選擇概率
newfit=cumsum(newfit);%計(jì)算各選擇概率之和
rNums=sort(rand(m,1));
fitIn=1;newIn=1;
while newIn<=m
if rNums(newIn)<newfit(fitIn)
selectpop(newIn,:)=pop(fitIn,:);
newIn=newIn+1;
else
fitIn=fitIn+1;
end
end
%交叉操作
function [NewPop]=CrossOver(OldPop,pCross,opts)
%OldPop為父代種群,pcross為交叉概率
global m n NewPop
r=rand(1,m);
y1=find(r<pCross);
y2=find(r>=pCross);
len=length(y1);
if len>2&mod(len,2)==1%如果用來進(jìn)行交叉的染色體的條數(shù)為奇數(shù),將其調(diào)整為偶數(shù)
y2(length(y2)+1)=y1(len);
y1(len)=[];
end
if length(y1)>=2
for i=0:2:length(y1)-2
if opts==0
[NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=EqualCrossOver(OldPop(y1(i+1),:),OldPop(y1(i+2),:));
else
[NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=MultiPointCross(OldPop(y1(i+1),:),OldPop(y1(i+2),:));
end
end
end
NewPop(y2,:)=OldPop(y2,:);
%采用均勻交叉
function [children1,children2]=EqualCrossOver(parent1,parent2)
global n children1 children2
hidecode=round(rand(1,n));%隨機(jī)生成掩碼
crossposition=find(hidecode==1);
holdposition=find(hidecode==0);
children1(crossposition)=parent1(crossposition);%掩碼為1,父1為子1提供基因
children1(holdposition)=parent2(holdposition);%掩碼為0,父2為子1提供基因
children2(crossposition)=parent2(crossposition);%掩碼為1,父2為子2提供基因
children2(holdposition)=parent1(holdposition);%掩碼為0,父1為子2提供基因
%采用多點(diǎn)交叉,交叉點(diǎn)數(shù)由變量數(shù)決定
function [Children1,Children2]=MultiPointCross(Parent1,Parent2)
global n Children1 Children2 VarNum
Children1=Parent1;
Children2=Parent2;
Points=sort(unidrnd(n,1,2*VarNum));
for i=1:VarNum
Children1(Points(2*i-1):Points(2*i))=Parent2(Points(2*i-1):Points(2*i));
Children2(Points(2*i-1):Points(2*i))=Parent1(Points(2*i-1):Points(2*i));
end
%變異操作
function [NewPop]=Mutation(OldPop,pMutation,VarNum)
global m n NewPop
r=rand(1,m);
position=find(r<=pMutation);
len=length(position);
if len>=1
for i=1:len
k=unidrnd(n,1,VarNum); %設(shè)置變異點(diǎn)數(shù),一般設(shè)置1點(diǎn)
for j=1:length(k)
if OldPop(position(i),k(j))==1
OldPop(position(i),k(j))=0;
else
OldPop(position(i),k(j))=1;
end
end
end
end
NewPop=OldPop;
%倒位操作
function [NewPop]=Inversion(OldPop,pInversion)
global m n NewPop
NewPop=OldPop;
r=rand(1,m);
PopIn=find(r<=pInversion);
len=length(PopIn);
if len>=1
for i=1:len
d=sort(unidrnd(n,1,2));
if d(1)~=1&d(2)~=n
NewPop(PopIn(i),1:d(1)-1)=OldPop(PopIn(i),1:d(1)-1);
NewPop(PopIn(i),d(1):d(2))=OldPop(PopIn(i),d(2):-1:d(1));
NewPop(PopIn(i),d(2)+1:n)=OldPop(PopIn(i),d(2)+1:n);
end
end
end
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -