{
"cells": [
{
"cell_type": "markdown",
"id": "93efc908",
"metadata": {},
"source": [
"# GlobalMinimization\n",
"Example based in\n",
"http://cran.r-project.org/web/packages/DEoptim/DEoptim.pdf\n",
"Please install the R package DEoptim before run this example.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Omar Zapata \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:27 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "fedcc2a7",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:38.028129Z",
"iopub.status.busy": "2026-05-19T20:27:38.028024Z",
"iopub.status.idle": "2026-05-19T20:27:38.053886Z",
"shell.execute_reply": "2026-05-19T20:27:38.048341Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_44:1:9: fatal error: 'TRInterface.h' file not found\n",
"#include\n",
" ^~~~~~~~~~~~~~~\n"
]
}
],
"source": [
"%%cpp -d\n",
"#include\n",
"#include\n",
"#include\n",
"#include"
]
},
{
"cell_type": "markdown",
"id": "9376a83e",
"metadata": {},
"source": [
" In the next function the *double pointer should be changed by a TVectorD datatype,\n",
"because the pointer has no meaning in R's enviroment.\n",
"This is a generalization of the RosenBrock function, with the min xi=1 and i>0.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "152488b1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:38.057390Z",
"iopub.status.busy": "2026-05-19T20:27:38.057274Z",
"iopub.status.idle": "2026-05-19T20:27:38.063056Z",
"shell.execute_reply": "2026-05-19T20:27:38.062523Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"Double_t GenRosenBrock(const TVectorD xx )\n",
"{\n",
" int length=xx.GetNoElements();\n",
"\n",
" Double_t result=0;\n",
" for (int i=0;i<(length-1);i++) {\n",
" result+=pow(1-xx[i],2)+100*pow(xx[i+1]-pow(xx[i],2),2);\n",
" }\n",
" return result;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "c276196b",
"metadata": {},
"source": [
" the min xi=0 i>0\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5d5c83ac",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:38.065470Z",
"iopub.status.busy": "2026-05-19T20:27:38.065348Z",
"iopub.status.idle": "2026-05-19T20:27:38.069965Z",
"shell.execute_reply": "2026-05-19T20:27:38.068842Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"Double_t Rastrigin(const TVectorD xx)\n",
"{\n",
" int length=xx.GetNoElements();\n",
" Double_t result=10*length;\n",
" for (int i=0;i: Error evaluating expression (r << \"suppressMessages(library(DEoptim, quietly = TRUE))\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"suppressMessages(library(DEoptim, quietly = TRUE))\";"
]
},
{
"cell_type": "markdown",
"id": "78b45fd9",
"metadata": {},
"source": [
"passing RosenBrock function to R"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4b229038",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:38.596858Z",
"iopub.status.busy": "2026-05-19T20:27:38.596740Z",
"iopub.status.idle": "2026-05-19T20:27:38.801997Z",
"shell.execute_reply": "2026-05-19T20:27:38.801356Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_58:2:28: error: 'R' is not a class, namespace, or enumeration\n",
" r[\"GenRosenBrock\"]<: Error evaluating expression (r[\"MaxIter\"] << 5000)\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r[\"MaxIter\"]<<5000;"
]
},
{
"cell_type": "markdown",
"id": "d14fd533",
"metadata": {},
"source": [
"n = size of vector that is an argument for GenRosenBrock"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9d37240a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:39.010444Z",
"iopub.status.busy": "2026-05-19T20:27:39.010330Z",
"iopub.status.idle": "2026-05-19T20:27:39.215487Z",
"shell.execute_reply": "2026-05-19T20:27:39.214939Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_62:2:3: error: use of undeclared identifier 'r'\n",
" (r[\"n\"] << 3)\n",
" ^\n",
"Error in : Error evaluating expression (r[\"n\"] << 3)\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r[\"n\"]<<3;"
]
},
{
"cell_type": "markdown",
"id": "f0981fd1",
"metadata": {},
"source": [
"lower limits"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "cdcac9bd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:39.217870Z",
"iopub.status.busy": "2026-05-19T20:27:39.217752Z",
"iopub.status.idle": "2026-05-19T20:27:39.423010Z",
"shell.execute_reply": "2026-05-19T20:27:39.422382Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_64:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"ll<-rep(-25, n)\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"ll<-rep(-25, n)\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"ll<-rep(-25, n)\";"
]
},
{
"cell_type": "markdown",
"id": "6dbc092c",
"metadata": {},
"source": [
"upper limits"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "de60a000",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:39.424863Z",
"iopub.status.busy": "2026-05-19T20:27:39.424745Z",
"iopub.status.idle": "2026-05-19T20:27:39.638317Z",
"shell.execute_reply": "2026-05-19T20:27:39.637559Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_66:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"ul<-rep(25, n)\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"ul<-rep(25, n)\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"ul<-rep(25, n)\";\n",
"\n",
"bench.Start(\"GlobalMinimizationRosenBrock\");"
]
},
{
"cell_type": "markdown",
"id": "eb089bd3",
"metadata": {},
"source": [
"calling minimization and timing it."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "264c95dc",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:39.640063Z",
"iopub.status.busy": "2026-05-19T20:27:39.639944Z",
"iopub.status.idle": "2026-05-19T20:27:39.845028Z",
"shell.execute_reply": "2026-05-19T20:27:39.844697Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_68:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"result1<-DEoptim(fn=GenRosenBrock,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))\";\n",
"std::cout<<\"-----------------------------------------\"<: Error evaluating expression (bench.Show(\"GlobalMinimizationRosenBrock\"))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"bench.Show(\"GlobalMinimizationRosenBrock\");"
]
},
{
"cell_type": "markdown",
"id": "861a771f",
"metadata": {},
"source": [
"passing RosenBrock function to R"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "bf7083e4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:40.053685Z",
"iopub.status.busy": "2026-05-19T20:27:40.053529Z",
"iopub.status.idle": "2026-05-19T20:27:40.258650Z",
"shell.execute_reply": "2026-05-19T20:27:40.258108Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_71:2:24: error: 'R' is not a class, namespace, or enumeration\n",
" r[\"Rastrigin\"]<: Error evaluating expression (r[\"MaxIter\"] << 2000)\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r[\"MaxIter\"]<<2000;"
]
},
{
"cell_type": "markdown",
"id": "52b0595d",
"metadata": {},
"source": [
"n = size of a vector which is an argument for Rastrigin"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3ee8fc91",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:40.466574Z",
"iopub.status.busy": "2026-05-19T20:27:40.466464Z",
"iopub.status.idle": "2026-05-19T20:27:40.671540Z",
"shell.execute_reply": "2026-05-19T20:27:40.671011Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_75:2:3: error: use of undeclared identifier 'r'\n",
" (r[\"n\"] << 3)\n",
" ^\n",
"Error in : Error evaluating expression (r[\"n\"] << 3)\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r[\"n\"]<<3;"
]
},
{
"cell_type": "markdown",
"id": "be175048",
"metadata": {},
"source": [
"lower limits"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "e63c8590",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:40.673198Z",
"iopub.status.busy": "2026-05-19T20:27:40.673086Z",
"iopub.status.idle": "2026-05-19T20:27:40.879155Z",
"shell.execute_reply": "2026-05-19T20:27:40.878304Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_77:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"ll<-rep(-5, n)\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"ll<-rep(-5, n)\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"ll<-rep(-5, n)\";"
]
},
{
"cell_type": "markdown",
"id": "6b021d0b",
"metadata": {},
"source": [
"upper limits"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "079ac161",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:40.880562Z",
"iopub.status.busy": "2026-05-19T20:27:40.880436Z",
"iopub.status.idle": "2026-05-19T20:27:41.086858Z",
"shell.execute_reply": "2026-05-19T20:27:41.086003Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_79:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"ul<-rep(5, n)\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"ul<-rep(5, n)\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"ul<-rep(5, n)\";\n",
"\n",
"bench.Start(\"GlobalMinimizationRastrigin\");"
]
},
{
"cell_type": "markdown",
"id": "e0446c18",
"metadata": {},
"source": [
"calling minimization and timing it."
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "25355bd7",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:41.088317Z",
"iopub.status.busy": "2026-05-19T20:27:41.088194Z",
"iopub.status.idle": "2026-05-19T20:27:41.294525Z",
"shell.execute_reply": "2026-05-19T20:27:41.293700Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_81:2:3: error: use of undeclared identifier 'r'\n",
" (r << \"result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))\")\n",
" ^\n",
"Error in : Error evaluating expression (r << \"result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))\")\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"r<<\"result2<-DEoptim(fn=Rastrigin,lower=ll,upper=ul,control=list(NP=10*n,itermax=MaxIter,trace=FALSE))\";\n",
"std::cout<<\"-----------------------------------------\"<: Error evaluating expression (bench.Show(\"GlobalMinimizationRastrigin\"))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"bench.Show(\"GlobalMinimizationRastrigin\");"
]
},
{
"cell_type": "markdown",
"id": "8690f1c7",
"metadata": {},
"source": [
"skip R plotting in batch mode"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "fc5c38bd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:41.503474Z",
"iopub.status.busy": "2026-05-19T20:27:41.503338Z",
"iopub.status.idle": "2026-05-19T20:27:41.706142Z",
"shell.execute_reply": "2026-05-19T20:27:41.705017Z"
}
},
"outputs": [],
"source": [
"if (!gROOT->IsBatch()) {\n",
" r<<\"dev.new(title='RosenBrock Convergence')\";\n",
" r<<\"plot(result1,type='o',pch='.')\";\n",
" r<<\"dev.off()\";\n",
" r<<\"dev.new(title='Rastrigin Convergence')\";\n",
" r<<\"plot(result2,type='o',pch='.')\";\n",
" r<<\"dev.off()\";\n",
"}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}