Back to Top page.

EGTMutation.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 $Id: EGTMutation.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__MUTATION_H__FILE
00008 #define INCLUDE__MUTATION_H__FILE
00009 
00010 #include "defs.h"
00011 #include "env.h"
00012 #include "EGException.h"
00013 #include "EGRandom.h"
00014 #include "EGTVector.h"
00015 
00023 template<class TP>
00024 class EGTMutation {
00025 public:
00026   typedef TP                       Pop;
00027   typedef typename Pop::Graph      Graph;
00028   typedef EGTVector<const Graph*>  Graphs;
00029   typedef typename Graph::SubGraph SubGraph;
00030 
00032   virtual ~EGTMutation() { }
00033   virtual UInt Mutation(const Graphs& src, Pop& dest);
00034 
00035 protected:
00036   virtual bool DoMutation(const Graph& src, Pop& dest);
00037 };
00038 
00039 //============================================================================
00047 template<class TP>
00048 UInt EGTMutation<TP>::Mutation(const Graphs& src, Pop& dest) {
00049   Float fMutationRate = Env::Instance().MutationRate();
00050   UInt uPopSize = Env::Instance().PopSize();           
00051 
00052   if (fMutationRate > 1.0) {  
00053     fMutationRate = 1.0;
00054   }
00055   UInt uMax = src.Size();
00056   if (uMax == 0) {
00057     throw EGException("EGTMutation::Mutation(), "
00058                       "Can't perform the mutation operation: no individuals.",
00059                       __FILE__, __LINE__);
00060   }
00061   // Number of mutation operations
00062   UInt uMutationCount = (UInt)(uPopSize * fMutationRate);
00063 
00064   UInt uSel = (UInt)(uMax * EGRandom::NextReal());
00065 
00066   UInt n = 0;
00067   for (UInt i=0; i<uMutationCount; ++i) {
00068     if (uSel == uMax) {
00069       uSel = 0;
00070     }
00071     if (DoMutation(*src[uSel++], dest) == true) {
00072       ++n;
00073     }
00074   }
00075   return n;
00076 }
00077 
00078 //============================================================================
00086 template<class TP>
00087 bool EGTMutation<TP>::DoMutation(const Graph& src, Pop& dest)
00088 {
00089   UInt uNumSubGraphs = src.GetNumSubGraphs();
00090   if (uNumSubGraphs == 0) {
00091     return false;
00092   }
00093   //Selects a SubGraph randomly from the Graph.
00094   const UInt uRan = EGRandom::NextInt(0, uNumSubGraphs - 1);
00095   const SubGraph* ptSelSubGraph = src.GetSubGraph(uRan);
00096 
00097   if (ptSelSubGraph == 0) {
00098     return false;
00099   }
00100   //Creates a compatible SubGraph with the selected SubGraph.
00101   const UInt uIn = ptSelSubGraph->GetNumInTerms();
00102   const UInt uOut = ptSelSubGraph->GetNumOutTerms();
00103   SubGraph* ptNewSubGraph = new SubGraph;
00104   if (ptNewSubGraph->InitSubGraph(uIn, uOut) == false) {
00105     delete ptNewSubGraph;
00106     return false;
00107   }
00108 
00109   UInt uMaxNodeSize = Env::Instance().MaxNodeSize();
00110   //Creates the complement of the selected SubGraph.
00111   UInt uMax = 2 * uMaxNodeSize - ptNewSubGraph->GetNumNodes();
00112   SubGraph* ptRestSubGraph = src.CreateComplementarySubGraph(uRan, uMax);
00113   if (ptRestSubGraph == 0) {
00114     return false;
00115   }
00116 
00117   Graph* ptNewGraph = new Graph;
00118 
00119   //Combines "ptNewSubGraph" with "ptRestSubGraph".
00120   if (ptNewGraph->InitGraph(*ptRestSubGraph, *ptNewSubGraph, 2) == false) {
00121     delete ptNewGraph;
00122     delete ptNewSubGraph;
00123     delete ptRestSubGraph;
00124     return false;
00125   }
00126 
00127   ptNewGraph->SetOperator(OP_MUTATION);
00128   ptNewGraph->SetMother(src.GetGraphID());
00129   //Generated individuals are stored in "dest".
00130   dest.AddGraph(ptNewGraph);
00131 
00132 #ifdef MUTATION_DETAIL
00133   std::cout << "The mutation operation performed.\n";
00134   std::cout << "*** Base SubGraph ***\n";      ptRestSubGraph->Display(std::cout);
00135   std::cout << "*** Generated SubGraph ***\n"; ptNewSubGraph->Display(std::cout);
00136 #endif
00137 
00138   delete ptNewSubGraph;
00139   delete ptRestSubGraph;
00140   return true;
00141 }
00142 
00143 #endif //INCLUDE__MUTATION_H__FILE