45 ClassImp(TMVA::GeneticPopulation);
52 TMVA::GeneticPopulation::GeneticPopulation(
const std::vector<Interval*>& ranges, Int_t size, UInt_t seed)
54 fRanges(ranges.size()),
55 fLogger( new MsgLogger(
"GeneticPopulation") )
60 fRandomGenerator =
new TRandom3( 100 );
61 fRandomGenerator->Uniform(0.,1.);
62 fRandomGenerator->SetSeed( seed );
64 for (
unsigned int i = 0; i < ranges.size(); ++i )
65 fRanges[i] =
new TMVA::GeneticRange( fRandomGenerator, ranges[i] );
67 vector<Double_t> newEntry( fRanges.size() );
68 for (
int i = 0; i < size; ++i )
70 for (
unsigned int rIt = 0; rIt < fRanges.size(); ++rIt )
71 newEntry[rIt] = fRanges[rIt]->Random();
72 fGenePool[i] = TMVA::GeneticGenes( newEntry);
75 fPopulationSizeLimit = size;
81 TMVA::GeneticPopulation::~GeneticPopulation()
83 if (fRandomGenerator != NULL)
delete fRandomGenerator;
85 std::vector<GeneticRange*>::iterator it = fRanges.begin();
86 for (;it!=fRanges.end(); ++it)
delete *it;
96 void TMVA::GeneticPopulation::SetRandomSeed( UInt_t seed )
98 fRandomGenerator->SetSeed( seed );
107 void TMVA::GeneticPopulation::MakeCopies(
int number )
110 for (std::vector<TMVA::GeneticGenes>::iterator it = fGenePool.begin();
111 it != fGenePool.end() && i < number;
113 GiveHint( it->GetFactors(), it->GetFitness() );
122 void TMVA::GeneticPopulation::MakeChildren()
124 #ifdef _GLIBCXX_PARALLEL
128 for (
int it = 0; it < (int) (fGenePool.size() / 2); ++it )
130 Int_t pos = (Int_t)fRandomGenerator->Integer( fGenePool.size()/2 );
131 fGenePool[(fGenePool.size() / 2) + it] = MakeSex( fGenePool[it], fGenePool[pos] );
139 TMVA::GeneticGenes TMVA::GeneticPopulation::MakeSex( TMVA::GeneticGenes male,
140 TMVA::GeneticGenes female )
142 vector< Double_t > child(fRanges.size());
143 for (
unsigned int i = 0; i < fRanges.size(); ++i) {
144 if (fRandomGenerator->Integer( 2 ) == 0) {
145 child[i] = male.GetFactors()[i];
147 child[i] = female.GetFactors()[i];
150 return TMVA::GeneticGenes( child );
171 void TMVA::GeneticPopulation::Mutate( Double_t probability , Int_t startIndex,
172 Bool_t near, Double_t spread, Bool_t mirror )
174 vector< Double_t>::iterator vec;
175 vector< TMVA::GeneticRange* >::iterator vecRange;
182 for (
int it = startIndex; it < (int) fGenePool.size(); ++it) {
183 vecRange = fRanges.begin();
184 for (vec = (fGenePool[it].GetFactors()).begin(); vec < (fGenePool[it].GetFactors()).end(); ++vec) {
185 if (fRandomGenerator->Uniform( 100 ) <= probability) {
186 (*vec) = (*vecRange)->Random( near, (*vec), spread, mirror );
197 TMVA::GeneticGenes* TMVA::GeneticPopulation::GetGenes( Int_t index )
199 return &(fGenePool[index]);
206 void TMVA::GeneticPopulation::Print( Int_t untilIndex )
208 for (
unsigned int it = 0; it < fGenePool.size(); ++it )
211 if (untilIndex >= -1 ) {
212 if (untilIndex == -1 )
return;
215 Log() <<
"fitness: " << fGenePool[it].GetFitness() <<
" ";
216 for (vector< Double_t >::iterator vec = fGenePool[it].GetFactors().begin();
217 vec < fGenePool[it].GetFactors().end(); ++vec ) {
218 Log() <<
"f_" << n++ <<
": " << (*vec) <<
" ";
228 void TMVA::GeneticPopulation::Print( ostream & out, Int_t untilIndex )
230 for (
unsigned int it = 0; it < fGenePool.size(); ++it ) {
232 if (untilIndex >= -1 ) {
233 if (untilIndex == -1 )
return;
236 out <<
"fitness: " << fGenePool[it].GetFitness() <<
" ";
237 for (vector< Double_t >::iterator vec = fGenePool[it].GetFactors().begin();
238 vec < fGenePool[it].GetFactors().end(); ++vec ) {
239 out <<
"f_" << n++ <<
": " << (*vec) <<
" ";
254 TH1F* TMVA::GeneticPopulation::VariableDistribution( Int_t varNumber, Int_t bins,
255 Int_t min, Int_t max )
257 std::cout <<
"FAILED! TMVA::GeneticPopulation::VariableDistribution" << std::endl;
259 std::stringstream histName;
262 histName << varNumber;
263 TH1F *hist =
new TH1F( histName.str().c_str(),histName.str().c_str(), bins,min,max );
271 vector<Double_t> TMVA::GeneticPopulation::VariableDistribution( Int_t )
273 std::cout <<
"FAILED! TMVA::GeneticPopulation::VariableDistribution" << std::endl;
275 vector< Double_t > varDist;
283 void TMVA::GeneticPopulation::AddPopulation( GeneticPopulation *strangers )
285 for (std::vector<TMVA::GeneticGenes>::iterator it = strangers->fGenePool.begin();
286 it != strangers->fGenePool.end(); ++it ) {
287 GiveHint( it->GetFactors(), it->GetFitness() );
294 void TMVA::GeneticPopulation::AddPopulation( GeneticPopulation &strangers )
296 AddPopulation(&strangers);
302 void TMVA::GeneticPopulation::TrimPopulation()
304 std::sort(fGenePool.begin(), fGenePool.end());
305 while ( fGenePool.size() > (
unsigned int) fPopulationSizeLimit )
306 fGenePool.pop_back();
313 void TMVA::GeneticPopulation::GiveHint( std::vector< Double_t >& hint, Double_t fitness )
315 TMVA::GeneticGenes g(hint);
316 g.SetFitness(fitness);
318 fGenePool.push_back( g );
324 void TMVA::GeneticPopulation::Sort()
326 std::sort(fGenePool.begin(), fGenePool.end());