[Rcpp-devel] Using lbfgsb with a Rcpp::List.
Jason Lessels
jlessels at gmail.com
Mon Nov 21 00:29:22 CET 2011
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.
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))
More information about the Rcpp-devel
mailing list