roulettewheelselection.cpp
1 /********************************************************************************
2  * FARSA Genetic Algorithm Library *
3  * Copyright (C) 2007-2009 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #include "gaconfig.h"
21 #include "core/genome.h"
22 #include "selections/roulettewheelselection.h"
23 #include "configurationparameters.h"
24 #include "randomgenerator.h"
25 
26 namespace farsa {
27 
29  Selection(),
30  m_notSelectedForBreeding(0),
31  m_cumulativeFitness()
32 {
33 }
34 
36 {
37  //--- nothing to do
38 }
39 
41 {
42  int selected;
43  double reference = globalRNG->getDouble(0, m_cumulativeFitness.last() );
44  for (selected = m_cumulativeFitness.size() - 2; selected >= 0 ; selected-- ) {
45  if( m_cumulativeFitness[selected] < reference ) {
46  break;
47  }
48  }
49  return genome()->at( ++selected );
50 }
51 
53 {
56  double tmp = 0.0;
57  for( int i = 0; i < m_cumulativeFitness.size(); i++ ){
58  m_cumulativeFitness[i] = tmp + g->at(i)->rank();
59  tmp = m_cumulativeFitness[i];
60  }
61 }
62 
63 void RouletteWheelSelection::setNotSelectedForBreeding( int notSelectedForBreeding )
64 {
66 }
67 
69 {
71 }
72 
74 {
75  m_notSelectedForBreeding = params.getValue( prefix + QString( "notSelectedForBreeding" ) ).toInt();
76 }
77 
79 {
80  params.createParameter( prefix, QString("type"), "RouletteWheelSelection" );
81  params.createParameter( prefix, QString("notSelectedForBreeding"), QString("%1").arg( m_notSelectedForBreeding ) );
82 }
83 
84 void RouletteWheelSelection::describe( QString type ) {
85  Descriptor d = addTypeDescription( type, "Selects the individual with a probabilistic roulette wheel method" );
86  d.describeInt( "notSelectedForBreeding" ).limits( 0, INT_MAX ).help( "The worst notSelectedForBreeding individuals will be never selected for reproducing. The selection probability of an individual is proportional to its rank value" );
87 }
88 
89 } // end namespace farsa