Example based in http://root.cern.ch/root/html/tutorials/fit/NumericalMinimization.C.html http://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html
Author: Omar Zapata
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 28, 2023 at 09:54 AM.
%%cpp -d
#include<TRInterface.h>
In file included from input_line_43: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:64:9: fatal error: 'RcppCommon.h' file not found #include<RcppCommon.h> ^~~~~~~~~~~~~~
in the next function the *double pointer must be changed by a TVectorD, because the pointer has no meaning in R enviroment.
%%cpp -d
Double_t RosenBrock(const TVectorD xx )
{
const Double_t x = xx[0];
const Double_t y = xx[1];
const Double_t tmp1 = y-x*x;
const Double_t tmp2 = 1-x;
return 100*tmp1*tmp1+tmp2*tmp2;
}
Definition of a helper function:
%%cpp -d
TVectorD RosenBrockGrad(const TVectorD xx )
{
const Double_t x = xx[0];
const Double_t y = xx[1];
TVectorD grad(2);
grad[0]=-400 * x * (y - x * x) - 2 * (1 - x);
grad[1]=200 * (y - x * x);
return grad;
}
ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
In file included from libRInterface dictionary payload:5: /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:64: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_46:2:35: 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;}} ^
passsing RosenBrock function to R
r["RosenBrock"]=ROOT::R::TRFunctionExport(RosenBrock);
In file included from libRInterface dictionary payload:5: /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/RExports.h:64: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_57:2:27: error: no member named 'TRFunctionExport' in namespace 'ROOT::R' r["RosenBrock"]=ROOT::R::TRFunctionExport(RosenBrock); ~~~~~~~~~^
passsing RosenBrockGrad function to R
r["RosenBrockGrad"]=ROOT::R::TRFunctionExport(RosenBrockGrad);
input_line_59:2:31: error: no member named 'TRFunctionExport' in namespace 'ROOT::R' r["RosenBrockGrad"]=ROOT::R::TRFunctionExport(RosenBrockGrad); ~~~~~~~~~^
the option "method" could be "Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN","Brent"
the option "control" lets you put some constraints like "maxit" The maximum number of iterations. "abstol" The absolute convergence tolerance.
r.Execute("result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )");
input_line_61:2:3: error: use of undeclared identifier 'r' (r.Execute("result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )")) ^ Error in <HandleInterpreterException>: Error evaluating expression (r.Execute("result <- optim( c(0.01,0.01), RosenBrock,method='BFGS',control = list(maxit = 1000000) )")) Execution of your code was aborted.
"reltol" Relative convergence tolerance.
Getting results from R
TVectorD min=r.Eval("result$par");
std::cout.precision(8);
input_line_62:2:2: warning: 'min' shadows a declaration with the same name in the 'std' namespace; use '::min' to reference this declaration TVectorD min=r.Eval("result$par"); ^ [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN12__cling_N52424__dynamic__cling_Un1Qu30E, _GLOBAL__sub_I_cling_module_311, $.cling-module-311.__inits.0, _ZNSt8ios_base9precisionEl, _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, _ZN12__cling_N5243minE, __cxx_global_var_initcling_module_311_.2, _ZN12__cling_N52416__cling_Un1Qu324EPv, __cxx_global_var_initcling_module_311_, __orc_init_func.cling-module-311 }) } IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*) Maybe you need to load the corresponding shared library? IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler() Maybe you need to load the corresponding shared library?
printing results
std::cout<<"-----------------------------------------"<<std::endl;
std::cout<<"Minimum x="<<min[0]<<" y="<<min[1]<<std::endl;
std::cout<<"Value at minimum ="<<RosenBrock(min)<<std::endl;
input_line_63:3:26: error: reference to 'min' is ambiguous std::cout<<"Minimum x="<<min[0]<<" y="<<min[1]<<std::endl; ^ input_line_62:2:12: note: candidate found by name lookup is 'min' TVectorD min=r.Eval("result$par"); ^ /usr/include/c++/9/bits/stl_algo.h:3456:5: note: candidate found by name lookup is 'std::min' min(initializer_list<_Tp> __l, _Compare __comp) ^ /usr/include/c++/9/bits/algorithmfwd.h:383:5: note: candidate found by name lookup is 'std::min' min(const _Tp&, const _Tp&); ^ /usr/include/c++/9/bits/algorithmfwd.h:388:5: note: candidate found by name lookup is 'std::min' min(const _Tp&, const _Tp&, _Compare); ^ /usr/include/c++/9/bits/stl_algo.h:3450:5: note: candidate found by name lookup is 'std::min' min(initializer_list<_Tp> __l) ^ input_line_63:3:41: error: reference to 'min' is ambiguous std::cout<<"Minimum x="<<min[0]<<" y="<<min[1]<<std::endl; ^ input_line_62:2:12: note: candidate found by name lookup is 'min' TVectorD min=r.Eval("result$par"); ^ /usr/include/c++/9/bits/stl_algo.h:3456:5: note: candidate found by name lookup is 'std::min' min(initializer_list<_Tp> __l, _Compare __comp) ^ /usr/include/c++/9/bits/algorithmfwd.h:383:5: note: candidate found by name lookup is 'std::min' min(const _Tp&, const _Tp&); ^ /usr/include/c++/9/bits/algorithmfwd.h:388:5: note: candidate found by name lookup is 'std::min' min(const _Tp&, const _Tp&, _Compare); ^ /usr/include/c++/9/bits/stl_algo.h:3450:5: note: candidate found by name lookup is 'std::min' min(initializer_list<_Tp> __l) ^ input_line_63:4:45: error: reference to 'min' is ambiguous std::cout<<"Value at minimum ="<<RosenBrock(min)<<std::endl; ^ input_line_62:2:12: note: candidate found by name lookup is 'min' TVectorD min=r.Eval("result$par"); ^ /usr/include/c++/9/bits/stl_algo.h:3456:5: note: candidate found by name lookup is 'std::min' min(initializer_list<_Tp> __l, _Compare __comp) ^ /usr/include/c++/9/bits/algorithmfwd.h:383:5: note: candidate found by name lookup is 'std::min' min(const _Tp&, const _Tp&); ^ /usr/include/c++/9/bits/algorithmfwd.h:388:5: note: candidate found by name lookup is 'std::min' min(const _Tp&, const _Tp&, _Compare); ^ /usr/include/c++/9/bits/stl_algo.h:3450:5: note: candidate found by name lookup is 'std::min' min(initializer_list<_Tp> __l) ^
using the gradient
r.Execute("optimHess(result$par, RosenBrock, RosenBrockGrad)");
r.Execute("hresult <- optim(c(-1.2,1), RosenBrock, NULL, method = 'BFGS', hessian = TRUE)");
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-311 }) } input_line_65:2:3: error: use of undeclared identifier 'r' (r.Execute("optimHess(result$par, RosenBrock, RosenBrockGrad)")) ^ Error in <HandleInterpreterException>: Error evaluating expression (r.Execute("optimHess(result$par, RosenBrock, RosenBrockGrad)")) Execution of your code was aborted.
getting the min calculated with the gradient
TVectorD hmin=r.Eval("hresult$par");
cling JIT session error: Failed to materialize symbols: { (main, { _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv }) } [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { $.cling-module-314.__inits.0, _ZN12__cling_N52724__dynamic__cling_Un1Qu31E, _GLOBAL__sub_I_cling_module_314, _ZN12__cling_N52716__cling_Un1Qu328EPv, __cxx_global_var_initcling_module_314_.2, _ZN12__cling_N5274hminE, __cxx_global_var_initcling_module_314_, __orc_init_func.cling-module-314 }) }
printing results
std::cout<<"-----------------------------------------"<<std::endl;
std::cout<<"Minimization with the Gradient"<<std::endl;
std::cout<<"Minimum x="<<hmin[0]<<" y="<<hmin[1]<<std::endl;
std::cout<<"Value at minimum ="<<RosenBrock(hmin)<<std::endl;
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-314 }) } cling JIT session error: Failed to materialize symbols: { (main, { _ZN12__cling_N5274hminE }) }