ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Minimization.C
Go to the documentation of this file.
1 //Example based in
2 //http://root.cern.ch/root/html/tutorials/fit/NumericalMinimization.C.html
3 //http://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html
4 //Author: Omar Zapata
5 
6 #include<TRInterface.h>
7 
8 //in the next function the *double pointer must be changed by a TVectorD,
9 //because the pointer has no meaning in R enviroment.
11 {
12  const Double_t x = xx[0];
13  const Double_t y = xx[1];
14  const Double_t tmp1 = y-x*x;
15  const Double_t tmp2 = 1-x;
16  return 100*tmp1*tmp1+tmp2*tmp2;
17 }
18 
20 {
21  const Double_t x = xx[0];
22  const Double_t y = xx[1];
23  TVectorD grad(2);
24  grad[0]=-400 * x * (y - x * x) - 2 * (1 - x);
25  grad[1]=200 * (y - x * x);
26  return grad;
27 }
28 
29 
31 {
33 
34  //passsing RosenBrock function to R
35  r["RosenBrock"]=ROOT::R::TRFunctionExport(RosenBrock);
36 
37  //passsing RosenBrockGrad function to R
38  r["RosenBrockGrad"]=ROOT::R::TRFunctionExport(RosenBrockGrad);
39  //the option "method" could be "Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN","Brent"
40 
41  //the option "control" lets you put some constraints like
42  //"maxit" The maximum number of iterations.
43  //"abstol" The absolute convergence tolerance.
44  r.Execute("result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )");
45  //"reltol" Relative convergence tolerance.
46 
47  //Getting results from R
48  TVectorD min=r.Eval("result$par");
49 
50  std::cout.precision(8);
51  //printing results
52  std::cout<<"-----------------------------------------"<<std::endl;
53  std::cout<<"Minimum x="<<min[0]<<" y="<<min[1]<<std::endl;
54  std::cout<<"Value at minimum ="<<RosenBrock(min)<<std::endl;
55 
56  //using the gradient
57  r.Execute("optimHess(result$par, RosenBrock, RosenBrockGrad)");
58  r.Execute("hresult <- optim(c(-1.2,1), RosenBrock, NULL, method = 'BFGS', hessian = TRUE)");
59  //getting the min calculated with the gradient
60  TVectorD hmin=r.Eval("hresult$par");
61 
62  //printing results
63  std::cout<<"-----------------------------------------"<<std::endl;
64  std::cout<<"Minimization with the Gradient"<<std::endl;
65  std::cout<<"Minimum x="<<hmin[0]<<" y="<<hmin[1]<<std::endl;
66  std::cout<<"Value at minimum ="<<RosenBrock(hmin)<<std::endl;
67 
68 }
void Execute(const TString &code)
Method to eval R code.
Definition: TRInterface.cxx:77
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
void grad()
Definition: grad.C:17
void Minimization()
Definition: Minimization.C:30
Double_t x[n]
Definition: legend1.C:17
ROOT::R::TRInterface & r
Definition: Object.C:4
Double_t RosenBrock(const TVectorD xx)
Definition: Minimization.C:10
TVectorD RosenBrockGrad(const TVectorD xx)
Definition: Minimization.C:19
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
static TRInterface & Instance()
static method to get an TRInterface instance reference
Int_t Eval(const TString &code, TRObject &ans)
Method to eval R code and you get the result in a reference to TRObject.
Definition: TRInterface.cxx:58
This is a class to pass functions from ROOT to R