Skip to content

[Rcpp-devel] Numerical Optimization in C++

14 messages · Yan Zhou, c s, Dirk Eddelbuettel +1 more

#
Hi fellows,

I am using the RcppArmadillo package and I need in C++ some optimization methods. Before I used always the Scythe Statistical Library which had both: linear algebra and optimization methods. Armadillo has only linear algebra support. 

Now my question: What numerical optimization libraries in C++ do you use, when working with RcppArmadillo? It should be fast! 


Best

Simon
#
I good numerical optimization library I used before is NLOpt, for non-linear optimization. http://ab-initio.mit.edu/wiki/index.php/NLopt

You can use it just like and C/C++ library in your own code or you can use its own R interface as an R package.

Best,

Yan
On Feb 3, 2013, at 11:47 AM, Simon Zehnder <szehnder at uni-bonn.de> wrote:

            
#
Thanks Yan,

this looks promising! 

Best

Simon
On Feb 3, 2013, at 12:50 PM, Yan Zhou <zhouyan at me.com> wrote:

            
c s
#
There is also MLPACK, which is a general machine learning package that
uses Armadillo:
http://mlpack.org/

(there is currently a small issue with using Armadillo 3.6.2, due a
bug in MLPACK, but earlier versions of Armadillo, such as 3.6.1, work
fine).
On Sun, Feb 3, 2013 at 9:50 PM, Yan Zhou <zhouyan at me.com> wrote:
#
Hi Conrad,
On 3 February 2013 at 12:47, Simon Zehnder wrote:
| I am using the RcppArmadillo package and I need in C++ some optimization methods. Before I used always the Scythe Statistical Library which had both: linear algebra and optimization methods. Armadillo has only linear algebra support. 
| 
| Now my question: What numerical optimization libraries in C++ do you use, when working with RcppArmadillo? It should be fast! 

Yan already pointed out that "any" is a good answer as Rcpp etc use the
standard C++ interfaces such as std::vector etc.  You can literally use
"whatever".  In some case you may need to package it, or provide linkage to
it.  That is what Rcpp is about, and the 'Rcpp-extending' vignette has
documentation on how one can write as<>() and wrap() converters for one's
desired external library if it uses specific data types.

Conrad already noted that there are also add-on libraries that use Armadillo
making RcppArmadillo an easy choice.

What has not been mentioned yet is that R itself does of course offer
optimization routines, and the Writing R Extension manual talks about how to
use them.  So you can also go back to the R API if you want to.

Dirk
#
Hi Dirk,

that is also a very good idea! I will look at this, as it would leave out any packaging of third-party libraries into a package. 

Best

Simon
On Feb 3, 2013, at 2:55 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
#
I also found this one here: http://www.alglib.net/optimization/lbfgsandcg.php

and on this thread a very good experience with it and Rcpp: http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2012-July/004038.html

Best 

Simon
On Feb 3, 2013, at 2:55 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
#
Hi Yan,

do you know if one can also write a C++ objective function in NLOpt, that works with Armadillo Objects? 

Best

Simon
On Feb 3, 2013, at 12:50 PM, Yan Zhou <zhouyan at me.com> wrote:

            
#
I believe the answer is no. But most Arma object provide memptr to get the raw pointer which you can pass through nlopt interface. Inside the objective function you can create Arma object using auxiliary pointer. AFAIK, for const double * argument you can only creat Arma object by copy while for double * you can creat object that use the pointer directly. After this step you can use armadillo inside and outside the objective function.

Best,

Yan
On Feb 3, 2013, at 4:35 PM, Simon Zehnder <szehnder at uni-bonn.de> wrote:

            
#
Perfect! That's adequate enough! 

Thanks!

Simon
On Feb 3, 2013, at 5:45 PM, Yan Zhou <zhouyan at me.com> wrote:

            
#
Mmmmmh, I just think about the function data: It is pointed to via void*. That should give me the possibility to directly point to a arma::mat, right? 
That would be the easiest way to do it.

Best

Simon
On Feb 3, 2013, at 5:45 PM, Yan Zhou <zhouyan at me.com> wrote:

            
#
Yes, of course.

I was thinking about the situation where NLOpt pass the parameter and (possible) gradient vector through pointers and you need to use Armadillo on these vectors. But if all you need is to pass a matrix as the function data, in principle you can pass almost anything through void *.

Best,

Yan Zhou
On Feb 03, 2013, at 05:13 PM, Simon Zehnder <szehnder at uni-bonn.de> wrote:
Mmmmmh, I just think about the function data: It is pointed to via void*. That should give me the possibility to directly point to a arma::mat, right? 
That would be the easiest way to do it.

Best

Simon
On Feb 3, 2013, at 5:45 PM, Yan Zhou <zhouyan at me.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130203/0eb45470/attachment.html>
#
Very good, static_cast should then do it!

Best 

Simon
On Feb 3, 2013, at 6:26 PM, Yan Zhou <zhouyan at me.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130203/2e1bcfa0/attachment-0001.html>
#
Hi Yan, 

maybe you have an answer for me: I call the following in my code:

119                 nlopt::opt opt_GMM = nlopt::opt(nlopt::NLOPT_LD_LBFGS, 3);
120                 opt_GMM.set_min_objective(objective_fun, &sample);
121                 nlopt::result opt_GMM.optimize(&opar, &funv);

I compile it via R CMD INSTALL .... and I get:

MCgmmS.cc:119: error: 'NLOPT_LD_LBFGS' is not a member of 'nlopt'
MCgmmS.cc:121: error: expected initializer before '.' token

My Makevars looks like this:

PKG_CPPFLAGS = -Inlopt-2.3/include -g -O1
PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) -Lnlopt-2.3/libs/ -lnlopt

I use this reference from NLopt: http://ab-initio.mit.edu/wiki/index.php/NLopt_C-plus-plus_Reference#Objective_function

In some way the compiler doesn't find the algorithm object.... did you implemented your version in C or C++?

Best

Simon
On Feb 3, 2013, at 6:26 PM, Yan Zhou <zhouyan at me.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130203/5db4d0da/attachment.html>