EvolvingObjects
eoEsGlobalXover.h
00001 
00023 //-----------------------------------------------------------------------------
00024 
00025 
00026 #ifndef _eoEsGlobalXover_H
00027 #define _eoEsGlobalXover_H
00028 
00029 #include <utils/eoRNG.h>
00030 
00031 #include <es/eoEsSimple.h>
00032 #include <es/eoEsStdev.h>
00033 #include <es/eoEsFull.h>
00034 
00035 #include <eoGenOp.h>
00036 // needs a selector - here random
00037 #include <eoRandomSelect.h>
00038 
00046 template<class EOT>
00047 class eoEsGlobalXover: public eoGenOp<EOT>
00048 {
00049 public:
00050   typedef typename EOT::Fitness FitT;
00051 
00055   eoEsGlobalXover(eoBinOp<double> & _crossObj, eoBinOp<double> & _crossMut) :
00056     crossObj(_crossObj), crossMut(_crossMut) {}
00057 
00059   virtual std::string className() const { return "eoEsGlobalXover"; }
00060 
00062   unsigned max_production(void) { return 1; }
00063 
00070   void apply(eoPopulator<EOT>& _plop)
00071   {
00072     // First, select as many parents as you will have offspring
00073     EOT& parent = *_plop; // select the first parent
00074 
00075     // first, the object variables
00076     for (unsigned i=0; i<parent.size(); i++)
00077       {
00078         // get extra parents - use private selector
00079         // _plop.source() is the eoPop<EOT> used by _plop to get parents
00080         const EOT& realParent1 = sel(_plop.source());
00081         const EOT& realParent2 = sel(_plop.source());
00082         parent[i] = realParent1[i];
00083         crossObj(parent[i], realParent2[i]); // apply eoBinOp
00084       }
00085     // then the self-adaptation parameters
00086     cross_self_adapt(parent, _plop.source());
00087     // dont' forget to invalidate
00088     parent.invalidate();
00089   }
00090 
00091 private:
00092 
00097   void cross_self_adapt(eoEsSimple<FitT> & _parent, const eoPop<eoEsSimple<FitT> >& _pop)
00098   {
00099     const EOT& realParent1 = sel(_pop);
00100     const EOT& realParent2 = sel(_pop);
00101     _parent.stdev = realParent1.stdev;
00102     crossMut(_parent.stdev, realParent2.stdev); // apply eoBinOp
00103   }
00104 
00109   void cross_self_adapt(eoEsStdev<FitT> & _parent, const eoPop<eoEsStdev<FitT> >& _pop)
00110   {
00111     for (unsigned i=0; i<_parent.size(); i++)
00112       {
00113         const EOT& realParent1 = sel(_pop);
00114         const EOT& realParent2 = sel(_pop);
00115         _parent.stdevs[i] = realParent1.stdevs[i];
00116         crossMut(_parent.stdevs[i], realParent2.stdevs[i]); // apply eoBinOp
00117       }
00118   }
00119 
00124   void cross_self_adapt(eoEsFull<FitT> & _parent, const eoPop<eoEsFull<FitT> >& _pop)
00125   {
00126     unsigned i;
00127     // the StDev
00128     for (i=0; i<_parent.size(); i++)
00129       {
00130         const EOT& realParent1 = sel(_pop);
00131         const EOT& realParent2 = sel(_pop);
00132         _parent.stdevs[i] = realParent1.stdevs[i];
00133         crossMut(_parent.stdevs[i], realParent2.stdevs[i]); // apply eoBinOp
00134       }
00135     // the roataion angles
00136     for (i=0; i<_parent.correlations.size(); i++)
00137       {
00138         const EOT& realParent1 = sel(_pop);
00139         const EOT& realParent2 = sel(_pop);
00140         _parent.correlations[i] = realParent1.correlations[i];
00141         crossMut(_parent.correlations[i], realParent2.correlations[i]); // apply eoBinOp
00142       }
00143 
00144   }
00145 
00146   // the data
00147   eoRandomSelect<EOT> sel;
00148   eoBinOp<double> & crossObj;
00149   eoBinOp<double> & crossMut;
00150 };
00151 
00152 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends