<html><body><div style="font-family: times new roman, new york, times, serif; font-size: 12pt; color: #000000"><div>Dear Rcpp and R developpers,</div><div><br></div><div>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.</div><div>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.</div><div>So I would like to use the cmaes library libcmaes available on <a href="https://github.com/beniz/libcmaes">https://github.com/beniz/libcmaes</a> to perform the optimization within the C++  code.</div><div>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)</div><div>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.</div><div>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)</div><div><br></div><div>I have tried to add the correct compilation option in the Makevars (even far to be portable for now)</div><div><p style="margin: 0px;" data-mce-style="margin: 0px;">CXX_STD = CXX11<br>PKG_CPPFLAGS=-I/usr/local/include/libcmaes -I../inst/include -I/usr/local/include/eigen3/<br>PKG_LIBS = -L/usr/local/lib -l cmaes`$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`</p></div><div><br></div><div><br></div><div>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</div><div><br></div><div>Cheers</div><div><br></div><div>Marie</div><div><br></div><div>Here is the log of the build (made using RStudio)</div><div><br></div><div><p style="margin: 0px;" data-mce-style="margin: 0px;">==> R CMD INSTALL --preclean --no-multiarch --with-keep.source testCMaes</p><p style="margin: 0px;" data-mce-style="margin: 0px;">* installing to library ‘/home/metienne/R/x86_64-pc-linux-gnu-library/3.1’<br>* installing *source* package ‘testCMaes’ ...<br>** libs<br>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<br>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<br>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<br>installing to /home/metienne/R/x86_64-pc-linux-gnu-library/3.1/testCMaes/libs<br>** R<br>** preparing package for lazy loading<br>** help<br>Warning: /home/metienne/EnCours/testCMaes/man/testCMaes-package.Rd:30: All text must be in a section<br>Warning: /home/metienne/EnCours/testCMaes/man/testCMaes-package.Rd:31: All text must be in a section<br>*** installing help indices<br>** building package indices<br>** testing if installed package can be loaded<br>Error in dyn.load(file, DLLpath = DLLpath, ...) : <br> unable to load shared object '/home/metienne/R/x86_64-pc-linux-gnu-library/3.1/testCMaes/libs/testCMaes.so':<br> libcmaes.so.0: cannot open shared object file: No such file or directory<br>Error: loading failed<br>Execution halted<br>ERROR: loading failed<br>* removing ‘/home/metienne/R/x86_64-pc-linux-gnu-library/3.1/testCMaes’</p><p style="margin: 0px;" data-mce-style="margin: 0px;"><br></p><p style="margin: 0px;" data-mce-style="margin: 0px;"><br></p><p style="margin: 0px;" data-mce-style="margin: 0px;">Here is the test.cpp file</p></div><div><br></div><div><p style="margin: 0px;" data-mce-style="margin: 0px;">#include <cmaes.h><br>#include <cmastrategy.h><br>#include <llogging.h><br>#include <Rcpp.h></p><p style="margin: 0px;" data-mce-style="margin: 0px;">using namespace Rcpp;<br>using namespace libcmaes;<br></p><p style="margin: 0px;" data-mce-style="margin: 0px;"><br></p><p style="margin: 0px;" data-mce-style="margin: 0px;">libcmaes::FitFunc cigtab = [](const double *x, const int N)<br>{<br> int i;<br> double sum = 1e4*x[0]*x[0] + 1e-4*x[1]*x[1];<br> for(i = 2; i < N; ++i)<br> sum += x[i]*x[i];<br> return sum;<br>};</p><p style="margin: 0px;" data-mce-style="margin: 0px;">// [[Rcpp::export]]<br>int test(const NumericVector x0, const NumericVector sigma)<br>{</p><p style="margin: 0px;" data-mce-style="margin: 0px;">int dim = x0.size();<br> std::vector<double> x0_stl= Rcpp::as<std::vector<double> >(x0);<br> std::vector<double> sigma_stl = Rcpp::as<std::vector<double> >(sigma);<br> <br> int lambda = x0.size();<br> CMAParameters<> cmaparams(x0_stl,sigma_stl,lambda);<br> ESOptimizer<CMAStrategy<CovarianceUpdate>,CMAParameters<>> cmaes(cigtab,cmaparams);<br> cmaes.optimize();<br> double edm = cmaes.edm();<br> std::cerr << "EDM=" << edm << " / EDM/fm=" << edm / cmaes.get_solutions().best_candidate().get_fvalue() << std::endl;<br>};</p> </div><div> </div><div><br></div><div><span name="x"></span>Marie-Pierre Etienne<br>AgroParistech /INRA UMR 518 MIA<br>marie.etienne@agroparistech.fr<br>http://mpetienne.org<br>Tel : 01 44 08 86 82<br><span name="x"></span><br></div></div></body></html>