三个遗传算法程序精品毕业设计完整版Word格式.docx
《三个遗传算法程序精品毕业设计完整版Word格式.docx》由会员分享,可在线阅读,更多相关《三个遗传算法程序精品毕业设计完整版Word格式.docx(22页珍藏版)》请在冰豆网上搜索。
![三个遗传算法程序精品毕业设计完整版Word格式.docx](https://file1.bdocx.com/fileroot1/2022-12/7/39fb1d74-da4f-4106-8ec8-16b2ec158a72/39fb1d74-da4f-4106-8ec8-16b2ec158a721.gif)
error('
数据输入错误,请重新输入(LB<
UB):
'
s=sprintf('
程序运行需要约%.4f秒钟时间,请稍等......'
(eranum*popsize/1000));
disp(s);
globalmnNewPopchildren1children2VarNum
bounds=[LB;
UB]'
;
bits=[];
VarNum=size(bounds,1);
precision=options
(2);
%由求解精度确定二进制编码长度
bits=ceil(log2((bounds(:
2)-bounds(:
1))'
./precision));
%由设定精度划分区间
[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;
whilei<
=eranum
forj=1:
m
value(j)=feval(FUN(1,:
),(b2f(Pop(j,:
),bounds,bits)));
%计算适应度
[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));
%采用多点交叉和均匀交叉,且逐步增大均匀交叉的概率
%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);
%随着种群向前进化,逐步增大变异率至1/2交叉率
p(i)=pMutation;
i=i+1;
t=1:
eranum;
plot(t,Trace(:
1)'
title('
函数优化的遗传算法'
xlabel('
进化世代数(eranum)'
ylabel('
每一代最优适应度(maxfitness)'
[MaxFval,I]=max(Trace(:
1));
X=Trace(I,(2:
length(bits)+1));
holdon;
plot(I,MaxFval,'
*'
text(I+5,MaxFval,['
FMAX='
num2str(MaxFval)]);
str1=sprintf('
进化到%d代,自变量为%s时,得本次求解的最优值%f\n对应染色体是:
%s'
I,num2str(X),MaxFval,num2str(BestPop(I,:
)));
disp(str1);
%figure
(2);
plot(t,p);
%绘制变异值增大过程
T2=clock;
elapsed_time=T2-T1;
ifelapsed_time(6)<
elapsed_time(6)=elapsed_time(6)+60;
elapsed_time(5)=elapsed_time(5)-1;
ifelapsed_time(5)<
elapsed_time(5)=elapsed_time(5)+60;
elapsed_time(4)=elapsed_time(4)-1;
end%像这种程序当然不考虑运行上小时啦
str2=sprintf('
程序运行耗时%d小时%d分钟%.4f秒'
elapsed_time(4),elapsed_time(5),elapsed_time(6));
disp(str2);
%采用二进制Gray编码,其目的是为了克服二进制编码的Hamming悬崖缺点
function[initpop]=InitPopGray(popsize,bits)
len=sum(bits);
initpop=zeros(popsize,len);
%Thewholezeroencodingindividual
fori=2:
popsize-1
pop=round(rand(1,len));
pop=mod(([0pop]+[pop0]),2);
%i=1时,b
(1)=a
(1);
i>
1时,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);
initpop(popsize,:
)=ones(1,len);
%Thewholeoneencodingindividual
%解码
function[fval]=b2f(bval,bounds,bits)
%fval-表征各变量的十进制数
%bval-表征各变量的二进制编码串
%bounds-各变量的取值范围
%bits-各变量的二进制编码长度
scale=(bounds(:
./(2.^bits-1);
%Therangeofthevariables
numV=size(bounds,1);
cs=[0cumsum(bits)];
fori=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);
%选择操作
%采用基于轮盘赌法的非线性排名选择
%各个体成员按适应值从大到小分配选择概率:
%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)
globalmn
selectpop=zeros(m,n);
fit=zeros(m,1);
fit(i)=feval(FUN(1,:
),(b2f(pop(i,:
%以函数值为适应值做排名依据
selectprob=fit/sum(fit);
%计算各个体相对适应度(0,1)
q=max(selectprob);
%选择最优的概率
x=zeros(m,2);
x(:
1)=[m:
1]'
[yx(:
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;
whilenewIn<
=m
ifrNums(newIn)<
newfit(fitIn)
selectpop(newIn,:
)=pop(fitIn,:
newIn=newIn+1;
else
fitIn=fitIn+1;
%交叉操作
function[NewPop]=CrossOver(OldPop,pCross,opts)
%OldPop为父代种群,pcross为交叉概率
globalmnNewPop
r=rand(1,m);
y1=find(r<
pCross);
y2=find(r>
=pCross);
len=length(y1);
iflen>
2&
mod(len,2)==1%如果用来进行交叉的染色体的条数为奇数,将其调整为偶数
y2(length(y2)+1)=y1(len);
y1(len)=[];
iflength(y1)>
=2
fori=0:
2:
length(y1)-2
ifopts==0
[NewPop(y1(i+1),:
),NewPop(y1(i+2),:
)]=EqualCrossOver(OldPop(y1(i+1),:
),OldPop(y1(i+2),:
));
)]=MultiPointCross(OldPop(y1(i+1),:
end
NewPop(y2,:
)=OldPop(y2,:
%采用均匀交叉
function[children1,children2]=EqualCrossOver(parent1,parent2)
globalnchildren1children2
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)
globalnChildren1Children2VarNum
Children1=Parent1;
Children2=Parent2;
Points=sort(unidrnd(n,1,2*VarNum));
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):
%变异操作
function[NewPop]=Mutation(OldPop,pMutation,VarNum)
globalmnNewPop
position=find(r<
=pMutation);
len=length(position);
=1
len
k=unidrnd(n,1,VarNum);
%设置变异点数,一般设置1点
length(k)
ifOldPop(position(i),k(j))==1
OldPop(position(i),k(j))=0;
OldPop(position(i),k(j))=1;
NewPop=OldPop;
%倒位操作
function[NewPop]=Inversion(OldPop,pInversion)
PopIn=find(r<
=pInversion);
len=length(PopIn);
d=sort(unidrnd(n,1,2));
ifd
(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):
d
(1));
NewPop(PopIn(i),d
(2)+1:
n)=OldPop(PopIn(i),d
(2)+1:
n);
遗传算法程序
(二):
functionyouhuafun
D=code;
N=50;
%Tunable
maxgen=50;
crossrate=0.5;
%Tunable
muterate=0.08;
generation=1;
num=length(D);
fatherrand=randint(num,N,3);
score=zeros(maxgen,N);
whilegeneration<
=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=rndtmp=A(ind);
A(ind)=B(ind);
B(ind)=tmp;
%%两点交叉
%forkk=1:
(N-2)/2
%rndtmp=randint(1,1,num)+1;
%tmp=A(1:
rndtmp,kk);
%A(1:
rndtmp,kk)=B(1:
%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(:
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);
fork=3:
N
tmprnd=rand;
tmpind=tmprnddifind=[0,diff(tmpind)];
if~any(difind)
difind
(1)=1;
childind(k)=scoreind(logical(difind));
fatherrand=fatherrand(:
childind);
generation=generation+1;
%score
maxV=max(score,[],2);
minV=11*300-maxV;
plot(minV,'
各代的目标函数值'
F4=D(:
4);
FF4=F4-fatherrand(:
1);
FF4=max(FF4,1);
D(:
5)=FF4;
saveDDataD
functionD=code
loadyouhua.mat
%propertiesF2andF3
F1=A(:
F2=A(:
2);
F3=A(:
3);
if(max(F2)>
1450)||(min(F2)<
=900)
DATApropertyF2exceedit'
srange(900,1450]'
)
%getgrouppropertyF1ofdata,accordingtoF2value
F4=zeros(size(F1));
forite=11:
1
index=find(F2<
=900+ite*50);
F4(index)=ite;
D=[F1,F2,F3,F4];
functionScoreN=scorefun(fatherrand,D)
F3=D(:
N=size(fatherrand,2);
FF4=F4*ones(1,N);
FF4rnd=FF4-fatherrand;
FF4rnd=max(FF4rnd,1);
ScoreN=ones(1,N)*300*11;
%这里有待优化
fork=1:
FF4k=FF4rnd(:
k);
forite=1:
11
F0index=find(FF4k==ite);
if~isempty(F0index)
tmpMat=F3(F0index);
tmpSco=sum(tmpMat);
ScoreBin(ite)=mod(tmpSco,300);
Scorek(k)=sum(ScoreBin);
ScoreN=ScoreN-Scorek;
遗传算法程序(三):
%IAGA
functionbest=ga
clear
MAX_gen=200;
%最大迭代步数
best.max_f=0;
%当前最大的适应度
STOP_f=14.5;
%停止循环的适应度
RANGE=[0255];
%初始取值范围[0255]
SPEEDUP_INTER=5;
%进入加速迭代的间隔
advance_k=0;
%优化的次数
popus=init;
%初始化
forgen=1:
MAX_gen
fitness=fit(popus,RANGE);
%求适应度
f=fitness.f;
picked=choose(popus,fitness);
%选择
popus=intercross(popus,picked);
%杂交
popus=aberrance(popus,picked);
%变异
ifmax(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;
ifmod(advance_k,SPEEDUP_INTER)==0
RANGE=minmax(x_better);
RANGE
advance=0;
return;
functionpopus=init%初始化
M=50;
%种群个体数目
N=30;
%编码长度
popus=round(rand(M,N));