Back to Top page.

EGTRouletteSel.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 $Id: EGTRouletteSel.h,v 1.1.1.1 2002/10/19 08:14:51 motegi Exp $
00004 Copyright (C) 2002 Higuchi Lab. All rights reserved.
00005 
00006 *****************************************************************************/
00007 #ifndef INCLUDE__ROULETTESEL_H__FILE
00008 #define INCLUDE__ROULETTESEL_H__FILE
00009 
00010 #include "defs.h"
00011 #include "EGException.h"
00012 #include "EGRandom.h"
00013 #include "EGTSelection.h"
00014 #include "EGTVector.h"
00015 
00022 template<class TP>
00023 class EGTRouletteSel : public EGTSelection<TP> {
00024 public:
00025   typedef TP                      Pop;
00026   typedef typename Pop::Graph     Graph;
00027   typedef EGTVector<const Graph*> Graphs;
00028 
00030   EGTRouletteSel() : m_tRoulette(), m_uPopSize(0) { }
00032   virtual ~EGTRouletteSel() { }
00033   virtual void Selection(const Pop& src, Graphs& dest, UInt uCount);
00034   virtual void Selection(const Pop& src, Pop& dest, UInt uCount);
00035 
00036 private:
00038   EGTVector<Float> m_tRoulette;
00040   UInt m_uPopSize;
00041 
00042   void SetRoulette(const Pop& src);
00043   UInt Select() const;
00044 };
00045 
00046 //============================================================================
00053  template<class TP>
00054 void EGTRouletteSel<TP>::Selection(const Pop& src, Graphs& dest, UInt uCount)
00055 {
00056   SetRoulette(src);
00057   const Graph* pg;
00058   for (UInt i=0; i<uCount; ++i) {
00059     pg = src.GetGraph(Select());
00060     dest.PushBack(pg);
00061   }
00062 }
00063 
00064 //============================================================================
00071 template<class TP>
00072 void EGTRouletteSel<TP>::Selection(const Pop& src, Pop& dest, UInt uCount)
00073 {
00074   UInt uMax = src.GetNumGraphs();
00075   if (uMax < 1) {
00076     throw EGException("EGTRouletteSel::Selection(), "
00077                       "Can't fill the Population: no individuals",
00078                       __FILE__, __LINE__);
00079   }
00080   UInt uSel = (UInt)(uMax * EGRandom::NextReal());
00081   const Graph* pg;
00082   for (UInt i=0; i<uCount; ++i) {
00083     if (uSel == uMax) {
00084       uSel = 0;
00085     }
00086     pg = src.GetGraph(uSel++);
00087     dest.AddGraph(new Graph(*pg));
00088   }
00089 }
00090 
00091 //============================================================================
00099 template<class TP>
00100 void EGTRouletteSel<TP>::SetRoulette(const Pop& src) 
00101 {
00102   UInt i;
00103   m_uPopSize = src.GetNumGraphs();
00104   if (m_tRoulette.Size() < m_uPopSize) {
00105     m_tRoulette.Resize(m_uPopSize, 0);
00106   }
00107   m_tRoulette[0] = src.GetGraph(0)->GetFitness();
00108   for (i=1; i<m_uPopSize; ++i) {
00109     m_tRoulette[i] = m_tRoulette[i-1] + src.GetGraph(i)->GetFitness();
00110   }
00111   if (m_tRoulette[m_uPopSize-1] == 0) {
00112     throw EGException("EGTRouletteSel::SetRoulette(), "
00113                       "Can't perform the selection operation: "
00114                       "The fitness values of all individuals are equal to 0.",
00115                       __FILE__, __LINE__);
00116   }
00117   for (i=0; i<m_uPopSize; ++i) {
00118     m_tRoulette[i] /= m_tRoulette[m_uPopSize-1];
00119   }
00120 }
00121 
00122 //============================================================================
00129 template<class TP>
00130 UInt EGTRouletteSel<TP>::Select() const
00131 {
00132   Float fRan = EGRandom::NextReal();
00133   UInt i;
00134   for (i=0; i<m_uPopSize; ++i) {
00135     if (fRan <= m_tRoulette[i]) {
00136       break;
00137     }
00138   }
00139   if (i == m_uPopSize) {
00140     throw EGException("EGTRouletteSel::Select()", __FILE__, __LINE__);
00141   }
00142   return i;
00143 }
00144 
00145 #endif //INCLUDE__ROULETTESEL_H__FILE