MATLAB遗传算法程序.doc_第1页
MATLAB遗传算法程序.doc_第2页
MATLAB遗传算法程序.doc_第3页
MATLAB遗传算法程序.doc_第4页
MATLAB遗传算法程序.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

function bestsole,bestpop=ga_scomplex(num,bounds,ps,pc,pm,b,dtc)% GAscomple是基于浮点编码的单纯形遗传算法主程序% num定义种群数量%bounds定义参数取值范围,格式为Var1min,Var1max;Var2min,Var2max;.;%ps选择概率%pc交叉概率%pm变异概率%b变异程度系数%dtc迭代次数% $Date: 2005/5/25 $% %email:damao_numvars=size(bounds,1);xzomelength=numvars+1;flag=1;n=1;pop=initializega(num,bounds);while (n0.00001) newpop=select(pop,ps); crosspop=crossover(bounds,newpop,pc); mupop=mutation(crosspop,bounds,pm,b,n,dtc); spop=newpop;crosspop;mupop; y,x=sort(spop(:,xzomelength); spop=spop(x,:); n_spop=size(spop,1); bestpop=spop(n_spop:-1:n_spop-num+1,:); scopop=scomplex(bounds,bestpop(1,1:numvars), 100); %加入单纯形算法 bestpop(num,:)=scopop; bestpop(num,xzomelength)=-scopop(xzomelength); trace1(n)=mean(bestpop(:,xzomelength); bestsole=bestpop(1,1:numvars); trace2(n)=bestpop(1,xzomelength); flag=abs(trace2(n)-trace1(n); bmax=max(bestpop(:,1:numvars); bmin=min(bestpop(:,1:numvars); bounds=bmin,bmax; pop=bestpop; n=n+1;endt=1:n-1;plot(t,trace1,r-,t,trace2,g-);legend(均值,最优解);toc%初始种群值生成子程序%function pop=initializega(num,bounds)numvars=size(bounds,1);rng=(bounds(:,2)-bounds(:,1);xzomelength=numvars+1;pop=zeros(num,xzomelength);pop(:,1:numvars)=(ones(num,1)*rng).*(rand(num,numvars)+(ones(num,1)*bounds(:,1);pop(:,xzomelength)=fitness(pop(:,1:numvars);%选择子程序%5function newpop=select(oldpop,options)q=options;e=size(oldpop,2);n=size(oldpop,1);fit=zeros(n,1);x=zeros(n,2);x(:,1)=n:-1:1;y,x(:,2)=sort(oldpop(:,e);r=q/(1-(1-q).n);fit(x(:,2)=r*(1-q).(x(:,1)-1);fit=cumsum(fit);rnums=sort(rand(n,1);fitin=1;newin=1;while newin=n if(rnums(newin)fit(fitin) newpop(newin,:)=oldpop(fitin,:); newin=newin+1; else fitin=fitin+1; endend%交叉子程序%function crosspop=crossover(bounds,newpop,ops)pc=ops;sn=size(newpop,1);sz=size(newpop,2)-1;nc=pc*sn*sz;cs=1;while cs1 t=0.99; disp(sprintf(max gen %d current gen %d setting ratio = 1,gm,gc);endn=1;while n=nm nsite=round(rand*sn+0.5); zsite=round(rand*sz+0.5); md=round(rand); if md y=bounds(zsite,2)-crosspop(nsite,zsite); dvl=y.*(rand.*(1-t).b; newvalue=crosspop(nsite,zsite)+dvl; else y=crosspop(nsite,zsite)-bounds(zsite,1); dvl=y.*(rand.*(1-t).b; newvalue=crosspop(nsite,zsite)-dvl; end crosspop(nsite,zsite)=newvalue; crosspop(nsite,xzomelength)=fitness(crosspop(nsite,1:sz); mupop(n,:)=crosspop(nsite,:); n=n+1;end%单纯形子程序%function out=scomplex(bounds,x, maxiter)% step 1alf=1.3 ; %反映系数1belt=0.5; % 收缩系数(0,1)glm=2.0; %延伸,扩展系数1ct=0.5; %紧缩(压缩)系数(0,1)cgm= 0.01; %允许误差0n=size(bounds,1);% 转换区间bs=bounds(:,2)-bounds(:,1);xx=x-bounds(:,1)./bs;%求各参数值a=0.2;q=a.*(sqrt(n+1)-1)./(n.*sqrt(2);p=a.*(sqrt(n+1)+n-1)./(n.*sqrt(2);% 定义n+1个点dx0=xx;dx=ones(n,1)*xx;dx=dx+q;for i=1:n dx(i,i)=dx0(i)+p-q;enddx=dx0;dx;%区间转回px=ones(n+1,1)*bs.*dx+ones(n+1,1)*bounds(:,1);px(:,n+1)=-fitness(px(:,1:n); % 求值 注意此处加了负号 是为求出最小值px(n+2,:)=(sum(px(1:n,:)./n; % 重心px(n+2,n+1)=-fitness(px(n+2,1:n);itercount=1;how=step2;while itercount maxiter % step2 if strcmp(how,step2) %排序 y,t=sort(px(:,n+1); px=px(t,:); how=step3; end % step3 if strcmp(how,step3) px(n+2,:)=(sum(px(1:n,:)./n; % 重心 px(n+2,n+1)=-fitness(px(n+2,1:n); flg=1./(n+1).*sqrt(sum(px(1:n+1,n+1)-px(n+2,n+1).*ones(n+1,1).2); if flgbounds(:,2) | any(px(n+3,1:n)bounds(:,1) xs=xs-.01; px(n+3,:)=px(n+2,:)+xs.*alf.*(px(n+2,:)-px(n+1,:); r1=r1+1 end px(n+3,n+1)=-fitness(px(n+3,1:n); if px(n+3,n+1)px(1,n+1) % goto step5 how=step5; else if px(n+3,n+1)bounds(:,2)| any(px(n+4,1:n)bounds(:,1) xs=xs-0.01; px(n+4,:)=px(n+2,:)+xs.*glm.*(px(n+3,:)-px(n+2,:); r2=r2+1 end px(n+4,n+1)=-fitness(px(n+4,1:n); if px(n+4,n+1)bounds(:,2) | any(px(n+5,1:n)bounds(:,1) xs=xs-.01; px(n+5,:)=px(n+2,:)+0.9*belt.*(px(n+1,:)-px(n+2,:); r3=r3+1 end px(n+5,n+1)=-fitness(px(n+5,1:n); if px(n+5,n+1)ones(n+1,1)*bounds(:,2) any(px(1:n+1,1:n)ones(n+1,1)*bound

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论