📄 xtal_v51.c
字号:
while( ! ( isdigit(*para_ptr)) ){ if(*para_ptr) ++(para_ptr); else return 0 ; } num = atoi( para_ptr ); while ( isdigit(*para_ptr) ) ++(para_ptr) ; *a = num ; if( *para_ptr != ',') { printf("Illegal syntax in operaton file paramater list\n"); exit(0); } ++(para_ptr); num = atoi( para_ptr ); while ( isdigit(*para_ptr) ) ++(para_ptr) ; *b = num ; return 1 ;}void ppr1(){ char ppconname[] = "pp.config"; char ppr_result_file[] = "ppr.res"; FILE *ppconfile,*testfile, *resultfile, *outfile ,*maxerr; int m_max , m_min ; int flag , k ,status; char line[241]; char *str ; char **pstr; double mse_sum,rms,diff,y,y_est,max_diff,fvu,max_ratio,mse,rmsr ; while( one_param( &m_min ) ){ m_max = m_min+5 ; ppconfile = fopen(ppconname,"w"); fprintf(ppconfile,"%s\n",trainname); fprintf(ppconfile,"%d\n",trn_samp_num); fprintf(ppconfile,"%1d\n",predx); fprintf(ppconfile,"%s\n",testname); fprintf(ppconfile,"%d\n",tst_samp_num); fprintf(ppconfile,"%d\n",m_max); fprintf(ppconfile,"%d\n",m_min); fprintf(ppconfile,"%s\n",ppr_result_file); fclose(ppconfile); status = system("./xpp"); if (status!=0) { outfile = fopen(out_file_name,"a"); fprintf(outfile,"PRPR terms = %d failed with status = %x\n",m_min,status); fclose(outfile); continue; } testfile = fopen(testname,"r"); resultfile = fopen(ppr_result_file,"r"); if(resultfile == NULL) { printf("Can't open %s\n",ppr_result_file); return ; } mse_sum = 0; max_diff = 0.0; for(k=1 ;k<=tst_samp_num ;k++) { flag = fscanf(resultfile,"%[^\n]\n",line); if(flag == EOF) break; str = line; pstr = &str; y = pardub(pstr); y_est = pardub(pstr); diff = y_est - y ; mse_sum += diff * diff ; if(diff > max_diff) max_diff = diff ; } rms = sqrt(mse_sum/tst_samp_num); mse = mse_sum/tst_samp_num ; fvu = mse/response_mean_variance ; rmsr = rms/response_mean_rms ; outfile = fopen(out_file_name,"a"); fprintf(outfile,"PRPR terms = %d NRMS error ==> %f\n",m_min,rmsr); star_bar_plot(outfile,rms,response_mean_rms); fclose(testfile); fclose(resultfile); fclose(outfile); maxerr =fopen(maxerr_name,"a"); max_ratio = max_diff/tst_diff ; fprintf(maxerr,"PRPR terms = %d NMAX error ==> %f\n",m_min,max_ratio); star_bar_plot(maxerr,max_diff,tst_diff); fclose(maxerr); } return;}void mars1() { char marsconname[] = "mars.con"; char mars_result_file[] = "mars.res"; char mars_message_file[] = "mars.mes"; char mars_rms_file[] = "mars.rms" ; FILE *marsconfile,*testfile, *resultfile, *outfile ,*maxerr; int basismax ,degrees; int flag , k, n ,status ; char line[241]; char *str ; char **pstr; double mse_sum,rms,diff,y,y_est,max_diff,fvu,max_ratio,mse,rmsr ; int spline = SPLINE ; while( two_param( &basismax , °rees) ) { marsconfile = fopen(marsconname,"w"); if(marsconfile == NULL) { printf("Can't open %s\n",marsconname); return ; } fprintf(marsconfile,"%s\n",trainname); fprintf(marsconfile,"%d\n",trn_samp_num); fprintf(marsconfile,"%d\n",predx); fprintf(marsconfile,"%s\n",testname); fprintf(marsconfile,"%d\n",tst_samp_num); fprintf(marsconfile,"%d\n",basismax); fprintf(marsconfile,"%d\n",degrees); fprintf(marsconfile,"%d\n",predx); fprintf(marsconfile,"%d\n",spline); fprintf(marsconfile,"%s\n",mars_message_file); fprintf(marsconfile,"%s\n",mars_result_file); fprintf(marsconfile,"%s\n",mars_rms_file); fclose(marsconfile); status = system("./xmars <mars.con"); if (status!=0) { outfile = fopen(out_file_name,"a"); fprintf(outfile,"MARS with %d max. basis functions and %d d.o.f. failed with status = %x\n",basismax,degrees,status); fclose(outfile); continue; } testfile = fopen(testname,"r"); resultfile = fopen(mars_result_file,"r"); if(resultfile == NULL) { printf("Can't open %s\n",mars_result_file); return ; } max_diff= 0.0 ; mse_sum = 0; for(k=1 ;k<=tst_samp_num ;k++) { flag= fscanf(testfile,"%[^\n]\n",line); if(flag==EOF) break; str = line; pstr = &str; for( n=1 ; n<=predx ; n++) { pardub(pstr); } y = pardub(pstr); flag= fscanf(resultfile,"%[^\n]\n",line); if(flag==EOF) break; str = line; pstr = &str; for( n=1 ; n<=predx ; n++) { pardub(pstr); } y_est = pardub(pstr); diff = y_est - y ; mse_sum += diff * diff ; if(diff > max_diff) max_diff = diff ; } rms = sqrt(mse_sum/tst_samp_num); mse = mse_sum/tst_samp_num ; fvu = mse/response_mean_variance ; rmsr = rms/response_mean_rms ; outfile = fopen(out_file_name,"a"); fprintf(outfile,"MARS with %d max. basis functions and %d d.o.f. NRMS error ==>> %f\n",basismax,degrees,rmsr); star_bar_plot(outfile,rms,response_mean_rms); fclose(testfile); fclose(resultfile); fclose(outfile); maxerr =fopen(maxerr_name,"a"); max_ratio = max_diff/tst_diff ; fprintf(maxerr,"MARS with %d max basis functions and %d d.o.f. NMAX error ==> %f\n",basismax,degrees,max_ratio); star_bar_plot(maxerr,max_diff,tst_diff); fclose(maxerr); } return ;}void ann1(){ char annconname[] = "ann1.con"; char ann_result_file[] = "ann_temp.res"; char unscaled_ann_result_file[] = "ann1.res"; FILE *unscaled_resultfile ; FILE *annconfile,*trainfile,*testfile, *resultfile, *outfile ,*maxerr; int neurons ; int flag, i , k ,n ,status; char line[241]; char s[30] ; char *str ; char **pstr; double mse_sum,rms,diff,y,y_est ,max_diff,x,fvu,max_ratio,mse,rmsr ; while ( one_param( &neurons ) ){ annconfile = fopen(annconname,"w"); if(annconfile == NULL){ printf("Can't open %s\n",annconname); return ; } /* must remove any previous contents of the ann result file because the NEURAL program only appends results to an existing file instead of clearing it first */ resultfile = fopen(ann_result_file,"w"); fclose(resultfile); strcpy(s,"rm "); strcat(s,ann_result_file); system(s); fprintf(annconfile,"NETWORK MODEL : LAYER\n"); fprintf(annconfile,"LAYER INIT : %s\n",ANNINITIALIZATION); fprintf(annconfile,"OUTPUT MODEL : GENERAL\n"); fprintf(annconfile,"QUIT RETRIES : %d\n",RETRIES); fprintf(annconfile,"N INPUTS : %d\n",predx); fprintf(annconfile,"N OUTPUTS : 1\n"); fprintf(annconfile,"N HIDDEN1 : %d\n",neurons); fprintf(annconfile,"N HIDDEN2 : 0\n"); fprintf(annconfile,"TRAIN : %s\n",temptrainname); fprintf(annconfile,"LEARN:\n"); fprintf(annconfile,"OUTPUT FILE : %s\n",ann_result_file); fprintf(annconfile,"EXECUTE : %s\n",temptestname); fprintf(annconfile,"CLEAR WEIGHTS :\n"); fprintf(annconfile,"CLEAR TRAINING :\n"); fprintf(annconfile,"QUIT :\n"); fclose(annconfile); status = system("./neural <ann1.con"); if (status!=0) { outfile = fopen(out_file_name,"a"); fprintf(outfile,"ANN1 with %d hidden neurons failed with status = %x\n",neurons,status); fclose(outfile); continue; } testfile = fopen(testname,"r"); resultfile = fopen(ann_result_file,"r"); if(resultfile == NULL) { printf("Can't open %s\n",ann_result_file); return ; } unscaled_resultfile = fopen(unscaled_ann_result_file,"w"); max_diff =0.0; mse_sum = 0; for(k=1 ;k<=tst_samp_num ;k++) { flag= fscanf(testfile,"%[^\n]\n",line); if(flag==EOF) break; str = line; pstr = &str; for( n=1 ; n<=predx ; n++) { x = pardub(pstr); fprintf(unscaled_resultfile,"%f ",x); } y = pardub(pstr); flag= fscanf(resultfile,"%[^\n]\n",line); if(flag ==EOF) break; str = line; pstr = &str; y_est = pardub(pstr); /********** unscale estimate ***************/ y_est =( ((y_est-.1)*scale)/.8) + response_min ; fprintf(unscaled_resultfile,"%f\n",y_est); diff = y_est - y ; mse_sum += diff * diff ; if(diff > max_diff) max_diff = diff; } rms = sqrt(mse_sum/tst_samp_num); mse = mse_sum/tst_samp_num ; fvu = (mse_sum/tst_samp_num)/response_mean_variance ; rmsr = rms/response_mean_rms ; fclose(testfile); fclose(resultfile); fclose(unscaled_resultfile); strcpy(s,"rm "); strcat(s,ann_result_file); system(s); outfile = fopen(out_file_name,"a"); fprintf(outfile,"ANN1 with %d hidden neurons NRMS error ==>> %f\n",neurons,rmsr); star_bar_plot(outfile,rms,response_mean_rms); fclose(outfile); maxerr =fopen(maxerr_name,"a"); max_ratio = max_diff/tst_diff ; fprintf(maxerr,"ANN1 with %d hidden neurons NMAX error ==> %f\n",neurons,max_ratio); star_bar_plot(maxerr,max_diff,tst_diff); fclose(maxerr); } return ;}void ctm1(){ char ctmconname[] = "ctm.con"; char ctm_result_file[] = "fit1"; FILE *ctmconfile,*testfile, *resultfile, *outfile ,*maxerr; int mapdim , smoothness ; int flag , k, n ,status ; char line[241]; char *str ; char **pstr; double mse_sum,rms,diff,y,y_est,sigmax ,max_diff,fvu,max_ratio,mse,rmsr ; int iterations = ITERATIONS ; while( two_param( &mapdim , &smoothness ) ){ ctmconfile = fopen(ctmconname,"w"); if(ctmconfile == NULL){ printf("Can't open %s\n",ctmconname); return ; } fprintf(ctmconfile,"%s\n",trainname); fprintf(ctmconfile,"%s\n",testname); fprintf(ctmconfile,"%d\n",mapdim); fprintf(ctmconfile,"%d\n",smoothness); fprintf(ctmconfile,"0\n"); fprintf(ctmconfile,"1\n"); fprintf(ctmconfile,"done\n"); fclose(ctmconfile); status = system("./bctm <ctm.con"); if (status!=0) { outfile = fopen(out_file_name,"a"); fprintf(outfile,"CTMR with %d map dimensions and %d smoothness failed with status = %x\n",mapdim,smoothness,status); fclose(outfile); continue; } testfile = fopen(testname,"r"); resultfile = fopen(ctm_result_file,"r"); if(resultfile == NULL) { printf("Can't open %s\n",ctm_result_file); return ; } max_diff = 0.0; mse_sum = 0; for(k=1 ;k<=tst_samp_num ;k++){ flag= fscanf(testfile,"%[^\n]\n",line); if(flag==EOF) break; str = line; pstr = &str; for( n=1 ; n<=predx ; n++){ pardub(pstr); } y = pardub(pstr); flag= fscanf(resultfile,"%[^\n]\n",line); if(flag==EOF) break; str = line; pstr = &str; for( n=1 ; n<=predx ; n++){ pardub(pstr); } y_est = pardub(pstr); diff = y_est - y ; mse_sum += diff * diff ; if(diff > max_diff) max_diff = diff ; } rms = sqrt(mse_sum/tst_samp_num); mse = mse_sum/tst_samp_num ; fvu = mse/response_mean_variance ; rmsr = rms/response_mean_rms ; outfile = fopen(out_file_name,"a"); fprintf(outfile,"CTMR with %d map dimensions and %d smoothness level NRMS error ==>> %f\n",mapdim,smoothness,rmsr); star_bar_plot(outfile,rms,response_mean_rms); fclose(testfile); fclose(resultfile); fclose(outfile); maxerr =fopen(maxerr_name,"a"); max_ratio = max_diff/tst_diff; fprintf(maxerr,"CTMR with %d map dimensions and %d smoothness level NMAX error ==> %f\n",mapdim,smoothness,max_ratio); star_bar_plot(maxerr,max_diff,tst_diff); fclose(maxerr); } return ;}void knn1() {#define MAX_SAMP 10001#define MAX_IN 10#define MAX_K 100 int samp_num; double trn[MAX_SAMP][MAX_IN+2]; char knn_result_name[16] = "knn.res" ; int i,j,k,flag,test_num,valid_num ; char *str; char **pstr; char line[241]; FILE *trainfile; FILE *testfile,*unscaled_testfile ; FILE *outfile ,*maxerr; FILE *resultfile; double mse_sum,mse_best,y,y_est,diff,rms,max_diff,fvu,max_ratio,mse,rmsr ; double x[MAX_IN+1]; double x_near[MAX_K+1]; double y_near[MAX_K+1]; double distance,xdif,xtemp,ytemp,yy; double ysum =0.0; resultfile = fopen(knn_result_name,"w"); trainfile =fopen(tem2trainname,"r"); samp_num = 0; do{ flag = fscanf(trainfile,"%[^\n]\n",line); if(flag == EOF) break; samp_num++; str = line; pstr = &str; for( i=1 ; i<=predx+1 ; i++){ trn[samp_num][i] =pardub(pstr); } }while(1); fclose(trainfile); while( one_param(&k) ) { testfile = fopen(tem2testname,"r"); unscaled_testfile = fopen(testname,"r"); mse_sum = 0.0; test_num = 0; do{ flag = fscanf(testfile,"%[^\n]\n",line); if(flag==EOF) break; str = line; pstr = &str; for( i=1 ; i<=predx ; i++){ x[i] = pardub(pstr); } y = pardub(pstr); /******** compute the nearest neighbor *****************/ for(j=1 ; j<=MAX_K ; j++) x_near[j] = 999999999999.0 ; ysum = 0.0; for(i=1; i<= samp_num ; i++){ distance = 0.0; for(j=1; j<=predx ; j++){ xdif = (x[j]-trn[i][j]); distance += xdif*xdif; } distance = sqrt(distance); yy = trn[i][predx+1]; for(j=1 ; j<=k ; j++){ if(distance < x_near[j] ) { xtemp = x_near[j]; ytemp = y_near[j]; x_near[j] = distance; y_near[j] = yy; distance = xtemp; yy = ytemp; } } } for(j=1; j<=k ; j++){ ysum += y_near[j]; } y_est = ysum/ (double) k ; /*****************************************************/ diff = y_est - y ; mse_sum += diff * diff ; test_num++; if(diff > max_diff) max_diff = diff; fscanf(unscaled_testfile,"%[^\n]\n",line); str = line ; pstr = &str ; for(i=1 ; i<=predx ; i++) fprintf(resultfile,"%f ",pardub(pstr) ); fprintf(resultfile,"%11.8f\n",y_est); }while(1); rms = sqrt(mse_sum/tst_samp_num); mse = mse_sum/tst_samp_num ; fvu =mse/response_mean_variance ; rmsr = rms/response_mean_rms ; outfile = fopen(out_file_name,"a"); fprintf(outfile,"K-NN with %d neighbors NRMS error ===>> %f\n",k,rmsr); star_bar_plot(outfile,rms,response_mean_rms); fclose(testfile); fclose(unscaled_testfile); fclose(outfile); /* this causes a crash for some reason. HR */ //fclose(resultfile); maxerr =fopen(maxerr_name,"a"); max_ratio = max_diff/tst_diff; fprintf(maxerr,"K-NN with %d neighbors NMAX error ==> %f\n",k,max_ratio); star_bar_plot(maxerr,max_diff,tst_diff); fclose(maxerr); } return ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -