您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > 遗传算法MATLAB仿真程序
%function[psoF]=PSO_2D()%FUNCTIONPSO--------USEParticleSwarmOptimizationAlgorithm%globalpresent;%closeall;pop_size=10;%pop_size种群大小part_size=2;%part_size粒子大小,**=n-Dgbest=zeros(1,part_size+1);%gbest当前搜索到的最小的值max_gen=80;%max_gen最大迭代次数region=zeros(part_size,2);%设定搜索空间范围region=[-3,3;-3,3];%**每一维设定不同范围rand('state',sum(100*clock));%重置随机数发生器状态arr_present=ini_pos(pop_size,part_size);%present当前位置,随机初始化,rand()的范围为0~1v=ini_v(pop_size,part_size);%初始化当前速度pbest=zeros(pop_size,part_size+1);%pbest粒子以前搜索到的最优值,最后一列包括这些值的适应度w_max=0.9;%w_max权系数最大值w_min=0.4;v_max=2;%**最大速度,为粒子的范围宽度c1=2;%学习因子c2=2;%学习因子best_record=zeros(1,max_gen);%best_record记录最好的粒子的适应度。%————————————————————————%计算原始种群的适应度,及初始化%————————————————————————arr_present(:,end)=ini_fit(arr_present,pop_size,part_size);%fork=1:pop_size%present(k,end)=fitness(present(k,1:part_size));%计算原始种群的适应度%endpbest=arr_present;%初始化各个粒子最优值[best_valuebest_index]=min(arr_present(:,end));%初始化全局最优,即适应度为全局最小的值,根据需要也可以选取为最大值gbest=arr_present(best_index,:);%v=zeros(pop_size,1);%v速度%————————————————————————%迭代%————————————————————————%globalm;%m=moviein(1000);%生成帧矩阵x=[-3:0.01:3];y=[-3:0.01:3];z=@(x,y)3*(1-x).^2.*exp(-(x.^2)-(y+1).^2)...-10*(x/5-x.^3-y.^5).*exp(-x.^2-y.^2)...-1/3*exp(-(x+1).^2-y.^2);fori=1:max_gengridon;plot3(x,y,z);%subplot(121),ezmesh(z),holdon,gridon,plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*'),holdoff;%subplot(122),ezmesh(z),view([145,90]),holdon,gridon,plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*'),holdoff;ezmesh(z),holdon,gridon,plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*'),holdoff;drawnowF(i)=getframe;%ezmesh(z)%%view([-37,90])%holdon;%gridon;%%plot(-0.0898,0.7126,'ro');%plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*');%改为三维%axis([-2*pi,2*pi,-pi,pi,-50,10]);%holdoff;pause(0.01);%m(:,i)=getframe;%添加图形w=w_max-(w_max-w_min)*i/max_gen;%fprintf('#%i代开始!\n',i);%确定是否对打散已经收敛的粒子群——————————————————————————————reset=0;%reset=1时设置为粒子群过分收敛时将其打散,如果=1则不打散ifreset==1bit=1;fork=1:part_sizebit=bit&(range(arr_present(:,k))0.1);endifbit==1%bit=1时对粒子位置及速度进行随机重置arr_present=ini_pos(pop_size,part_size);%present当前位置,随机初始化v=ini_v(pop_size,part_size);%速度初始化fork=1:pop_size%重新计算适应度arr_present(k,end)=fitness(arr_present(k,1:part_size));endwarning('粒子过分集中!重新初始化……');%给出信息display(i);endendforj=1:pop_sizev(j,:)=w.*v(j,:)+c1.*rand.*(pbest(j,1:part_size)-arr_present(j,1:part_size))...+c2.*rand.*(gbest(1:part_size)-arr_present(j,1:part_size));%粒子速度更新(a)%判断v的大小,限制v的绝对值小于5————————————————————————————c=find(abs(v)6);%**最大速度设置,粒子的范围宽度v(c)=sign(v(c))*6;%如果速度大于3.14则,速度为3.14arr_present(j,1:part_size)=arr_present(j,1:part_size)+v(j,1:part_size);%粒子位置更新(b)arr_present(j,end)=fitness(arr_present(j,1:part_size));if(arr_present(j,end)pbest(j,end))&(Region_in(arr_present(j,:),region))%根据条件更新pbest,如果是最小的值为小于号,相反则为大于号pbest(j,:)=arr_present(j,:);endend[bestbest_index]=max(arr_present(:,end));%如果是最小的值为min,相反则为maxifbestgbest(end)&(Region_in(arr_present(best_index,:),region))%如果当前最好的结果比以前的好,则更新最优值gbest,如果是最小的值为小于号,相反则为大于号gbest=arr_present(best_index,:);endbest_record(i)=gbest(end);endpso=gbest;display(gbest);%figure;%plot(best_record);%movie2avi(F,'pso_2D1.avi','compression','MSVC');%***************************************************************************%计算适应度%***************************************************************************functionfit=fitness(present)fit=3*(1-present(1)).^2.*exp(-(present(1).^2)-(present(2)+1).^2)...%**需要求极值的函数,本例即peaks函数-10*(present(1)/5-present(1).^3-present(2).^5).*exp(-present(1).^2-present(2).^2)...-1/3*exp(-(present(1)+1).^2-present(2).^2);functionini_present=ini_pos(pop_size,part_size)ini_present=3*rand(pop_size,part_size+1);%初始化当前粒子位置,使其随机的分布在工作空间%**6即为自变量范围functionini_velocity=ini_v(pop_size,part_size)ini_velocity=3/2*(rand(pop_size,part_size));%初始化当前粒子速度,使其随机的分布在速度范围内functionflag=Region_in(pos_present,region)[mn]=size(pos_present);flag=1;forj=1:n-1flag=flag&(pos_present(1:j)=region(j,1))&(pos_present(1:j)=region(j,2));endfunctionarr_fitness=ini_fit(pos_present,pop_size,part_size)fork=1:pop_sizearr_fitness(k,1)=fitness(pos_present(k,1:part_size));%计算原始种群的适应度end
本文标题:遗传算法MATLAB仿真程序
链接地址:https://www.777doc.com/doc-2009827 .html