//报告文件的类型 //fname:文件名 //返回值:文件类型编号.0XFF,表示无法识别. u8 f_typetell(u8 *fname) { u8 tbuf[5]; u8 i=0,j; while(i<200) { i++; if(*fname=='\0')break;//偏移到了最后了. fname++; } if(i==200)return 0XFF;//错误的字符串. fname--; //最后一个字符的位置 for(i=0;i<5;i++)//复制后缀名 { tbuf[4-i]=*fname; fname--; } if(tbuf[0]=='.')//后缀名为4个字节 { tbuf[0]=f_upper(tbuf[1]); tbuf[1]=f_upper(tbuf[2]); tbuf[2]=f_upper(tbuf[3]); tbuf[3]=f_upper(tbuf[4]); tbuf[4]='\0';//加入结束符 }else if(tbuf[1]=='.')//3个字节 { tbuf[0]=f_upper(tbuf[2]); tbuf[1]=f_upper(tbuf[3]); tbuf[2]=f_upper(tbuf[4]); tbuf[3]='\0';//加入结束符 }else if(tbuf[2]=='.')//2个字节 { tbuf[0]=f_upper(tbuf[3]); tbuf[1]=f_upper(tbuf[4]); tbuf[2]='\0';//加入结束符 }else if(tbuf[3]=='.')//1个字节 { tbuf[0]=f_upper(tbuf[4]); tbuf[1]='\0';//加入结束符 }else return 0XFF;//未找到后缀名. for(i=0;i<4;i++) { for(j=0;j<13;j++) { if(*FILE_TYPE_TAB[i][j]==0)break;//此组已经没有可对比的成员了. if(f_strcmp((u8 *)FILE_TYPE_TAB[i][j],tbuf))//找到了 { return (i<<4)|j; } } } return 0XFF;//没找到 }
RcppExport SEXP DEoptim(SEXP lowerS, SEXP upperS, SEXP fnS, SEXP controlS, SEXP rhoS) { try { Rcpp::NumericVector f_lower(lowerS), f_upper(upperS); // User-defined bounds Rcpp::List control(controlS); // named list of params double VTR = Rcpp::as<double>(control["VTR"]); // value to reach int i_strategy = Rcpp::as<int>(control["strategy"]); // chooses DE-strategy int i_itermax = Rcpp::as<int>(control["itermax"]); // Maximum number of generations long l_nfeval = 0; // nb of function evals (NOT passed in) int i_D = Rcpp::as<int>(control["npar"]); // Dimension of parameter vector int i_NP = Rcpp::as<int>(control["NP"]); // Number of population members int i_storepopfrom = Rcpp::as<int>(control["storepopfrom"]) - 1; // When to start storing populations int i_storepopfreq = Rcpp::as<int>(control["storepopfreq"]); // How often to store populations int i_specinitialpop = Rcpp::as<int>(control["specinitialpop"]);// User-defined inital population Rcpp::NumericMatrix initialpopm = Rcpp::as<Rcpp::NumericMatrix>(control["initialpop"]); double f_weight = Rcpp::as<double>(control["F"]); // stepsize double f_cross = Rcpp::as<double>(control["CR"]); // crossover probability int i_bs_flag = Rcpp::as<int>(control["bs"]); // Best of parent and child int i_trace = Rcpp::as<int>(control["trace"]); // Print progress? int i_check_winner = Rcpp::as<int>(control["checkWinner"]); // Re-evaluate best parameter vector? int i_av_winner = Rcpp::as<int>(control["avWinner"]); // Average double i_pPct = Rcpp::as<double>(control["p"]); // p to define the top 100p% best solutions arma::colvec minbound(f_lower.begin(), f_lower.size(), false); // convert Rcpp vectors to arma vectors arma::colvec maxbound(f_upper.begin(), f_upper.size(), false); arma::mat initpopm(initialpopm.begin(), initialpopm.rows(), initialpopm.cols(), false); arma::mat ta_popP(i_D, i_NP*2); // Data structures for parameter vectors arma::mat ta_oldP(i_D, i_NP); arma::mat ta_newP(i_D, i_NP); arma::colvec t_bestP(i_D); arma::colvec ta_popC(i_NP*2); // Data structures for obj. fun. values arma::colvec ta_oldC(i_NP); arma::colvec ta_newC(i_NP); double t_bestC; arma::colvec t_bestitP(i_D); arma::colvec t_tmpP(i_D); int i_nstorepop = ceil((i_itermax - i_storepopfrom) / i_storepopfreq); arma::mat d_pop(i_D, i_NP); Rcpp::List d_storepop(i_nstorepop); arma::mat d_bestmemit(i_D, i_itermax); arma::colvec d_bestvalit(i_itermax); int i_iter = 0; // call actual Differential Evolution optimization given the parameters devol(VTR, f_weight, f_cross, i_bs_flag, minbound, maxbound, fnS, rhoS, i_trace, i_strategy, i_D, i_NP, i_itermax, initpopm, i_storepopfrom, i_storepopfreq, i_specinitialpop, i_check_winner, i_av_winner, ta_popP, ta_oldP, ta_newP, t_bestP, ta_popC, ta_oldC, ta_newC, t_bestC, t_bestitP, t_tmpP, d_pop, d_storepop, d_bestmemit, d_bestvalit, i_iter, i_pPct, l_nfeval); return Rcpp::List::create(Rcpp::Named("bestmem") = t_bestP, // and return a named list with results to R Rcpp::Named("bestval") = t_bestC, Rcpp::Named("nfeval") = l_nfeval, Rcpp::Named("iter") = i_iter, Rcpp::Named("bestmemit") = trans(d_bestmemit), Rcpp::Named("bestvalit") = d_bestvalit, Rcpp::Named("pop") = trans(d_pop), Rcpp::Named("storepop") = d_storepop); } catch( std::exception& ex) { forward_exception_to_r(ex); } catch(...) { ::Rf_error( "c++ exception (unknown reason)"); } return R_NilValue; }