Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
GeneticRange.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Peter Speckmayer
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : TMVA::GeneticRange *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header for description) *
12  * *
13  * Authors (alphabetical): *
14  * Peter Speckmayer <speckmay@mail.cern.ch> - CERN, Switzerland *
15  * *
16  * Copyright (c) 2005: *
17  * CERN, Switzerland *
18  * MPI-K Heidelberg, Germany *
19  * *
20  * Redistribution and use in source and binary forms, with or without *
21  * modification, are permitted according to the terms listed in LICENSE *
22  * (http://tmva.sourceforge.net/LICENSE) *
23  * *
24  * File and Version Information: *
25  **********************************************************************************/
26 
27 /*! \class TMVA::GeneticRange
28 \ingroup TMVA
29 
30 Range definition for genetic algorithm.
31 
32 */
33 
34 #include "TRandom3.h"
35 
36 #include "TMVA/GeneticRange.h"
37 #include "TMVA/Interval.h"
38 
39 ClassImp(TMVA::GeneticRange);
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// defines the "f" (from) and "t" (to) of the coefficient
43 /// and takes a randomgenerator
44 
45 TMVA::GeneticRange::GeneticRange( TRandom3*rnd, Interval *interval )
46 {
47  fInterval = interval;
48 
49  fFrom = fInterval->GetMin();
50  fTo = fInterval->GetMax();
51  fNbins= fInterval->GetNbins();
52  fTotalLength = fTo-fFrom;
53 
54  fRandomGenerator = rnd;
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// creates a new random value for the coefficient; returns a discrete value
59 
60 Double_t TMVA::GeneticRange::RandomDiscrete()
61 {
62  Double_t value = fRandomGenerator->Uniform(0, 1);
63  return fInterval->GetElement( Int_t(value*fNbins) );
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// creates a new random value for the coefficient
68 /// Parameters:
69 /// - Bool_t near : takes a random value near the current value
70 /// - double value : this is the current value
71 /// - double spread : the sigma of the gaussian which is taken to calculate the new value
72 /// - Bool_t mirror : if the new value would be outside of the range, mirror = false
73 /// maps the value between the constraints by periodic boundary conditions.
74 /// With mirror = true, the value gets "reflected" on the boundaries.
75 
76 Double_t TMVA::GeneticRange::Random( Bool_t near, Double_t value, Double_t spread, Bool_t mirror )
77 {
78  if (fInterval->GetNbins() > 0) { // discrete interval
79  return RandomDiscrete();
80  }
81  else if (fFrom == fTo) {
82  return fFrom;
83  }
84  else if (near) {
85  Double_t ret;
86  ret = fRandomGenerator->Gaus( value, fTotalLength*spread );
87  if (mirror ) return ReMapMirror( ret );
88  else return ReMap( ret );
89  }
90  return fRandomGenerator->Uniform(fFrom, fTo);
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// remapping the value to the allowed space
95 
96 Double_t TMVA::GeneticRange::ReMap( Double_t val )
97 {
98  if (fFrom >= fTo ) return val;
99  if (val < fFrom ) return ReMap( (val-fFrom) + fTo );
100  if (val >= fTo ) return ReMap( (val-fTo) + fFrom );
101  return val;
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// remapping the value to the allowed space by reflecting on the boundaries
106 
107 Double_t TMVA::GeneticRange::ReMapMirror( Double_t val )
108 {
109  if (fFrom >= fTo ) return val;
110  if (val < fFrom ) return ReMap( fFrom - (val-fFrom) );
111  if (val >= fTo ) return ReMap( fTo - (val-fTo) );
112  return val;
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// destructor
117 
118 TMVA::GeneticRange::~GeneticRange()
119 {
120 }
121