This repository has been archived on 2026-03-28. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
eodev/eo/src/eoLottery.h
marc cbc26ed5b2 Changed all "float" to "double"
But more than that, this is a test of CVS for me
Marc
2000-02-16 04:40:20 +00:00

97 lines
3.1 KiB
C++

// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoLottery.h
// Implements the lottery procedure for selection
// (c) GeNeura Team, 1998 - Marc Schoenauer, 2000
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/
//-----------------------------------------------------------------------------
#ifndef eoLottery_h
#define eoLottery_h
//-----------------------------------------------------------------------------
#include <functional> //
#include <numeric> // accumulate
#include "eoPopOps.h"
#include "eoRNG.h"
//-----------------------------------------------------------------------------
/// eoLottery: a selection method.
/// requires EOT::Fitness to be float castable
//-----------------------------------------------------------------------------
template<class EOT> class eoLottery: public eoBinPopOp<EOT>
{
public:
/// (Default) Constructor.
eoLottery(const double & _rate = 1.0): rate(_rate) {}
/** actually selects individuals from pop and put them into breeders
* until breeders has the right size: rate*pop.size()
* BUT what happens if breeders is already too big?
*/
void operator()( eoPop<EOT>& pop, eoPop<EOT>& breeders)
{
// scores of chromosomes
vector<double> score(pop.size());
// calculates total scores for chromosomes
double total = 0;
for (unsigned i = 0; i < pop.size(); i++) {
score[i] = static_cast<double>(pop[i].fitness());
total += score[i];
}
// number of offspring needed
int target = (int)rint(rate * pop.size());
// test of consistency
if (breeders.size() >= target) {
throw("Problem in eoLottery: already too many offspring");
}
// selection of chromosomes
while (breeders.size() < target) {
unsigned indloc = rng.roulette_wheel(score, total);
breeders.push_back(pop[indloc]);
}
}
/// accessor to private rate
double Rate() {return rate;}
/** @name Methods from eoObject */
//@{
/** readFrom and printOn inherited from eoMerge */
/** Inherited from eoObject. Returns the class name.
@see eoObject
*/
virtual string className() const {return "eoLottery";};
//@}
private:
double rate; // selection rate
};
//-----------------------------------------------------------------------------
#endif eoLottery_h