EvolvingObjects
eoGeneralBreeder.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoGeneralBreeder.h
00005 // (c) Maarten Keijzer and Marc Schoenauer, 2001
00006 /*
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: mkeijzer@dhi.dk
00022              Marc.Schoenauer@polytechnique.fr
00023  */
00024 //-----------------------------------------------------------------------------
00025 
00026 #ifndef eoGeneralBreeder_h
00027 #define eoGeneralBreeder_h
00028 
00029 //-----------------------------------------------------------------------------
00030 
00031 /*****************************************************************************
00032  * eoGeneralBreeder: transforms a population using the generalOp construct.
00033  *****************************************************************************/
00034 
00035 #include <eoOp.h>
00036 #include <eoGenOp.h>
00037 #include <eoPopulator.h>
00038 #include <eoSelectOne.h>
00039 #include <eoBreed.h>
00040 #include <utils/eoHowMany.h>
00041 
00047 template<class EOT>
00048 class eoGeneralBreeder: public eoBreed<EOT>
00049 {
00050  public:
00058   eoGeneralBreeder(
00059           eoSelectOne<EOT>& _select,
00060           eoGenOp<EOT>& _op,
00061                 double  _rate=1.0,
00062           bool _interpret_as_rate = true) :
00063       select( _select ), op(_op),  howMany(_rate, _interpret_as_rate) {}
00064 
00071   eoGeneralBreeder(
00072           eoSelectOne<EOT>& _select,
00073           eoGenOp<EOT>& _op,
00074           eoHowMany _howMany ) :
00075       select( _select ), op(_op),  howMany(_howMany) {}
00076 
00082   void operator()(const eoPop<EOT>& _parents, eoPop<EOT>& _offspring)
00083     {
00084       unsigned target = howMany(_parents.size());
00085 
00086       _offspring.clear();
00087       eoSelectivePopulator<EOT> it(_parents, _offspring, select);
00088 
00089       while (_offspring.size() < target)
00090             {
00091               op(it);
00092               ++it;
00093             }
00094 
00095       _offspring.resize(target);   // you might have generated a few more
00096     }
00097 
00099   virtual std::string className() const { return "eoGeneralBreeder"; }
00100 
00101  private:
00102   eoSelectOne<EOT>& select;
00103   eoGenOp<EOT>& op;
00104   eoHowMany howMany;
00105 };
00106 
00107 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends