Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement R's "optimize" function in C++?

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!

like image 674
user918804 Avatar asked Aug 30 '11 01:08

user918804


People also ask

What is r in optimization?

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 does optimize a function mean?

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.”


2 Answers

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

like image 171
IRTFM Avatar answered Oct 31 '22 16:10

IRTFM


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.

like image 5
AShelly Avatar answered Oct 31 '22 18:10

AShelly