// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoBreeder.h // Takes two populations and mixes them // (c) GeNeura Team, 1998 /* 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 eoBreeder_h #define eoBreeder_h //----------------------------------------------------------------------------- #include // vector #include #include // eoOp, eoMonOp, eoBinOp #include // eoPop #include // eoOpSelector #include #include #include using namespace std; /***************************************************************************** * eoBreeder: transforms a population using genetic operators. * * For every operator there is a rated to be applyed. * *****************************************************************************/ template class eoBreeder: public eoTransform //eoUnaryFunctor&> { public: /// Default constructor. eoBreeder( eoOpSelector& _opSel): opSel( _opSel ) {} /// Destructor. virtual ~eoBreeder() {} /** * Transforms a population. * @param pop The population to be transformed. */ void operator()(eoPop& pop) { size_t orgsize = pop.size(); for (unsigned i = 0; i < pop.size(); i++) { eoOp* op = opSel.Op(); switch (op->getType()) { case eoOp::unary: { eoMonOp* monop = static_cast* >(op); (*monop)( pop[i] ); break; } case eoOp::binary: { eoBinOp* binop = static_cast* >(op); (*binop)(pop[i], pop[ rng.random(pop.size()) ] ); break; } case eoOp::quadratic: { eoQuadraticOp* Qop = static_cast* >(op); (*Qop)(pop[i], pop[ rng.random(pop.size()) ] ); break; } case eoOp::general : { eoGeneralOp* Gop = static_cast* >(op); eoRandomIndiSelector selector; eoBackInserter inserter; (*Gop)(selector.bind(pop, orgsize).bias(i), inserter.bind(pop)); break; } } } }; /// The class name. string className() const { return "eoBreeder"; } private: eoOpSelector& opSel; }; //----------------------------------------------------------------------------- #endif eoBreeder_h