// -*- 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 // #include // accumulate #include "eoPopOps.h" #include "eoRNG.h" //----------------------------------------------------------------------------- /// eoLottery: a selection method. /// requires EOT::Fitness to be float castable //----------------------------------------------------------------------------- template class eoLottery: public eoBinPopOp { 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& pop, eoPop& breeders) { // scores of chromosomes vector score(pop.size()); // calculates total scores for chromosomes double total = 0; for (unsigned i = 0; i < pop.size(); i++) { score[i] = static_cast(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