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

📄 initialize.cpp

📁 SMO工具箱
💻 CPP
字号:
#include <stdafx.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "initialize.h"
//#include "classify.h"

FeaturePtr **example;

FeaturePtr **sv;
double *lambda;
int *svNonZeroFeature;
int *nonZeroFeature;
int *target;
double *weight;
double *output;
double rbfConstant;
int degree;
double b;
int numSv;
int numExample;
int kernelType;
int maxFeature;
int *zeroFeatureExample;


#define MODELTEMP 100
#define DATATEMP1 10000
#define DATATEMP2 1000

static double C;
static double sigmaSqr;
static int maxFeatureRead;

static int readString(char *store, char delimiter, FILE *in);
static void skip(char end, FILE *in);
static int initializeModel(int size);
static int initializeData(int dataSize);


static int readString(char *store, char delimiter, FILE *in)
{
	char c;
	int i;

	i =0;
	c =getc(in);
	while( c!=delimiter && c !='\n' && c !=EOF){
		store[i] =c;
		i++;
		c =getc(in);
	}
	if (c==EOF || c=='\n')
		return 0;
	else
		store[i] ='\0';
	return 1;
}

void skip(char end ,FILE *in)
{
	char c;
	while ((c=getc(in)) !=end)
		;

}

static int initializeModel( int size)
{
	int i;
	lambda =(double*)malloc((size+1)*sizeof(double));
	if (lambda ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	}
	svNonZeroFeature =(int*)malloc((size+1)*sizeof(int));
   	if (svNonZeroFeature ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	}
	sv =(FeaturePtr**)malloc((size+1)*sizeof(FeaturePtr*));
	if (sv ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	}
	for(i=1;i<=numSv;i++){
		sv[i]=(FeaturePtr*)malloc((maxFeature)*sizeof(FeaturePtr));
	  if (sv[i] ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	  }
	}
	return 1;
}


static int initializeData(int dataSize)
{
	example =(FeaturePtr**)malloc(dataSize *sizeof(FeaturePtr*));
    if (example ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	  }
	output =(double*)malloc(dataSize *sizeof(double));
	if (output ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	  }
	target =(int*)malloc(dataSize*sizeof(int));
	if (target ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	  }
	zeroFeatureExample =(int*)malloc(dataSize/2*sizeof(int));
	if (zeroFeatureExample ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	  }
	nonZeroFeature =(int *)malloc(dataSize*sizeof(int));
	if (nonZeroFeature ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	  }
	return 1;
}


int readModel(FILE *in)
{
  //	int numNonZeroFeature;
	int i,j;
	char c;
	char temp[MODELTEMP];

	printf("Reading model file...\n");
	c=getc(in);

	if(c=='0')
		kernelType =0;
	else if(c =='1'){
		kernelType =1;
		skip(' ',in);
		readString(temp,' ',in);
		degree =atoi(temp);
	}
	else if(c =='2'){
		kernelType =2;
		skip(' ',in);
		readString(temp,' ',in);
		sigmaSqr =atof(temp);
		rbfConstant =1/(2*sigmaSqr);
	}
	skip('\n',in);

	/***read number of features of training examples**/
    readString(temp,' ',in);
	maxFeatureRead =atoi(temp);
	skip('\n',in);

	/**read the weight if the kernel type is linear*****/
	if(kernelType ==0){
		weight =(double*)malloc((maxFeatureRead+1)*sizeof(double));
		if (weight ==NULL){
		fprintf(stderr,"Memory allocation failed\n");
		return 0;
	  }
		for(i=0;i<=maxFeatureRead;i++)
			weight[i]=0;
		for(i=1;i<=maxFeatureRead;i++){
			readString(temp,' ',in);
			weight[i] =atof(temp);
		}
		skip('\n',in);
	}

	/**read the threshold b***/
	readString(temp,' ',in);
	b =atof(temp);
	skip('\n',in);
	/***read C parameter*****/
    readString(temp,' ',in);
	C =atof(temp);
	skip('\n',in);

	/****read the number of support vetor*************/
	readString(temp,' ',in);
	numSv =atoi(temp);
	skip('\n',in);
	if(!initializeModel(numSv))
		return 0;

	/******read the product of lambda of a support vector with class label*****/
	for(i=1;i<=numSv;i++){
		readString(temp,' ',in);
		lambda[i] =atof(temp);
		j =0;
		/**********read the id/value pairs of the features of a sv**/
		while(readString(temp,':',in)){
			sv[i][j] =(FeaturePtr)malloc(sizeof(struct feature));
	    	if (sv[i][j] ==NULL){
		    fprintf(stderr,"Memory allocation failed\n");
		     return 0;
			}
			sv[i][j]->id =atoi(temp);
			if(!readString(temp,' ',in))
				readString(temp,' ',in);
			sv[i][j]->value =atof(temp);
			j++;
		}
		svNonZeroFeature[i] =j; //sv non zero feature number
	}
	return 1;
}

