C-PSO聚类matlab代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

%2009-7-8%粒子群聚类算法function[m_pattern]=C_PSO(m_pattern,patternNum)disType=DisSelDlg();%获得距离计算型[centerNumiterNum]=InputClassDlg;%获得类中心数和最大迭代次数particleNum=200;%初始化粒子数%初始化中心和速度globalNwidth;fori=1:centerNumm_center(i).feature=zeros(Nwidth,Nwidth);m_center(i).patternNum=0;m_center(i).index=i;m_velocity(i).feature=zeros(Nwidth,Nwidth);fori=1:particleNumParticle(i).location=m_center;%粒子各中心Particle(i).velocity=m_velocity;%粒子各中心速度Particle(i).fitness=0;%适应度P_id(i).velocity=m_velocity;%粒子最优速度P_id(i).location=m_center;%粒子最优中心P_id(i).fitness=0;%粒子最优适应度endP_gd.location=m_center;%全局粒子最优中心P_gd.velocity=m_velocity;%全局粒子最优速度P_gd.fitness=0;%粒子全局最优适应度P_gd.string=zeros(1,patternNum);ptDitrib=zeros(particleNum,patternNum);%初始化粒子分布矩阵fori=1:particleNum%生产随机粒子分布矩阵ptDitrib(i,:)=randperm(patternNum);forj=1:patternNumif(ptDitrib(i,j)centerNum)ptDitrib(i,j)=fix(rand*centerNum+1);endendend%生产初始粒子群fori=1:particleNumforj=1:patternNumm_pattern(j).category=ptDitrib(i,j);endforj=1:centerNumm_center(j)=CalCenter(m_center(j),m_pattern,patternNum);endParticle(i).location=m_center;end%初始化参数w_max=1;w_min=0;h1=2;h2=2;foriter=1:iterNum%计算粒子适应度fori=1:particleNumtemp=0;forj=1:patternNumtemp=temp+GetDistance(m_pattern(j),Particle(i).location(ptDitrib(i,j)),disType);endif(temp==0)%最优解,直接退出iter=iterNum+1;break;endParticle(i).fitness=1/temp;endif(iteriterNum)break;endw=w_max-iter*(w_max-w_min)/iterNum;%更新权重系数fori=1:particleNum%更新P_id,P_gdif(Particle(i).fitnessP_id(i).fitness)P_id(i).fitness=Particle(i).fitness;P_id(i).location=Particle(i).location;P_id(i).velocity=Particle(i).velocity;if(Particle(i).fitnessP_gd(i).fitness)P_gd(i).fitness=Particle(i).fitness;P_gd(i).location=Particle(i).location;P_gd(i).velocity=Particle(i).velocity;P_gd.string=ptDitrib(i,:);endendend%更新粒子速度,位置fori=1:particleNumforj=1:centerNumParticle(i).velocity(j).feature=...w*Particle(i).velocity(j).feature+h1*rand(Nwidth,Nwidth).*...(P_id(i).location(j).feature-Particle(i).location(j).feature)...+h2*rand(Nwidth,Nwidth).*(P_gd.location(j).feature...-Particle(i).location(j).feature);Particle(i).location(j).feature=Particle(i).location(j).feature...+Particle(i).velocity(j).feature;endend%最近邻聚类fori=1:particleNumforj=1:patternNummin=inf;fork=1:centerNumtempDis=GetDistance(m_pattern(j),Particle(i).location(k),disType);if(tempDismin)min=tempDis;m_pattern(j).category=k;ptDitrib(i,j)=k;endendend%重新计算聚类中心forj=1:centerNumParticle(i).location(j)=CalCenter(Particle(i).location(j),m_pattern,patternNum);endendfori=1:patternNumm_pattern(i).category=P_gd.string(1,i);endendend

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功