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
43
44using std::vector, std::ostream;
45
46////////////////////////////////////////////////////////////////////////////////
47/// Constructor
48
49TMVA::GeneticPopulation::GeneticPopulation(const std::vector<Interval*>& ranges, Int_t size, UInt_t seed)
50 : fGenePool(size),
51 fRanges(ranges.size()),
52 fLogger( new MsgLogger("GeneticPopulation") )
53{
54 // create a randomGenerator for this population and set a seed
55 // create the genePools
56 //
57 fRandomGenerator = new TRandom3( 100 ); //please check
60
61 for ( unsigned int i = 0; i < ranges.size(); ++i )
62 fRanges[i] = new TMVA::GeneticRange( fRandomGenerator, ranges[i] );
63
64 vector<Double_t> newEntry( fRanges.size() );
65 for ( int i = 0; i < size; ++i )
66 {
67 for ( unsigned int rIt = 0; rIt < fRanges.size(); ++rIt )
68 newEntry[rIt] = fRanges[rIt]->Random();
69 fGenePool[i] = TMVA::GeneticGenes( newEntry);
70 }
71
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// destructor
77
79{
80 if (fRandomGenerator != NULL) delete fRandomGenerator;
81
82 std::vector<GeneticRange*>::iterator it = fRanges.begin();
83 for (;it!=fRanges.end(); ++it) delete *it;
84
85 delete fLogger;
86}
87
88
89
90////////////////////////////////////////////////////////////////////////////////
91/// the random seed of the random generator
92
94{
95 fRandomGenerator->SetSeed( seed );
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Produces offspring which is are copies of their parents.
100///
101/// Parameters:
102/// - int number : the number of the last individual to be copied
103
105{
106 int i=0;
107 for (std::vector<TMVA::GeneticGenes>::iterator it = fGenePool.begin();
108 it != fGenePool.end() && i < number;
109 ++it, ++i ) {
110 GiveHint( it->GetFactors(), it->GetFitness() );
111 }
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Creates children out of members of the current generation.
116///
117/// Children have a combination of the coefficients of their parents
118
120{
121#ifdef _GLIBCXX_PARALLEL
122#pragma omp parallel
123#pragma omp for
124#endif
125 for ( int it = 0; it < (int) (fGenePool.size() / 2); ++it )
126 {
127 Int_t pos = (Int_t)fRandomGenerator->Integer( fGenePool.size()/2 );
128 fGenePool[(fGenePool.size() / 2) + it] = MakeSex( fGenePool[it], fGenePool[pos] );
129 }
130}
131
132////////////////////////////////////////////////////////////////////////////////
133/// this function takes two individuals and produces offspring by mixing
134/// (recombining) their coefficients.
135
137 TMVA::GeneticGenes female )
138{
139 vector< Double_t > child(fRanges.size());
140 for (unsigned int i = 0; i < fRanges.size(); ++i) {
141 if (fRandomGenerator->Integer( 2 ) == 0) {
142 child[i] = male.GetFactors()[i];
143 }else{
144 child[i] = female.GetFactors()[i];
145 }
146 }
147 return TMVA::GeneticGenes( child );
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Mutates the individuals in the genePool.
152///
153/// Parameters:
154///
155/// - double probability : gives the probability (in percent) of a mutation of a coefficient
156/// - int startIndex : leaves unchanged (without mutation) the individuals which are better ranked
157/// than indicated by "startIndex". This means: if "startIndex==3", the first (and best)
158/// three individuals are not mutated. This allows to preserve the best result of the
159/// current Generation for the next generation.
160/// - Bool_t near : if true, the mutation will produce a new coefficient which is "near" the old one
161/// (gaussian around the current value)
162/// - double spread : if near==true, spread gives the sigma of the gaussian
163/// - Bool_t mirror : if the new value obtained would be outside of the given constraints
164/// the value is mapped between the constraints again. This can be done either
165/// by a kind of periodic boundary conditions or mirrored at the boundary.
166/// (mirror = true seems more "natural")
167
168void TMVA::GeneticPopulation::Mutate( Double_t probability , Int_t startIndex,
169 Bool_t near, Double_t spread, Bool_t mirror )
170{
171 vector< Double_t>::iterator vec;
172 vector< TMVA::GeneticRange* >::iterator vecRange;
173
174 //#ifdef _GLIBCXX_PARALLEL
175 // #pragma omp parallel
176 // #pragma omp for
177 //#endif
178 // The range methods are not thread safe!
179 for (int it = startIndex; it < (int) fGenePool.size(); ++it) {
180 vecRange = fRanges.begin();
181 for (vec = (fGenePool[it].GetFactors()).begin(); vec < (fGenePool[it].GetFactors()).end(); ++vec) {
182 if (fRandomGenerator->Uniform( 100 ) <= probability) {
183 (*vec) = (*vecRange)->Random( near, (*vec), spread, mirror );
184 }
185 ++vecRange;
186 }
187 }
188}
189
190
191////////////////////////////////////////////////////////////////////////////////
192/// gives back the "Genes" of the population with the given index.
193
195{
196 return &(fGenePool[index]);
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// make a little printout of the individuals up to index "untilIndex"
201/// this means, .. write out the best "untilIndex" individuals.
202
204{
205 for ( unsigned int it = 0; it < fGenePool.size(); ++it )
206 {
207 Int_t n=0;
208 if (untilIndex >= -1 ) {
209 if (untilIndex == -1 ) return;
210 untilIndex--;
211 }
212 Log() << "fitness: " << fGenePool[it].GetFitness() << " ";
213 for (vector< Double_t >::iterator vec = fGenePool[it].GetFactors().begin();
214 vec < fGenePool[it].GetFactors().end(); ++vec ) {
215 Log() << "f_" << n++ << ": " << (*vec) << " ";
216 }
217 Log() << Endl;
218 }
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// make a little printout to the stream "out" of the individuals up to index "untilIndex"
223/// this means, .. write out the best "untilIndex" individuals.
224
225void TMVA::GeneticPopulation::Print( ostream & out, Int_t untilIndex )
226{
227 for ( unsigned int it = 0; it < fGenePool.size(); ++it ) {
228 Int_t n=0;
229 if (untilIndex >= -1 ) {
230 if (untilIndex == -1 ) return;
231 untilIndex--;
232 }
233 out << "fitness: " << fGenePool[it].GetFitness() << " ";
234 for (vector< Double_t >::iterator vec = fGenePool[it].GetFactors().begin();
235 vec < fGenePool[it].GetFactors().end(); ++vec ) {
236 out << "f_" << n++ << ": " << (*vec) << " ";
237 }
238 out << std::endl;
239 }
240}
241
242////////////////////////////////////////////////////////////////////////////////
243/// give back a histogram with the distribution of the coefficients.
244///
245/// Parameters:
246///
247/// - int bins : number of bins of the histogram
248/// - int min : histogram minimum
249/// - int max : maximum value of the histogram
250
252 Int_t min, Int_t max )
253{
254 std::cout << "FAILED! TMVA::GeneticPopulation::VariableDistribution" << std::endl;
255
256 std::stringstream histName;
257 histName.clear();
258 histName.str("v");
259 histName << varNumber;
260 TH1F *hist = new TH1F( histName.str().c_str(),histName.str().c_str(), bins,min,max );
261
262 return hist;
263}
264
265////////////////////////////////////////////////////////////////////////////////
266/// gives back all the values of coefficient "varNumber" of the current generation
267
269{
270 std::cout << "FAILED! TMVA::GeneticPopulation::VariableDistribution" << std::endl;
271
272 vector< Double_t > varDist;
273
274 return varDist;
275}
276
277////////////////////////////////////////////////////////////////////////////////
278/// add another population (strangers) to the one of this GeneticPopulation
279
281{
282 for (std::vector<TMVA::GeneticGenes>::iterator it = strangers->fGenePool.begin();
283 it != strangers->fGenePool.end(); ++it ) {
284 GiveHint( it->GetFactors(), it->GetFitness() );
285 }
286}
287
288////////////////////////////////////////////////////////////////////////////////
289/// add another population (strangers) to the one of this GeneticPopulation
290
292{
293 AddPopulation(&strangers);
294}
295
296////////////////////////////////////////////////////////////////////////////////
297/// trim the population to the predefined size
298
300{
301 std::sort(fGenePool.begin(), fGenePool.end());
302 while ( fGenePool.size() > (unsigned int) fPopulationSizeLimit )
303 fGenePool.pop_back();
304}
305
306////////////////////////////////////////////////////////////////////////////////
307/// add an individual (a set of variables) to the population
308/// if there is a set of variables which is known to perform good, they can be given as a hint to the population
309
310void TMVA::GeneticPopulation::GiveHint( std::vector< Double_t >& hint, Double_t fitness )
311{
312 TMVA::GeneticGenes g(hint);
313 g.SetFitness(fitness);
314
315 fGenePool.push_back( g );
316}
317
318////////////////////////////////////////////////////////////////////////////////
319/// sort the genepool according to the fitness of the individuals
320
322{
323 std::sort(fGenePool.begin(), fGenePool.end());
324}
325
#define g(i)
Definition RSha256.hxx:105
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Definition RtypesCore.h:45
#define ClassImp(name)
Definition Rtypes.h:377
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
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:622
Cut optimisation interface class for genetic algorithm.
std::vector< Double_t > & GetFactors()
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 if seed is 0 (default value) a TUUID is generated and used to fill ...
Definition TRandom3.cxx:206
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:682
const Int_t n
Definition legend1.C:16
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148