// @(#)root/tmva $Id$   
// Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Krzysztof Danielowski, Kamil Kraszewski, Maciej Kruk

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : SimulatedAnnealing                                                    *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      Implementation of simulated annealing fitting procedure                   *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Krzysztof Danielowski <danielow@cern.ch>       - IFJ & AGH, Poland        *
 *      Kamil Kraszewski      <kalq@cern.ch>           - IFJ & UJ, Poland         *
 *      Maciej Kruk           <mkruk@cern.ch>          - IFJ & AGH, Poland        *
 *                                                                                *
 * Copyright (c) 2008:                                                            *
 *      IFJ-Krakow, Poland                                                        *
 *      CERN, Switzerland                                                         * 
 *      MPI-K Heidelberg, Germany                                                 * 
 *                                                                                *
 * Redistribution and use in source and binary forms, with or without             *
 * modification, are permitted according to the terms listed in LICENSE           *
 * (http://tmva.sourceforge.net/LICENSE)                                          *
 **********************************************************************************/

#ifndef ROOT_TMVA_SimulatedAnnealing
#define ROOT_TMVA_SimulatedAnnealing

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// SimulatedAnnealing                                                   //
//                                                                      //
// Base implementation of simulated annealing fitting procedure         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include <vector>
#include <list>

#ifndef ROOT_TMVA_Types
#include "TMVA/Types.h"
#endif

class TRandom;

namespace TMVA {

   class IFitterTarget;
   class Interval;
   class MsgLogger;

   class SimulatedAnnealing {

   public:

      SimulatedAnnealing( IFitterTarget& target, const std::vector<TMVA::Interval*>& ranges );
      virtual ~SimulatedAnnealing();

      // returns FCN value at minimum
      Double_t Minimize( std::vector<Double_t>& parameters );

      // accessors
      void SetMaxCalls          ( Int_t    mc    ) { fMaxCalls = mc; }
      void SetInitTemp          ( Double_t it    ) { fInitialTemperature = it; }
      void SetMinTemp           ( Double_t min   ) { fMinTemperature = min; }
      void SetAccuracy          ( Double_t eps   ) { fEps = eps; }
      void SetTemperatureScale  ( Double_t scale ) { fTemperatureScale = scale; }
      void SetAdaptiveSpeed     ( Double_t speed ) { fAdaptiveSpeed = speed; }

      void SetOptions( Int_t maxCalls, Double_t initialTemperature, Double_t minTemperature, Double_t eps,
                       TString  kernelTemperatureS, Double_t temperatureScale, Double_t adaptiveSpeed, 
                       Double_t temperatureAdaptiveStep, Bool_t useDefaultScale, Bool_t useDefaultTemperature );


   private:

      enum EKernelTemperature {
         kSqrt = 0,
         kIncreasingAdaptive,
         kDecreasingAdaptive,
         kLog,
         kHomo,
         kSin,
         kGeo
      } fKernelTemperature;

      void FillWithRandomValues( std::vector<Double_t>& parameters );
      void ReWriteParameters( std::vector<Double_t>& from, std::vector<Double_t>& to );
      void GenerateNewTemperature(Double_t& currentTemperature, Int_t Iter );
      void GenerateNeighbour( std::vector<Double_t>& parameters, std::vector<Double_t>& oldParameters, Double_t currentTemperature );
      Bool_t ShouldGoIn( Double_t currentFit, Double_t localFit, Double_t currentTemperature );
      void SetDefaultScale();
      Double_t GenerateMaxTemperature( std::vector<Double_t>& parameters );
      std::vector<Double_t> GenerateNeighbour( std::vector<Double_t>& parameters, Double_t currentTemperature );

      IFitterTarget&                fFitterTarget;           // the fitter target
      TRandom*                      fRandom;                 // random generator
      const std::vector<TMVA::Interval*>& fRanges;                 // parameter ranges

      // fitter setup 
      Int_t                         fMaxCalls;               // maximum number of minimisation calls
      Double_t                      fInitialTemperature;     // initial temperature
      Double_t                      fMinTemperature;         // mimimum temperature
      Double_t                      fEps;                    // epsilon
      Double_t                      fTemperatureScale;       // how fast temperature change
      Double_t                      fAdaptiveSpeed;          // how fast temperature change in adaptive (in adaptive two variables describe
                                                             // the change of temperature, but fAdaptiveSpeed should be 1.0 and its not 
                                                             // recomended to change it)
      Double_t                      fTemperatureAdaptiveStep;// used to calculate InitialTemperature if fUseDefaultTemperature

      Bool_t                        fUseDefaultScale;        // if TRUE, SA calculates its own TemperatureScale
      Bool_t                        fUseDefaultTemperature;  // if TRUE, SA calculates its own InitialTemperature (MinTemperautre)

      mutable MsgLogger*            fLogger;   // message logger
      MsgLogger& Log() const { return *fLogger; }

      Double_t fProgress;

      ClassDef(SimulatedAnnealing,0)  // Base class for Simulated Annealing fitting
   };

} // namespace TMVA

#endif

 SimulatedAnnealing.h:1
 SimulatedAnnealing.h:2
 SimulatedAnnealing.h:3
 SimulatedAnnealing.h:4
 SimulatedAnnealing.h:5
 SimulatedAnnealing.h:6
 SimulatedAnnealing.h:7
 SimulatedAnnealing.h:8
 SimulatedAnnealing.h:9
 SimulatedAnnealing.h:10
 SimulatedAnnealing.h:11
 SimulatedAnnealing.h:12
 SimulatedAnnealing.h:13
 SimulatedAnnealing.h:14
 SimulatedAnnealing.h:15
 SimulatedAnnealing.h:16
 SimulatedAnnealing.h:17
 SimulatedAnnealing.h:18
 SimulatedAnnealing.h:19
 SimulatedAnnealing.h:20
 SimulatedAnnealing.h:21
 SimulatedAnnealing.h:22
 SimulatedAnnealing.h:23
 SimulatedAnnealing.h:24
 SimulatedAnnealing.h:25
 SimulatedAnnealing.h:26
 SimulatedAnnealing.h:27
 SimulatedAnnealing.h:28
 SimulatedAnnealing.h:29
 SimulatedAnnealing.h:30
 SimulatedAnnealing.h:31
 SimulatedAnnealing.h:32
 SimulatedAnnealing.h:33
 SimulatedAnnealing.h:34
 SimulatedAnnealing.h:35
 SimulatedAnnealing.h:36
 SimulatedAnnealing.h:37
 SimulatedAnnealing.h:38
 SimulatedAnnealing.h:39
 SimulatedAnnealing.h:40
 SimulatedAnnealing.h:41
 SimulatedAnnealing.h:42
 SimulatedAnnealing.h:43
 SimulatedAnnealing.h:44
 SimulatedAnnealing.h:45
 SimulatedAnnealing.h:46
 SimulatedAnnealing.h:47
 SimulatedAnnealing.h:48
 SimulatedAnnealing.h:49
 SimulatedAnnealing.h:50
 SimulatedAnnealing.h:51
 SimulatedAnnealing.h:52
 SimulatedAnnealing.h:53
 SimulatedAnnealing.h:54
 SimulatedAnnealing.h:55
 SimulatedAnnealing.h:56
 SimulatedAnnealing.h:57
 SimulatedAnnealing.h:58
 SimulatedAnnealing.h:59
 SimulatedAnnealing.h:60
 SimulatedAnnealing.h:61
 SimulatedAnnealing.h:62
 SimulatedAnnealing.h:63
 SimulatedAnnealing.h:64
 SimulatedAnnealing.h:65
 SimulatedAnnealing.h:66
 SimulatedAnnealing.h:67
 SimulatedAnnealing.h:68
 SimulatedAnnealing.h:69
 SimulatedAnnealing.h:70
 SimulatedAnnealing.h:71
 SimulatedAnnealing.h:72
 SimulatedAnnealing.h:73
 SimulatedAnnealing.h:74
 SimulatedAnnealing.h:75
 SimulatedAnnealing.h:76
 SimulatedAnnealing.h:77
 SimulatedAnnealing.h:78
 SimulatedAnnealing.h:79
 SimulatedAnnealing.h:80
 SimulatedAnnealing.h:81
 SimulatedAnnealing.h:82
 SimulatedAnnealing.h:83
 SimulatedAnnealing.h:84
 SimulatedAnnealing.h:85
 SimulatedAnnealing.h:86
 SimulatedAnnealing.h:87
 SimulatedAnnealing.h:88
 SimulatedAnnealing.h:89
 SimulatedAnnealing.h:90
 SimulatedAnnealing.h:91
 SimulatedAnnealing.h:92
 SimulatedAnnealing.h:93
 SimulatedAnnealing.h:94
 SimulatedAnnealing.h:95
 SimulatedAnnealing.h:96
 SimulatedAnnealing.h:97
 SimulatedAnnealing.h:98
 SimulatedAnnealing.h:99
 SimulatedAnnealing.h:100
 SimulatedAnnealing.h:101
 SimulatedAnnealing.h:102
 SimulatedAnnealing.h:103
 SimulatedAnnealing.h:104
 SimulatedAnnealing.h:105
 SimulatedAnnealing.h:106
 SimulatedAnnealing.h:107
 SimulatedAnnealing.h:108
 SimulatedAnnealing.h:109
 SimulatedAnnealing.h:110
 SimulatedAnnealing.h:111
 SimulatedAnnealing.h:112
 SimulatedAnnealing.h:113
 SimulatedAnnealing.h:114
 SimulatedAnnealing.h:115
 SimulatedAnnealing.h:116
 SimulatedAnnealing.h:117
 SimulatedAnnealing.h:118
 SimulatedAnnealing.h:119
 SimulatedAnnealing.h:120
 SimulatedAnnealing.h:121
 SimulatedAnnealing.h:122
 SimulatedAnnealing.h:123
 SimulatedAnnealing.h:124
 SimulatedAnnealing.h:125
 SimulatedAnnealing.h:126
 SimulatedAnnealing.h:127