// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoMerge.h // Base class for population-merging classes // (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 eoMerge_h #define eoMerge_h //----------------------------------------------------------------------------- #include // EO includes #include // eoPop #include // eoMerge /** * eoMerge: Base class for replacement algorithms */ template class eoMerge: public eoBinPopOp { public: /// (Default) Constructor. eoMerge(const float& _rate = 1.0): eoBinPopOp(), repRate( _rate ) {} /// Ctor from istream eoMerge( istream& _is): eoBinPopOp() { readFrom( _is ); }; /** * Creates a new population based on breeders and original populations. * @param breeders The population of breeders. Should be sorted to work correctly * @param pop The original population. */ void operator()( eoPop& _breeders, eoPop& _pop) { unsigned target = static_cast(rint(_pop.size() * rate())); _pop.swap(_breeders); if (target < _pop.size()) { partial_sort(_pop.begin(), _pop.begin() + target, _pop.end(), greater()); _pop.erase(_pop.begin() + target, _pop.end()); } else { target = min(target - _pop.size(), _breeders.size()); partial_sort(_breeders.begin(), _breeders.begin() + target, _breeders.end(), greater()); copy(_breeders.begin(), _breeders.begin() + target, back_insert_iterator >(_pop)); } }; /** @name Methods from eoObject */ //@{ /** * Read object. The EOT class must have a ctor from a stream; in this case, a strstream is used. * @param _is A istream. */ virtual void readFrom(istream& _is) { _is >> repRate; } /** * Write object. Prints relevant parameters to standard output * @param _os A ostream. In this case, prints the population to standard output. The EOT class must hav standard output with cout, but since it should be an eoObject anyways, it's no big deal. */ virtual void printOn(ostream& _os) const { _os << repRate; }; /** Inherited from eoObject. Returns the class name. @see eoObject */ virtual string className() const {return "eoMerge";}; //@} protected: float rate() { return repRate;}; private: float repRate; }; //----------------------------------------------------------------------------- #endif eoInsertion_h