Back to Top page.

EGTPopulation.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 $Id: EGTPopulation.h,v 1.8 2003/01/27 10:11:47 miyabi Exp $
00004 Copyright (C) 2002 Higuchi Lab. All rights reserved.
00005 
00006 *****************************************************************************/
00007 #ifndef INCLUDE__POPULATION_H__FILE
00008 #define INCLUDE__POPULATION_H__FILE
00009 
00010 #include "defs.h"
00011 #include "env.h"
00012 #include "EGException.h"
00013 #include "EGTGraph.h"
00014 #include "EGTVector.h"
00015 #include <fstream>
00016 //#include <iomanip>
00017 #include "EGFileIO.h" // for GetDatFileName()
00018 
00024 template<class TG>
00025 class EGTPopulation {
00026 public:
00027   typedef TG                   Graph;
00028   typedef EGTVector<Graph*>    Graphs;
00029   typedef typename Graphs::Iterator GraphsIterator;
00030   typedef typename Graphs::ConstIterator GraphsConstIterator;
00031   typedef EGTPopulation<Graph> Self;
00032   typedef EGTEvaluation<Graph> Eval;
00033 
00035   EGTPopulation() { }
00036   EGTPopulation(const Self&);
00037   virtual ~EGTPopulation();
00038 
00039   void InitPopulation(UInt n = 0);
00040   Float GetFitnessAvg() const;
00041   void SavePopulation() const;
00042   bool LoadPopulation();
00043   void Display(std::ostream&) const;
00044 
00045   void AssignGraphID();
00046   Graph* GetGraph(UInt);
00047   const Graph* GetGraph(UInt) const;
00048   Graph* GetBestGraph() const;
00049   EGTVector<const Graph*> GetGraphs() const;
00050 
00052   void AddGraph(Graph* pg) { m_tGraphs.PushBack(pg); }
00054   UInt GetNumGraphs() const {  return m_tGraphs.Size(); }
00055 
00056 protected:
00057   void CheckIndex(UInt uNum, char* filename, UInt uLine) const;
00058 
00059 private:
00061   Graphs m_tGraphs;
00063   Self& operator=(const Self&);
00064 };
00065 
00066 //============================================================================
00071 template<class TG>
00072 EGTVector<const TG*>
00073 EGTPopulation<TG>::GetGraphs() const
00074 {
00075   EGTVector<const Graph*> graphs;
00076   GraphsConstIterator it    = m_tGraphs.Begin();
00077   GraphsConstIterator itEnd = m_tGraphs.End();
00078   for (; it != itEnd; ++it) {
00079     graphs.PushBack(*it);
00080   }
00081   return graphs;
00082 }
00083 
00084 //============================================================================
00089 template<class TG>
00090 EGTPopulation<TG>::EGTPopulation(const Self& rhs)
00091 {
00092   GraphsConstIterator itEnd = rhs.m_tGraphs.End();
00093   for (GraphsConstIterator it = rhs.m_tGraphs.Begin(); it != itEnd; ++it) {
00094     m_tGraphs.PushBack(new Graph(**it));
00095   }
00096 }
00097 
00098 //============================================================================
00102 template<class TG>
00103 EGTPopulation<TG>::~EGTPopulation()
00104 {
00105   GraphsIterator itEnd = m_tGraphs.End();
00106   for (GraphsIterator it = m_tGraphs.Begin(); it != itEnd; ++it) {
00107     delete (*it);
00108   }
00109 }
00110 
00111 //============================================================================
00117 template<class TG> 
00118 void EGTPopulation<TG>::InitPopulation(UInt n)
00119 {
00120   if (n == 0) {
00121     n = Env::Instance().PopSize();
00122   }
00123   if (n < m_tGraphs.Size()) {
00124     return;
00125   }
00126   n -= m_tGraphs.Size();
00127   Graph* pg;
00128   for (UInt i=0; i<n; ++i) {
00129     pg = new Graph();
00130     if (pg->InitGraph() == false) {
00131       delete pg;
00132       throw EGException("EGTPopulation::InitPopulation(), "
00133                         "Initialization failed.", __FILE__, __LINE__);
00134     }
00135     m_tGraphs.PushBack(pg);
00136   }
00137 }
00138 
00139 //============================================================================
00145 template<class TG>
00146 const TG* EGTPopulation<TG>::GetGraph(UInt uNum) const
00147 {
00148   CheckIndex(uNum, __FILE__, __LINE__);
00149   return m_tGraphs[uNum];
00150 }
00151 
00152 //============================================================================
00159 template<class TG>
00160 TG* EGTPopulation<TG>::GetGraph(UInt uNum)
00161 {
00162   CheckIndex(uNum, __FILE__, __LINE__);
00163   return m_tGraphs[uNum];
00164 }
00165 
00166 //============================================================================
00171 template<class TG>
00172 TG* EGTPopulation<TG>::GetBestGraph() const
00173 {
00174   GraphsConstIterator it    = m_tGraphs.Begin();
00175   GraphsConstIterator itEnd = m_tGraphs.End();
00176   if (it == itEnd) {
00177     throw EGException("EGTPopulatoin::GetBestGraph(), There is no graph.",
00178                       __FILE__, __LINE__);
00179   }
00180   Graph* pg = *it;
00181   Float fit = (*it)->GetFitness();
00182 
00183   for (++it; it != itEnd; ++it) {
00184     if ((*it)->GetFitness() > fit) {
00185       fit = (*it)->GetFitness();
00186       pg = *it;
00187     }
00188   }
00189   return pg;
00190 }
00191 
00192 //============================================================================
00197 template<class TG>
00198 Float EGTPopulation<TG>::GetFitnessAvg() const
00199 {
00200   const UInt uPopSize = m_tGraphs.Size();
00201   if (uPopSize == 0) {
00202     return 0;
00203   }
00204   Float fSum = 0.0;
00205   GraphsConstIterator itEnd = m_tGraphs.End();
00206   for (GraphsConstIterator it = m_tGraphs.Begin(); it != itEnd; ++it) {
00207     fSum += (*it)->GetFitness();
00208   }
00209   return fSum / uPopSize;
00210 }
00211 
00212 //============================================================================
00216 template<class TG>
00217 void EGTPopulation<TG>::AssignGraphID()
00218 {
00219   UInt i=0;
00220   GraphsIterator it = m_tGraphs.Begin();
00221   GraphsIterator itEnd = m_tGraphs.End();
00222   for (; it != itEnd; ++it) {
00223     (*it)->SetGraphID(i++);
00224   }
00225 }
00226 
00227 
00228 //============================================================================
00234 template<class TG>
00235 void EGTPopulation<TG>::SavePopulation() const
00236 {
00237   UInt i = 0;
00238   GraphsConstIterator itEnd = m_tGraphs.End();
00239   for (GraphsConstIterator it = m_tGraphs.Begin(); it != itEnd; ++it) {
00240     const std::string name = EGFileIO::GetDatFileName(i++);
00241     std::ofstream fout(name.c_str());
00242     if (!fout) {
00243       throw EGException("Can't open file", __FILE__, __LINE__);
00244     }
00245     (*it)->SaveGraph(fout);
00246   }
00247 }
00248 
00249 //============================================================================
00258 template<class TG>
00259 bool EGTPopulation<TG>::LoadPopulation()
00260 {
00261   UInt uPopSize = Env::Instance().PopSize();
00262   Graph* pg;
00263   for (UInt i=0; i<uPopSize; ++i) {
00264     pg = new Graph;
00265     const std::string name = EGFileIO::GetDatFileName(i);
00266     if (pg->LoadGraph(name.c_str()) == false) {
00267       delete pg;
00268       return false;
00269     }
00270     else {
00271       AddGraph(pg);
00272     }
00273   }
00274   return true;
00275 }
00276 
00277 //============================================================================
00282 template<class TG>
00283 void EGTPopulation<TG>::Display(std::ostream& out) const
00284 {
00285   UInt i = 0;
00286   GraphsConstIterator it = m_tGraphs.Begin();
00287   GraphsConstIterator itEnd = m_tGraphs.End();
00288 
00289   out << "#Graph   ";
00290   (*it)->DisplayFitness(out, true);
00291   out << "\tOperation\n";
00292 
00293   for (; it != itEnd; ++it) {
00294     out << i++ << '\t';
00295     (*it)->DisplayFitness(out, false);
00296     switch ((*it)->GetOperator()) {
00297     case OP_CROSSOVER:
00298       out << "\tcrossover(" << (*it)->GetMother() << ", " << (*it)->GetFather() << ")\n"; break;
00299     case OP_MUTATION:
00300       out << "\tmutation(" << (*it)->GetMother() << ")\n"; break;
00301     case OP_SELECTION:
00302       out << "\tselection(" << (*it)->GetMother() << ")\n"; break;
00303     default: out << "\tundefined\n"; break;
00304     }
00305   }
00306 }
00307 
00308 //============================================================================
00316 template<class TG>
00317 void EGTPopulation<TG>::CheckIndex(UInt uNum, 
00318                                    char* filename,
00319                                    UInt uLine) const
00320 {
00321 #ifdef DEBUG
00322   if (uNum > m_tGraphs.Size()) {
00323     throw EGException("EGTPopulation, "
00324                       "Index is greater than the number of Population.",
00325                       filename, uLine);
00326   }
00327 #endif //DEBUG
00328 }
00329 
00330 #endif //INCLUDE__POPULATION_H__FILE