00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "gaconfig.h"
00021 #include "core/genome.h"
00022 #include "selections/roulettewheelselection.h"
00023 #include "configurationparameters.h"
00024 #include "randomgenerator.h"
00025
00026 namespace farsa {
00027
00028 RouletteWheelSelection::RouletteWheelSelection() :
00029 Selection(),
00030 m_notSelectedForBreeding(0),
00031 m_cumulativeFitness()
00032 {
00033 }
00034
00035 RouletteWheelSelection::~RouletteWheelSelection()
00036 {
00037
00038 }
00039
00040 const Genotype* RouletteWheelSelection::select()
00041 {
00042 int selected;
00043 double reference = globalRNG->getDouble(0, m_cumulativeFitness.last() );
00044 for (selected = m_cumulativeFitness.size() - 2; selected >= 0 ; selected-- ) {
00045 if( m_cumulativeFitness[selected] < reference ) {
00046 break;
00047 }
00048 }
00049 return genome()->at( ++selected );
00050 }
00051
00052 void RouletteWheelSelection::setGenome( const Genome* g )
00053 {
00054 Selection::setGenome( g );
00055 m_cumulativeFitness.resize(g->size() - m_notSelectedForBreeding );
00056 double tmp = 0.0;
00057 for( int i = 0; i < m_cumulativeFitness.size(); i++ ){
00058 m_cumulativeFitness[i] = tmp + g->at(i)->rank();
00059 tmp = m_cumulativeFitness[i];
00060 }
00061 }
00062
00063 void RouletteWheelSelection::setNotSelectedForBreeding( int notSelectedForBreeding )
00064 {
00065 m_notSelectedForBreeding = notSelectedForBreeding;
00066 }
00067
00068 int RouletteWheelSelection::notSelectedForBreeding()
00069 {
00070 return m_notSelectedForBreeding;
00071 }
00072
00073 void RouletteWheelSelection::configure( ConfigurationParameters& params, QString prefix )
00074 {
00075 m_notSelectedForBreeding = params.getValue( prefix + QString( "notSelectedForBreeding" ) ).toInt();
00076 }
00077
00078 void RouletteWheelSelection::save( ConfigurationParameters& params, QString prefix )
00079 {
00080 params.createParameter( prefix, QString("type"), "RouletteWheelSelection" );
00081 params.createParameter( prefix, QString("notSelectedForBreeding"), QString("%1").arg( m_notSelectedForBreeding ) );
00082 }
00083
00084 void RouletteWheelSelection::describe( QString type ) {
00085 Descriptor d = addTypeDescription( type, "Selects the individual with a probabilistic roulette wheel method" );
00086 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" );
00087 }
00088
00089 }