Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
GeneticPopulation.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::GeneticPopulation *
8 * *
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 * (see tmva/doc/LICENSE) *
23 **********************************************************************************/
24
25/*! \class TMVA::GeneticPopulation
26\ingroup TMVA
27
28Population definition for genetic algorithm.
29
30*/
31
32#include <algorithm>
33
34#include "Rstrstream.h"
35#include "TRandom3.h"
36#include "TH1.h"
37
39#include "TMVA/GeneticGenes.h"
40#include "TMVA/MsgLogger.h"
41
42
43using std::vector, std::ostream;
44
45////////////////////////////////////////////////////////////////////////////////
46/// Constructor
47
48TMVA::GeneticPopulation::GeneticPopulation(const std::vector<Interval*>& ranges, Int_t size, UInt_t seed)
49 : fGenePool(size),
50 fRanges(ranges.size()),
51 fLogger( new MsgLogger("GeneticPopulation") )
52{
53 // create a randomGenerator for this population and set a seed
54 // create the genePools
55 //
56 fRandomGenerator = new TRandom3( 100 ); //please check
59
60 for ( unsigned int i = 0; i < ranges.size(); ++i )
61 fRanges[i] = new TMVA::GeneticRange( fRandomGenerator, ranges[i] );
62
64 for ( int i = 0; i < size; ++i )
65 {
66 for ( unsigned int rIt = 0; rIt < fRanges.size(); ++rIt )
67 newEntry[rIt] = fRanges[rIt]->Random();
69 }
70
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// destructor
76
78{
79 if (fRandomGenerator != NULL) delete fRandomGenerator;
80
81 std::vector<GeneticRange*>::iterator it = fRanges.begin();
82 for (;it!=fRanges.end(); ++it) delete *it;
83
84 delete fLogger;
85}
86
87
88
89////////////////////////////////////////////////////////////////////////////////
90/// the random seed of the random generator
91
93{
94 fRandomGenerator->SetSeed( seed );
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Produces offspring which is are copies of their parents.
99///
100/// Parameters:
101/// - int number : the number of the last individual to be copied
102
104{
105 int i=0;
106 for (std::vector<TMVA::GeneticGenes>::iterator it = fGenePool.begin();
107 it != fGenePool.end() && i < number;
108 ++it, ++i ) {
109 GiveHint( it->GetFactors(), it->GetFitness() );
110 }
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Creates children out of members of the current generation.
115///
116/// Children have a combination of the coefficients of their parents
117
119{
120#ifdef _GLIBCXX_PARALLEL
121#pragma omp parallel
122#pragma omp for
123#endif
124 for ( int it = 0; it < (int) (fGenePool.size() / 2); ++it )
125 {
126 Int_t pos = (Int_t)fRandomGenerator->Integer( fGenePool.size()/2 );
127 fGenePool[(fGenePool.size() / 2) + it] = MakeSex( fGenePool[it], fGenePool[pos] );
128 }
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// this function takes two individuals and produces offspring by mixing
133/// (recombining) their coefficients.
134
137{
138 vector< Double_t > child(fRanges.size());
139 for (unsigned int i = 0; i < fRanges.size(); ++i) {
140 if (fRandomGenerator->Integer( 2 ) == 0) {
141 child[i] = male.GetFactors()[i];
142 }else{
143 child[i] = female.GetFactors()[i];
144 }
145 }
146 return TMVA::GeneticGenes( child );
147}
148
149////////////////////////////////////////////////////////////////////////////////
150/// Mutates the individuals in the genePool.
151///
152/// Parameters:
153///
154/// - double probability : gives the probability (in percent) of a mutation of a coefficient
155/// - int startIndex : leaves unchanged (without mutation) the individuals which are better ranked
156/// than indicated by "startIndex". This means: if "startIndex==3", the first (and best)
157/// three individuals are not mutated. This allows to preserve the best result of the
158/// current Generation for the next generation.
159/// - Bool_t near : if true, the mutation will produce a new coefficient which is "near" the old one
160/// (gaussian around the current value)
161/// - double spread : if near==true, spread gives the sigma of the gaussian
162/// - Bool_t mirror : if the new value obtained would be outside of the given constraints
163/// the value is mapped between the constraints again. This can be done either
164/// by a kind of periodic boundary conditions or mirrored at the boundary.
165/// (mirror = true seems more "natural")
166
169{
170 vector< Double_t>::iterator vec;
171 vector< TMVA::GeneticRange* >::iterator vecRange;
172
173 //#ifdef _GLIBCXX_PARALLEL
174 // #pragma omp parallel
175 // #pragma omp for
176 //#endif
177 // The range methods are not thread safe!
178 for (int it = startIndex; it < (int) fGenePool.size(); ++it) {
179 vecRange = fRanges.begin();
180 for (vec = (fGenePool[it].GetFactors()).begin(); vec < (fGenePool[it].GetFactors()).end(); ++vec) {
181 if (fRandomGenerator->Uniform( 100 ) <= probability) {
182 (*vec) = (*vecRange)->Random( near, (*vec), spread, mirror );
183 }
184 ++vecRange;
185 }
186 }
187}
188
189
190////////////////////////////////////////////////////////////////////////////////
191/// gives back the "Genes" of the population with the given index.
192
197
198////////////////////////////////////////////////////////////////////////////////
199/// make a little printout of the individuals up to index "untilIndex"
200/// this means, .. write out the best "untilIndex" individuals.
201
203{
204 for ( unsigned int it = 0; it < fGenePool.size(); ++it )
205 {
206 Int_t n=0;
207 if (untilIndex >= -1 ) {
208 if (untilIndex == -1 ) return;
209 untilIndex--;
210 }
211 Log() << "fitness: " << fGenePool[it].GetFitness() << " ";
212 for (vector< Double_t >::iterator vec = fGenePool[it].GetFactors().begin();
213 vec < fGenePool[it].GetFactors().end(); ++vec ) {
214 Log() << "f_" << n++ << ": " << (*vec) << " ";
215 }
216 Log() << Endl;
217 }
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// make a little printout to the stream "out" of the individuals up to index "untilIndex"
222/// this means, .. write out the best "untilIndex" individuals.
223
225{
226 for ( unsigned int it = 0; it < fGenePool.size(); ++it ) {
227 Int_t n=0;
228 if (untilIndex >= -1 ) {
229 if (untilIndex == -1 ) return;
230 untilIndex--;
231 }
232 out << "fitness: " << fGenePool[it].GetFitness() << " ";
233 for (vector< Double_t >::iterator vec = fGenePool[it].GetFactors().begin();
234 vec < fGenePool[it].GetFactors().end(); ++vec ) {
235 out << "f_" << n++ << ": " << (*vec) << " ";
236 }
237 out << std::endl;
238 }
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// give back a histogram with the distribution of the coefficients.
243///
244/// Parameters:
245///
246/// - int bins : number of bins of the histogram
247/// - int min : histogram minimum
248/// - int max : maximum value of the histogram
249
251 Int_t min, Int_t max )
252{
253 std::cout << "FAILED! TMVA::GeneticPopulation::VariableDistribution" << std::endl;
254
255 std::stringstream histName;
256 histName.clear();
257 histName.str("v");
258 histName << varNumber;
259 TH1F *hist = new TH1F( histName.str().c_str(),histName.str().c_str(), bins,min,max );
260
261 return hist;
262}
263
264////////////////////////////////////////////////////////////////////////////////
265/// gives back all the values of coefficient "varNumber" of the current generation
266
268{
269 std::cout << "FAILED! TMVA::GeneticPopulation::VariableDistribution" << std::endl;
270
271 vector< Double_t > varDist;
272
273 return varDist;
274}
275
276////////////////////////////////////////////////////////////////////////////////
277/// add another population (strangers) to the one of this GeneticPopulation
278
280{
281 for (std::vector<TMVA::GeneticGenes>::iterator it = strangers->fGenePool.begin();
282 it != strangers->fGenePool.end(); ++it ) {
283 GiveHint( it->GetFactors(), it->GetFitness() );
284 }
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// add another population (strangers) to the one of this GeneticPopulation
289
294
295////////////////////////////////////////////////////////////////////////////////
296/// trim the population to the predefined size
297
299{
300 std::sort(fGenePool.begin(), fGenePool.end());
301 while ( fGenePool.size() > (unsigned int) fPopulationSizeLimit )
302 fGenePool.pop_back();
303}
304
305////////////////////////////////////////////////////////////////////////////////
306/// add an individual (a set of variables) to the population
307/// if there is a set of variables which is known to perform good, they can be given as a hint to the population
308
310{
312 g.SetFitness(fitness);
313
314 fGenePool.push_back( g );
315}
316
317////////////////////////////////////////////////////////////////////////////////
318/// sort the genepool according to the fitness of the individuals
319
321{
322 std::sort(fGenePool.begin(), fGenePool.end());
323}
324
#define g(i)
Definition RSha256.hxx:105
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t child
const_iterator begin() const
const_iterator end() const
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:879
Cut optimisation interface class for genetic algorithm.
Population definition for genetic algorithm.
void Mutate(Double_t probability=20, Int_t startIndex=0, Bool_t near=kFALSE, Double_t spread=0.1, Bool_t mirror=kFALSE)
Mutates the individuals in the genePool.
virtual ~GeneticPopulation()
destructor
std::vector< TMVA::GeneticRange * > fRanges
contains the ranges in between the values of the coefficients have to be
TRandom3 * fRandomGenerator
random Generator for this population
void Sort()
sort the genepool according to the fitness of the individuals
void MakeCopies(int number)
Produces offspring which is are copies of their parents.
void TrimPopulation()
trim the population to the predefined size
GeneticGenes * GetGenes(Int_t index)
gives back the "Genes" of the population with the given index.
GeneticPopulation(const std::vector< TMVA::Interval * > &ranges, Int_t size, UInt_t seed=0)
Constructor.
void Print(Int_t untilIndex=-1)
make a little printout of the individuals up to index "untilIndex" this means, .
void MakeChildren()
Creates children out of members of the current generation.
void GiveHint(std::vector< Double_t > &hint, Double_t fitness=0)
add an individual (a set of variables) to the population if there is a set of variables which is know...
std::vector< TMVA::GeneticGenes > fGenePool
the "genePool" where the individuals of the current generation are stored
void AddPopulation(GeneticPopulation *strangers)
add another population (strangers) to the one of this GeneticPopulation
void SetRandomSeed(UInt_t seed=0)
the random seed of the random generator
GeneticGenes MakeSex(GeneticGenes male, GeneticGenes female)
this function takes two individuals and produces offspring by mixing (recombining) their coefficients...
TH1F * VariableDistribution(Int_t varNumber, Int_t bins, Int_t min, Int_t max)
give back a histogram with the distribution of the coefficients.
Range definition for genetic algorithm.
ostringstream derivative to redirect and format output
Definition MsgLogger.h:57
Random number generator class based on M.
Definition TRandom3.h:27
void SetSeed(ULong_t seed=0) override
Set the random generator sequence.
Definition TRandom3.cxx:216
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:681
const Int_t n
Definition legend1.C:16
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148