ROOT   Reference Guide
GlobalMinimization.C File Reference

## Detailed Description

Example based in http://cran.r-project.org/web/packages/DEoptim/DEoptim.pdf Please install the R package DEoptim before run this example.

#include<TRInterface.h>
#include<TBenchmark.h>
#include<math.h>
#include<stdlib.h>
// In the next function the *double pointer should be changed by a TVectorD datatype,
// because the pointer has no meaning in R's enviroment.
// This is a generalization of the RosenBrock function, with the min xi=1 and i>0.
Double_t GenRosenBrock(const TVectorD xx )
{
for (int i=0;i<(length-1);i++) {
result+=pow(1-xx[i],2)+100*pow(xx[i+1]-pow(xx[i],2),2);
}
return result;
}
//the min xi=0 i>0
Double_t Rastrigin(const TVectorD xx)
{
for (int i=0;i<length;i++) {
result+=xx[i]*xx[i]-10*cos(6.2831853*xx[i]);
}
return result;
}
void GlobalMinimization()
{
TBenchmark bench;
Bool_t installed=r.Eval("is.element('DEoptim', installed.packages()[,1])");
if (!installed) {
std::cout<<"Package DEoptim no installed in R"<<std::endl;
std::cout<<"Run install.packages('DEoptim') in R's environment"<<std::endl;
return;
}
r<<"suppressMessages(library(DEoptim, quietly = TRUE))";
// passing RosenBrock function to R
r["GenRosenBrock"]<<ROOT::R::TRFunctionExport(GenRosenBrock);
// maximun number of iterations
r["MaxIter"]<<5000;
// n = size of vector that is an argument for GenRosenBrock
r["n"]<<3;
// lower limits
r<<"ll<-rep(-25, n)";
//upper limits
r<<"ul<-rep(25, n)";
bench.Start("GlobalMinimizationRosenBrock");
// calling minimization and timing it.
r<<"result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))";
std::cout<<"-----------------------------------------"<<std::endl;
std::cout<<"RosenBrock's minimum in: "<<std::endl;
r<<"print(result1$optim$bestmem)";
std::cout<<"Bechmark Times"<<std::endl;
// printing times
bench.Show("GlobalMinimizationRosenBrock");
// passing RosenBrock function to R
r["Rastrigin"]<<ROOT::R::TRFunctionExport(Rastrigin);
// maximun number of iterations
r["MaxIter"]<<2000;
// n = size of a vector which is an argument for Rastrigin
r["n"]<<3;
// lower limits
r<<"ll<-rep(-5, n)";
// upper limits
r<<"ul<-rep(5, n)";
bench.Start("GlobalMinimizationRastrigin");
// calling minimization and timing it.
r<<"result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))";
std::cout<<"-----------------------------------------"<<std::endl;
std::cout<<"Rastrigin's minimum in: "<<std::endl;
r<<"print(result2$optim$bestmem)";
std::cout<<"Bechmark Times"<<std::endl;
// printing times
bench.Show("GlobalMinimizationRastrigin");
// skip R plotting in batch mode
if (!gROOT->IsBatch()) {
r<<"dev.new(title='RosenBrock Convergence')";
r<<"plot(result1,type='o',pch='.')";
r<<"dev.off()";
r<<"dev.new(title='Rastrigin Convergence')";
r<<"plot(result2,type='o',pch='.')";
r<<"dev.off()";
}
}
bool Bool_t
Definition: RtypesCore.h:63
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 WindowAttributes_t Float_t r
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 Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
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 Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
#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
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
RVec< PromoteType< T > > cos(const RVec< T > &v)
Definition: RVec.hxx:1776
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
Definition: RVec.hxx:1770

Definition in file GlobalMinimization.C.