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

📄 main.cpp

📁 c均值算法用vc实现。作为重要的算法
💻 CPP
字号:
/********************************************************************
	FILE NAME: Main.cpp
*********************************************************************/
#include <iostream>
#include <math.h>
#include "Samples.h"		
#include "CSpecies.h"	

/*********************************************************************
			Sample Data (from Iris)
*********************************************************************/
double da[9][4]=
{
	//Samples for test1
	//from group1
	{5.1 , 3.5 , 1.4 , 0.2},	//1
	{4.9 , 3.0 , 1.4 , 0.2},	//2
	{5.0 , 3.3 , 1.4 , 0.2},	//50
	
	
	//from group2
	{7.0 , 3.2 , 4.7 , 1.4},    //51
	{6.4 , 3.2 , 4.5 , 1.5},    //52
	{5.7 , 2.8 , 4.1 , 1.3},    //100
	
	//from group3
	{6.3 , 3.3 , 6.0 , 2.5},    //101
	{5.8 , 2.7 , 5.1 , 1.9},    //102
	{5.9 , 3.0 , 5.1 , 1.8}     //150


	//Samples for test2
// 		//from group1
// 	{5.1 , 3.5 , 1.4 , 0.2},    //1
// 	{4.9 , 3.0 , 1.4 , 0.2},
// 	{4.7 , 3.2 , 1.3 , 0.2},
// 	{4.6 , 3.1 , 1.5 , 0.2},
// 	{5.0 , 3.6 , 1.4 , 0.2},
// 	{5.4 , 3.7 , 1.5 , 0.2},
// 	{4.6 , 3.4 , 1.4 , 0.3},
// 	{5.0 , 3.4 , 1.5 , 0.2},
// 	{4.4 , 2.9 , 1.4 , 0.2},
// 	{4.9 , 3.1 , 1.5 , 0.1},	
// 
// 	{5.4 , 3.7 , 1.5 , 0.2},	//11
// 	{4.8 , 3.4 , 1.6 , 0.2},
// 	{4.8 , 3.0 , 1.4 , 0.1},
// 	{4.3 , 3.0 , 1.1 , 0.1},
// 	{5.8 , 4.0 , 1.2 , 0.2},
// 	{5.7 , 4.4 , 1.5 , 0.4},
// 	{5.4 , 3.9 , 1.3 , 0.4},
// 	{5.1 , 3.5 , 1.4 , 0.3},
// 	{5.7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// 	{7.2 ,3.0 ,5.8 ,1.6},
// 
// 	{7.4 ,2.8 ,6.1 ,1.9},	//131
// 	{7.9 ,3.8 ,6.4 ,2.0},
// 	{6.4 ,2.8 ,5.6 ,2.2},
// 	{6.3 ,2.8 ,5.1 ,1.5},
// 	{6.1 ,2.6 ,5.6 ,1.4},
// 	{7.7 ,3.0 ,6.1 ,2.3},
// 	{6.3 ,3.4 ,5.6 ,2.4},
// 	{6.4 ,3.1 ,5.5 ,1.8},
// 	{6.0 ,3.0 ,4.8 ,1.8},
// 	{6.9 ,3.1 ,5.4 ,2.1},
// 
// 	{6.7 ,3.1 ,5.6 ,2.4},	//141
// 	{6.9 ,3.1 ,5.1 ,2.3},
// 	{5.8 ,2.7 ,5.1 ,1.9},
// 	{6.8 ,3.2 ,5.9 ,2.3},
// 	{6.7 ,3.3 ,5.7 ,2.5},
// 	{6.7 ,3.0 ,5.2 ,2.3},
// 	{6.3 ,2.5 ,5.0 ,1.9},
// 	{6.5 ,3.0 ,5.2 ,2.0},
// 	{6.2 ,3.4 ,5.4 ,2.3},
// 	{5.9 ,3.0 ,5.1 ,1.8}
};

/*********************************************************************
              Global Variables Declaration
*********************************************************************/
int N;				   //the number of the samples
int K;				   //the number of the species

bool ischanged=0;	   //whether the center has been changed
int	 timescontr=0;	   //time control
char issetcontr=0;     //need reset the center by hand? 0-no, 1-yes

/********************************************************************
             calculate the distance between the sample and its center 
*********************************************************************/
double Distan(const CSample& samp,const CSpecies& spec)
{
	double dis,temp;

	temp=fabs(samp.x1posi-spec.centerx1);
	temp=temp*temp;
	dis=temp;
	temp=fabs(samp.x2posi-spec.centerx2);
	temp=temp*temp;
	dis+=temp;
	temp=fabs(samp.x3posi-spec.centerx3);
	temp=temp*temp;
	dis+=temp;
	temp=fabs(samp.x4posi-spec.centerx4);
	temp=temp*temp;
	dis+=temp;
	dis=sqrt(dis);

	return dis;
}

