Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:23:04

0001 // @(#)root/tmva $Id$
0002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Krzysztof Danielowski, Kamil Kraszewski, Maciej Kruk
0003 
0004 /**********************************************************************************
0005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
0006  * Package: TMVA                                                                  *
0007  * Class  : SimulatedAnnealing                                                    *
0008  *                                             *
0009  *                                                                                *
0010  * Description:                                                                   *
0011  *      Implementation of simulated annealing fitting procedure                   *
0012  *                                                                                *
0013  * Authors (alphabetical):                                                        *
0014  *      Krzysztof Danielowski <danielow@cern.ch>       - IFJ & AGH, Poland        *
0015  *      Kamil Kraszewski      <kalq@cern.ch>           - IFJ & UJ, Poland         *
0016  *      Maciej Kruk           <mkruk@cern.ch>          - IFJ & AGH, Poland        *
0017  *                                                                                *
0018  * Copyright (c) 2008:                                                            *
0019  *      IFJ-Krakow, Poland                                                        *
0020  *      CERN, Switzerland                                                         *
0021  *      MPI-K Heidelberg, Germany                                                 *
0022  *                                                                                *
0023  * Redistribution and use in source and binary forms, with or without             *
0024  * modification, are permitted according to the terms listed in LICENSE           *
0025  * (see tmva/doc/LICENSE)                                          *
0026  **********************************************************************************/
0027 
0028 #ifndef ROOT_TMVA_SimulatedAnnealing
0029 #define ROOT_TMVA_SimulatedAnnealing
0030 
0031 //////////////////////////////////////////////////////////////////////////
0032 //                                                                      //
0033 // SimulatedAnnealing                                                   //
0034 //                                                                      //
0035 // Base implementation of simulated annealing fitting procedure         //
0036 //                                                                      //
0037 //////////////////////////////////////////////////////////////////////////
0038 
0039 #include <vector>
0040 
0041 #include "TMVA/Types.h"
0042 
0043 class TRandom;
0044 
0045 namespace TMVA {
0046 
0047    class IFitterTarget;
0048    class Interval;
0049    class MsgLogger;
0050 
0051    class SimulatedAnnealing {
0052 
0053    public:
0054 
0055       SimulatedAnnealing( IFitterTarget& target, const std::vector<TMVA::Interval*>& ranges );
0056       virtual ~SimulatedAnnealing();
0057 
0058       // returns FCN value at minimum
0059       Double_t Minimize( std::vector<Double_t>& parameters );
0060 
0061       // accessors
0062       void SetMaxCalls          ( Int_t    mc    ) { fMaxCalls = mc; }
0063       void SetInitTemp          ( Double_t it    ) { fInitialTemperature = it; }
0064       void SetMinTemp           ( Double_t min   ) { fMinTemperature = min; }
0065       void SetAccuracy          ( Double_t eps   ) { fEps = eps; }
0066       void SetTemperatureScale  ( Double_t scale ) { fTemperatureScale = scale; }
0067       void SetAdaptiveSpeed     ( Double_t speed ) { fAdaptiveSpeed = speed; }
0068 
0069       void SetOptions( Int_t maxCalls, Double_t initialTemperature, Double_t minTemperature, Double_t eps,
0070                        TString  kernelTemperatureS, Double_t temperatureScale, Double_t adaptiveSpeed,
0071                        Double_t temperatureAdaptiveStep, Bool_t useDefaultScale, Bool_t useDefaultTemperature );
0072 
0073       //setting up helper variables for JsMVA
0074       void SetIPythonInteractive(bool* ExitFromTraining, UInt_t *fIPyCurrentIter_){
0075         fExitFromTraining = ExitFromTraining;
0076         fIPyCurrentIter = fIPyCurrentIter_;
0077       }
0078 
0079    private:
0080 
0081       enum EKernelTemperature {
0082          kSqrt = 0,
0083          kIncreasingAdaptive,
0084          kDecreasingAdaptive,
0085          kLog,
0086          kHomo,
0087          kSin,
0088          kGeo
0089       } fKernelTemperature;
0090 
0091       void FillWithRandomValues( std::vector<Double_t>& parameters );
0092       void ReWriteParameters( std::vector<Double_t>& from, std::vector<Double_t>& to );
0093       void GenerateNewTemperature(Double_t& currentTemperature, Int_t Iter );
0094       void GenerateNeighbour( std::vector<Double_t>& parameters, std::vector<Double_t>& oldParameters, Double_t currentTemperature );
0095       Bool_t ShouldGoIn( Double_t currentFit, Double_t localFit, Double_t currentTemperature );
0096       void SetDefaultScale();
0097       Double_t GenerateMaxTemperature( std::vector<Double_t>& parameters );
0098       std::vector<Double_t> GenerateNeighbour( std::vector<Double_t>& parameters, Double_t currentTemperature );
0099 
0100       IFitterTarget&                fFitterTarget;           ///< the fitter target
0101       TRandom*                      fRandom;                 ///< random generator
0102       const std::vector<TMVA::Interval*>& fRanges;           ///< parameter ranges
0103 
0104       // fitter setup
0105       Int_t                         fMaxCalls;               ///< maximum number of minimisation calls
0106       Double_t                      fInitialTemperature;     ///< initial temperature
0107       Double_t                      fMinTemperature;         ///< minimum temperature
0108       Double_t                      fEps;                    ///< epsilon
0109       Double_t                      fTemperatureScale;       ///< how fast temperature change
0110       Double_t                      fAdaptiveSpeed;          ///< how fast temperature change in adaptive (in adaptive two variables describe
0111                                                              ///< the change of temperature, but fAdaptiveSpeed should be 1.0 and its not
0112                                                              ///< recommended to change it)
0113       Double_t                      fTemperatureAdaptiveStep;///< used to calculate InitialTemperature if fUseDefaultTemperature
0114 
0115       Bool_t                        fUseDefaultScale;        ///< if TRUE, SA calculates its own TemperatureScale
0116       Bool_t                        fUseDefaultTemperature;  ///< if TRUE, SA calculates its own InitialTemperature (MinTemperautre)
0117 
0118       mutable MsgLogger*            fLogger;   ///<! message logger
0119       MsgLogger& Log() const { return *fLogger; }
0120 
0121       Double_t fProgress;
0122 
0123       // variables for JsMVA
0124       UInt_t *fIPyCurrentIter = nullptr;
0125       bool * fExitFromTraining = nullptr;
0126 
0127       ClassDef(SimulatedAnnealing,0);  // Base class for Simulated Annealing fitting
0128    };
0129 
0130 } // namespace TMVA
0131 
0132 #endif
0133