1、error(数据输入错误,请重新输入(LBUB):s=sprintf(程序运行需要约%.4f 秒钟时间,请稍等.,(eranum*popsize/1000);disp(s);global m n NewPop children1 children2 VarNumbounds=LB;UB;bits=;VarNum=size(bounds,1);precision=options(2);%由求解精度确定二进制编码长度bits=ceil(log2(bounds(:,2)-bounds(:,1) ./ precision);%由设定精度划分区间Pop=InitPopGray(popsize,bits)
2、;%初始化种群m,n=size(Pop);NewPop=zeros(m,n);children1=zeros(1,n);children2=zeros(1,n);pm0=pMutation;BestPop=zeros(eranum,n);%分配初始解空间BestPop,TraceTrace=zeros(eranum,length(bits)+1);i=1;while i=eranumfor j=1:mvalue(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits);%计算适应度MaxValue,Index=max(value);BestPop(i,:)=Po
3、p(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(Cross
4、OverPop,pMutation,VarNum);%变异InversionPop=Inversion(MutationPop,pInversion);%倒位Pop=InversionPop;%更新pMutation=pm0+(i4)*(pCross/3-pm0)/(eranum4);%随着种群向前进化,逐步增大变异率至1/2交叉率p(i)=pMutation;i=i+1;t=1:eranum;plot(t,Trace(:,1)title(函数优化的遗传算法xlabel(进化世代数(eranum)ylabel(每一代最优适应度(maxfitness)MaxFval,I=max(Trace(:,
5、1);X=Trace(I,(2:length(bits)+1);hold on; plot(I,MaxFval,*text(I+5,MaxFval,FMAX= num2str(MaxFval);str1=sprintf (进化到 %d 代 ,自变量为 %s 时,得本次求解的最优值 %fn对应染色体是:%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)elapsed_time(6)=
6、elapsed_time(6)+60; elapsed_time(5)=elapsed_time(5)-1;if elapsed_time(5)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);%The whole one encoding individual%解码function fval = b2f(bval,bounds,bits) % fval - 表征各变量的十进制数% bval -
7、表征各变量的二进制编码串% bounds - 各变量的取值范围% bits - 各变量的二进制编码长度scale=(bounds(:./(2.bits-1); %The range of the variablesnumV=size(bounds,1);cs=0 cumsum(bits);for i=1:numVa=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)
8、*(1-q)i, 其中 P(0)P(1).P(n), sum(P(i)=1function selectpop=NonlinearRankSelect(FUN,pop,bounds,bits) global m nselectpop=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:1y x(:,2)=sort(select
9、prob);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=mif rNums(newIn)newfit(fitIn)selectpop(newIn,:)=pop(fitIn,:newIn=newIn+1;elsefitIn=fitIn+1;%交叉操作function NewPop=CrossOver(OldPop,pCross,opts) %OldPop为父
10、代种群,pcross为交叉概率global m n NewPop r=rand(1,m);y1=find(r=pCross);len=length(y1);if len2&mod(len,2)=1%如果用来进行交叉的染色体的条数为奇数,将其调整为偶数y2(length(y2)+1)=y1(len);y1(len)=;if length(y1)=2for i=0:2:length(y1)-2if opts=0NewPop(y1(i+1),:),NewPop(y1(i+2),:)=EqualCrossOver(OldPop(y1(i+1),:),OldPop(y1(i+2),:);)=MultiP
11、ointCross(OldPop(y1(i+1),: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为
12、子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 V
13、arNumChildren1=Parent1;Children2=Parent2;Points=sort(unidrnd(n,1,2*VarNum);VarNumChildren1(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)global m n NewPopposition=find(r=pMut
14、ation);len=length(position);=1lenk=unidrnd(n,1,VarNum); %设置变异点数,一般设置1点length(k)if OldPop(position(i),k(j)=1OldPop(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);if d(1)=1&d(2
15、)=nNewPop(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);遗传算法程序(二):function youhuafunD=code;N=50; % Tunable maxgen=50;crossrate=0.5; %Tunable muterate=0.08;generation=1;num = length(D);fatherrand=ra
16、ndint(num,N,3);score = zeros(maxgen,N);while generation1450)|(min(F2)=900) DATA property F2 exceed its range (900,1450) % get group property F1 of data, according to F2 value F4=zeros(size(F1);for ite=11:1 index=find(F2best.max_fadvance_k=advance_k+1;x_better(advance_k)=fitness.x;best.max_f=max(f);best.popus=popus;best.x=fitness.x;if mod(advance_k,SPEEDUP_INTER)=0RANGE=minmax(x_better);RANGEadvance=0;return;function popus=init%初始化M=50;%种群个体数目N=30;%编码长度popus=round(rand(M,N);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1