How to properly use Genetic fitting

I am a little bit lost in the examples and documentation. Simply selecting Genetic algorithm with TVirtualFitter does not respect TF2::FixParameter. I don’t know if it is a bug or not, but I followed root.cern.ch/root/html/tutorials … Fit.C.html example for another approach to fitting and modified it to fit my case. Minuit fit works fine. However, when I just change to Genetic algorithm, I get:

So it seems that not everything is passed to Genetic fitter. There is an example of Minimization with different algorithms:

root.cern.ch/root/html/tutorials … ion.C.html

but it follows completely different approach than the previous example which actually fits the function, not looks for its minimum. So how am I supposed to fit with Genetic algorithm?

Hi,

For using the Genetic minimizer it is first better to set a reasonable range for all parameters. If this is not done, it is done automatically (for this reason you get that message), but this often is not optimal.

Then I noticed there is also a bug, which I will fix, when using fixed parameters. As a work around you can set a very narrow range for the fixed parameters.
As example, I attach a modified combinedFit.C macro which will work with the Genetic fitter.

Thank you for your report

Lorenzo
combinedFit.C (4.21 KB)

Hi,
I have fixed in the current ROOT trunk, revision 42472, the Genetic fitting with fixed parameters
Best Regards
Lorenzo

Great! Works!

It is my first try with Genetic Algorithm, and I have some questions.

First, the algorithm seems to perform some unnecessary iterations. Parameters changes are probably so small, that out of default scope of fitter decimal places printing. Is there any possibility to restrain fitter from attempting such small changes? Here is an output example:

New Iteration 35 with  parameter values :
-0.19410830681    -0.36847761469    
        Fitness function value = 6.8850016601
New Iteration 36 with  parameter values :
-0.19410830681    -0.36847761469    
        Fitness function value = 6.8850016601
New Iteration 37 with  parameter values :
-0.19410830681    -0.36847761469    
        Fitness function value = 6.8850016601
New Iteration 38 with  parameter values :
-0.19410830681    -0.36847761469    
        Fitness function value = 6.8850016601
New Iteration 39 with  parameter values :
-0.19391126507    -0.36847761469    
        Fitness function value = 6.8833013402
New Iteration 40 with  parameter values :
-0.19391126507    -0.36847761469    
        Fitness function value = 6.8833013402
result size = 9

The other thing is connected to my FCN function. As a result of custom FCN, I get:

Minimizer is Genetic
MinFCN                    =          nan
NDf                       =         1637
Edm                       =            0
NCalls                    =        12300

Does this nan, etc. influence the fitter in any way?

And a general question: is there a way to set population and other common parameters of genetic fitting algorithm?

Hi,
Having a nan is not good. You should protect in your fitting function that you do not produce NaN. If you think is the algorithm doing it, please let me know

FOr changing the parameter of the algorithm, you can change all parameters described in the Table 7 at page 49 in the TMVA User guide (tmva.sourceforge.net/docu/TMVAUsersGuide.pdf)

The syntax to use for changing the parameters is for example

ROOT::Math::IOptions & geneticOpt = ROOT::Math::MinimizerOptions::Default("Genetic");
   geneticOpt.SetValue("PopSize",popSize);
   geneticOpt.SetValue("Steps", nsteps);
   geneticOpt.SetValue("Cycles", ncycles);
   geneticOpt.SetValue("SC_steps", SC_steps);
   geneticOpt.SetValue("SC_rate", SC_rate);
   geneticOpt.SetValue("SC_factor", SC_factor);
   geneticOpt.SetValue("ConvCrit", ConvCrit);

You can try playing with them to find optimal one in your case

Best Regards

Lorenzo

My algorithm does not produce nans, as you can see from the fitter output:

etc.

the problem is that the fitter does not get proper chi2, Ndf etc. to the final result (although, as above, seems to get proper chi2 during the fit). I expect I should set Ndf and chi2 also in some other way, but I am not sure how.

The MinFCN value print at the end, should be the fitness function value of the last iteration.
If this is not the case, please attach the full log file printed in debug mode (MinimizerOptions::SetDefaultPrintLevel(3) ) and possibly also the code you are using to run the Genetic algorithm, so I can have a look into it,

Thanks

Lorenzo

Below is the full log:

I doubt you can do anything with just a log. The code is messy and complicated - reads function parameters from histograms, convolves a function, etc. So there are many files needed and the code is long. I tried to regenerate the problem with simple code with custom chi2 function, but… everything works then. I expect I need to dig deeper…

Maybe the problem is of a general nature …

Info in GeneticMinimizer::Minimize: Start iterating - max iterations = 24 , conv criteria (tolerance) = 1.000000e-01

Info in GeneticMinimizer::Minimize: Max number of iterations 24 reached - stop iterating

Finished Iteration (niter = 25 with fitness function value = nan

… i.e. something gets wrong when the iteration procedure is terminated because the “max. number of iterations” limit was reached.

It seems so, however only in this case (simple gaussian to gaussian fitting works). However, I noticed something peculiar, which may be responsible for this - I cannot now see how this could be due to some kind of bug in my code:

There is a part of fitting output with my output as well:

multifun 596.00085449 -0.0076556352801
        Fitness function value = 4476521.7
result size = 9

chi2 596.00085449 596.00085449

multifun 596.00085449 -0.0076556352801 shows par[0] and par[1]. Here they are as they should be. Just after results size = 9 the par[0] is copied to par[1] somehow. Strange, for if my code did it in any way, it should do it a long time before this final iterations end…

Hi,
There was a bug in computing the minimum function value after the fitting in case you have fixed parameters.
This should be now fixed in the trunk after r42494.
Please try and let me know if you still have problems,
Thank you for your reports !

Lorenzo

Works. Thanks!

Btw. do you think it had a chance to affect fit result in any way?

Thank you for your quick reply !
Yes, the bug affected the computation of the minimum value (chi2 or likelihood) which was also passed to the fit result. So it could happen, due to use a wrong array of parameters, that minimum value is computed as a NaN.

Best Regards

Lorenzo

And how can I see the current values of these parameters? I tried geneticOpt.IValue/RValue/NamedValue and GetIntValue, GetRealValue, but non of these sees “PopSize”. Setting “PopSize” works…

Hi,
You can query the default minimizer options from the GeneticMinimizer class.
You can do

ROOT::Math::GeneticMinimizer gmin; 
gmin.Options().Print();

Lorenzo