Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
SimulatedAnnealingFitter.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andraes Hoecker, Kamil Kraszewski, Maciej Kruk
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : SimulatedAnnealingFitter *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation *
12  * *
13  * Authors (alphabetical): *
14  * Krzysztof Danielowski <danielow@cern.ch> - IFJ & AGH, Poland *
15  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
16  * Kamil Kraszewski <kalq@cern.ch> - IFJ & UJ, Poland *
17  * Maciej Kruk <mkruk@cern.ch> - IFJ & AGH, Poland *
18  * *
19  * Copyright (c) 2008: *
20  * IFJ-Krakow, Poland *
21  * CERN, Switzerland *
22  * MPI-K Heidelberg, Germany *
23  * *
24  * Redistribution and use in source and binary forms, with or without *
25  * modification, are permitted according to the terms listed in LICENSE *
26  * (http://tmva.sourceforge.net/LICENSE) *
27  **********************************************************************************/
28 
29 /*! \class TMVA::SimulatedAnnealingFitter
30 \ingroup TMVA
31 Fitter using a Simulated Annealing Algorithm
32 */
33 
35 
36 #include "TMVA/Configurable.h"
37 #include "TMVA/FitterBase.h"
38 #include "TMVA/Interval.h"
39 #include "TMVA/MsgLogger.h"
41 #include "TMVA/Types.h"
42 
43 #include "Rtypes.h"
44 #include "TString.h"
45 
46 ClassImp(TMVA::SimulatedAnnealingFitter);
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// constructor
50 
51 TMVA::SimulatedAnnealingFitter::SimulatedAnnealingFitter( IFitterTarget& target,
52  const TString& name,
53  const std::vector<Interval*>& ranges,
54  const TString& theOption )
55 : TMVA::FitterBase( target, name, ranges, theOption )
56 {
57  // default parameters settings for Simulated Annealing algorithm
58  DeclareOptions();
59  ParseOptions();
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// declare SA options.
64 ///
65 /// - MaxCalls <int> maximum number of calls for simulated annealing
66 /// - TemperatureGradient <float> temperature gradient for simulated annealing
67 /// - UseAdaptiveTemperature <bool> use of adaptive temperature for simulated annealing
68 /// - InitialTemperature <float> initial temperature for simulated annealing
69 /// - MinTemperature <float> minimum temperature for simulated annealing
70 /// - Eps <int> number of epochs for simulated annealing
71 /// - NFunLoops <int> number of loops for simulated annealing
72 /// - NEps <int> number of epochs for simulated annealing
73 
74 void TMVA::SimulatedAnnealingFitter::DeclareOptions()
75 {
76 
77  // default settings
78  fMaxCalls = 100000;
79  fInitialTemperature = 1e+6;
80  fMinTemperature = 1e-6;
81  fEps = 1e-10;
82  fTemperatureScale = 1.0;
83  fAdaptiveSpeed = 1.0;
84  fTemperatureAdaptiveStep = 0.009875;
85  fKernelTemperatureS = "IncAdaptive";
86  fUseDefaultScale = kFALSE;
87  fUseDefaultTemperature = kFALSE;
88 
89  DeclareOptionRef(fMaxCalls, "MaxCalls", "Maximum number of minimisation calls");
90  DeclareOptionRef(fInitialTemperature, "InitialTemp", "Initial temperature");
91  DeclareOptionRef(fMinTemperature, "MinTemp", "Minimum temperature");
92  DeclareOptionRef(fEps, "Eps", "Epsilon");
93  DeclareOptionRef(fTemperatureScale, "TempScale", "Temperature scale");
94  DeclareOptionRef(fAdaptiveSpeed, "AdaptiveSpeed", "Adaptive speed");
95  DeclareOptionRef(fTemperatureAdaptiveStep,"TempAdaptiveStep", "Step made in each generation temperature adaptive");
96  DeclareOptionRef(fUseDefaultScale, "UseDefaultScale", "Use default temperature scale for temperature minimisation algorithm");
97  DeclareOptionRef(fUseDefaultTemperature, "UseDefaultTemp", "Use default initial temperature");
98 
99  DeclareOptionRef(fKernelTemperatureS, "KernelTemp", "Temperature minimisation algorithm");
100  AddPreDefVal(TString("IncAdaptive"));
101  AddPreDefVal(TString("DecAdaptive"));
102  AddPreDefVal(TString("Sqrt"));
103  AddPreDefVal(TString("Log"));
104  AddPreDefVal(TString("Sin"));
105  AddPreDefVal(TString("Homo"));
106  AddPreDefVal(TString("Geo"));
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// set SA configuration parameters
111 
112 void TMVA::SimulatedAnnealingFitter::SetParameters( Int_t maxCalls,
113  Double_t initialTemperature,
114  Double_t minTemperature,
115  Double_t eps,
116  TString kernelTemperatureS,
117  Double_t temperatureScale,
118  Double_t temperatureAdaptiveStep,
119  Bool_t useDefaultScale,
120  Bool_t useDefaultTemperature)
121 {
122  fMaxCalls = maxCalls;
123  fInitialTemperature = initialTemperature;
124  fMinTemperature = minTemperature;
125  fEps = eps;
126  fKernelTemperatureS = kernelTemperatureS;
127  fTemperatureScale = temperatureScale;
128  fTemperatureAdaptiveStep = temperatureAdaptiveStep;
129  fUseDefaultScale = useDefaultScale;
130  fUseDefaultTemperature = useDefaultTemperature;
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Execute fitting
135 
136 Double_t TMVA::SimulatedAnnealingFitter::Run( std::vector<Double_t>& pars )
137 {
138  Log() << kHEADER << "<SimulatedAnnealingFitter> Optimisation, please be patient ... " << Endl;
139  Log() << kINFO << "(progress timing may be inaccurate for SA)" << Endl;
140 
141  SimulatedAnnealing sa( GetFitterTarget(), fRanges );
142 
143  // set driving parameters
144  sa.SetOptions( fMaxCalls, fInitialTemperature, fMinTemperature, fEps, fKernelTemperatureS,
145  fTemperatureScale, fAdaptiveSpeed, fTemperatureAdaptiveStep,
146  fUseDefaultScale, fUseDefaultTemperature );
147 
148  if (fIPyMaxIter){
149  *fIPyMaxIter = fMaxCalls;
150  sa.SetIPythonInteractive(fExitFromTraining, fIPyCurrentIter);
151  }
152  // minimise
153  Double_t fcn = sa.Minimize( pars );
154 
155  return fcn;
156 }