";
cin>>ch;
}
if(ch=='A'||ch=='a')
GETDATA:
:
Input();
}
doubleGETDATA:
:
FRand(doublerand1,doublerand2)
{
returnrand1+(double)(((double)rand()/(double)RAND_MAX)*(rand2-rand1));
}
//***********************************************************
//类SSA:
K-均值算法的实现***
//功能:
根据设定的K,DataNum,Dimension等聚类***
//***********************************************************
classSAA
{
public:
structDataType
{
double*data;
intfather;
double*uncle;
};
structClusterType
{
double*center;
intsonnum;
};
SAA();
voidInitialize();
voidKMeans();
voidSA();
voidDisPlay();
voidGetDataset(DataType*p1,double*p2,intdatanum,intdim);
voidGetValue(double*str1,double*str2,intdim);
intFindFather(double*p,intk);
doubleSquareDistance(double*str1,double*str2,intdim);
intCompare(double*p1,double*p2,intdim);
voidNewCenterPlus(ClusterType*p1,intt,double*p2,intdim);
voidNewCenterReduce(ClusterType*p1,intt,double*p2,intdim);
doubleMaxFunc();
voidGenerate(DataType*p1,ClusterType*c1);
doubleCompare(DataType*p1,ClusterType*c1,DataType*p2,ClusterType*c2);
voidCopyStatus(DataType*p1,ClusterType*c1,DataType*p2,ClusterType*c2);
intSecondFather(DataType*p,intt,intk);
doubleAimFunction(DataType*q,ClusterType*c);
doubleFRand(double,double);
voidKMeans1();
protected:
doubleTemp;
//doubleCO;
//doubleDeclineRate;
//intMarkovLengh;
//intMaxInnerLoop;
//intMaxOuterLoop;
doubleAimFunc;
DataType*DataMember,*KResult,*CurrentStatus,*NewStatus;
ClusterType*ClusterMember,*NewCluster,*CurrentCluster;
};//endofclassSAA
//************建立构造函数,初始化保护成员
SAA:
:
SAA()
{
inti;
//DeclineRate=(double)0.9;
//MarkovLengh=1000;
//MaxInnerLoop=200;
//MaxOuterLoop=10;
//CO=1;
DataMember=newDataType[DataNum];
ClusterMember=newClusterType[K];
for(i=0;i{
DataMember[i].data=newdouble[Dimension];
DataMember[i].uncle=newdouble[K];
}
for(i=0;iClusterMember[i].center=newdouble[Dimension];
GetDataset(DataMember,DataSet,DataNum,Dimension);
}//endSAA
//****************初始化参数,及开始搜索状态
voidSAA:
:
Initialize()
{
//K-均值聚类法建立退火聚类的初始状态
//KMeans();
}
//*******************k-均值法进行聚类
//************接口:
数据,数量,维数,类别
//逐点聚类方式
voidSAA:
:
KMeans()
{
inti,j,M=1;
intpa,pb,fa;
ClusterType*OldCluster;
//初始化聚类中心
OldCluster=newClusterType[K];
for(i=0;i{
//cout<";
GetValue(ClusterMember[i].center,DataMember[i].data,Dimension);
ClusterMember[i].sonnum=1;
OldCluster[i].center=newdouble[Dimension];
GetValue(OldCluster[i].center,ClusterMember[i].center,Dimension);
}
for(i=0;i{
//cout<"<"<for(j=0;j{
DataMember[i].uncle[j]=SquareDistance(DataMember[i].data,ClusterMember[j].center,Dimension);
//cout<<""<"<"<"<}
pa=DataMember[i].father=FindFather(DataMember[i].uncle,K);
if(i>=K)
{
//cout<"<ClusterMember[pa].sonnum+=1;
//cout<"<NewCenterPlus(ClusterMember,pa,DataMember[i].data,Dimension);
//cout<"<"<GetValue(OldCluster[pa].center,ClusterMember[pa].center,Dimension);
}
}
//开始聚类,直到聚类中心不再发生变化。
××逐个修改法××
while(!
HALT)
{
//一次聚类循环:
.重新归类;.修改类中心
for(i=0;i{
//cout<for(j=0;j{
//cout<<"D"<DataMember[i].uncle[j]=SquareDistance(DataMember[i].data,ClusterMember[j].center,Dimension);
//cout<"<"<//cout<"<}
fa=DataMember[i].father;
if(fa!
=FindFather(DataMember[i].uncle,K)&&ClusterMember[fa].sonnum>1)
{
pa=DataMember[i].father;
ClusterMember[pa].sonnum-=1;
pb=DataMember[i].father=FindFather(DataMember[i].uncle,K);
ClusterMember[pb].sonnum+=1;
NewCenterReduce(ClusterMember,pa,DataMember[i].data,Dimension);
NewCenterPlus(ClusterMember,pb,DataMember[i].data,Dimension);
/*cout<*****************";//聚一次类输出一次结果
cout<"<";
for(t=0;t{
cout<"<"<}
DisPlay();
M=M+1;
*/
}
}//endfor
//判断聚类是否完成,HALT=1,停止聚类
HALT=0;
for(j=0;jif(Compare(OldCluster[j].center,ClusterMember[j].center,Dimension))
break;
if(j==K)
HALT=1;
for(j=0;jGetValue(OldCluster[j].center,ClusterMember[j].center,Dimension);
}//endwhile
}//endofKMeans
//批聚类方式
voidSAA:
:
KMeans1()
{
inti,j,M=1;
intpa,pb,fa;
ClusterType*OldCluster;
//初始化聚类中心
OldCluster=newClusterType[K];
for(i=0;iOldCluster[i].center=newdouble[Dimension];
for(j=0;jGetValue(OldClust