Global Minimization

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

Author: Omar Zapata
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Sunday, September 19, 2021 at 08:23 AM.

In [1]:
%%cpp -d
#include<TRInterface.h>
#include<TBenchmark.h>
#include<math.h>
#include<stdlib.h>
In file included from input_line_42:1:
In file included from /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/TRInterface.h:15:
In file included from /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/TRObject.h:14:
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:53:9: fatal error: 'RcppCommon.h' file not found
#include<RcppCommon.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.

In [2]:
%%cpp -d
Double_t GenRosenBrock(const TVectorD xx )
{
   int length=xx.GetNoElements();

   Double_t result=0;
   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

In [3]:
%%cpp -d
Double_t Rastrigin(const TVectorD xx)
{
   int length=xx.GetNoElements();
   Double_t result=10*length;
   for (int i=0;i<length;i++) {
      result+=xx[i]*xx[i]-10*cos(6.2831853*xx[i]);
   }
   return result;
}
In [4]:
TBenchmark bench;
ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();

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;
}
In file included from libRInterface dictionary payload:5:
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:53:9: fatal error: 'RcppCommon.h' file not found
#include<RcppCommon.h>
        ^~~~~~~~~~~~~~
Error in <TInterpreter::AutoParse>: Error parsing payload code for class ROOT::R::TRInterface with content:

#line 1 "libRInterface dictionary payload"


#define _BACKWARD_BACKWARD_WARNING_H
// Inline headers
#include "RExports.h"
#include "TRDataFrame.h"
#include "TRFunctionExport.h"
#include "TRFunctionImport.h"
#include "TRInterface.h"
#include "TRInternalFunction.h"
#include "TRObject.h"

#undef  _BACKWARD_BACKWARD_WARNING_H

input_line_45:3:34: error: incomplete type 'ROOT::R::TRInterface' named in nested name specifier
ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
                        ~~~~~~~~~^~~~~~~~~~~~~
libRInterface dictionary forward declarations' payload:9:93: note: forward declaration of 'ROOT::R::TRInterface'
namespace ROOT{namespace R{class __attribute__((annotate("$clingAutoload$TRInterface.h")))  TRInterface;}}
                                                                                            ^
input_line_45:5:19: error: member access into incomplete type 'ROOT::R::TRInterface'
Bool_t installed=r.Eval("is.element('DEoptim', installed.packages()[,1])");
                  ^
libRInterface dictionary forward declarations' payload:9:93: note: forward declaration of 'ROOT::R::TRInterface'
namespace ROOT{namespace R{class __attribute__((annotate("$clingAutoload$TRInterface.h")))  TRInterface;}}
                                                                                            ^

Loading deoptim

In [5]:
r<<"suppressMessages(library(DEoptim, quietly = TRUE))";
input_line_54:2:3: error: use of undeclared identifier 'r'
 (r << "suppressMessages(library(DEoptim, quietly = TRUE))")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "suppressMessages(library(DEoptim, quietly = TRUE))").
Execution of your code was aborted.

Passing rosenbrock function to r

In [6]:
r["GenRosenBrock"]<<ROOT::R::TRFunctionExport(GenRosenBrock);
In file included from libRInterface dictionary payload:5:
/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:53:9: fatal error: 'RcppCommon.h' file not found
#include<RcppCommon.h>
        ^~~~~~~~~~~~~~
Error in <TInterpreter::AutoParse>: Error parsing payload code for class ROOT::R::TRFunctionExport with content:

#line 1 "libRInterface dictionary payload"


#define _BACKWARD_BACKWARD_WARNING_H
// Inline headers
#include "RExports.h"
#include "TRDataFrame.h"
#include "TRFunctionExport.h"
#include "TRFunctionImport.h"
#include "TRInterface.h"
#include "TRInternalFunction.h"
#include "TRObject.h"

#undef  _BACKWARD_BACKWARD_WARNING_H

input_line_55:2:31: error: no member named 'TRFunctionExport' in namespace 'ROOT::R'
 r["GenRosenBrock"]<<ROOT::R::TRFunctionExport(GenRosenBrock);
                     ~~~~~~~~~^

Maximun number of iterations

In [7]:
r["MaxIter"]<<5000;
input_line_58:2:3: error: use of undeclared identifier 'r'
 (r["MaxIter"] << 5000)
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r["MaxIter"] << 5000).
Execution of your code was aborted.

N = size of vector that is an argument for genrosenbrock

In [8]:
r["n"]<<3;
input_line_60:2:3: error: use of undeclared identifier 'r'
 (r["n"] << 3)
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r["n"] << 3).
Execution of your code was aborted.

Lower limits

In [9]:
r<<"ll<-rep(-25, n)";
input_line_62:2:3: error: use of undeclared identifier 'r'
 (r << "ll<-rep(-25, n)")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "ll<-rep(-25, n)").
Execution of your code was aborted.

Upper limits

In [10]:
r<<"ul<-rep(25, n)";

bench.Start("GlobalMinimizationRosenBrock");
input_line_64:2:3: error: use of undeclared identifier 'r'
 (r << "ul<-rep(25, n)")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "ul<-rep(25, n)").
Execution of your code was aborted.

Calling minimization and timing it.

In [11]:
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;
input_line_66:2:3: error: use of undeclared identifier 'r'
 (r << "result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))").
Execution of your code was aborted.

Printing times

In [12]:
bench.Show("GlobalMinimizationRosenBrock");
input_line_68:2:3: error: use of undeclared identifier 'bench'
 (bench.Show("GlobalMinimizationRosenBrock"))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (bench.Show("GlobalMinimizationRosenBrock")).
Execution of your code was aborted.

Passing rosenbrock function to r

In [13]:
r["Rastrigin"]<<ROOT::R::TRFunctionExport(Rastrigin);
input_line_69:2:27: error: no member named 'TRFunctionExport' in namespace 'ROOT::R'
 r["Rastrigin"]<<ROOT::R::TRFunctionExport(Rastrigin);
                 ~~~~~~~~~^

Maximun number of iterations

In [14]:
r["MaxIter"]<<2000;
input_line_71:2:3: error: use of undeclared identifier 'r'
 (r["MaxIter"] << 2000)
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r["MaxIter"] << 2000).
Execution of your code was aborted.

N = size of a vector which is an argument for rastrigin

In [15]:
r["n"]<<3;
input_line_73:2:3: error: use of undeclared identifier 'r'
 (r["n"] << 3)
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r["n"] << 3).
Execution of your code was aborted.

Lower limits

In [16]:
r<<"ll<-rep(-5, n)";
input_line_75:2:3: error: use of undeclared identifier 'r'
 (r << "ll<-rep(-5, n)")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "ll<-rep(-5, n)").
Execution of your code was aborted.

Upper limits

In [17]:
r<<"ul<-rep(5, n)";

bench.Start("GlobalMinimizationRastrigin");
input_line_77:2:3: error: use of undeclared identifier 'r'
 (r << "ul<-rep(5, n)")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "ul<-rep(5, n)").
Execution of your code was aborted.

Calling minimization and timing it.

In [18]:
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;
input_line_79:2:3: error: use of undeclared identifier 'r'
 (r << "result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))")
  ^
Error in <HandleInterpreterException>: Error evaluating expression (r << "result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))").
Execution of your code was aborted.

Printing times

In [19]:
bench.Show("GlobalMinimizationRastrigin");
input_line_81:2:3: error: use of undeclared identifier 'bench'
 (bench.Show("GlobalMinimizationRastrigin"))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (bench.Show("GlobalMinimizationRastrigin")).
Execution of your code was aborted.

Skip r plotting in batch mode

In [20]:
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()";
}