ROOT   Reference Guide
GlobalMinimization.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_r
3/// \notebook -nodraw
4/// Example based in
5/// http://cran.r-project.org/web/packages/DEoptim/DEoptim.pdf
6/// Please install the R package DEoptim before run this example.
7///
8/// \macro_code
9///
10/// \author Omar Zapata
11
12#include<TRInterface.h>
13#include<TBenchmark.h>
14#include<math.h>
15#include<stdlib.h>
16
17// In the next function the *double pointer should be changed by a TVectorD datatype,
18// because the pointer has no meaning in R's enviroment.
19// This is a generalization of the RosenBrock function, with the min xi=1 and i>0.
20Double_t GenRosenBrock(const TVectorD xx )
21{
22 int length=xx.GetNoElements();
23
24 Double_t result=0;
25 for (int i=0;i<(length-1);i++) {
26 result+=pow(1-xx[i],2)+100*pow(xx[i+1]-pow(xx[i],2),2);
27 }
28 return result;
29}
30
31//the min xi=0 i>0
32Double_t Rastrigin(const TVectorD xx)
33{
34 int length=xx.GetNoElements();
35 Double_t result=10*length;
36 for (int i=0;i<length;i++) {
37 result+=xx[i]*xx[i]-10*cos(6.2831853*xx[i]);
38 }
39 return result;
40}
41
42void GlobalMinimization()
43{
44 TBenchmark bench;
46
47 Bool_t installed=r.Eval("is.element('DEoptim', installed.packages()[,1])");
48 if (!installed) {
49 std::cout<<"Package DEoptim no installed in R"<<std::endl;
50 std::cout<<"Run install.packages('DEoptim') in R's environment"<<std::endl;
51 return;
52 }
53
55 r<<"suppressMessages(library(DEoptim, quietly = TRUE))";
56
57 // passing RosenBrock function to R
58 r["GenRosenBrock"]<<ROOT::R::TRFunctionExport(GenRosenBrock);
59
60 // maximun number of iterations
61 r["MaxIter"]<<5000;
62 // n = size of vector that is an argument for GenRosenBrock
63 r["n"]<<3;
64
65 // lower limits
66 r<<"ll<-rep(-25, n)";
67 //upper limits
68 r<<"ul<-rep(25, n)";
69
70 bench.Start("GlobalMinimizationRosenBrock");
71 // calling minimization and timing it.
72 r<<"result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))";
73 std::cout<<"-----------------------------------------"<<std::endl;
74 std::cout<<"RosenBrock's minimum in: "<<std::endl;
75 r<<"print(result1$optim$bestmem)";
76 std::cout<<"Bechmark Times"<<std::endl;
77 // printing times
78 bench.Show("GlobalMinimizationRosenBrock");
79
80 // passing RosenBrock function to R
81 r["Rastrigin"]<<ROOT::R::TRFunctionExport(Rastrigin);
82 // maximun number of iterations
83 r["MaxIter"]<<2000;
84 // n = size of a vector which is an argument for Rastrigin
85 r["n"]<<3;
86 // lower limits
87 r<<"ll<-rep(-5, n)";
88 // upper limits
89 r<<"ul<-rep(5, n)";
90
91 bench.Start("GlobalMinimizationRastrigin");
92 // calling minimization and timing it.
93 r<<"result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))";
94 std::cout<<"-----------------------------------------"<<std::endl;
95 std::cout<<"Rastrigin's minimum in: "<<std::endl;
96 r<<"print(result2$optim$bestmem)";
97 std::cout<<"Bechmark Times"<<std::endl;
98 // printing times
99 bench.Show("GlobalMinimizationRastrigin");
100 // skip R plotting in batch mode
101 if (!gROOT->IsBatch()) {
102 r<<"dev.new(title='RosenBrock Convergence')";
103 r<<"plot(result1,type='o',pch='.')";
104 r<<"dev.off()";
105 r<<"dev.new(title='Rastrigin Convergence')";
106 r<<"plot(result2,type='o',pch='.')";
107 r<<"dev.off()";
108 }
109}
ROOT::R::TRInterface & r
Definition: Object.C:4
bool Bool_t
Definition: RtypesCore.h:63
double Double_t
Definition: RtypesCore.h:59
double cos(double)
double pow(double, double)
#define gROOT
Definition: TROOT.h:404
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
Definition: TRInterface.h:136
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:80
This class is a ROOT utility to help benchmarking applications.
Definition: TBenchmark.h:29
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
Int_t GetNoElements() const
Definition: TVectorT.h:76