EvolvingObjects
eoOneToOneBreeder.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoOneToOneBreeder.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 eoOneToOneBreeder_h
00027 #define eoOneToOneBreeder_h
00028 
00029 //-----------------------------------------------------------------------------
00030 
00031 #include <eoOp.h>
00032 #include <eoGenOp.h>
00033 #include <eoPopulator.h>
00034 #include <eoSelectOne.h>
00035 #include <eoSequentialSelect.h>
00036 #include <eoBreed.h>
00037 #include <eoEvalFunc.h>
00038 #include <eoPopulator.h>
00039 #include <utils/eoHowMany.h>
00040 
00051 template<class EOT>
00052 class eoOneToOneBreeder: public eoBreed<EOT>
00053 {
00054  public:
00061   eoOneToOneBreeder(
00062           eoGenOp<EOT>& _op,
00063           eoEvalFunc<EOT> & _eval,
00064           double _pReplace = 1.0,
00065           eoHowMany _howMany = eoHowMany(1.0) ) :
00066       op(_op), eval(_eval), select( false ),
00067       pReplace(_pReplace), howMany(_howMany) {}
00068 
00069 
00077   void operator()(const eoPop<EOT>& _parents, eoPop<EOT>& _offspring)
00078     {
00079       unsigned target = howMany(_parents.size());
00080 
00081       _offspring.clear();
00082       eoSelectivePopulator<EOT> popit(_parents, _offspring, select);
00083 
00084       for (unsigned iParent=0; iParent<target; iParent++)
00085         {
00086           unsigned pos = popit.tellp(); // remember current position
00087           EOT theParent = *popit;  // remember the parent itself
00088 
00089           // now apply operator - will modify the parent
00090           op(popit);
00091 
00092           // replacement
00093           EOT & leOffspring = *popit;
00094 
00095           // check: only one offspring?
00096           unsigned posEnd = popit.tellp();
00097           if (posEnd != pos)
00098             throw std::runtime_error("Operator can only generate a SINGLE offspring in eoOneToOneBreeder");
00099 
00100           // do the tournament between parent and offspring
00101           eval(leOffspring);  // first need to evaluate the offspring
00102           if (theParent > leOffspring) // old parent better than offspring
00103             if (rng.uniform() < pReplace) // if probability
00104               leOffspring = theParent; // replace
00105           // finally, go to next guy to handle
00106           ++popit;
00107         }
00108     }
00109 
00111   virtual std::string className() const { return "eoOneToOneBreeder"; }
00112 
00113  private:
00114   eoGenOp<EOT>& op;
00115   eoEvalFunc<EOT> & eval;
00116   eoSequentialSelect<EOT> select;
00117   double pReplace;
00118   eoHowMany howMany;
00119 };
00120 
00121 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends