📄 adaboost_common.cpp
字号:
c=(char)ic;
current_length++;
if(c == ' ')
{
current_wol++;
}
if(c == '\n')
{
(*nol)++;
if(current_length>(*ll))
{
(*ll)=current_length;
}
if(current_wol>(*wol))
{
(*wol)=current_wol;
}
current_length=0;
current_wol=0;
}
}
fclose(fl);
}
long get_runtime()
{
clock_t start;
start = clock();
return((long)((double)start*100.0/(double)CLOCKS_PER_SEC));
}
int isnan(double a)
{
return(_isnan(a));
}
void * my_malloc(long size)
{
void *ptr;
ptr=(void *)malloc(size);
if(!ptr)
{
printe ("Out of memory!");
return (NULL);
}
return(ptr);
}
//print error on screen
void printe(char* str)
{
char err[200]="--error--";
strcat(err,str);
theApp.ShowM(err);
}
//print message on screen
void printm(char* str)
{
theApp.ShowM(str);
}
void SetInitParam()
{
com_param.biased_Hyperplane=1;
com_param.remove_inconsitant=0;
com_param.C =0.0;
com_param.cost_factor =1.0;
com_param.loo =0;
com_param.search_depth = 0;
com_param.rho = 1.0;
com_param.fraction =1.0;
com_param.rbf_gamma =1.0;
com_param.poly_c =0.0;
com_param.poly_s =1.0;
com_param.poly_degree =1;
com_param.kernel_type =0;
//com_param.user_u = pp4.m_strU ;
com_param.epsion =0.001;
com_param.iteration_time =100;
com_param.cache_size =40;
com_param.new_variable =com_param.maximum_size;
com_param.maximum_size =10;
com_param.final_test = 1;
com_param.blAadaboostresults=TRUE;
com_param.Running=FALSE;
com_param.Finished=TRUE;
com_param.Close=FALSE;
//prompt default values
com_pro.show_action=TRUE;
com_pro.show_compute_1=TRUE;
com_pro.show_compute_2=FALSE;
com_pro.show_compute_3=FALSE;
com_pro.show_other=FALSE;
com_pro.show_readfile=FALSE;
com_pro.show_writefile=FALSE;
com_pro.show_testresult=TRUE;
com_pro.show_trainresult=FALSE;
}
/******************** tmp code ****************************/
/*if (com_pro.show_compute_1)
{
sprintf(temstr,"index: %.d\n",index);
printm(temstr);
}
//i=floor(tt/16)+1;
if (index > 400 && index < 500 ){
(*i) = floor(index/17)+1;
if (*i == (c_train-1)){
*i = (c_train-10);
}
}
if (index > 300 && index < 400 ){
(*i) = floor(index/12)+1;
if (*i == (c_train-1)){
*i = (c_train-10);
}
}
if (index > 200 && index < 300){
(*i) = floor(index/7)+1;
if (*i == (c_train-1)){
*i = (c_train-10);
}
}
if (index > 100 && index < 200){
(*i) = floor(index/5)+1;
if (*i == (c_train-1)){
*i = (c_train-10);
}
}
if (index > 0 && index < 100){
(*i) = floor(index/2)+1;
if (*i == (c_train-1)){
*i = (c_train-10);
}
}
//Matrix* i_all = new Matrix(cycles,value_one);
i_all->setValue(j,*i);
if (j > 2)
{
get_i_all->matrixCompareAndSet(i,c_train,*i_all);
}
// Get the avarage of thr data
double Sum_ava_data=0;
Ava_data_matrix->matrixSumCol(&Sum_ava_data, *train);
(*t) = Sum_ava_data / (step * (c_train-2));
// if (step >= 300){
// (*t) = (*t) + 15;
// }
/*
//t=16*(mod(tt,16)+1);
//(*t) = sqrt_c_train * ((index % 16) + 1);
(*t) = 9 * ((index % 13) + 1);
t_all->setValue(j,*t);
if (j > 1)
{
get_t_all->matrixCompareAndSetfor_t(j,t,c_train,*t_all);
} */
/*
//(*i) = index;
// t=16*(mod(tt,4)+1);
int i_rand_tmp;
//int i_rand;
srand( (unsigned)time( NULL ) );
i_rand_tmp=rand();
if (i_rand_tmp > 100 && i_rand_tmp < 1000){
//i_rand=floor(i_rand_tmp/100)+1;
(*t)=floor(i_rand_tmp/10)+50;
if (*t < 75) {
(*t)=floor(i_rand_tmp/10)+70;
}
}
if (i_rand_tmp > 1000 && i_rand_tmp < 10000){
//i_rand=floor(i_rand_tmp/100)+1;
(*t)=floor(i_rand_tmp/100)+60;
if (*t > 105) {
(*t)=floor(i_rand_tmp/1000)+70;
}
if (*t < 75) {
(*t)=floor(i_rand_tmp/100)+70;
}
}
if (i_rand_tmp > 10000 && i_rand_tmp < 100000){
//i_rand=floor(i_rand_tmp/1000)+100;
(*t)=floor(i_rand_tmp/1000)+80;
if (*t > 105) {
(*t)=floor(i_rand_tmp/1000)+70;
}
}
if (*t < 75 ){
(*t) = (*t) + 10;
}
if (*t > 110) {
(*t)=(*t) - 10;
}
t_all->setValue(j,*t);
if (j > 1)
{
get_t_all->matrixCompareAndSetfor_t(j,t,c_train,*t_all);
}
(*t)=120;
*/
//(*t) = 3 * sqrt_c_train * ((index % sqrt_c_train) + 1);
//double error_train_array[443];
//Matrix* getArrayTrain_error = new Matrix(443,value_one);
//getArrayTrain_error->matrixToArrayold(error_train_array,*errorAbsMinusScalar);
/*
int parse_dataset(char *line,long *train_label,long*numwords,long train_max_words_doc)
{
register long wpos,pos;
//long wnum;
//double weight;
pos=0;
int len;
len = strlen(line);
len=len-2;
while(line[len])
{ // cut off comments
if(line[pos] == '#')
{
line[pos]=0;
}
else
{
pos++;
}
}
wpos=0;
if((sscanf(line,"%ld",train_label)) == EOF) return(0);
pos=0;
}
int read_documents(char *docfile,
DOC *docs,
long *label,
long max_words_doc,
long ll,
long *totwords,
long *totdoc)
{
char *line;
DOC doc;
long dnum=0,wpos,i,dpos=0,dneg=0,dunlab=0;
long doc_label;
FILE *docfl;
line = (char *)my_malloc(sizeof(char)*ll);
if ((docfl = fopen (docfile, "r")) == NULL)
{
printe (docfile);
return -1;
}
doc.words = (SVM_WORD *)my_malloc(sizeof(SVM_WORD)*(max_words_doc+10));
if (com_pro.show_readfile)
{
sprintf(temstr,"Reading examples into memory...");
printm(temstr);
}
dnum=0;
(*totwords)=0;
while((!feof(docfl)) && fgets(line,(int)ll,docfl)) {
if(line[0] == '#') continue; // line contains comments
if(!parse_document(line,&doc,&doc_label,&wpos,max_words_doc))
{
sprintf(temstr,"Parsing error in line %ld!",dnum);
printm(temstr);
}
label[dnum]=doc_label;
if(doc_label > 0) dpos++;
if (doc_label < 0) dneg++;
if (doc_label == 0) dunlab++;
if((wpos>1) && ((doc.words[wpos-2]).wnum>(*totwords)))
(*totwords)=(doc.words[wpos-2]).wnum;
docs[dnum].words = (SVM_WORD *)my_malloc(sizeof(SVM_WORD)*wpos);
docs[dnum].docnum=dnum;
for(i=0;i<wpos;i++)
docs[dnum].words[i]=doc.words[i];
docs[dnum].twonorm_sq=doc.twonorm_sq;
dnum++;
if((dnum % 100) == 0&&com_pro.show_readfile)
{
sprintf(temstr,"read %ld..",dnum);
printm(temstr);
}
}
fclose(docfl);
free(line);
free(doc.words);
if (com_pro.show_action)
{
sprintf(temstr, "OK. (%ld examples read)", dnum);
printm(temstr);
sprintf(temstr,"%ld positive, %ld negative, and %ld unlabeled examples.",dpos,dneg,dunlab);
printm(temstr);
}
(*totdoc)=dnum;
}
// Parse one line of data file
int parse_document(char *line,DOC *doc,long *label,long*numwords,long max_words_doc)
{
register long wpos,pos;
long wnum;
double weight;
pos=0;
while(line[pos])
{ // cut off comments
if(line[pos] == '#')
{
line[pos]=0;
}
else
{
pos++;
}
}
wpos=0;
if((sscanf(line,"%ld",label)) == EOF) return(0);
pos=0;
while(line[pos]==' ') pos++;
while(line[pos]>' ') pos++;
char sring_weight[5];
char NaN[5]="NaN";
int result;
while((sscanf(line+pos,"%ld:%lf",&wnum,&weight)!= EOF) && (wpos<max_words_doc))
{
if(sscanf(line+pos,"%ld:%s",&wnum,&sring_weight)!= EOF)
{
result=strcmp(NaN,sring_weight);
if(result == 0)
{
weight = 0;
}
}
while(line[pos++]==' ');
while(line[++pos]>' ');
if(wnum<=0)
{
printe ("Feature numbers must be larger or equal to 1!!!");
sprintf(temstr,"LINE: %s",line);
printm(temstr);
return (0);
}
if((wpos>0) && ((doc->words[wpos-1]).wnum >= wnum))
{
printe ("Features must be in increasing order!!!");
sprintf(temstr,"LINE: %s",line);
printm(temstr);
return (0);
}
(doc->words[wpos]).wnum=wnum;
(doc->words[wpos]).weight=weight;
wpos++;
}
(doc->words[wpos]).wnum=0;
(*numwords)=wpos+1;
doc->docnum=-1;
//doc->twonorm_sq=sprod_ss(doc->words,doc->words);
return(1);
}
double Array_train[10];
double Array_test[10];
Matrix* getArrayTrain = new Matrix(10,one);
Matrix* getArrayTest = new Matrix(10,one);
getArrayTrain->matrixToArrayold(Array_train,*errorTrain_acc);
getArrayTest->matrixToArrayold(Array_test,*errorTest_acc);
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -