{ "cells": [ { "cell_type": "markdown", "id": "1aaca36b", "metadata": {}, "source": [ "# NumericalMinimization\n", "Example on how to use the Minimizer class in ROOT.\n", "\n", "Show usage with all the possible minimizers.\n", "Minimize the Rosenbrock function (a 2D -function)\n", "\n", "input : minimizer name + algorithm name\n", "randomSeed: = <0 : fixed value: 0 random with seed 0; >0 random with given seed\n", "\n", "\n", "\n", "\n", "**Author:** Lorenzo Moneta \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:26 PM." ] }, { "cell_type": "markdown", "id": "7cf2802a", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "54994b60", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:20.357652Z", "iopub.status.busy": "2026-05-19T20:26:20.357518Z", "iopub.status.idle": "2026-05-19T20:26:20.361243Z", "shell.execute_reply": "2026-05-19T20:26:20.360787Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "#include \"Math/Minimizer.h\"\n", "#include \"Math/Factory.h\"\n", "#include \"Math/Functor.h\"\n", "#include \"TRandom2.h\"\n", "#include \"TError.h\"\n", "#include \n", "\n", "double RosenBrock(const double *xx )\n", "{\n", " const double x = xx[0];\n", " const double y = xx[1];\n", " const double tmp1 = y-x*x;\n", " const double tmp2 = 1-x;\n", " return 100*tmp1*tmp1+tmp2*tmp2;\n", "}" ] }, { "cell_type": "markdown", "id": "ba2b9d0f", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 2, "id": "70e195e7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:20.362707Z", "iopub.status.busy": "2026-05-19T20:26:20.362571Z", "iopub.status.idle": "2026-05-19T20:26:20.683352Z", "shell.execute_reply": "2026-05-19T20:26:20.682717Z" } }, "outputs": [], "source": [ "const char * minName = \"Minuit2\";\n", "const char *algoName = \"\";\n", "int randomSeed = -1;" ] }, { "cell_type": "markdown", "id": "cdc6a536", "metadata": {}, "source": [ "create minimizer giving a name and a name (optionally) for the specific\n", "algorithm\n", "possible choices are:\n", "minName algoName\n", "Minuit /Minuit2 Migrad, Simplex,Combined,Scan (default is Migrad)\n", "Minuit2 Fumili2\n", "Fumili\n", "GSLMultiMin ConjugateFR, ConjugatePR, BFGS,\n", "BFGS2, SteepestDescent\n", "GSLMultiFit\n", "GSLSimAn\n", "Genetic" ] }, { "cell_type": "code", "execution_count": 3, "id": "8c85f661", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:20.685271Z", "iopub.status.busy": "2026-05-19T20:26:20.685153Z", "iopub.status.idle": "2026-05-19T20:26:20.887475Z", "shell.execute_reply": "2026-05-19T20:26:20.886818Z" } }, "outputs": [], "source": [ "ROOT::Math::Minimizer* minimum =\n", " ROOT::Math::Factory::CreateMinimizer(minName, algoName);\n", "if (!minimum) {\n", " std::cerr << \"Error: cannot create minimizer \\\"\" << minName\n", " << \"\\\". Maybe the required library was not built?\" << std::endl;\n", " return 1;\n", "}" ] }, { "cell_type": "markdown", "id": "ea29736a", "metadata": {}, "source": [ "set tolerance , etc..." ] }, { "cell_type": "code", "execution_count": 4, "id": "f58ead96", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:20.889363Z", "iopub.status.busy": "2026-05-19T20:26:20.889246Z", "iopub.status.idle": "2026-05-19T20:26:21.096423Z", "shell.execute_reply": "2026-05-19T20:26:21.095757Z" } }, "outputs": [], "source": [ "minimum->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2\n", "minimum->SetMaxIterations(10000); // for GSL\n", "minimum->SetTolerance(0.001);\n", "minimum->SetPrintLevel(1);" ] }, { "cell_type": "markdown", "id": "83d8d27f", "metadata": {}, "source": [ "create function wrapper for minimizer\n", "a IMultiGenFunction type" ] }, { "cell_type": "code", "execution_count": 5, "id": "951d78d6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:21.098306Z", "iopub.status.busy": "2026-05-19T20:26:21.098189Z", "iopub.status.idle": "2026-05-19T20:26:21.304842Z", "shell.execute_reply": "2026-05-19T20:26:21.304413Z" } }, "outputs": [], "source": [ "ROOT::Math::Functor f(&RosenBrock,2);\n", "double step[2] = {0.01,0.01};" ] }, { "cell_type": "markdown", "id": "73ec41f9", "metadata": {}, "source": [ "starting point" ] }, { "cell_type": "code", "execution_count": 6, "id": "dd812db8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:21.320420Z", "iopub.status.busy": "2026-05-19T20:26:21.320276Z", "iopub.status.idle": "2026-05-19T20:26:21.527166Z", "shell.execute_reply": "2026-05-19T20:26:21.526583Z" } }, "outputs": [], "source": [ "double variable[2] = { -1.,1.2};\n", "if (randomSeed >= 0) {\n", " TRandom2 r(randomSeed);\n", " variable[0] = r.Uniform(-20,20);\n", " variable[1] = r.Uniform(-20,20);\n", "}\n", "\n", "minimum->SetFunction(f);" ] }, { "cell_type": "markdown", "id": "ef0002f0", "metadata": {}, "source": [ "Set the free variables to be minimized !" ] }, { "cell_type": "code", "execution_count": 7, "id": "957f0c3f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:21.529144Z", "iopub.status.busy": "2026-05-19T20:26:21.529026Z", "iopub.status.idle": "2026-05-19T20:26:21.735877Z", "shell.execute_reply": "2026-05-19T20:26:21.735227Z" } }, "outputs": [], "source": [ "minimum->SetVariable(0,\"x\",variable[0], step[0]);\n", "minimum->SetVariable(1,\"y\",variable[1], step[1]);" ] }, { "cell_type": "markdown", "id": "6ca2bff4", "metadata": {}, "source": [ "do the minimization" ] }, { "cell_type": "code", "execution_count": 8, "id": "66196b01", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:21.737894Z", "iopub.status.busy": "2026-05-19T20:26:21.737776Z", "iopub.status.idle": "2026-05-19T20:26:21.944909Z", "shell.execute_reply": "2026-05-19T20:26:21.944404Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Minuit2Minimizer: Minimize with max-calls 1000000 convergence for edm < 0.001 strategy 1\n", "Minuit2Minimizer : Valid minimum - status = 0\n", "FVAL = 1.86131340625335265e-08\n", "Edm = 1.86927714210849701e-08\n", "Nfcn = 174\n", "x\t = 0.999902\t +/- 1.00395\n", "y\t = 0.999795\t +/- 2.01023\n", "Minimum: f(0.999902,0.999795): 1.86131e-08\n" ] } ], "source": [ "minimum->Minimize();\n", "\n", "const double *xs = minimum->X();\n", "std::cout << \"Minimum: f(\" << xs[0] << \",\" << xs[1] << \"): \"\n", " << minimum->MinValue() << std::endl;" ] }, { "cell_type": "markdown", "id": "4d12f28b", "metadata": {}, "source": [ "expected minimum is 0" ] }, { "cell_type": "code", "execution_count": 9, "id": "6a209431", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:26:21.946414Z", "iopub.status.busy": "2026-05-19T20:26:21.946297Z", "iopub.status.idle": "2026-05-19T20:26:22.153190Z", "shell.execute_reply": "2026-05-19T20:26:22.152682Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Minimizer Minuit2 - converged to the right minimum\n" ] } ], "source": [ "if ( minimum->MinValue() < 1.E-4 )\n", " std::cout << \"Minimizer \" << minName << \" - \" << algoName\n", " << \" converged to the right minimum\" << std::endl;\n", "else {\n", " std::cout << \"Minimizer \" << minName << \" - \" << algoName\n", " << \" failed to converge !!!\" << std::endl;\n", " Error(\"NumericalMinimization\",\"fail to converge\");\n", "}\n", "\n", "return 0;" ] } ], "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 }