/*********************************************************************
                        Main
*********************************************************************/
void main()
{
	//1. obtain the number of samples and centers
	N=9;
	K=3;
	
	//2. initialate the samples and centers	
	CSample*  sam=new CSample[N];	 
	CSpecies* spe=new CSpecies[K];	 
		
	//3. obain and set the coordinate of the samples
	for (int i=0;i<N;i++)
		(*(sam+i)).SetSample(da[i][0],da[i][1],da[i][2],da[i][3]);	//set the coordinate of sample i

	//4. reset the centers
	std::cout<<"是否要重新设置聚合的初始中心?(Y/N)"<<std::endl
		     <<"Y代表重新设置K个聚合的初始中心坐标"<<std::endl
			 <<"N代表默认第i个聚合的初始中心为第i个样本的坐标"<<std::endl;		     
	std::cin>>issetcontr;
	while (issetcontr!='Y' && issetcontr=='y' && issetcontr=='N' && issetcontr=='n')
	{
		std::cout<<"\a,Input error!"<<std::endl;
		std::cout<<"是否要设置聚合的初始中心?(Y/N)"<<std::endl;
		std::cin>>issetcontr;
	}
	if (issetcontr=='Y'||issetcontr=='y')//reset the centers by hand
	{
		int ncenter=0;
		for (int i=0;i<K;i++)
		{
			std::cout<<"设置第"<<i+1<<"个聚合的初始中心为 样本(1-N): ";
			std::cin>>ncenter;
			while (ncenter>N || ncenter<=0)
			{
				std::cout<<"\a,Input error!"<<std::endl;
				std::cout<<"设置第"<<i+1<<"个聚合的初始中心为 样本(1-N): ";
			    std::cin>>ncenter;
			}				
			//set the center i and the coordinates of sample n
			(*(spe+i)).SetCenter((*(sam+ncenter-1)).x1posi,
				                 (*(sam+ncenter-1)).x2posi,
								 (*(sam+ncenter-1)).x3posi,
								 (*(sam+ncenter-1)).x4posi);
		}
	} 
	else if (issetcontr=='N'||issetcontr=='n')//set the coordinates of sample i to k as the default centers
	{
		for (int i=0;i<K;i++)
			(*(spe+i)).SetCenter((*(sam+i)).x1posi,
			                     (*(sam+i)).x2posi,
								 (*(sam+i)).x3posi,
								 (*(sam+i)).x4posi);
	}
	
	//5. iteration
	do 
	{
		for (int j=0;j<K;j++)	         //clear the count of the species
		{
			(*(spe+j)).count=0;
		}
        ischanged=0;			         //set inschanged as 0

		//1). calculate the distance one by one
		for (i=0;i<N;i++)	             
		{
			int f=(*(sam+i)).flag;       //the record of the spieces stored in f			
			double distan=0,temp=Distan((*(sam+i)),(*(spe+f)));

			for (int j=0;j<K;j++)	     
			{
				distan=Distan((*(sam+i)),(*(spe+j)));		     //calculate the distances between sample i and specie j
				if (distan<temp)
				{
					temp=distan;	     //temp stored the shortest distance
					(*(sam+i)).flag=j;	 
				}
			}
			//set sample i to the species of shortest distance
			int s=(*(sam+i)).flag;		 //s stored which sp the sample i is in
			int n=(*(spe+s)).count++;	 //the number add with 1
			(*(spe+s)).member[n]=i;	     //member[] stored which samples are in species s
		}

		//2).recalculate the centers
		for (i=0;i<K;i++)
		{
			int c=(*(spe+i)).count;              
			double tempx1=0,tempx2=0,tempx3=0,tempx4=0,temp=0;
			for (int j=0;j<c;j++)
			{
				int n=(*(spe+i)).member[j];	     
				temp=(*(sam+n)).x1posi/c;   
				tempx1+=temp;
				temp=(*(sam+n)).x2posi/c;
				tempx2+=temp;
				temp=(*(sam+n)).x3posi/c;
				tempx3+=temp;
				temp=(*(sam+n)).x4posi/c;
				tempx4+=temp;
			}
			(*(spe+i)).SetCenter(tempx1,tempx2,tempx3,tempx4);	 //set the coordinate of the centers
		}

		//3).is there any changes of the centers?
		for (i=0;i<K;i++)
		{
			ischanged=ischanged||(*(spe+i)).IsCenterChanged();
		}

	} while(ischanged && timescontr<10000);//4.any changes?

	//6. output
	std::cout<<std::endl<<"最后分类结果为:"<<std::endl;
	for (i=0;i<K;i++)
	{
		std::cout<<"第"<<i+1<<"个聚合共有"<<(*(spe+i)).count<<"个样本,中心位置为:("
				 <<(*(spe+i)).centerx1<<","
				 <<(*(spe+i)).centerx2<<","
				 <<(*(spe+i)).centerx3<<","
				 <<(*(spe+i)).centerx4<<","
				 <<")"<<std::endl;
		for (int j=0;j<(*(spe+i)).count;j++)
		{
			int n=(*(spe+i)).member[j];
			std::cout<<"第"<<j+1<<"个样本为:"<<"("
				     <<(*(sam+n)).x1posi<<","
				     <<(*(sam+n)).x2posi<<","
					 <<(*(sam+n)).x3posi<<","
					 <<(*(sam+n)).x4posi<<","					 
					 <<")"<<"   是原来第"<<n+1<<"个样本"<<std::endl;
		}
		std::cout<<std::endl;
	}
	
}




⌨️ 快捷键说明

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