ga/src/selections/roulettewheelselection.cpp

00001 /********************************************************************************
00002  *  FARSA Genetic Algorithm Library                                             *
00003  *  Copyright (C) 2007-2009 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
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     //--- nothing to do
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 } // end namespace farsa