Dear Rcpp and R developpers, 

I have recently used Rcpp to build a package and found it very well documented 
and easy as I don't need to go very deep in Writing R extensions mechanisms. 
My code uses an optimisation algorithm named cmaes, and this use was first made 
through a cmaes R package. As my function to be optimized depends on some Monte 
Carlo simulations performed using functions written in C++ this procedure 
requires a lot of exchange between R and C++ ( basically every step of cmaes 
calls a c++ function), which is of course not very efficient. 
So I would like to use the cmaes library libcmaes available on 
https://github.com/beniz/libcmaes to perform the optimization within the C++ 
code. 
I have tried the examples provided in the github repository and I am able to 
build and run them, so I assume that the cmaes library is correctly installed 
(in /usr/local) 
Then I have decided to embed the very simple example in R to check if I can use 
CMAES from R. I have built a toy package for that. 
This packahe only has a test.cpp file in directory src/ which is exactly the 
sample-code.cpp example but using NumericVector instead of std::vector<>. 
(presented below) 

I have tried to add the correct compilation option in the Makevars (even far to 
be portable for now) 


CXX_STD = CXX11 
PKG_CPPFLAGS=-I/usr/local/include/libcmaes -I../inst/include 
-I/usr/local/include/eigen3/ 
PKG_LIBS = -L/usr/local/lib -l cmaes`$(R_HOME)/bin/Rscript -e 
"Rcpp:::LdFlags()"` 


Everything seems fine except that the shared library cmaes.so.0 is not in the 
expected directory when loading the R package. Any help would be very much 
appreciated 

Cheers 

Marie 

Here is the log of the build (made using RStudio) 



==> R CMD INSTALL --preclean --no-multiarch --with-keep.source testCMaes 

* installing to library ‘/home/metienne/R/x86_64-pc-linux-gnu-library/3.1’ 
* installing *source* package ‘testCMaes’ ... 
** libs 
g++ -std=c++0x -I/usr/share/R/include -DNDEBUG -I/usr/local/include/libcmaes 
-I../inst/include -I/usr/local/include/eigen3/ 
-I"/home/metienne/R/x86_64-pc-linux-gnu-library/3.1/Rcpp/include" -fpic -g -O2 
-fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security 
-Werror=format-security -D_FORTIFY_SOURCE=2 -g -c RcppExports.cpp -o 
RcppExports.o 
g++ -std=c++0x -I/usr/share/R/include -DNDEBUG -I/usr/local/include/libcmaes 
-I../inst/include -I/usr/local/include/eigen3/ 
-I"/home/metienne/R/x86_64-pc-linux-gnu-library/3.1/Rcpp/include" -fpic -g -O2 
-fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security 
-Werror=format-security -D_FORTIFY_SOURCE=2 -g -c test.cpp -o test.o 
g++ -std=c++0x -shared -Wl,-Bsymbolic-functions -Wl,-z,relro -o testCMaes.so 
RcppExports.o test.o -L/usr/local/lib -l cmaes -L/usr/lib/R/lib -lR 
installing to /home/metienne/R/x86_64-pc-linux-gnu-library/3.1/testCMaes/libs 
** R 
** preparing package for lazy loading 
** help 
Warning: /home/metienne/EnCours/testCMaes/man/testCMaes-package.Rd:30: All text 
must be in a section 
Warning: /home/metienne/EnCours/testCMaes/man/testCMaes-package.Rd:31: All text 
must be in a section 
*** installing help indices 
** building package indices 
** testing if installed package can be loaded 
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
unable to load shared object 
'/home/metienne/R/x86_64-pc-linux-gnu-library/3.1/testCMaes/libs/testCMaes.so': 
libcmaes.so.0: cannot open shared object file: No such file or directory 
Error: loading failed 
Execution halted 
ERROR: loading failed 
* removing ‘/home/metienne/R/x86_64-pc-linux-gnu-library/3.1/testCMaes’ 







Here is the test.cpp file 



#include <cmaes.h> 
#include <cmastrategy.h> 
#include <llogging.h> 
#include <Rcpp.h> 

using namespace Rcpp; 
using namespace libcmaes; 





libcmaes::FitFunc cigtab = [](const double *x, const int N) 
{ 
int i; 
double sum = 1e4*x[0]*x[0] + 1e-4*x[1]*x[1]; 
for(i = 2; i < N; ++i) 
sum += x[i]*x[i]; 
return sum; 
}; 

// [[Rcpp::export]] 
int test(const NumericVector x0, const NumericVector sigma) 
{ 

int dim = x0.size(); 
std::vector<double> x0_stl= Rcpp::as<std::vector<double> >(x0); 
std::vector<double> sigma_stl = Rcpp::as<std::vector<double> >(sigma); 

int lambda = x0.size(); 
CMAParameters<> cmaparams(x0_stl,sigma_stl,lambda); 
ESOptimizer<CMAStrategy<CovarianceUpdate>,CMAParameters<>> 
cmaes(cigtab,cmaparams); 
cmaes.optimize(); 
double edm = cmaes.edm(); 
std::cerr << "EDM=" << edm << " / EDM/fm=" << edm / 
cmaes.get_solutions().best_candidate().get_fvalue() << std::endl; 
}; 

Marie-Pierre Etienne 
AgroParistech /INRA UMR 518 MIA 
marie.etie...@agroparistech.fr 
http://mpetienne.org 
Tel : 01 44 08 86 82 

_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to