[Rcpp-commits] r2314 - in pkg/RcppDE: . src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 16 22:26:37 CEST 2010


Author: edd
Date: 2010-10-16 22:26:37 +0200 (Sat, 16 Oct 2010)
New Revision: 2314

Modified:
   pkg/RcppDE/benchmark.txt
   pkg/RcppDE/src/de4_0.cpp
   pkg/RcppDE/src/evaluate.cpp
Log:
every vector is now a rowvec


Modified: pkg/RcppDE/benchmark.txt
===================================================================
--- pkg/RcppDE/benchmark.txt	2010-10-16 19:21:27 UTC (rev 2313)
+++ pkg/RcppDE/benchmark.txt	2010-10-16 20:26:37 UTC (rev 2314)
@@ -95,3 +95,35 @@
 Genrose20   0.894167 0.896722          1.00286      -0.28580
 Genrose50   2.527278 2.562778          1.01405      -1.40467
 MEANS       0.565917 0.570300          1.00775      -0.77455
+
+
+# At  2010-10-16 14:34:43.987088
+# SVN  2313M 
+             DEoptim   RcppDE ratioRcppToBasic pctGainOfRcpp
+Rastrigin2  0.040389 0.041556           1.0289      -2.88858
+Rastrigin5  0.105556 0.108667           1.0295      -2.94737
+Rastrigin20 0.543667 0.550444           1.0125      -1.24668
+Wild2       0.066222 0.067167           1.0143      -1.42617
+Wild5       0.179833 0.182556           1.0151      -1.51375
+Wild20      1.035556 1.044944           1.0091      -0.90665
+Genrose2    0.071111 0.072056           1.0133      -1.32813
+Genrose5    0.186278 0.188667           1.0128      -1.28243
+Genrose20   0.890889 0.905389           1.0163      -1.62759
+Genrose50   2.530889 2.577167           1.0183      -1.82852
+MEANS       0.565039 0.573861           1.0156      -1.56135
+
+
+# At  2010-10-16 14:59:19.219468 
+# SVN  2313M 
+             DEoptim   RcppDE ratioRcppToBasic pctGainOfRcpp
+Rastrigin2  0.039889 0.041167           1.0320      -3.20334
+Rastrigin5  0.107667 0.107944           1.0026      -0.25800
+Rastrigin20 0.535500 0.541944           1.0120      -1.20344
+Wild2       0.065944 0.067000           1.0160      -1.60067
+Wild5       0.180167 0.183222           1.0170      -1.69596
+Wild20      1.031222 1.039500           1.0080      -0.80272
+Genrose2    0.069500 0.070722           1.0176      -1.75859
+Genrose5    0.184778 0.185444           1.0036      -0.36079
+Genrose20   0.884500 0.896111           1.0131      -1.31273
+Genrose50   2.503333 2.542278           1.0156      -1.55570
+MEANS       0.560250 0.567533           1.0130      -1.3000

Modified: pkg/RcppDE/src/de4_0.cpp
===================================================================
--- pkg/RcppDE/src/de4_0.cpp	2010-10-16 19:21:27 UTC (rev 2313)
+++ pkg/RcppDE/src/de4_0.cpp	2010-10-16 20:26:37 UTC (rev 2314)
@@ -11,17 +11,18 @@
 
 RcppExport SEXP DEoptimC(SEXP lower, SEXP upper, SEXP fn, SEXP control, SEXP rho);
 void devol(double VTR, double f_weight, double fcross, int i_bs_flag, 
-           arma::colvec & lower, arma::colvec & upper, SEXP fcall, SEXP rho, int i_trace,
+           arma::rowvec & lower, arma::rowvec & upper, SEXP fcall, SEXP rho, int i_trace,
            int i_strategy, int i_D, int i_NP, int i_itermax,
-           arma::colvec & initpopv, int i_storepopfreq, int i_storepopfrom,
+           arma::rowvec & initpopv, int i_storepopfreq, int i_storepopfrom,
            int i_specinitialpop, int i_check_winner, int i_av_winner,
            arma::mat    & ta_popP, arma::mat    & ta_oldP, arma::mat    & ta_newP, arma::rowvec & t_bestP, 
-	   arma::colvec & ta_popC, arma::colvec & ta_oldC, arma::colvec & ta_newC, double       & t_bestC,	
-           arma::colvec & t_bestitP, arma::rowvec & t_tmpP, arma::colvec & tempP,
-           arma::colvec & d_pop, arma::colvec & d_storepop, arma::colvec & d_bestmemit, arma::colvec & d_bestvalit,
+	   arma::rowvec & ta_popC, arma::rowvec & ta_oldC, arma::rowvec & ta_newC, double       & t_bestC,	
+           arma::rowvec & t_bestitP, arma::rowvec & t_tmpP, arma::rowvec & tempP,
+           arma::rowvec & d_pop, arma::rowvec & d_storepop, arma::rowvec & d_bestmemit, arma::rowvec & d_bestvalit,
            int & i_iterations, double i_pPct, long & l_nfeval);
 void permute(int ia_urn2[], int i_urn2_depth, int i_NP, int i_avoid, int ia_urntmp[]);
