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

📄 smo1.c

📁 求解SVM问题的一种高效算法
💻 C
📖 第 1 页 / 共 2 页
字号:
	char path[100];	
	unsigned int i,j;
	float *p;/*float *p=new float[m_dimension]; */
	int y,decisionY;

	getFileName(path,fileName);
	strcat(path,".result");
	if ((fin = fopen(fileName, "r")) == NULL)
	{
		printf( "cannot open  file %s\n",fileName );
		exit( 0 );
	}
	if ((fout = fopen(path, "w")) == NULL)
	{
		printf( "cannot open  file %s\n",path );
		exit( 0 );
	}
	p = (float *)malloc(sizeof(float)*m_dimension);
	fprintf(fout,"id\t y \t decision\n");/*fout<<"id\t y \t decision"<<endl; */
	for(i=0;i<number;i++)
	{
		for(j=0;j<m_dimension;j++)
		{
/*			fin>>p[j];*/
/*			if(fin.eof()==3)*/
/*				goto End;*/

		fscanf(fin,"%f",&p[j]);
		if(m_max[j] != m_min[j])
			p[j] = (p[j] - m_min[j]) / (m_max[j] - m_min[j]);
		else
			p[j] = 0;
		}
/*		fin>>y;		*/			
/*		if(fin.eof()==3)*/
/*			goto End;*/
		fscanf(fin,"%d",&y);
		decisionY = indicator_function( p );
		if(decisionY == y)
			right++;
		else
		{
/*			fout<<i+1<<"\t"<<y<<"\t"<<decisionY<<endl; */
			fprintf(fout,"%d\t%d\t%d\n",i+1,y,decisionY);
			wrong++;
		}
		
	}
	
/*End:*/
/*	delete[] p;*/
/*	fin.close();*/
/*	fout.close();*/
/*	return 0;*/

	fprintf(fout,"the right num: %d\n",right);
	fprintf(fout,"the error num: %d\n",wrong);
	precision = (float)right/(right+wrong);
	fprintf(fout,"the precision: %f",precision);
	free(p);
	fclose(fin);
	fclose(fout);
	return 0;
  }
int	LMToFile(char * fileName)
{
	unsigned int i;
	FILE * out;
	if ((out = fopen(fileName, "w")) == NULL)
	{
		printf( "cannot open  file %s\n",fileName );
		exit( 0 );
	}
	fprintf(out,"dimension is:%d\n",m_xNum);
	fprintf(out,"using gauss kernel function, para is:%f\n",m_two_sigma_squared);
	fprintf(out,"the b in \'wx+b\' is :%f\n",m_b);
	fprintf(out,"a = \n");          

	for(i = 0; i < m_xNum; i++)
	{
		fprintf(out,"%f\n",m_a[i]);
/*		out << m_a[i] << endl; */
	}
	fclose(out);
	return 0;
}

int	readPoint(char * FileName,unsigned int number,unsigned int dimession)
{
	/*dimession not contain the decision */
	FILE * fin;
	unsigned int i,j;
	float *p;
	float *y;

	if(FileName == NULL || dimession==0 || number == 0)
		return 1;
	allocMemory(number,dimession);
	p=m_x;
	y = m_y;
	if((fin=fopen(FileName,"r"))==NULL)
	{
		printf( "cannot open file %s\n",FileName );
		exit( 0 );
	}
	for(i=0;i<number;i++)
	{
		for(j=0;j<dimession;j++)
		{
/*			fin>>*p; */
			fscanf(fin,"%f",p);
			p++;
			if(feof(fin))
				return 1;
		}
		fscanf(fin,"%f",y);
//		if(feof(fin))
//			return 1;
		y++;
	}
	fclose(fin);
	m_xNum = number;
	m_dimension = dimession;
	m_end_support_i = number;
	return 0;
}
void RenormalizeData(float * instance,unsigned int number)
{
	float t;
	float *p;
	unsigned int k,l;
	for(k = 0; k < m_dimension; k++)
	{ /*for every dimension */
		m_max[k] = instance[k];
		m_min[k] = instance[k];
		p=instance;
		for(l = 0; l < number; l++)
		{ /*for every point */
			t = p[k];
			if(t > m_max[k]) m_max[k] = t;
			if(t < m_min[k]) m_min[k] = t;
			p = p + m_dimension;
		}
		p=instance;
		for(l = 0; l < number; l++)
		{ /*for every point */
			if (m_max[k] != m_min[k])
				p[k] = (p[k] - m_min[k]) / (m_max[k] - m_min[k]);
			else
				p[k] = 0;
			p = p + m_dimension;
		}
	}
}

int	pointToFile(char * FileName,float * x ,float * y,unsigned int number)
{
	FILE * fout;
	unsigned int i,j;

	if(FileName==NULL || number==0 || x ==NULL)
		return 1;
	if((fout=fopen(FileName,"w"))==NULL)
	{
		printf( "cannot open  file %s\n",FileName );
		exit( 0 );
	}
	for(i=0;i<number;i++)
	{
		for(j=0;j<m_dimension;j++)
		{
			fprintf(fout," %f",*x);/*fout<<" "<<*x; */
			x++;
		}
		if(y)
		{
			fprintf(fout," %f",*y);/*fout<<*y; */
			y++;
		}
		fprintf(fout,"\n");/*fout<<endl; */    
	}		
	fclose(fout);
	return 0;
}

