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