%function [m_pattern]=C_PSO(m_pattern,patternNum)
clear all;
%获得类中心数和最大迭代次数
zuobiao=inputdlg({'K','itermun'});
F=[str2double(zuobiao{1}) str2double(zuobiao{2})];
centerNum=F(1,1);
iterNum=F(1,2);
%载入文件
pause(1);
load('A.mat');
load('iris.mat');
% [filename,pathname]=uigetfile({'*.mat'},'选择数据文件');
% load(filename);
[patternNum,dim]=size(iris);
% m_pattern=zeros(patternNum,dim+1);
% m_pattern=iris(:,:);
m_pattern=repmat(iris,1,1);
particleNum=30;%初始化粒子数
fit=zeros(1,20);
%初始化中心和速度
%最大范围
v_max=max(iris);
v_min=min(iris);
% v_mean=mean(iris)
for i=1:centerNum
m_center(i).feature=zeros(1,dim);%zeros(1,维数)
m_center(i).patternNum=0;
m_center(i).index=i;
m_velocity(i).feature=zeros(1,dim);
m_velocity(i).feature=rand*1.2*(v_max(1,:)+v_min(1,:))/2; %随机初始化速度*0.14
% m_velocity(i).feature=rand*v_mean;
end
for i=1:particleNum
Particle(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;%粒子最优适应度
end
P_gd.location=m_center;%全局粒子最优中心
P_gd.velocity=m_velocity;%全局粒子最优速度
P_gd.fitness=0;%粒子全局最优适应度
P_gd.string=zeros(1,patternNum);
ptDitrib=zeros(particleNum,patternNum);%初始化粒子分布矩阵
for i=1:particleNum %生产随机粒子分布矩阵
ptDitrib(i,:)=randperm(patternNum);%产生1到n的整数的无重复的随机排列,利用它就可以得到无重复的随机数
for j=1:patternNum
if (ptDitrib(i,j)>centerNum)
ptDitrib(i,j)=fix(rand*centerNum+1);%向0取整
end
end
end
%生产初始粒子群
for i=1:particleNum
for j=1:centerNum
m_center(j)=CalCenter(m_center(j),m_pattern,patternNum,dim,ptDitrib(i,:));
end
Particle(i).location=m_center;
end
%初始化参数
w_max=1.4;
w_min=0;
h1=2;
h2=2;
for iter=1:iterNum
%计算粒子适应度
disp(iter)
for i=1:particleNum
temp=0;
for j=1:patternNum
temp=temp+GetDistance(m_pattern(j,:),Particle(i).location(ptDitrib(i,j)).feature,dim);%类内离散度
end
if (temp==0) %最优解,直接退出
iter=iterNum+1;
break;
end
n1=find(histc(P_gd.string(1,1:50),[1,2,3])>=47);
n2=find(histc(P_gd.string(1,51:100),[1,2,3])>=47);
n3=find(histc(P_gd.string(1,101:150),[1,2,3])>=46);
if ((n1~=n2)&(n2~=n3)&(n1~=n3))
iter=iterNum+1;
break;
end
Particle(i).fitness=1/temp;
end
if(iter>iterNum)
break;
end
w=w_max-iter*(w_max-w_min)/iterNum;%更新权重系数 线形递减策略
for i=1:particleNum %更新P_id,P_gd
if (Particle(i).fitness>P_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).fitness>P_gd.fitness)
P_gd.fitness=Particle(i).fitness;
P_gd.location=Particle(i).location;
P_gd.velocity=Particle(i).velocity;
P_gd.string=ptDitrib(i,:); %粒子初始分类情况
end
end
end
fit(iter)=P_gd.fitness;
%更新粒子速度,位置
for i=1:particleNum
for j=1:centerNum
Particle(i).velocity(j).feature =... %是个dim维数据
w*Particle(i).velocity(j).feature+h1*rand*...
(P_id(i).location(j).feature-Particle(i).location(j).feature)...
+h2*rand*(P_gd.location(j).feature...
-Particle(i).location(j).feature);
if(Particle(i).velocity(j).feature>v_max) %速度超过最大值
Particle(i).velocity(j).feature=v_max;
end
if(Particle(i).velocity(j).feature<v_min) %速度超过最小值
Particle(i).velocity(j).feature=v_min;
end
Particle(i).location(j).feature= Particle(i).location(j).feature...
+ Particle(i).velocity(j).feature;
end
end
%最近邻聚类
for i=1:particleNum
for j=1:patternNum
min=inf;
for k=1:centerNum
tempDis=GetDistance(m_pattern(j,:), Particle(i).location(k).feature,dim);
if(tempDis<min)
min=tempDis;
ptDitrib(i,j)=k;
end
end
end
Particle(i).location(1).patternNum=histc(ptDitrib(i,:),1);%%重新统计各类中样本数目
Particle(i).location(2).patternNum=histc(ptDitrib(i,:),2);
Particle(i).location(3).patternNum=histc(ptDitrib(i,:),3);
[Particle(i).location,ptDitrib(i,:)]=blank(Particle(i).location, ptDitrib(i,:),m_pattern,patternNum,dim,i);
%重新计算聚类中心
for j=1:centerNum
Particle(i).location(j)=CalCenter(Particle(i).location(j),m_pattern,patternNum,dim, ptDitrib(i,:));
end
end
end
disp(['最优适应值为:',num2str(P_gd.fitness)]);
figure(1);
plot(fit,'b-*') ;
hold on
figure(2); %图1分类统计图
axis([1,3,0,60])
r1=hist(P_gd.string(1,1:50),[1,2,3])
subplot(3,1,1)
bar(r1)
title('样本分类统计图');
text(3,70,'x轴--类别,y轴--个数')
r2=histc(P_gd.string(1,51:100),[1,2,3])
subplot(3,1,2)
bar(r2)
r3=hist(P_gd.string(1,101:150),[1,2,3])
subplot(3,1,3)
bar(r3);
figure(3);
title('算法聚类结果与标准结果对比图')
subplot(1,2,1);
axis([0,150,1,3])
set(gca, 'YTickLabel',{'1 ','','','','','2 ','','','','','3 '});
title('算法分类情况');
xlabel('样本');
ylabel('category');