Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Minimization.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_r
3/// \notebook -nodraw
4/// Example based on
5/// http://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html
6///
7/// \macro_code
8///
9/// \author Omar Zapata
10
11#include<TRInterface.h>
12
13//in the next function the *double pointer must be changed by a TVectorD,
14//because the pointer has no meaning in R enviroment.
15Double_t RosenBrock(const TVectorD xx )
16{
17 const Double_t x = xx[0];
18 const Double_t y = xx[1];
19 const Double_t tmp1 = y-x*x;
20 const Double_t tmp2 = 1-x;
21 return 100*tmp1*tmp1+tmp2*tmp2;
22}
23
24TVectorD RosenBrockGrad(const TVectorD xx )
25{
26 const Double_t x = xx[0];
27 const Double_t y = xx[1];
28 TVectorD grad(2);
29 grad[0]=-400 * x * (y - x * x) - 2 * (1 - x);
30 grad[1]=200 * (y - x * x);
31 return grad;
32}
33
34void Minimization()
35{
37
38 //passsing RosenBrock function to R
39 r["RosenBrock"]=ROOT::R::TRFunctionExport(RosenBrock);
40
41 //passsing RosenBrockGrad function to R
42 r["RosenBrockGrad"]=ROOT::R::TRFunctionExport(RosenBrockGrad);
43 //the option "method" could be "Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN","Brent"
44
45 //the option "control" lets you put some constraints like
46 //"maxit" The maximum number of iterations.
47 //"abstol" The absolute convergence tolerance.
48 r.Execute("result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )");
49 //"reltol" Relative convergence tolerance.
50
51 //Getting results from R
52 TVectorD min=r.Eval("result$par");
53
54 std::cout.precision(8);
55 //printing results
56 std::cout<<"-----------------------------------------"<<std::endl;
57 std::cout<<"Minimum x="<<min[0]<<" y="<<min[1]<<std::endl;
58 std::cout<<"Value at minimum ="<<RosenBrock(min)<<std::endl;
59
60 //using the gradient
61 r.Execute("optimHess(result$par, RosenBrock, RosenBrockGrad)");
62 r.Execute("hresult <- optim(c(-1.2,1), RosenBrock, NULL, method = 'BFGS', hessian = TRUE)");
63 //getting the min calculated with the gradient
64 TVectorD hmin=r.Eval("hresult$par");
65
66 //printing results
67 std::cout<<"-----------------------------------------"<<std::endl;
68 std::cout<<"Minimization with the Gradient"<<std::endl;
69 std::cout<<"Minimum x="<<hmin[0]<<" y="<<hmin[1]<<std::endl;
70 std::cout<<"Value at minimum ="<<RosenBrock(hmin)<<std::endl;
71}
double Double_t
Definition RtypesCore.h:59
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t hmin
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
This is a class to pass functions from ROOT to R.
ROOT R was implemented using the R Project library and the modules Rcpp and RInside
static TRInterface & Instance()
static method to get an TRInterface instance reference
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17