{
"cells": [
{
"cell_type": "markdown",
"id": "67b20655",
"metadata": {},
"source": [
"# TMVAGAexample2\n",
"This executable gives an example of a very simple use of the genetic algorithm\n",
"of TMVA.\n",
"- Project : TMVA - a Root-integrated toolkit for multivariate data analysis\n",
"- Package : TMVA\n",
"- Executable: TMVAGAexample\n",
"\n",
"\n",
"\n",
"**Author:** Andreas Hoecker \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:24 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d5183ae1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:11.914380Z",
"iopub.status.busy": "2026-05-19T20:24:11.914245Z",
"iopub.status.idle": "2026-05-19T20:24:11.956575Z",
"shell.execute_reply": "2026-05-19T20:24:11.955951Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"\n",
"#include // Stream declarations\n",
"#include \n",
"\n",
"#include \"TMVA/GeneticAlgorithm.h\"\n",
"#include \"TMVA/GeneticFitter.h\"\n",
"#include \"TMVA/IFitterTarget.h\"\n",
"\n",
"using std::vector, std::cout, std::endl;\n",
"\n",
"namespace TMVA {\n",
"\n",
"\n",
"class MyFitness : public IFitterTarget {\n",
" public:\n",
" MyFitness() : IFitterTarget() {\n",
" }\n",
"\n",
" // the fitness-function goes here\n",
" // the factors are optimized such that the return-value of this function is minimized\n",
" // take care!! the fitness-function must never fail, .. means: you have to prevent\n",
" // the function from reaching undefined values (such as x=0 for 1/x or so)\n",
" //\n",
" // HINT: to use INTEGER variables, it is sufficient to cast the \"factor\" in the fitness-function\n",
" // to (int). In this case the variable-range has to be chosen +1 ( to get 0..5, take Interval(0,6) )\n",
" // since the introduction of \"Interval\" ranges can be defined with a third parameter\n",
" // which gives the number of bins within the interval. With that technique discrete values\n",
" // can be achieved easier. The random selection out of this discrete numbers is completely uniform.\n",
" //\n",
" Double_t EstimatorFunction( std::vector & factors ){\n",
" //return (10.- (int)factors.at(0) *factors.at(1) + (int)factors.at(2));\n",
" return (10.- factors.at(0) *factors.at(1) + factors.at(2));\n",
"\n",
" //return 100.- (10 + factors.at(1)) *factors.at(2)* TMath::Abs( TMath::Sin(factors.at(0)) );\n",
" }\n",
"};\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"void exampleGA(){\n",
" std::cout << \"\\nEXAMPLE\" << std::endl;\n",
" // define all the parameters by their minimum and maximum value\n",
" // in this example 3 parameters are defined.\n",
" vector ranges;\n",
" ranges.push_back( new Interval(0,15,30) );\n",
" ranges.push_back( new Interval(0,13) );\n",
" ranges.push_back( new Interval(0,5,3) );\n",
"\n",
" for( std::vector::iterator it = ranges.begin(); it != ranges.end(); it++ ){\n",
" std::cout << \" range: \" << (*it)->GetMin() << \" \" << (*it)->GetMax() << std::endl;\n",
" }\n",
"\n",
" IFitterTarget* myFitness = new MyFitness();\n",
"\n",
" // prepare the genetic algorithm with an initial population size of 20\n",
" // mind: big population sizes will help in searching the domain space of the solution\n",
" // but you have to weight this out to the number of generations\n",
" // the extreme case of 1 generation and populationsize n is equal to\n",
" // a Monte Carlo calculation with n tries\n",
"\n",
" const TString name( \"exampleGA\" );\n",
" const TString opts( \"PopSize=100:Steps=30\" );\n",
"\n",
" GeneticFitter mg( *myFitness, name, ranges, opts);\n",
" // mg.SetParameters( 4, 30, 200, 10,5, 0.95, 0.001 );\n",
"\n",
" std::vector result;\n",
" Double_t estimator = mg.Run(result);\n",
"\n",
" int n = 0;\n",
" for( std::vector::iterator it = result.begin(); it Optimisation, please be patient ... (inaccurate progress timing for GA)\n",
" : Elapsed time: 0.00696 sec \n",
"FACTOR 0 : 15\n",
"FACTOR 1 : 13\n",
"FACTOR 2 : 0\n"
]
}
],
"source": [
"cout << \"Start Test TMVAGAexample\" << endl\n",
" << \"========================\" << endl\n",
" << endl;\n",
"\n",
"TMVA::exampleGA();"
]
}
],
"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
}