Disclaimer: I've searched for an answer using the keywords: R, optimize, C++, C, optima, maxima, minima, local maximum, optimization, Newton's Method, Gradient descent, etc. and haven't found any satisfactory answers. R's optimize man page gives the original Fortran code but not the C translation of it. Please let me know if I should have searched for other keywords or if you can quickly find a website that clearly answers this question.
Question: I'm new to C++ and want to convert one of my R programs into C++. I use the optimize function in R and want to know if there are any libraries/header files/functions in C++ that will easily give me the same results. Please give an example if possible.
Here is a simple example of R's optimize, maximizing f(p) = p*(1-p) over (0,1) where the maximum is at p = 0.5 and f(0.5) = 0.25:
> optimize(function(p) p*(1-p),c(0,1),maximum=T)
$maximum
[1] 0.5
$objective
[1] 0.25
Thank you for your help!
The R Optimization Infrastructure (ROI) package provides a framework for handling optimization problems in R. It uses an object-oriented approach to define and solve various optimization tasks from different problem classes (e.g., linear, quadratic, non-linear programming problems).
WHAT IS OPTIMIZATION? Optimization problem: Maximizing or minimizing some function relative to some set, often representing a range of choices available in a certain situation. The function allows comparison of the different choices for determining which might be “best.”
http://cran.r-project.org/src/base/R-2/R-2.13.1.tar.gz
The code is in ../R-2.13.1/src/main/optimize.c
The R source code is available at http://cran.r-project.org/. You should be able to get the c implementation there, making it c++ should be trivial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With