50 TMVA::SimulatedAnnealing::SimulatedAnnealing( IFitterTarget& target, const std::vector<Interval*>& ranges )
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") ),
67 fKernelTemperature = kIncreasingAdaptive;
82 Bool_t useDefaultTemperature)
89 if (kernelTemperatureS ==
"IncreasingAdaptive") {
91 Log() <<
kINFO <<
"Using increasing adaptive algorithm" <<
Endl;
93 else if (kernelTemperatureS ==
"DecreasingAdaptive") {
95 Log() <<
kINFO <<
"Using decreasing adaptive algorithm" <<
Endl;
97 else if (kernelTemperatureS ==
"Sqrt") {
101 else if (kernelTemperatureS ==
"Homo") {
105 else if (kernelTemperatureS ==
"Log") {
109 else if (kernelTemperatureS ==
"Sin") {
133 for (
UInt_t rIter = 0; rIter < parameters.size(); rIter++) {
134 parameters[rIter] = fRandom->Uniform(0.0,1.0)*(fRanges[rIter]->GetMax() - fRanges[rIter]->GetMin()) + fRanges[rIter]->GetMin();
143 for (
UInt_t rIter = 0; rIter < from.size(); rIter++) to[rIter] = from[rIter];
152 ReWriteParameters( parameters, oldParameters );
154 for (
UInt_t rIter=0;rIter<parameters.size();rIter++) {
157 uni = fRandom->Uniform(0.0,1.0);
158 sign = (uni - 0.5 >= 0.0) ? (1.0) : (-1.0);
160 parameters[rIter] = oldParameters[rIter] + (fRanges[rIter]->GetMax()-fRanges[rIter]->GetMin())*0.1*distribution;
162 while (parameters[rIter] < fRanges[rIter]->GetMin() || parameters[rIter] > fRanges[rIter]->GetMax() );
170 std::vector<Double_t> newParameters( fRanges.size() );
172 for (
UInt_t rIter=0; rIter<parameters.size(); rIter++) {
175 uni = fRandom->Uniform(0.0,1.0);
176 sign = (uni - 0.5 >= 0.0) ? (1.0) : (-1.0);
178 newParameters[rIter] = parameters[rIter] + (fRanges[rIter]->GetMax()-fRanges[rIter]->GetMin())*0.1*distribution;
180 while (newParameters[rIter] < fRanges[rIter]->GetMin() || newParameters[rIter] > fRanges[rIter]->GetMax() );
183 return newParameters;
191 if (fKernelTemperature == kSqrt) {
192 currentTemperature = fInitialTemperature/(
Double_t)
TMath::Sqrt(Iter+2) * fTemperatureScale;
194 else if (fKernelTemperature == kLog) {
195 currentTemperature = fInitialTemperature/(
Double_t)
TMath::Log(Iter+2) * fTemperatureScale;
197 else if (fKernelTemperature == kHomo) {
198 currentTemperature = fInitialTemperature/(
Double_t)(Iter+2) * fTemperatureScale;
200 else if (fKernelTemperature == kSin) {
201 currentTemperature = (
TMath::Sin( (
Double_t)Iter / fTemperatureScale ) + 1.0 )/ (
Double_t)(Iter+1.0) * fInitialTemperature + fEps;
203 else if (fKernelTemperature == kGeo) {
204 currentTemperature = currentTemperature*fTemperatureScale;
206 else if (fKernelTemperature == kIncreasingAdaptive) {
207 currentTemperature = fMinTemperature + fTemperatureScale*
TMath::Log(1.0+fProgress*fAdaptiveSpeed);
209 else if (fKernelTemperature == kDecreasingAdaptive) {
210 currentTemperature = currentTemperature*fTemperatureScale;
220 if (currentTemperature < fEps)
return kFALSE;
222 Double_t prob = fRandom->Uniform(0.0, 1.0);
231 if (fKernelTemperature == kSqrt) fTemperatureScale = 1.0;
232 else if (fKernelTemperature == kLog) fTemperatureScale = 1.0;
233 else if (fKernelTemperature == kHomo) fTemperatureScale = 1.0;
234 else if (fKernelTemperature == kSin) fTemperatureScale = 20.0;
235 else if (fKernelTemperature == kGeo) fTemperatureScale = 0.99997;
236 else if (fKernelTemperature == kDecreasingAdaptive) {
237 fTemperatureScale = 1.0;
240 fTemperatureScale -= 0.000001;
243 else if (fKernelTemperature == kIncreasingAdaptive) fTemperatureScale = 0.15*( 1.0 / (
Double_t)(fRanges.size() ) );
254 Double_t t, dT, cold, delta, deltaY,
y, yNew, yBest, yOld;
255 std::vector<Double_t>
x( fRanges.size() ), xNew( fRanges.size() ), xBest( fRanges.size() ), xOld( fRanges.size() );
258 dT = fTemperatureAdaptiveStep;
259 for (
UInt_t rIter = 0; rIter <
x.size(); rIter++)
260 x[rIter] = ( fRanges[rIter]->GetMax() + fRanges[rIter]->GetMin() ) / 2.0;
262 for (
Int_t i=0; i<fMaxCalls/50; i++) {
263 if ((i>0) && (deltaY>0.0)) {
268 x = xOld = GenerateNeighbour(
x,t);
269 y = yOld = fFitterTarget.EstimatorFunction( xOld );
271 for (
Int_t k=0; (k<30) && (equilibrium<=12); k++ ) {
272 xNew = GenerateNeighbour(
x,t);
274 yNew = fFitterTarget.EstimatorFunction( xNew );
284 if (y != 0.0) delta /=
y;
285 else if (yNew != 0.0) delta /=
y;
288 if (delta < 0.1) equilibrium++;
289 else equilibrium = 0;
295 yNew = fFitterTarget.EstimatorFunction( xNew );
296 deltaY = yNew - yOld;
297 if ( (deltaY < 0.0 )&&( yNew < yBest)) {
302 if ((stopper) && (deltaY >= (100.0 * cold)))
break;
313 std::vector<Double_t> bestParameters(fRanges.size());
314 std::vector<Double_t> oldParameters (fRanges.size());
316 Double_t currentTemperature, bestFit, currentFit;
321 if (fUseDefaultTemperature) {
322 if (fKernelTemperature == kIncreasingAdaptive) {
323 fMinTemperature = currentTemperature = 1e-06;
324 FillWithRandomValues( parameters );
326 else fInitialTemperature = currentTemperature = GenerateMaxTemperature( parameters );
329 if (fKernelTemperature == kIncreasingAdaptive)
330 currentTemperature = fMinTemperature;
332 currentTemperature = fInitialTemperature;
333 FillWithRandomValues( parameters );
336 if (fUseDefaultScale) SetDefaultScale();
339 <<
"Temperatur scale = " << fTemperatureScale
340 <<
", current temperature = " << currentTemperature <<
Endl;
342 bestParameters = parameters;
343 bestFit = currentFit = fFitterTarget.EstimatorFunction( bestParameters );
345 optimizeCalls = fMaxCalls/100;
346 generalCalls = fMaxCalls - optimizeCalls;
349 Timer timer( fMaxCalls, fLogger->GetSource().c_str() );
351 for (
Int_t sample = 0; sample < generalCalls; sample++) {
352 GenerateNeighbour( parameters, oldParameters, currentTemperature );
353 Double_t localFit = fFitterTarget.EstimatorFunction( parameters );
355 if (localFit < currentFit ||
TMath::Abs(currentFit-localFit) < fEps) {
366 currentFit = localFit;
368 if (currentFit < bestFit) {
369 ReWriteParameters( parameters, bestParameters );
370 bestFit = currentFit;
374 if (!ShouldGoIn(localFit, currentFit, currentTemperature))
375 ReWriteParameters( oldParameters, parameters );
377 currentFit = localFit;
383 GenerateNewTemperature( currentTemperature, sample );
385 if ((fMaxCalls<100) || sample%
Int_t(fMaxCalls/100.0) == 0)
timer.DrawProgressBar( sample );
394 Double_t startingTemperature = fMinTemperature*(fRanges.size())*2.0;
395 currentTemperature = startingTemperature;
398 for (
Int_t sample=0;sample<optimizeCalls;sample++) {
399 GenerateNeighbour( parameters, oldParameters, currentTemperature );
400 Double_t localFit = fFitterTarget.EstimatorFunction( parameters );
402 if (localFit < currentFit) {
403 currentFit = localFit;
406 if (currentFit < bestFit) {
407 ReWriteParameters( parameters, bestParameters );
408 bestFit = currentFit;
411 else ReWriteParameters( oldParameters, parameters );
413 currentTemperature-=(startingTemperature - fEps)/optimizeCalls;
416 ReWriteParameters( bestParameters, parameters );
Double_t GenerateMaxTemperature(std::vector< Double_t > ¶meters)
maximum temperature
void FillWithRandomValues(std::vector< Double_t > ¶meters)
random starting parameters
void GenerateNewTemperature(Double_t ¤tTemperature, Int_t Iter)
generate new temperature
Random number generator class based on M.
MsgLogger & Endl(MsgLogger &ml)
int equals(Double_t n1, Double_t n2, double ERRORLIMIT=1.E-10)
ClassImp(TMVA::SimulatedAnnealing) TMVA
constructor
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
void GenerateNeighbour(std::vector< Double_t > ¶meters, std::vector< Double_t > &oldParameters, Double_t currentTemperature)
generate adjacent parameters
static void swap(double &a, double &b)
Double_t fTemperatureAdaptiveStep
Bool_t fUseDefaultTemperature
virtual ~SimulatedAnnealing()
destructor
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
Bool_t ShouldGoIn(Double_t currentFit, Double_t localFit, Double_t currentTemperature)
result checker
enum TMVA::SimulatedAnnealing::EKernelTemperature fKernelTemperature
Double_t Minimize(std::vector< Double_t > ¶meters)
minimisation algorithm
Double_t fInitialTemperature
Double_t Sqrt(Double_t x)
Double_t fTemperatureScale
void ReWriteParameters(std::vector< Double_t > &from, std::vector< Double_t > &to)
copy parameters