int readData(FILE *in)
{

	char temp[DATATEMP1];
	char temp2[DATATEMP2];
	int numFeature;
	int exampleIndex,featureIndex;
	int i,j,dataSetSize;
	char c;
	int zeroFeatureNumber =0;

	dataSetSize =0;

	/**estimate the number of data***/
	while ((c=getc(in))!=EOF){
		if(c=='\n')
			dataSetSize++;
	}
	//dataSetSize++;
	rewind(in);
	if(!initializeData(dataSetSize))
		return 0;

	/*NonZeroFeature[0]stores the number of nonzero features of the weight vector**/
	nonZeroFeature[0] =maxFeature;
    
	numExample =0;
	exampleIndex =0;
	printf("Reading test data file...\n");
	c=getc(in);
	while(c!=EOF){
	/****ignore comment and comment blank lines*****/
		while(c == '#'||c =='\n'){
			 if (c =='#'){
				 while((c =getc(in)) !='\n')
					 ;
			 }
			 if(c =='\n')
				 c=getc(in);
			 }
		if (c == EOF)
			 break;
		else {   ///read one line of description
			exampleIndex++;
			i =0; 
			numFeature =0;
			temp [i]=c;
			i++;
            c= getc (in);
			while( (c !='\n')){
				temp[i] =c;
				i++;
				if(c ==':')
					numFeature++;
				c=getc(in);
			}
			temp[i]='\0';
			numExample++;
			nonZeroFeature[exampleIndex] =numFeature;

			if(numFeature !=0){
				example[exampleIndex] =(FeaturePtr*)malloc(numFeature *sizeof(FeaturePtr));
				if(example[exampleIndex] == NULL){
					fprintf(stderr,"Memory allocation failed \n");
					return 0;
				}

				for (j =0;j<numFeature;j++){
					example[exampleIndex][j] = (FeaturePtr)malloc(sizeof(struct feature));
                    if(example[exampleIndex][j] == NULL){
					fprintf(stderr,"Memory allocation failed \n");
					return 0;
					} 
				}
			}
			else{
				/***********/
				example[exampleIndex] =(FeaturePtr *)malloc(sizeof(FeaturePtr));
                if(example[exampleIndex] == NULL){
					fprintf(stderr,"Memory allocation failed \n");
					return 0;
				}
				example[exampleIndex][0]=(FeaturePtr)malloc (sizeof(struct feature));
                if(example[exampleIndex][0] == NULL){
					fprintf(stderr,"Memory allocation failed \n");
					return 0;
				}
				example[exampleIndex][0]->id =1;
                example[exampleIndex][0]->value =0;
				nonZeroFeature[exampleIndex] =0;
				zeroFeatureExample[zeroFeatureNumber]=exampleIndex;
				zeroFeatureNumber++;
			}
			i = 0;
			while (temp[i] != ' '){
				temp2[i] = temp[i];
				i++;
			}
			temp2[i] ='\0';
			target[exampleIndex] = atoi(temp2);
			i++;

			if(numFeature !=0){
				/*****extract id/value pairs of the features of the test data****/
				featureIndex =0;
				while(temp[i] !='\0'){
					j=0;
					while(temp[i] !=':'){
						temp2[j] =temp[i];
						i++;
						j++;
					}
					temp2[j] ='\0';
					example[exampleIndex][featureIndex]->id =atoi(temp2);
					j=0;
					i++;
					while( temp[i] !=' ' && temp[i] !='\0'){
	                    temp2[j] =temp[i];
						i++;
						j++;
					}
					temp2[j] ='\0';

					if( atof(temp2) !=0){
						example[exampleIndex][featureIndex]->value =atof(temp2);
						featureIndex++;
					}

				} //end while
				nonZeroFeature[exampleIndex] = featureIndex;
				if(example[exampleIndex][featureIndex -1]->id >maxFeature)
					maxFeature = example[exampleIndex][featureIndex-1]->id ;
			}
		} //end else
		c=getc(in);
	}//end while not eof

	/*********if the maxFeature read from model file is less than the number of 
	   feature the test data has,then extend the weight vector.*******/

	   if( maxFeatureRead < maxFeature){
		   weight =(double*)realloc(weight, maxFeature+1);
		   for (i =maxFeatureRead+1;i<=maxFeature;i++)
			   weight[i] =0;
	   }
	   return 1;

}


⌨️ 快捷键说明

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