遗传算法程序(一):
说明: fga.m 为遗传算法的主程序; 采用二进制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 - 最优的群体即为最优的染色体群
% Trace - 最佳染色体所对应的目标函数值
% FUN - 目标函数
% LB - 自变量下限
% UB - 自变量上限
% eranum - 种群的代数,取100--1000(默认200)
% popsize - 每一代种群的规模;此可取50--200(默认100)
% pcross - 交叉概率,一般取0.5--0.85之间较好(默认0.8)
% pmutation - 初始变异概率,一般取0.05-0.2之间较好(默认0.1)
% pInversion - 倒位概率,一般取0.05-0.3之间较好(默认0.2)
% options - 1*2矩阵,options(1)=0二进制编码(默认0),option(1)~=0十进制编
%码,option(2)设定求解精度(默认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('数据输入错误,请重新输入(LB1时,b(i)=mod(a(i-1)+a(i),2)
%其中原二进制串: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 - 表征各变量的十进制数
% bval - 表征各变量的二进制编码串
% bounds - 各变量的取值范围
% bits - 各变量的二进制编码长度
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
%选择操作
%采用基于轮盘赌法的非线性排名选择
%各个体成员按适应值从大到小分配选择概率:
%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)));%以函数值为适应值做排名依据
end
selectprob=fit/sum(fit);%计算各个体相对适应度(0,1)
q=max(selectprob);%选择最优的概率
x=zeros(m,2);
x(:,1)=[m:-1:1]';
[y x(:,2)]=sort(selectprob);
r=q/(1-(1-q)^m);%标准分布基值
newfit(x(:,2))=r*(1-q).^(x(:,1)-1);%生成选择概率
newfit=cumsum(newfit);%计算各选择概率之和
rNums=sort(rand(m,1));
fitIn=1;newIn=1;
while newIn<=m
if rNums(newIn)=pCross);
len=length(y1);
if len>2&mod(len,2)==1%如果用来进行交叉的染色体的条数为奇数,将其调整为偶数
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));%随机生成掩码
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提供基因
%采用多点交叉,交叉点数由变量数决定
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); %设置变异点数,一般设置1点
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
遗传算法程序(二):
function youhuafun
D=code;
N=50; % Tunable
maxgen=50; % Tunable
crossrate=0.5; %Tunable
muterate=0.08; %Tunable
generation=1;
num = length(D);
fatherrand=randint(num,N,3);
score = zeros(maxgen,N);
while generation<=maxgen
ind=randperm(N-2)+2; % 随机配对交叉
A=fatherrand(:,ind(1:(N-2)/2));
B=fatherrand(:,ind((N-2)/2+1:end));
% 多点交叉
rnd=rand(num,(N-2)/2);
ind=rnd tmp=A(ind);
A(ind)=B(ind);
B(ind)=tmp;
% % 两点交叉
% for kk=1:(N-2)/2
% rndtmp=randint(1,1,num)+1;
% tmp=A(1:rndtmp,kk);
% A(1:rndtmp,kk)=B(1:rndtmp,kk);
% B(1:rndtmp,kk)=tmp;
% end
fatherrand=[fatherrand(:,1:2),A,B];
% 变异
rnd=rand(num,N);
ind=rnd [m,n]=size(ind);
tmp=randint(m,n,2)+1;
tmp(:,1:2)=0;
fatherrand=tmp+fatherrand;
fatherrand=mod(fatherrand,3);
% fatherrand(ind)=tmp;
%评价、选择
scoreN=scorefun(fatherrand,D);% 求得N个个体的评价函数
score(generation,:)=scoreN;
[scoreSort,scoreind]=sort(scoreN);
sumscore=cumsum(scoreSort);
sumscore=sumscore./sumscore(end);
childind(1:2)=scoreind(end-1:end);
for k=3:N
tmprnd=rand;
tmpind=tmprnd difind=[0,diff(tmpind)];
if ~any(difind)
difind(1)=1;
end
childind(k)=scoreind(logical(difind));
end
fatherrand=fatherrand(:,childind);
generation=generation+1;
end
% score
maxV=max(score,[],2);
minV=11*300-maxV;
plot(minV,'*');title('各代的目标函数值');
F4=D(:,4);
FF4=F4-fatherrand(:,1);
FF4=max(FF4,1);
D(:,5)=FF4;
save DData D
function D=code
load youhua.mat
% properties F2 and F3
F1=A(:,1);
F2=A(:,2);
F3=A(:,3);
if (max(F2)>1450)||(min(F2)<=900)
error('DATA property F2 exceed it''s range (900,1450]')
end
% get group property F1 of data, according to F2 value
F4=zeros(size(F1));
for ite=11:-1:1
index=find(F2<=900+ite*50);
F4(index)=ite;
end
D=[F1,F2,F3,F4];
function ScoreN=scorefun(fatherrand,D)
F3=D(:,3);
F4=D(:,4);
N=size(fatherrand,2);
FF4=F4*ones(1,N);
FF4rnd=FF4-fatherrand;
FF4rnd=max(FF4rnd,1);
ScoreN=ones(1,N)*300*11;
% 这里有待优化
for k=1:N
FF4k=FF4rnd(:,k);
for ite=1:11
F0index=find(FF4k==ite);
if ~isempty(F0index)
tmpMat=F3(F0index);
tmpSco=sum(tmpMat);
ScoreBin(ite)=mod(tmpSco,300);
end
end
Scorek(k)=sum(ScoreBin);
end
ScoreN=ScoreN-Scorek;
遗传算法程序(三):
%IAGA
function best=ga
clear
MAX_gen=200; %最大迭代步数
best.max_f=0; %当前最大的适应度
STOP_f=14.5; %停止循环的适应度
RANGE=[0 255]; %初始取值范围[0 255]
SPEEDUP_INTER=5; %进入加速迭代的间隔
advance_k=0; %优化的次数
popus=init; %初始化
for gen=1:MAX_gen
fitness=fit(popus,RANGE); %求适应度
f=fitness.f;
picked=choose(popus,fitness); %选择
popus=intercross(popus,picked); %杂交
popus=aberrance(popus,picked); %变异
if max(f)>best.max_f
advance_k=advance_k+1;
x_better(advance_k)=fitness.x;
best.max_f=max(f);
best.popus=popus;
best.x=fitness.x;
end
if mod(advance_k,SPEEDUP_INTER)==0
RANGE=minmax(x_better);
RANGE
advance=0;
end
end
return;
function popus=init%初始化
M=50;%种群个体数目
N=30;%编码长度
popus=round(rand(M,N));
return;
function fitness=fit(popus,RANGE)%求适应度
[M,N]=size(popus);
fitness=zeros(M,1);%适应度
f=zeros(M,1);%函数值
A=RANGE(1);B=RANGE(2);%初始取值范围[0 255]
for m=1:M
x=0;
for n=1:N
x=x+popus(m,n)*(2^(n-1));
end
x=x*((B-A)/(2^N))+A;
for k=1:5
f(m,1)=f(m,1)-(k*sin((k+1)*x+k));
end
end
f_std=(f-min(f))./(max(f)-min(f));%函数值标准化
fitness.f=f;fitness.f_std=f_std;fitness.x=x;
return;
function picked=choose(popus,fitness)%选择
f=fitness.f;f_std=fitness.f_std;
[M,N]=size(popus);
choose_N=3; %选择choose_N对双亲
picked=zeros(choose_N,2); %记录选择好的双亲
p=zeros(M,1); %选择概率
d_order=zeros(M,1);
%把父代个体按适应度从大到小排序
f_t=sort(f,'descend');%将适应度按降序排列
for k=1:M
x=find(f==f_t(k));%降序排列的个体序号
d_order(k)=x(1);
end
for m=1:M
popus_t(m,:)=popus(d_order(m),:);
end
popus=popus_t;
f=f_t;
p=f_std./sum(f_std); %选择概率
c_p=cumsum(p)'; %累积概率
for cn=1:choose_N
picked(cn,1)=roulette(c_p); %轮盘赌
picked(cn,2)=roulette(c_p); %轮盘赌
popus=intercross(popus,picked(cn,:));%杂交
end
popus=aberrance(popus,picked);%变异
return;
function popus=intercross(popus,picked) %杂交
[M_p,N_p]=size(picked);
[M,N]=size(popus);
for cn=1:M_p
p(1)=ceil(rand*N);%生成杂交位置
p(2)=ceil(rand*N);
p=sort(p);
t=popus(picked(cn,1),p(1):p(2));
popus(picked(cn,1),p(1):p(2))=popus(picked(cn,2),p(1):p(2));
popus(picked(cn,2),p(1):p(2))=t;
end
return;
function popus=aberrance(popus,picked) %变异
P_a=0.05;%变异概率
[M,N]=size(popus);
[M_p,N_p]=size(picked);
U=rand(1,2);
for kp=1:M_p
if U(2)>=P_a %如果大于变异概率,就不变异
continue;
end
if U(1)>=0.5
a=picked(kp,1);
else
a=picked(kp,2);
end
p(1)=ceil(rand*N);%生成变异位置
p(2)=ceil(rand*N);
if popus(a,p(1))==1%0 1变换
popus(a,p(1))=0;
else
popus(a,p(1))=1;
end
if popus(a,p(2))==1
popus(a,p(2))=0;
else
popus(a,p(2))=1;
end
end
return;
function picked=roulette(c_p) %轮盘赌
[M,N]=size(c_p);
M=max([M N]);
U=rand;
if Uc_p(m) & U
全方位的两点杂交、两点变异的改进的加速遗传算法(IAGA)