{ "cells": [ { "cell_type": "markdown", "id": "d89ce687", "metadata": {}, "source": [ "# TMVAGAexample\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": null, "id": "d6044513", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\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;\n", "\n", "using namespace TMVA;\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", "%%cpp -d\n", "\n", "\n", "class MyGA2nd : public GeneticAlgorithm {\n", " public:\n", " MyGA2nd( IFitterTarget& target, Int_t size, vector& ranges ) : GeneticAlgorithm(target,\n", " size, ranges ){\n", " }\n", "\n", "\n", " // this method has to be activated if one wants to change the behaviour of the evolution\n", " // works only with the head version\n", " //void Evolution(){\n", " // fSexual = true;\n", " // if (fSexual) {\n", " // fPopulation.MakeCopies( 5 );\n", " // fPopulation.MakeChildren();\n", " // fPopulation.NextGeneration();\n", "\n", " // fPopulation.Mutate( 10, 3, kTRUE, fSpread, fMirror );\n", " // fPopulation.Mutate( 40, fPopulation.GetPopulationSize()*3/4 );\n", " // } else {\n", " // fPopulation.MakeCopies( 3 );\n", " // fPopulation.MakeMutants(100,true, 0.1, true);\n", " // fPopulation.NextGeneration();\n", " // }\n", " // }\n", "};" ] }, { "cell_type": "code", "execution_count": null, "id": "e52156fe", "metadata": { "collapsed": false }, "outputs": [], "source": [ "std::cout << \"Start Test TMVAGAexample\" << std::endl\n", " << \"========================\" << std::endl\n", " << \"\\nEXAMPLE\" << std::endl;" ] }, { "cell_type": "markdown", "id": "aa50c322", "metadata": {}, "source": [ "define all the parameters by their minimum and maximum value\n", "in this example 3 parameters are defined." ] }, { "cell_type": "code", "execution_count": null, "id": "8af60b35", "metadata": { "collapsed": false }, "outputs": [], "source": [ "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();" ] }, { "cell_type": "markdown", "id": "cacadf1f", "metadata": {}, "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "id": "e2ec941f", "metadata": { "collapsed": false }, "outputs": [], "source": [ "MyGA2nd mg( *myFitness, 100, ranges );" ] }, { "cell_type": "markdown", "id": "ae94fc11", "metadata": {}, "source": [ "mg.SetParameters( 4, 30, 200, 10,5, 0.95, 0.001 );" ] }, { "cell_type": "code", "execution_count": null, "id": "ba92681b", "metadata": { "collapsed": false }, "outputs": [], "source": [ "#define CONVSTEPS 20\n", "#define CONVCRIT 0.0001\n", "#define SCSTEPS 10\n", "#define SCRATE 5\n", "#define SCFACTOR 0.95\n", "\n", "do {\n", " // prepares the new generation and does evolution\n", " mg.Init();\n", "\n", " // assess the quality of the individuals\n", " mg.CalculateFitness();\n", "\n", " mg.GetGeneticPopulation().Print(0);\n", " std::cout << \"---\" << std::endl;\n", "\n", " // reduce the population size to the initially defined one\n", " mg.GetGeneticPopulation().TrimPopulation();\n", "\n", " // tricky thing: control the speed of how fast the \"solution space\" is searched through\n", " // this function basically influences the sigma of a gaussian around the actual value\n", " // of the parameter where the new value will be randomly thrown.\n", " // when the number of improvements within the last SCSTEPS\n", " // A) smaller than SCRATE: divide the preset sigma by SCFACTOR\n", " // B) equal to SCRATE: do nothing\n", " // C) greater than SCRATE: multiply the preset sigma by SCFACTOR\n", " // if you don't know what to do, leave it unchanged or even delete this function call\n", " mg.SpreadControl( SCSTEPS, SCRATE, SCFACTOR );\n", "\n", "} while (!mg.HasConverged( CONVSTEPS, CONVCRIT )); // converged if: fitness-improvement < CONVCRIT within the last CONVSTEPS loops\n", "\n", "GeneticGenes* genes = mg.GetGeneticPopulation().GetGenes( 0 );\n", "std::vector gvec;\n", "gvec = genes->GetFactors();\n", "int n = 0;\n", "for( std::vector::iterator it = gvec.begin(); it