// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoInplaceTransform.h // (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 eoInplaceTransform_h #define eoInplaceTransform_h //----------------------------------------------------------------------------- #include // vector #include #include // eoOp, eoMonOp, eoBinOp #include // eoPop #include // eoOpSelector #include #include #include /***************************************************************************** * eoInplaceTransform1: transforms a population using genetic operators. * * It does it in an SGA like manner *****************************************************************************/ template class eoInplaceTransform1 : public eoTransform { public: /// Default constructor. eoInplaceTransform1( eoOpSelector& _opSel): opSel( _opSel ), select(defaultSelect) {} eoInplaceTransform1( eoOpSelector& _opSel, eoSelectOne& _select) : opSel(_opSel), select(_select) {} /** * Transforms a population. * @param pop The population to be transformed. */ void operator()(eoPop& pop) { // copy the guys in a newpop // because otherwise eoSelectRandom might select freshly created individuals eoPop newpop; newpop.reserve(pop.size()); // Set up general op helper classes eoSelectOneIndiSelector inplace(select); eoBackInserter inserter; // set up selection routine select.setup(pop); for (unsigned i = 0; i < pop.size(); i++) { eoOp* op = opSel.Op(); switch (op->getType()) { case eoOp::unary: { eoMonOp* monop = static_cast* >(op); newpop.push_back(pop[i]); (*monop)( newpop.back() ); break; } case eoOp::binary: { eoBinOp* binop = static_cast* >(op); newpop.push_back(pop[i]); (*binop)(newpop[i], select(pop)); break; } case eoOp::quadratic: { eoQuadraticOp* Qop = static_cast* >(op); newpop.push_back(pop[i]); Chrom& indy1 = newpop.back(); if (++i == pop.size()) newpop.push_back(select(pop)); else newpop.push_back(pop[i]); (*Qop)(indy1, newpop.back() ); break; } case eoOp::general : { eoGeneralOp* Gop = static_cast* >(op); inplace.bind(pop); inplace.bias(i,i + 1); inserter.bind(newpop); unsigned orgsize = newpop.size(); (*Gop)(inplace, inserter); unsigned diff = newpop.size() - orgsize; i = i + (diff-1); break; } } pop.swap(newpop); // overwrite existing pop } }; private: eoOpSelector& opSel; eoRandomSelect defaultSelect; eoSelectOne& select; }; #include /***************************************************************************** * eoInplaceTransform2: transforms a population using general genetic operators. * * It does it in an SGA like manner *****************************************************************************/ template class eoInplaceTransform2 : public eoTransform { public: /// Default constructor. eoInplaceTransform2( eoGOpSelector& _opSel): opSel( _opSel ), select(defaultSelect) {} eoInplaceTransform2( eoGOpSelector& _opSel, eoSelectOne& _select) : opSel(_opSel), select(_select) {} /** * Transforms a population. * @param pop The population to be transformed. */ void operator()(eoPop& pop) { // copy the guys in a newpop // because otherwise eoSelectRandom might select freshly created individuals eoPop newpop; newpop.reserve(pop.size()); // Set up general op helper classes eoSelectOneIndiSelector inplace(select); eoBackInserter inserter; // set up selection routine select.setup(pop); for (unsigned i = 0; i < pop.size(); i++) { eoGeneralOp& Gop = opSel.selectOp(); inplace.bind(pop); inplace.bias(i,i + 1); inserter.bind(newpop); unsigned orgsize = newpop.size(); Gop(inplace, inserter); // see how many have been inserted and add that to i (minus one ofcourse) unsigned diff = newpop.size() - orgsize; i = i + (diff-1); } pop.swap(newpop); // overwrite existing pop } private: eoGOpSelector& opSel; eoRandomSelect defaultSelect; eoSelectOne& select; }; //----------------------------------------------------------------------------- #endif eoBreeder_h