资源描述:
《MATLAB遗传算法程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、function[bestsole,bestpop]=ga_scomplex(num,bounds,ps,pc,pm,b,dtc)%GA_scomple是基于浮点编码的单纯形遗传算法主程序%num定义种群数量% bounds定义参数取值范围,格式为[Var1min,Var1max;Var2min,Var2max;…….];%ps选择概率%pc交叉概率%pm变异概率%b 变异程度系数%dtc 迭代次数%$Date:2005/5/25$%%email:damao_pq@yahoo.com.cnnumvars=siz
2、e(bounds,1);xzomelength=numvars+1;flag=1;n=1;[pop]=initializega(num,bounds);while(n<=dtc&flag>0.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(:
3、,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));be
4、stsole=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('均值','最优解');to
5、c%%初始种群值生成子程序%%%%%%%%%%%%%%%%%%%%%%%%%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
6、)');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)
7、.^n);fit(x(:,2))=r*(1-q).^(x(:,1)-1);fit=cumsum(fit);rnums=sort(rand(n,1));fitin=1;newin=1;whilenewin<=nif(rnums(newin)8、crossover(bounds,newpop,ops)pc=ops;sn=size(newpop,1);sz=size(newpop,2)-1;nc=pc*sn*sz;cs=1;whilecs<=ncnsite=round(rand(2,1).*sn+0.5);zsite=round(rand*sz+0.5);r=rand;newpop(nsite,zsite)=[r,1-r;1-r