-RcppExport double evaluate(long &l_nfeval, const arma::rowvec & param, SEXP par, SEXP fcall, SEXP env);
+//RcppExport double evaluate(long & l_nfeval, const arma::rowvec & param, SEXP par, SEXP fcall, SEXP env);
+RcppExport double evaluate(long & l_nfeval, const double *param, SEXP par, SEXP fcall, SEXP env);
 
 RcppExport SEXP DEoptimC(SEXP lowerS, SEXP upperS, SEXP fnS, SEXP controlS, SEXP rhoS) {
     BEGIN_RCPP ;	// macro to fill in try part of try/catch exception handler
@@ -49,29 +50,29 @@
     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 three Rcpp vectors to arma vectors
-    arma::colvec maxbound(f_upper.begin(), f_upper.size(), false);
-    arma::colvec initpopv(initialpopv.begin(), initialpopv.size(), false);
+    arma::rowvec minbound(f_lower.begin(), f_lower.size(), false); 	// convert three Rcpp vectors to arma vectors
+    arma::rowvec maxbound(f_upper.begin(), f_upper.size(), false);
+    arma::rowvec initpopv(initialpopv.begin(), initialpopv.size(), false);
 
     arma::mat ta_popP(i_NP*2, i_D);    					// Data structures for parameter vectors 
     arma::mat ta_oldP(i_NP,   i_D);
     arma::mat ta_newP(i_NP,   i_D);
     arma::rowvec t_bestP(i_D); 
 
-    arma::colvec ta_popC(i_NP*2);  				    	// Data structures for obj. fun. values associated with par. vectors 
-    arma::colvec ta_oldC(i_NP);
-    arma::colvec ta_newC(i_NP);
+    arma::rowvec ta_popC(i_NP*2);  				    	// Data structures for obj. fun. values associated with par. vectors 
+    arma::rowvec ta_oldC(i_NP);
+    arma::rowvec ta_newC(i_NP);
     double t_bestC; 
 
-    arma::colvec t_bestitP(i_D);
+    arma::rowvec t_bestitP(i_D);
     arma::rowvec t_tmpP(i_D); 
-    arma::colvec tempP(i_D);
+    arma::rowvec tempP(i_D);
 
     int i_nstorepop = ceil((i_itermax - i_storepopfrom) / i_storepopfreq);
-    arma::colvec d_pop(i_NP*i_D); 
-    arma::colvec d_storepop(i_NP*i_D*i_nstorepop); 
-    arma::colvec d_bestmemit(i_itermax*i_D);       
-    arma::colvec d_bestvalit(i_itermax); 	 
+    arma::rowvec d_pop(i_NP*i_D); 
+    arma::rowvec d_storepop(i_NP*i_D*i_nstorepop); 
+    arma::rowvec d_bestmemit(i_itermax*i_D);       
+    arma::rowvec d_bestvalit(i_itermax); 	 
     int i_iter = 0;
 
     // call actual Differential Evolution optimization given the parameters
@@ -94,15 +95,15 @@
 
 
 void devol(double VTR, double f_weight, double f_cross, int i_bs_flag,
-           arma::colvec & fa_minbound, arma::colvec & fa_maxbound, SEXP fcall, SEXP rho, int i_trace,
+           arma::rowvec & fa_minbound, arma::rowvec & fa_maxbound, SEXP fcall, SEXP rho, int i_trace,
            int i_strategy, int i_D, int i_NP, int i_itermax,
-           arma::colvec & initialpopv, int i_storepopfrom, int i_storepopfreq, 
+           arma::rowvec & initialpopv, int i_storepopfrom, int i_storepopfreq, 
            int i_specinitialpop, int i_check_winner, int i_av_winner,
            arma::mat &ta_popP, arma::mat &ta_oldP, arma::mat &ta_newP, 
-	   arma::rowvec & t_bestP, arma::colvec & ta_popC, arma::colvec & ta_oldC, arma::colvec & ta_newC, 
+	   arma::rowvec & t_bestP, arma::rowvec & ta_popC, arma::rowvec & ta_oldC, arma::rowvec & ta_newC, 
 	   double & t_bestC,
-           arma::colvec & t_bestitP, arma::rowvec & t_tmpP, arma::colvec & tempP,
-           arma::colvec &d_pop, arma::colvec &d_storepop, arma::colvec & d_bestmemit, arma::colvec & d_bestvalit,
+           arma::rowvec & t_bestitP, arma::rowvec & t_tmpP, arma::rowvec & tempP,
+           arma::rowvec &d_pop, arma::rowvec &d_storepop, arma::rowvec & d_bestmemit, arma::rowvec & d_bestvalit,
            int & i_iterations, double i_pPct, long & l_nfeval) {
 
     const int urn_depth = 5;   			// 4 + one index to avoid 
@@ -121,7 +122,7 @@
     int i_pbest;    				// vars for DE/current-to-p-best/1 
     int p_NP = round(i_pPct * i_NP);  		// choose at least two best solutions 
     p_NP = p_NP < 2 ? 2 : p_NP;
-    arma::icolvec sortIndex(i_NP); 		// sorted values of ta_oldC 
+    arma::irowvec sortIndex(i_NP); 		// sorted values of ta_oldC 
     for(i = 0; i < i_NP; i++) sortIndex[i] = i;
 
     int i_len, done, step, bound;    		// vars for when i_bs_flag == 1 */
@@ -157,7 +158,8 @@
 	} else { /* or user-specified initial member */
 	    ta_popP.row(i) = initialpop.row(i);
 	} 
-	ta_popC[i] = evaluate(l_nfeval, ta_popP.row(i), par, fcall, rho);
+	arma::rowvec r = ta_popP.row(i);
+	ta_popC[i] = evaluate(l_nfeval, r.memptr(), par, fcall, rho);
 	if (i == 0 || ta_popC[i] <= t_bestC) {
 	    t_bestC = ta_popC[i];
 	    //for (j = 0; j < i_D; j++)  
@@ -205,7 +207,7 @@
 	/*---DE/current-to-p-best/1 -----------------------------------------------------*/
 	if (i_strategy == 6) {
 	    /* create a copy of ta_oldC to avoid changing it */
-	    arma::colvec temp_oldC = ta_oldC;
+	    arma::rowvec temp_oldC = ta_oldC;
 	    /* sort temp_oldC to use sortIndex later */
 	    rsort_with_index( temp_oldC.memptr(), sortIndex.begin(), i_NP );
 	}
@@ -354,7 +356,7 @@
 	    }
 
 	    /*------Trial mutation now in t_tmpP-----------------*/
-	    t_tmpC = evaluate(l_nfeval, t_tmpP, par, fcall, rho); 	    // Evaluate mutant in t_tmpP[]
+	    t_tmpC = evaluate(l_nfeval, t_tmpP.memptr(), par, fcall, rho); 	    // Evaluate mutant in t_tmpP[]
 
 	    /* note that i_bs_flag means that we will choose the
 	     *best NP vectors from the old and new population later*/
@@ -438,7 +440,7 @@
 		}
 	    if(same && i_iter > 1)  {
 		i_xav++;
-		tmp_best = evaluate(l_nfeval, t_bestP, par, fcall, rho);			// if re-evaluation of winner 
+		tmp_best = evaluate(l_nfeval, t_bestP.memptr(), par, fcall, rho);			// if re-evaluation of winner 
 
 		/* possibly letting the winner be the average of all past generations */
 		if(i_av_winner)
@@ -499,15 +501,12 @@
  ** Return Value   : -
  *********************************************************************/
 {
-    int  i, k, i_urn1, i_urn2;
-    // double ia_urn1[i_NP];
-  
     GetRNGstate();
 
-    k = i_NP;
-    i_urn1 = 0;
-    i_urn2 = 0;
-    for (i = 0; i < i_NP; i++)
+    int k = i_NP;
+    int i_urn1 = 0;
+    int i_urn2 = 0;
+    for (int i = 0; i < i_NP; i++)
 	ia_urn1[i] = i; /* initialize urn1 */
 
     i_urn1 = i_avoid;                      /* get rid of the index to be avoided and place it in position 0. */

Modified: pkg/RcppDE/src/evaluate.cpp
===================================================================
--- pkg/RcppDE/src/evaluate.cpp	2010-10-16 19:21:27 UTC (rev 2313)
+++ pkg/RcppDE/src/evaluate.cpp	2010-10-16 20:26:37 UTC (rev 2314)
@@ -7,9 +7,12 @@
 
 #include <RcppArmadillo.h>
 
-RcppExport double evaluate(long &l_nfeval, const arma::rowvec & param, SEXP parS, SEXP fcall, SEXP env) {
+//RcppExport double evaluate(long &l_nfeval, const arma::rowvec & param, SEXP parS, SEXP fcall, SEXP env) {
+RcppExport double evaluate(long &l_nfeval, const double *param, SEXP parS, SEXP fcall, SEXP env) {
     Rcpp::NumericVector par(parS); 			// access parS as numeric vector to fill it
-    std::copy(param.begin(), param.end(), par.begin()); // STL way of copying
+    //memcpy(par.begin(), param.memptr(), par.size() * sizeof(double));
+    //std::copy(param.begin(), param.end(), par.begin()); // STL way of copying
+    memcpy(par.begin(), param, par.size() * sizeof(double));
     SEXP fn = ::Rf_lang2(fcall, par); 			// this could be done with Rcpp 
     SEXP sexp_fvec = ::Rf_eval(fn, env);		// but is still a lot slower right now
     double f_result = Rcpp::as<double>(sexp_fvec);



More information about the Rcpp-commits mailing list