51: fKernelTemperature (kIncreasingAdaptive),
52 fFitterTarget ( target ),
56 fInitialTemperature ( 1000 ),
57 fMinTemperature ( 0 ),
59 fTemperatureScale ( 0.06 ),
60 fAdaptiveSpeed ( 1.0 ),
61 fTemperatureAdaptiveStep( 0.0 ),
62 fUseDefaultScale (
kFALSE ),
63 fUseDefaultTemperature (
kFALSE ),
64 fLogger( new
MsgLogger(
"SimulatedAnnealing") ),
82 Bool_t useDefaultTemperature)
85 fInitialTemperature = initialTemperature;
86 fMinTemperature = minTemperature;
89 if (kernelTemperatureS ==
"IncreasingAdaptive") {
90 fKernelTemperature = kIncreasingAdaptive;
91 Log() << kINFO <<
"Using increasing adaptive algorithm" <<
Endl;
93 else if (kernelTemperatureS ==
"DecreasingAdaptive") {
94 fKernelTemperature = kDecreasingAdaptive;
95 Log() << kINFO <<
"Using decreasing adaptive algorithm" <<
Endl;
97 else if (kernelTemperatureS ==
"Sqrt") {
98 fKernelTemperature = kSqrt;
99 Log() << kINFO <<
"Using \"Sqrt\" algorithm" <<
Endl;
101 else if (kernelTemperatureS ==
"Homo") {
102 fKernelTemperature = kHomo;
103 Log() << kINFO <<
"Using \"Homo\" algorithm" <<
Endl;
105 else if (kernelTemperatureS ==
"Log") {
106 fKernelTemperature = kLog;
107 Log() << kINFO <<
"Using \"Log\" algorithm" <<
Endl;
109 else if (kernelTemperatureS ==
"Sin") {
110 fKernelTemperature = kSin;
111 Log() << kINFO <<
"Using \"Sin\" algorithm" <<
Endl;
114 fTemperatureScale = temperatureScale;
115 fAdaptiveSpeed = adaptiveSpeed;
116 fTemperatureAdaptiveStep = temperatureAdaptiveStep;
118 fUseDefaultScale = useDefaultScale;
119 fUseDefaultTemperature = useDefaultTemperature;
134 for (
UInt_t rIter = 0; rIter < parameters.size(); rIter++) {
135 parameters[rIter] = fRandom->Uniform(0.0,1.0)*(fRanges[rIter]->GetMax() - fRanges[rIter]->GetMin()) + fRanges[rIter]->GetMin();
144 for (
UInt_t rIter = 0; rIter < from.size(); rIter++) to[rIter] = from[rIter];
153 ReWriteParameters( parameters, oldParameters );
155 for (
UInt_t rIter=0;rIter<parameters.size();rIter++) {
158 uni = fRandom->Uniform(0.0,1.0);
159 sign = (uni - 0.5 >= 0.0) ? (1.0) : (-1.0);
160 distribution = currentTemperature * (
TMath::Power(1.0 + 1.0/currentTemperature,
TMath::Abs(2.0*uni - 1.0)) -1.0)*sign;
161 parameters[rIter] = oldParameters[rIter] + (fRanges[rIter]->GetMax()-fRanges[rIter]->GetMin())*0.1*distribution;
163 while (parameters[rIter] < fRanges[rIter]->GetMin() || parameters[rIter] > fRanges[rIter]->GetMax() );
171 std::vector<Double_t> newParameters( fRanges.size() );
173 for (
UInt_t rIter=0; rIter<parameters.size(); rIter++) {
176 uni = fRandom->Uniform(0.0,1.0);
177 sign = (uni - 0.5 >= 0.0) ? (1.0) : (-1.0);
178 distribution = currentTemperature * (
TMath::Power(1.0 + 1.0/currentTemperature,
TMath::Abs(2.0*uni - 1.0)) -1.0)*sign;
179 newParameters[rIter] = parameters[rIter] + (fRanges[rIter]->GetMax()-fRanges[rIter]->GetMin())*0.1*distribution;
181 while (newParameters[rIter] < fRanges[rIter]->GetMin() || newParameters[rIter] > fRanges[rIter]->GetMax() );
184 return newParameters;
192 if (fKernelTemperature == kSqrt) {
193 currentTemperature = fInitialTemperature/(
Double_t)
TMath::Sqrt(Iter+2) * fTemperatureScale;
195 else if (fKernelTemperature == kLog) {
196 currentTemperature = fInitialTemperature/(
Double_t)
TMath::Log(Iter+2) * fTemperatureScale;
198 else if (fKernelTemperature == kHomo) {
199 currentTemperature = fInitialTemperature/(
Double_t)(Iter+2) * fTemperatureScale;
201 else if (fKernelTemperature == kSin) {
202 currentTemperature = (
TMath::Sin( (
Double_t)Iter / fTemperatureScale ) + 1.0 )/ (
Double_t)(Iter+1.0) * fInitialTemperature + fEps;
204 else if (fKernelTemperature == kGeo) {
205 currentTemperature = currentTemperature*fTemperatureScale;
207 else if (fKernelTemperature == kIncreasingAdaptive) {
208 currentTemperature = fMinTemperature + fTemperatureScale*
TMath::Log(1.0+fProgress*fAdaptiveSpeed);
210 else if (fKernelTemperature == kDecreasingAdaptive) {
211 currentTemperature = currentTemperature*fTemperatureScale;
213 else Log() << kFATAL <<
"No such kernel!" <<
Endl;
221 if (currentTemperature < fEps)
return kFALSE;
223 Double_t prob = fRandom->Uniform(0.0, 1.0);
232 if (fKernelTemperature == kSqrt) fTemperatureScale = 1.0;
233 else if (fKernelTemperature == kLog) fTemperatureScale = 1.0;
234 else if (fKernelTemperature == kHomo) fTemperatureScale = 1.0;
235 else if (fKernelTemperature == kSin) fTemperatureScale = 20.0;
236 else if (fKernelTemperature == kGeo) fTemperatureScale = 0.99997;
237 else if (fKernelTemperature == kDecreasingAdaptive) {
238 fTemperatureScale = 1.0;
241 fTemperatureScale -= 0.000001;
244 else if (fKernelTemperature == kIncreasingAdaptive) fTemperatureScale = 0.15*( 1.0 / (
Double_t)(fRanges.size() ) );
245 else Log() << kFATAL <<
"No such kernel!" <<
Endl;
255 Double_t t, dT, cold, delta, deltaY,
y, yNew, yBest, yOld;
256 std::vector<Double_t>
x( fRanges.size() ), xNew( fRanges.size() ), xBest( fRanges.size() ), xOld( fRanges.size() );
259 dT = fTemperatureAdaptiveStep;
260 for (
UInt_t rIter = 0; rIter <
x.size(); rIter++)
261 x[rIter] = ( fRanges[rIter]->GetMax() + fRanges[rIter]->GetMin() ) / 2.0;
263 for (
Int_t i=0; i<fMaxCalls/50; i++) {
264 if ((i>0) && (deltaY>0.0)) {
269 x = xOld = GenerateNeighbour(
x,t);
270 y = yOld = fFitterTarget.EstimatorFunction( xOld );
272 for (
Int_t k=0; (k<30) && (equilibrium<=12); k++ ) {
273 xNew = GenerateNeighbour(
x,t);
275 yNew = fFitterTarget.EstimatorFunction( xNew );
285 if (
y != 0.0) delta /=
y;
286 else if (yNew != 0.0) delta /=
y;
289 if (delta < 0.1) equilibrium++;
290 else equilibrium = 0;
296 yNew = fFitterTarget.EstimatorFunction( xNew );
297 deltaY = yNew - yOld;
298 if ( (deltaY < 0.0 )&&( yNew < yBest)) {
303 if ((stopper) && (deltaY >= (100.0 * cold)))
break;
314 std::vector<Double_t> bestParameters(fRanges.size());
315 std::vector<Double_t> oldParameters (fRanges.size());
317 Double_t currentTemperature, bestFit, currentFit;
318 Int_t optimizeCalls, generalCalls, equals;
322 if (fUseDefaultTemperature) {
323 if (fKernelTemperature == kIncreasingAdaptive) {
324 fMinTemperature = currentTemperature = 1
e-06;
325 FillWithRandomValues( parameters );
327 else fInitialTemperature = currentTemperature = GenerateMaxTemperature( parameters );
330 if (fKernelTemperature == kIncreasingAdaptive)
331 currentTemperature = fMinTemperature;
333 currentTemperature = fInitialTemperature;
334 FillWithRandomValues( parameters );
337 if (fUseDefaultScale) SetDefaultScale();
340 <<
"Temperatur scale = " << fTemperatureScale
341 <<
", current temperature = " << currentTemperature <<
Endl;
343 bestParameters = parameters;
344 bestFit = currentFit = fFitterTarget.EstimatorFunction( bestParameters );
346 optimizeCalls = fMaxCalls/100;
347 generalCalls = fMaxCalls - optimizeCalls;
350 Timer timer( fMaxCalls, fLogger->GetSource().c_str() );
352 for (
Int_t sample = 0; sample < generalCalls; sample++) {
353 if (fIPyCurrentIter) *fIPyCurrentIter = sample;
354 if (fExitFromTraining && *fExitFromTraining)
break;
355 GenerateNeighbour( parameters, oldParameters, currentTemperature );
356 Double_t localFit = fFitterTarget.EstimatorFunction( parameters );
358 if (localFit < currentFit ||
TMath::Abs(currentFit-localFit) < fEps) {
369 currentFit = localFit;
371 if (currentFit < bestFit) {
372 ReWriteParameters( parameters, bestParameters );
373 bestFit = currentFit;
377 if (!ShouldGoIn(localFit, currentFit, currentTemperature))
378 ReWriteParameters( oldParameters, parameters );
380 currentFit = localFit;
386 GenerateNewTemperature( currentTemperature, sample );
397 Double_t startingTemperature = fMinTemperature*(fRanges.size())*2.0;
398 currentTemperature = startingTemperature;
401 for (
Int_t sample=0;sample<optimizeCalls;sample++) {
402 GenerateNeighbour( parameters, oldParameters, currentTemperature );
403 Double_t localFit = fFitterTarget.EstimatorFunction( parameters );
405 if (localFit < currentFit) {
406 currentFit = localFit;
409 if (currentFit < bestFit) {
410 ReWriteParameters( parameters, bestParameters );
411 bestFit = currentFit;
414 else ReWriteParameters( oldParameters, parameters );
416 currentTemperature-=(startingTemperature - fEps)/optimizeCalls;
419 ReWriteParameters( bestParameters, parameters );
Interface for a fitter 'target'.
ostringstream derivative to redirect and format output
Base implementation of simulated annealing fitting procedure.
void GenerateNeighbour(std::vector< Double_t > ¶meters, std::vector< Double_t > &oldParameters, Double_t currentTemperature)
generate adjacent parameters
Double_t Minimize(std::vector< Double_t > ¶meters)
minimisation algorithm
virtual ~SimulatedAnnealing()
destructor
Double_t GenerateMaxTemperature(std::vector< Double_t > ¶meters)
maximum temperature
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)
option setter
void SetDefaultScale()
setting of default scale
void GenerateNewTemperature(Double_t ¤tTemperature, Int_t Iter)
generate new temperature
void FillWithRandomValues(std::vector< Double_t > ¶meters)
random starting parameters
void ReWriteParameters(std::vector< Double_t > &from, std::vector< Double_t > &to)
copy parameters
enum TMVA::SimulatedAnnealing::EKernelTemperature fKernelTemperature
SimulatedAnnealing(IFitterTarget &target, const std::vector< TMVA::Interval * > &ranges)
constructor
Bool_t ShouldGoIn(Double_t currentFit, Double_t localFit, Double_t currentTemperature)
result checker
Timing information for training and evaluation of MVA methods.
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
void DrawProgressBar(Int_t, const TString &comment="")
draws progress bar in color or B&W caution:
Random number generator class based on M.
MsgLogger & Endl(MsgLogger &ml)
Double_t Sqrt(Double_t x)
LongDouble_t Power(LongDouble_t x, LongDouble_t y)