int	createSmo()
{	
/*To train machine	*/
	unsigned int i,j,k;
	float *p = m_x;
	int numChanged = 0;
	int examineAll = 1;

	/*Renormalize data to [0,1]	*/
	RenormalizeData(m_x,m_xNum);
/*	pointToFile("normtrain.data",m_x,0,m_xNum); */
	/*Write normalized training data to file */
	/*self dot product */
	for(i = 0; i < m_xNum; i++)
	{ /*calculation of x*x */
		m_self_dot_product[i] = 0;
		for(j = 0; j < m_dimension; j++){
			m_self_dot_product[i] += (*p) * (*p);
			p++;
		}
	}
	/*------------------------------------------------------*/
	/*Routine to examine all the points */
	while(numChanged > 0 || examineAll == 1)
	{
		numChanged = 0;
		if(examineAll == 1)
		{
			for(k = 0; k < m_xNum; k++)
			{
				numChanged += tryLM(k);
			}
		}
		else
		{
			for(k = 0; k < m_xNum; k++)
			{
				if(m_a[k]!=0&&m_a[k]!=m_C)
				{
					numChanged += tryLM(k);
				}
			}
		}
		if(examineAll == 1) examineAll = 0;
		else if(numChanged == 0) examineAll = 1;
	}
	/*-------------------------------------------------------*/
	
	/*--------------------------------------------------------------------*/
	/*To classify unclassified data using trained machine */
	/*Read in data	*/
	/*-------------------------------------------------------------------- */
	return 0;
}

int	scaleToFile(char * FileName,float * x ,float * y,unsigned int number)
{
	
	FILE * fout;
	unsigned int i,j;

	if(FileName==NULL || number==0 || x ==NULL)
		return 1;
	if((fout=fopen(FileName,"w"))==NULL)
	{
		printf( "cannot open  file %s\n",FileName );
		exit( 0 );
	}
	for(i=0;i<number;i++)
	{
		if(y)
		{
			fprintf(fout,"%f",*y);/*fout<<*y; */
			y++;
		}
		for(j=0;j<m_dimension;j++)
		{
			fprintf(fout," %d:%f",*x);/*fout<<" "<<j+1<<":"<<*x; */
			x++;
		}		
		fprintf(fout,"\n");/*fout<<endl; */
	}		
	fclose(fout);
	return 0;
}

int scale(char * FileName,unsigned int number,unsigned int dimession)
{
	char fileName[200];
	
	if(readPoint(FileName,number,dimession))
		return 1;
	RenormalizeData(m_x,m_xNum);
	/*Write results to out-svm.data */
	getFileName(fileName,FileName);
	strcat(fileName,".scale");
	scaleToFile(fileName,m_x,m_y,m_xNum);
	return 0;
}

int	skipToFile(char * FileName,float * x ,float * y,unsigned int number)
{
	FILE * fout;
	unsigned int i,j;

	if(FileName==NULL || number==0 || x ==NULL)
		return 1;
	if((fout=fopen(FileName,"w"))==NULL)
	{
		printf( "cannot open  file %s\n",FileName );
		exit( 0 );
	}
	for(i=0;i<number;i++)
	{
		
		for(j=0;j<m_dimension;j++)
		{
			if (m_max[j] != m_min[j])
				fprintf(fout,"%f ",*x);
			x++;
		}
		if(y)
		{ 
			fprintf(fout,"%f ",*y);
			y++;
		}		
		fprintf(fout,"\n");
	}		
	fclose(fout);
	return 0;
}

int skip(char * FileName,unsigned int number,unsigned int dimession)
{
	char fileName[200];

	if(readPoint(FileName,number,dimession))
		return 1;
	RenormalizeData(m_x,m_xNum);
	/*Write results to out-svm.data */
	getFileName(fileName,FileName);
	strcat(fileName,".skip");
	skipToFile(fileName,m_x,m_y,m_xNum);
	return 0;
}


void main(int argc,char *argv[])
{
	double starttime,endtime;
	unsigned long runtime;
	char choice,filename[100];
	unsigned int number;

	if(argc<4)
	{
		printf("parameter error\n");
		return ;
	}

	MPI_Init( &argc, &argv );
	MPI_Comm_rank( MPI_COMM_WORLD, &myid );
	MPI_Comm_size( MPI_COMM_WORLD, &numprocs ); 

	srand((unsigned)time(NULL));
/*	mysmo smo(1000,0.001f,0.001f); */
	m_sigma = 1.0f;
	initCsmo();
	if(readPoint(argv[1],atoi(argv[2]),atoi(argv[3])))/*"train.data",189,13 */
	{
		return ;
	}
	starttime = MPI_Wtime();
	createSmo();
	endtime = MPI_Wtime();
	printf("train time is %f seconds",endtime-starttime);
	LMToFile("train.a");
	
	printf("\nContinue To Test?(Y/N)");
	scanf("%c",&choice);
	if(choice=='y'||choice=='Y')
	{
		printf("\nPlease Input TestFilename and Number\n");
		scanf("%s%d",filename,&number);
		starttime = MPI_Wtime();
		testing(filename,number);
		endtime = MPI_Wtime();
		printf("test time is %f seconds",endtime-starttime);
	}
	freeCsmo();
	MPI_Finalize();
	printf("\nPrograme End\n");

}

⌨️ 快捷键说明

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