[Rcpp-commits] r2342 - in pkg/RcppDE: . src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Oct 19 16:48:54 CEST 2010
Author: edd
Date: 2010-10-19 16:48:54 +0200 (Tue, 19 Oct 2010)
New Revision: 2342
Modified:
pkg/RcppDE/ChangeLog
pkg/RcppDE/src/de4_0.cpp
Log:
some more tweaks to core function
Modified: pkg/RcppDE/ChangeLog
===================================================================
--- pkg/RcppDE/ChangeLog 2010-10-19 13:57:34 UTC (rev 2341)
+++ pkg/RcppDE/ChangeLog 2010-10-19 14:48:54 UTC (rev 2342)
@@ -1,6 +1,33 @@
+2010-10-19 Dirk Eddelbuettel <edd at debian.org>
+
+ * src/de4_0.cpp (DEoptimC):
+ - Removed unused variable tempP
+ - moved some variable declaration down to where they are use
+ - turned one do-while into for(;;) loop
+
+2010-10-18 Dirk Eddelbuettel <edd at debian.org>
+
+ * src/de4_0.cpp (DEoptimC):
+ - Switched from NP in rows to cols which with Armadillo's internal
+ organisation gets a speed up for larger parameter vectors
+ (starting around n=10)
+ - d_storepop is now a Rcpp::List, filled directly with matrices
+ - the strategy if/elseif/else tree is now a switch()
+ - use static_cast<int>(::unif_rand())
+ - use ::unif_rand() to show this is a 'global' R namespace object
+ * R/DEoptim.R: No longer need to create list of populations by hand
+
+ * src/evaluate.cpp: Simplied using REAL() to get to pointer
+ * demo/SmallBenchmark.R: Added
+ * demo/LargeBenchmark.R: Added
+
2010-10-16 Dirk Eddelbuettel <edd at debian.org>
- * src/de4_0.cpp: converted to Armadillo use throughout
+ * src/de4_0.cpp:
+ - converted to Armadillo use throughout
+ - initialpop is a matrix (not vector) on entry
+ - d_pop is a matrix too
+ - removed a number of 'element copying' loop
* R/DEoptim.R: changed to send and receive matrices rather than
vectors that need to be recast to matrix
Modified: pkg/RcppDE/src/de4_0.cpp
===================================================================
--- pkg/RcppDE/src/de4_0.cpp 2010-10-19 13:57:34 UTC (rev 2341)
+++ pkg/RcppDE/src/de4_0.cpp 2010-10-19 14:48:54 UTC (rev 2342)
@@ -7,7 +7,7 @@
// and based on DE-Engine v4.0, Rainer Storn, 2004
// (http://www.icsi.berkeley.edu/~storn/DeWin.zip)
-#include <RcppArmadillo.h>
+#include <RcppArmadillo.h> // declarations for both Rcpp and RcppArmadillo offering Armadillo classes
//#include <google/profiler.h>
RcppExport SEXP DEoptimC(SEXP lower, SEXP upper, SEXP fn, SEXP control, SEXP rho);
@@ -18,7 +18,7 @@
int i_specinitialpop, int i_check_winner, int i_av_winner,
arma::mat & ta_popP, arma::mat & ta_oldP, arma::mat & ta_newP, arma::colvec & t_bestP,
arma::colvec & ta_popC, arma::colvec & ta_oldC, arma::colvec & ta_newC, double & t_bestC,
- arma::colvec & t_bestitP, arma::colvec & t_tmpP, arma::colvec & tempP,
+ arma::colvec & t_bestitP, arma::colvec & t_tmpP, //arma::colvec & tempP,
arma::mat & d_pop, Rcpp::List & d_storepop, arma::mat & d_bestmemit, arma::colvec & 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[]);
@@ -28,15 +28,15 @@
//ProfilerStart("/tmp/RcppDE.prof");
BEGIN_RCPP ; // macro to fill in try part of try/catch exception handler
- Rcpp::Function fn(fnS); // function to mininise
- Rcpp::Environment rho(rhoS); // environment to do it in
+ Rcpp::Function fn(fnS); // function to mininise
+ Rcpp::Environment rho(rhoS); // environment to do it in
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;//Rcpp::as<int>(control["nfeval"]); // number of function evaluations
+ long l_nfeval = 0;//Rcpp::as<int>(control["nfeval"]); // number of function evaluations (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
@@ -67,7 +67,6 @@
arma::colvec t_bestitP(i_D);
arma::colvec t_tmpP(i_D);
- arma::colvec tempP(i_D);
int i_nstorepop = ceil((i_itermax - i_storepopfrom) / i_storepopfreq);
arma::mat d_pop(i_D, i_NP);
@@ -79,7 +78,7 @@
// call actual Differential Evolution optimization given the parameters
devol(VTR, f_weight, f_cross, i_bs_flag, minbound, maxbound, Rcpp::wrap(fn), Rcpp::wrap(rho), 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, tempP,
+ 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);
// and return a named list to R
@@ -101,7 +100,7 @@
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::colvec & t_bestP,
arma::colvec & ta_popC, arma::colvec & ta_oldC, arma::colvec & ta_newC, double & t_bestC,
- arma::colvec & t_bestitP, arma::colvec & t_tmpP, arma::colvec & tempP,
+ arma::colvec & t_bestitP, arma::colvec & t_tmpP,
arma::mat &d_pop, Rcpp::List &d_storepop, arma::mat & d_bestmemit, arma::colvec & d_bestvalit,
int & i_iterations, double i_pPct, long & l_nfeval) {
@@ -114,18 +113,15 @@
int i_nstorepop = ceil((i_itermax - i_storepopfrom) / i_storepopfreq);
int i_xav, popcnt, bestacnt, same; // lazy cnters
- double f_jitter, f_dither, t_bestitC, t_tmpC, tmp_best; // , tempC
+ double f_jitter, f_dither, t_bestitC, t_tmpC, tmp_best;
arma::mat initialpop(i_D, i_NP);
- 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
- for(i = 0; i < i_NP; i++) sortIndex[i] = i;
+ for(i = 0; i < i_NP; i++) sortIndex[i] = i; // FIXME: does this really need to get filled here?
- int i_len, done, step, bound; // vars for when i_bs_flag == 1 */
-
GetRNGstate();
initialpop.zeros(); // initialize initial popuplation
@@ -238,7 +234,7 @@
break;
case 6: // ---DE/current-to-p-best/1 (JADE)--------------------------------------------
- i_pbest = sortIndex[static_cast<int>(::unif_rand() * p_NP)]; // select from [0, 1, 2, ..., (pNP-1)]
+ int i_pbest = sortIndex[static_cast<int>(::unif_rand() * p_NP)]; // select from [0, 1, 2, ..., (pNP-1)]
j = static_cast<int>(::unif_rand() * i_D); // random parameter
do { // add fluctuation to random target
t_tmpP[j] = ta_oldP.at(j,i) + f_weight * (ta_oldP.at(j,i_pbest) - ta_oldP.at(j,i)) + f_weight * (ta_oldP.at(j,i_r1) - ta_oldP.at(j,i_r2));
@@ -296,22 +292,21 @@
ta_popP.rows(i_NP, 2*i_NP-1) = ta_newP;
ta_popC.rows(i_NP, 2*i_NP-1) = ta_newC;
- i_len = 2 * i_NP;
- step = i_len; // array length
+ int i_len = 2 * i_NP;
+ int step = i_len; // array length
while (step > 1) {
step /= 2; // halve the step size
- do {
- done = 1;
- bound = i_len - step;
+ for (bool done=false; ! done; ) {
+ int bound = i_len - step;
for (j = 0; j < bound; j++) {
i = j + step + 1;
if (ta_popC[j] > ta_popC[i-1]) {
ta_popP.swap_cols(i-1, j);
ta_popC.swap_rows(i-1, j);
- done = 0;
+ done = true;
} // if
} // for
- } while (!done); // do .. while
+ } // for (!done)
} // while (step > 1)
ta_newP = ta_popP; // now the best NP are in first NP places in gta_pop, use them
ta_newC = ta_popC;
More information about the Rcpp-commits
mailing list