[Rcpp-devel] Using lbfgsb with a Rcpp::List.
Dirk Eddelbuettel
edd at debian.org
Mon Nov 21 00:56:02 CET 2011
On 21 November 2011 at 00:29, Jason Lessels wrote:
| Hi List,
| I was hoping that someone could possibly help me out with a problem I have. I am trying to use a Rcpp::List with the lbfgsb function provided by the R API. I have successfully replicated the example from help(optim), but was wondering if anyone could help me passing an Rcpp::List to the minimisation function. With my extremely limited c++ knowledge I don't know how to get around the void type of the function input, as the lbfgsb function requires the minimisation function to have the third parameter of type void. I have commented out the problem line in the fr function within the src.
The R API has only a C interface.
Rcpp can help you to get values from R to your (C/C++ mixed) source code more
easily. But at some point you have to unwrap the C++ data structures to their
C equivalents as these are the only ones known to R and its plain-C API.
Does that make sense?
My RcppDE (draft) package is a rewrite of (an older version of) DEoptim using
the Rcpp / RcppArmadillo paradigm. That may be of help, but I did some more
advanced things there too as e.g. passing a C++ function down to be optimised
by DEoptim.
Dirk
| Thanks
| Jason
|
| library(inline)
| library(RcppArmadillo)
| inc<-'
| #include <R_ext/Applic.h>
|
| double fr(int n, double *par, void *ex) {
| double x1 = par[0];
| double x2 = par[1];
| // double blah = Rcpp::as<double>(ex["numbers"]);
| return ( 100 * pow( ( x2 - x1 * x1 ) ,2.0 ) + pow ( ( 1 - x1 ) ,2.0 ) );
| }
| void grr(int n, double *par, double *gr, void *ex) {
| double x1 = par[0];
| double x2 = par[1];
| gr[0] = ( -400.0 * x1 * ( x2 - x1 * x1 ) - 2.0 * ( 1 - x1 ) );
| gr[1] = ( 200.0 * ( x2 - x1 * x1 ) );
|
| }
|
| '
|
| src<-'
| Rcpp::List TheList(listIn);
| double initial[2] = {0.8,1.0};
| double final;
| int convergenceCode;
| int trace = 6;
| int fncount;
| int grcount;
| double lower[2] = {-1e+7,-1e+7};
| double upper[2] = {1e+7,1e+7};
| double pgtol = 0;
| double factr = 1e-8;
| int nbd[2] = {2,2};
| int lmm = 5;
| int maxit = 100;
| int nREPORT = 10;
| char msg[100];
|
| lbfgsb(2,lmm,initial,lower,upper,nbd,&final,fr,grr,&convergenceCode,TheList,factr,pgtol,&fncount,&grcount,maxit,msg,trace,nREPORT);
|
| /* std::cout << msg << std::endl;
| std::cout << final << std::endl;
| std::cout << convergenceCode << std::endl;
| std::cout << initial[0];
| std::cout << initial[1] << std::endl;
| */
|
| Rcpp::NumericVector tes(initial,initial+2);
|
| return tes;
| '
|
| testing<-cxxfunction(signature(listIn="list"),src,includes=inc,plugin="RcppArmadillo")
| testing(listIn=list(blah=100))
|
|
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx
More information about the Rcpp-devel
mailing list