⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 breastcancerpso3.asv

📁 K-MEANS聚类算法
💻 ASV
字号:
function [F_gbest,inter_distance,intra_distance]=breastcancerpso3(MAXITER)
load breastcancer.mat datavectorcancer;%%%载入数据集
[vectorsize,vectorlength]=size(datavectorcancer);
popsize=10; %群体的粒子数%
centroid=2;%聚类的个数
dimension=9; %数据向量维数%
vmax=0.2; %最大速度%
c1=1.49;    %加速因子c1%
c2=1.49;%加速因子c2%
w=0.72;
% irange_l=0.1; %粒子初试化范围的左侧%
% irange_r=7.9;%粒子初试化范围的右侧%
e=2;
% MAXITER=100;
%%%%%%初始化粒子%%%%%
for i=1:popsize
    
    [r1,r2]=Initializecancer(datavectorcancer);
    partical(i).centroid=[r1;r2];
    
end
for i=1:popsize
    partical(i).pbest=partical(i).centroid;
    partical(i).velocity=2*vmax*rand(centroid,dimension)-vmax;
end
f_xx=zeros(1,popsize);
f_pbest=f_xx;
f_gbest=0;
gbest=zeros(2,9);

% SD=zeros(1,3);
% f=zeros(1,3);
% D=zeros(1,3);
%初始化f_pbest
for i=1:popsize %粒子的个数
    [Je(i)]=f4(partical(i).centroid,centroid,datavector,e);
    f_xx(i)=Je(i);%f_xx(i)代表粒子i当前的适应性值
    f_pbest(i)=f_xx(i);%f_pbest(i)代表粒子i最佳适应性值
end
%初始化f_gbest
 f_gbest=f_pbest(1);
for i=1:popsize
   
    if f_gbest>f_pbest(i)
       f_gbest=f_pbest(i);
   end
end

%%%%%%%初始化完毕%%%%%%%%%%        
        

%%%聚类的过程%%%%%%%%%%%%%
times=1;
while times<=MAXITER
%     for i=1:popsize
%         
%       [Je(i)]=f4(partical(i).centroid,centroid,datavector,e);
%       f_xx(i)=Je(i);%f_xx(i)代表粒子i当前的适应性值
%     end
%    for i=1:popsize%popsize粒子数
%        if f_pbest(i)>f_xx(i)
%           f_pbest(i)=f_xx(i);%f_pbest(i)代表粒子i最佳适应性值
%           partical(i).pbest=partical(i).centroid;%partical(i).pbest代表粒子i最佳位置
%       end
%    end
%   for i=1:popsize
%      if  f_gbest>f_pbest(i)
%          f_gbest=f_pbest(i);%f_gbest记录全局最佳适应性值
%          gbest=partical(i).pbest;%gbest记录全局最佳位置
%      end
%   end
   
for i=1:popsize
    for k=1:centroid
        for z=1:dimension
           partical(i).velocity(k,z)=w*partical(i).velocity(k,z)+c1*rand*(partical(i).pbest(k,z)-partical(i).centroid(k,z))+c2*rand*(gbest(k,z)-partical(i).centroid(k,z));
            if partical(i).velocity(k,z)>vmax
               partical(i).velocity(k,z)=vmax;
            end
            if partical(i).velocity(k,z)<-vmax                 
               partical(i).velocity(k,z)=-vmax;
            end
            partical(i).centroid(k,z)=partical(i).centroid(k,z)+partical(i).velocity(k,z);
        end
    end

[center1,f_x(i)] = fcm1(partical(i).centroid,datavector, centroid);
       if f_pbest(i)>f_x(i)
          f_pbest(i)=f_x(i);%f_pbest(i)代表粒子i最佳适应性值
         
      end
 partical(i).pbest=center1;%partical(i).pbest代表粒子i最佳位置
     if  f_gbest>f_pbest(i)
         f_gbest=f_pbest(i);%f_gbest记录全局最佳适应性值
         gbest=partical(i).pbest;%gbest记录全局最佳位置
     end

end
%%%%%%%%%%%更新聚类中心完毕%%%%%%%%%
%  inter_distance1=pdist(gbest);
%  inter_distance(times)=sum(inter_distance1)/3;
%  intra_distance(times)=sum(D)/centroid;
partical(i).centroid=center1;
F_gbest(times)=f_gbest;
inter_distance=pdist(gbest);

 
 dist = distfcm(gbest,datavector);       % fill the distance matrix
tmp = dist.^(-2/(e-1));      % calculate new U, suppose expo != 1
U= tmp./(ones(centroid, 1)*sum(tmp));
maxU = max(U);
% Find the data points with highest grade of membership in cluster 1
num1= find(U(1,:) == maxU);
 % Find the data points with highest grade of membership in cluster 2
num2 = find(U(2,:) == maxU);

cluster_num1=length(num1);
cluster_num2=length(num2);


for i1=1:cluster_num1
    datavector1=datavector(num1(i1));
end
for i2=1:cluster_num2
    datavector2=datavector(num2(i2));
end

    X1=datavector1-ones(cluster_num1,1)*gbest(1,:);
    Y1_1=sqrt(sum((X1.^2)'));
    Y1(times)=sum(Y1_1);
    Y1=X1.^2;
    X2=datavector2-ones(cluster_num2,1)*gbest(2,:);
    Y2_2=sqrt(sum((X2.^2)'));
    Y2(times)=sum(Y2_2);
    X3=datavector3-ones(cluster_num3,1)*gbest(3,:);
    Y3_3=sqrt(sum((X3.^2)'));
    Y3(times)=sum(Y3_3);
intra_distance(times)=(Y1(times)+Y2(times)+Y3(times))/3;
 times=times+1;
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -