Back to Top page.

EGTGraph.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 $Id: EGTGraph.h,v 1.10 2003/01/27 10:11:46 miyabi Exp $
00004 Copyright (C) 2002 Higuchi Lab. All rights reserved.
00005 
00006 *****************************************************************************/
00007 #ifndef INCLUDE__GRAPH_H__FILE
00008 #define INCLUDE__GRAPH_H__FILE
00009 
00010 #include "defs.h"
00011 #include "env.h"
00012 #include "EGException.h"
00013 #include "EGRandom.h"
00014 #include "EGTVector.h"
00015 #include <fstream>
00016 #include <iterator>  // for advance(), distance()
00017 #include <algorithm> // for find()
00024 template<class TSG, class TF>
00025 class EGTGraph {
00026 public:
00027   typedef TSG                     SubGraph;
00028   typedef EGTVector<TSG*>         SubGraphs;
00029   typedef typename SubGraphs::Iterator SubGraphsIterator;
00030   typedef typename SubGraphs::ConstIterator SubGraphsConstIterator;
00031   typedef TF                      Fitness;
00032   typedef typename SubGraph::Node Node;
00033   typedef EGTVector<Node*>        Nodes;
00034   typedef typename Nodes::Iterator NodesIterator;
00035   typedef typename Nodes::ConstIterator NodesConstIterator;
00036   typedef EGTGraph<TSG, TF>  Self;
00037 
00038   static Node* m_tCreator;
00039 
00040   EGTGraph();
00041   EGTGraph(const Self&);
00042   virtual ~EGTGraph();
00043 
00044   virtual bool InitGraph();
00045   virtual bool InitGraph(const SubGraph&);
00046   virtual bool InitGraph(const SubGraph&, const SubGraph&, UInt ope = 0);
00047   virtual bool LoadGraph(const char* pFileName);
00048   virtual void SaveGraph(std::ostream&) const;
00049   virtual void Display(std::ostream&) const;
00050   virtual void DisplayDot(std::ostream&) const;
00051 
00052   SubGraph* CreateComplementarySubGraph(UInt uNum, UInt uMax = 0) const;
00053   SubGraph* GetSubGraph(UInt);
00054   const SubGraph* GetSubGraph(UInt) const;
00055   const SubGraph* GetSubGraph(const SubGraph&, UInt*) const;
00056   UInt GetNumSubGraphs() const;
00057 
00059   bool GetEvaluated() const   { return m_bEvaluated; }
00061   void SetEvaluated(bool ret) { m_bEvaluated = ret;  }
00063   UInt GetGraphID() const   { return m_uGraphID; }
00065   void SetGraphID(UInt ret) { m_uGraphID = ret; }
00067   UInt GetNumNodes() const { return m_tNodes.Size(); }
00069   const Node* GetNode(UInt uNodeID) const { return m_tNodes[uNodeID]; }
00071   Node* GetNode(UInt uNodeID)             { return m_tNodes[uNodeID]; }
00073   Float GetFitness() const { return m_tFitness.GetFitness(); }
00074   void DisplayFitness(std::ostream&, bool) const;
00075   UInt GetOperator() const { return m_uOperator; }
00076   void SetOperator(UInt n) { m_uOperator = n; }
00077   UInt GetMother() const { return m_uMother; }
00078   void SetMother(UInt n) { m_uMother = n; }
00079   UInt GetFather() const { return m_uFather; }
00080   void SetFather(UInt n) { m_uFather=n; }
00081   void SetOrigin(UInt n);
00082 
00083 protected:
00084   void CreateSubGraphs() const;
00085   void CreateSubGraphsPowerSet() const;
00086   void CreateSubGraphsRandomly() const;
00087   SubGraph* CreateSubGraph() const;
00088   SubGraph* CreateSubGraphConstraint() const;
00089   bool IsConnected(Nodes nodes) const;
00090 
00091   Nodes GetCandidates() const;
00092   void CreatePowerSet(UInt) const;
00093   void ClearSubGraphs();
00094   void AssignNodeID();
00095   bool IsSubGraphRegistered(const SubGraph&) const;
00096 
00098   UInt m_uGraphID;                 
00100   Nodes m_tNodes;
00102   Fitness m_tFitness;
00104   bool m_bEvaluated;
00105 
00107   mutable SubGraphs m_tSubGraphs;
00108 
00110   static EGTVector<EGTVector<UInt> > m_tPowerSet;
00112   static bool m_bConstraint;
00113 
00120   enum {POWERSET_LIMIT = 6};
00121 
00122 private:
00124   Self& operator=(const Self&);
00125 
00127   UInt m_uOperator; 
00129   UInt m_uMother;   
00130   // Father's Graph ID number
00131   UInt m_uFather;   
00132 };
00133 
00134 
00135 //============================================================================
00136 //============================================================================
00141 template<class TSG, class TF>
00142 void EGTGraph<TSG, TF>::SetOrigin(UInt n)
00143 {
00144   NodesIterator itEnd = m_tNodes.End();
00145   for (NodesIterator it = m_tNodes.Begin(); it != itEnd; ++it) {
00146     (*it)->SetOrigin(n);
00147   }
00148 }
00149 
00150 //============================================================================
00155 template<class TSG, class TF>
00156 UInt EGTGraph<TSG, TF>::GetNumSubGraphs() const
00157 {
00158   if (m_tSubGraphs.Empty()) {
00159     CreateSubGraphs();
00160   }
00161   return m_tSubGraphs.Size();
00162 }
00163 
00164 //============================================================================
00168 template<class TSG, class TF>
00169 void EGTGraph<TSG, TF>::ClearSubGraphs()
00170 {
00171   SubGraphsIterator it = m_tSubGraphs.Begin();
00172   SubGraphsIterator itEnd = m_tSubGraphs.End();
00173   for (; it != itEnd; ++it) {
00174     delete (*it);
00175   }
00176   m_tSubGraphs.Clear();
00177 }
00178 
00179 //============================================================================
00183 template<class TSG, class TF>
00184 EGTGraph<TSG, TF>::EGTGraph()
00185   : m_uGraphID(0), m_bEvaluated(false), m_uOperator(OP_UNDEF), m_uMother(0), m_uFather(0)
00186 {
00187   if (m_tPowerSet.Empty()) {
00188     CreatePowerSet(POWERSET_LIMIT);
00189   }
00190 }
00191 
00192 //============================================================================
00196 template<class TSG, class TF>
00197 EGTGraph<TSG, TF>::EGTGraph(const Self& graph)
00198   : m_tFitness(graph.m_tFitness), m_bEvaluated(graph.m_bEvaluated), 
00199   m_uOperator(OP_UNDEF), m_uMother(0), m_uFather(0)
00200 {
00201   //Copies all the Nodes 
00202   NodesConstIterator itEnd = graph.m_tNodes.End();
00203   for (NodesConstIterator it = graph.m_tNodes.Begin(); it != itEnd; ++it) {
00204     m_tNodes.PushBack(new Node(**it));
00205   }
00206 
00207   //Copies all the information about directed edges
00208   UInt uTermNum, uNodeID;
00209   NodesIterator      itDest   = m_tNodes.Begin();
00210   NodesConstIterator itSrc    = graph.m_tNodes.Begin();
00211   NodesConstIterator itSrcEnd = graph.m_tNodes.End();
00212   for (; itSrc != itSrcEnd; ++itSrc, ++itDest) {
00213     for (UInt i=0; i<(*itSrc)->GetNumInTerms(); ++i) {
00214       if ((*itSrc)->GetInTerm(i)->GetTerm()) {
00215         uNodeID = (*itSrc)->GetInTerm(i)->GetDestNodeID();
00216         uTermNum = (*itSrc)->GetInTerm(i)->GetDestTermNum();
00217         (*itDest)->ConnectSrc(i, GetNode(uNodeID)->GetOutTerm(uTermNum));
00218       }
00219     }
00220   }
00221 
00222   //Copies all the SubGraphs.
00223   SubGraphsConstIterator itSubGraph    = graph.m_tSubGraphs.Begin();
00224   SubGraphsConstIterator itSubGraphEnd = graph.m_tSubGraphs.End();
00225   for (; itSubGraph != itSubGraphEnd; ++itSubGraph) {
00226     Nodes ptSrcNodes;
00227     for (NodesConstIterator it2 = (*itSubGraph)->GetNodes()->Begin();
00228          it2 != (*itSubGraph)->GetNodes()->End(); ++it2) {
00229       ptSrcNodes.PushBack(GetNode((*it2)->GetNodeID()));
00230     }
00231     SubGraph* pTmpSubGraph = new SubGraph(ptSrcNodes, false);
00232     m_tSubGraphs.PushBack(pTmpSubGraph);
00233   }
00234 }
00235 
00236 //============================================================================
00240 template<class TSG, class TF>
00241 EGTGraph<TSG, TF>::~EGTGraph()
00242 {
00243   NodesIterator itEnd = m_tNodes.End();
00244   for (NodesIterator it = m_tNodes.Begin(); it != itEnd; ++it) {
00245     delete (*it);
00246   }
00247   SubGraphsIterator itEnd2 = m_tSubGraphs.End();
00248   for (SubGraphsIterator it = m_tSubGraphs.Begin(); it != itEnd2; ++it) {
00249     delete (*it);
00250   }
00251 }
00252 
00253 //============================================================================
00259 template<class TSG, class TF>
00260 bool EGTGraph<TSG, TF>::InitGraph()
00261 {
00262   UInt uIn = Env::Instance().NumIn();
00263   UInt uOut = Env::Instance().NumOut();
00264 
00265   SubGraph* psg = new SubGraph;
00266   if ((psg->InitSubGraph(uIn, uOut) == false) ||
00267       (InitGraph(*psg) == false)) {
00268     delete psg;
00269     return false;
00270   }
00271   delete psg;
00272   return true;
00273 }
00274 
00275 //============================================================================
00282 template<class TSG, class TF>
00283 bool EGTGraph<TSG, TF>::InitGraph(const SubGraph& subgraph)
00284 {
00285   UInt uMaxNodeSize = Env::Instance().MaxNodeSize();
00286   if ((subgraph.GetNumNodes() == 0) ||
00287       (subgraph.GetNumNodes() +
00288        subgraph.GetNumInTerms() +
00289        subgraph.GetNumOutTerms() > 2 * uMaxNodeSize)) {
00290     return false;
00291   }
00292   SubGraph* psg = new SubGraph(subgraph);
00293 
00294   Node* pn;
00295   // Generates input Nodes.
00296   UInt uIn = psg->GetNumInTerms();
00297   for (UInt i=0; i<uIn; ++i) {
00298     pn = m_tCreator->CreateInputNode(i+1);
00299     pn->ConnectDest(0, psg->GetInTerm(i));
00300     m_tNodes.PushBack(pn);
00301   }
00302   // Generates output Nodes.
00303   UInt uOut = psg->GetNumOutTerms();
00304   for (UInt i=0; i<uOut; ++i) {
00305     pn = m_tCreator->CreateOutputNode(i+1);
00306     pn->ConnectSrc(0, psg->GetOutTerm(i));
00307     m_tNodes.PushBack(pn);
00308   }
00309   //Copies all Nodes.
00310   psg->Release(&m_tNodes);
00311   delete psg;
00312   AssignNodeID();
00313   return true;
00314 }
00315 
00316 //============================================================================
00325 template<class TSG, class TF>
00326 bool EGTGraph<TSG, TF>::InitGraph(const SubGraph& sub1,
00327                                   const SubGraph& sub2,
00328                                   UInt ope)
00329 {
00330   UInt uMaxNodeSize = Env::Instance().MaxNodeSize();
00331 
00332   if ((sub1.GetNumNodes() == 0) ||
00333       (sub2.GetNumNodes() == 0) ||
00334       (sub1.GetNumNodes() + sub2.GetNumNodes() > 2*uMaxNodeSize)) {
00335     return false;
00336   }
00337 
00338   //Combines the SubGraphs.
00339   SubGraph* psg1 = new SubGraph(sub1);
00340   SubGraph* psg2 = new SubGraph(sub2);
00341 
00342   if (ope == OP_CROSSOVER) { // crossover
00343     psg1->SetOrigin(ORI_CROSSOVER_A);
00344     psg2->SetOrigin(ORI_CROSSOVER_B);
00345   }
00346   else if (ope == OP_MUTATION) { // mutation
00347     psg1->SetOrigin(ORI_MUTATION_A);
00348     psg2->SetOrigin(ORI_MUTATION_B);
00349   }
00350   else {
00351     psg1->SetOrigin(ORI_UNDEF);
00352     psg2->SetOrigin(ORI_UNDEF);
00353   }
00354 
00355   psg1->ConnectSubGraph(*psg2);
00356   psg1->Release(&m_tNodes);
00357   psg2->Release(&m_tNodes);
00358   AssignNodeID();
00359   delete psg1;
00360   delete psg2;
00361   return true;
00362 }
00363 
00364 //============================================================================
00369 template<class TSG, class TF>
00370 void EGTGraph<TSG, TF>::SaveGraph(std::ostream& out) const
00371 {
00372   //Number of Nodes
00373   const UInt uNumNodes = m_tNodes.Size();
00374   out << uNumNodes << ' ';
00375 
00376   //Functional ID numbers
00377   for (UInt i=0; i<uNumNodes; ++i) {
00378     out << GetNode(i)->GetFuncID() << ' ';
00379   }
00380   out << "\n\n";
00381 
00382   //Edge information
00383   for (UInt i=0; i<uNumNodes; ++i) {
00384     const Node* pn = GetNode(i);
00385     UInt uIn = pn->GetNumInTerms();
00386     for (UInt j=0; j<uIn; ++j) {
00387       out << pn->GetInTerm(j)->GetDestNodeID()  << ' ';
00388       out << pn->GetInTerm(j)->GetDestTermNum() << ' ';
00389     }
00390     UInt uOut = pn->GetNumOutTerms();
00391     for (UInt j=0; j<uOut; ++j) {
00392       out << pn->GetOutTerm(j)->GetDestNodeID()  << ' ';
00393       out << pn->GetOutTerm(j)->GetDestTermNum() << ' ';
00394     }
00395     out << pn->GetNO() << ' ';
00396     pn->SaveExtraData(out);
00397     out << '\n';
00398   }
00399   out << '\n';
00400   //SubGraphs
00401   if (m_tSubGraphs.Size() == 0) {
00402     out << "0\n";
00403   }
00404   else {
00405     SubGraphsConstIterator it    = m_tSubGraphs.Begin();
00406     SubGraphsConstIterator itEnd = m_tSubGraphs.End();
00407     for (; it != itEnd; ++it) {
00408       (*it)->SaveSubGraph(out);
00409     }
00410   }
00411 }
00412 
00413 //============================================================================
00420 template<class TSG, class TF>
00421 bool EGTGraph<TSG, TF>::LoadGraph(const char* pFileName)
00422 {
00423   std::ifstream fin(pFileName);
00424   if (!fin) {
00425     throw EGException("EGTGraph::LoadGraph(), Can't open file",
00426                       __FILE__, __LINE__);
00427   }
00428   //Nodes
00429   UInt FuncID, NodeID, TermNum, uNO;
00430   UInt uNumNodes;
00431   fin >> uNumNodes;
00432   for (UInt i=0; i<uNumNodes; ++i) {
00433     fin >> FuncID;
00434     if (fin.eof()) {
00435       break;
00436     }
00437     Node* newNode = m_tCreator->CreateNode(FuncID);
00438     newNode->SetNodeID(i);
00439     m_tNodes.PushBack(newNode);
00440   }
00441   //Edge information
00442   for (UInt i=0; i<uNumNodes; ++i) {
00443     for (UInt j=0; j<GetNode(i)->GetNumInTerms(); ++j) {
00444       fin >> NodeID;
00445       fin >> TermNum;
00446       GetNode(i)->GetInTerm(j)->SetTerm(GetNode(NodeID)->GetOutTerm(TermNum));
00447     }
00448     for (UInt j=0; j<GetNode(i)->GetNumOutTerms(); ++j) {
00449       fin >> NodeID;
00450       fin >> TermNum;
00451       GetNode(i)->GetOutTerm(j)->SetTerm(GetNode(NodeID)->GetInTerm(TermNum));
00452     }
00453     fin >> uNO;
00454     GetNode(i)->SetNO(uNO);
00455     if (GetNode(i)->LoadExtraData(fin) == false) {
00456       throw EGException("LoadExtraData() == false", __FILE__, __LINE__);
00457     }
00458   }
00459   //SubGraphs.
00460   Nodes tmpNodes;
00461   SubGraph* tmpSubGraph;
00462   while (1) {
00463     fin >> uNumNodes;
00464     if (uNumNodes == 0 || fin.eof()) {
00465       break;
00466     }
00467     tmpNodes.Clear();
00468     for (UInt i=0; i<uNumNodes; ++i) {
00469       fin >> NodeID;
00470       tmpNodes.PushBack(GetNode(NodeID));
00471     }
00472     tmpSubGraph = new SubGraph(tmpNodes, false);
00473     m_tSubGraphs.PushBack(tmpSubGraph);
00474   }
00475   return true;
00476 }
00477 //============================================================================
00482 template<class TSG, class TF>
00483 typename EGTGraph<TSG, TF>::Nodes
00484 EGTGraph<TSG, TF>::GetCandidates() const
00485 {
00486   Nodes ret;
00487   NodesConstIterator it    = m_tNodes.Begin();
00488   NodesConstIterator itEnd = m_tNodes.End();
00489   for (; it != itEnd; ++it) {
00490     if ((*it)->IsCandidate()) {
00491       ret.PushBack(*it);
00492     }
00493   }
00494   return ret;
00495 }
00496 
00497 //============================================================================
00506 template<class TSG, class TF>
00507 TSG* EGTGraph<TSG, TF>::CreateSubGraph() const
00508 {
00509   //Gets the set of Nodes.
00510   Nodes tUnSelList = GetCandidates();
00511   if (tUnSelList.Empty()) {
00512     return 0;
00513   }
00514   UInt uMaxNodeSize = Env::Instance().MaxNodeSize();
00515   //Determines the number of Nodes.
00516   UInt uNumNodes = EGRandom::NextInt(1, tUnSelList.Size());
00517   if (uNumNodes > uMaxNodeSize) {
00518     uNumNodes = uMaxNodeSize;
00519   }
00520 
00521   UInt uSelNum;
00522   Nodes tSelList;
00523   while (tSelList.Size() < uNumNodes) {
00524     uSelNum = EGRandom::NextInt(0, tUnSelList.Size()-1);
00525     NodesIterator it = tUnSelList.Begin();
00526     std::advance(it, uSelNum);
00527     tSelList.PushBack(*it);
00528     tUnSelList.Erase(it);
00529   }
00530 
00531   SubGraph* psg = new SubGraph(tSelList, false);
00532   return psg;
00533 }
00534 
00535 //============================================================================
00543 template<class TSG, class TF>
00544 void EGTGraph<TSG, TF>::CreateSubGraphs() const
00545 {
00546   Nodes tCandidates = GetCandidates();
00547   if (tCandidates.Size() <= POWERSET_LIMIT) {
00548     CreateSubGraphsPowerSet();
00549   }
00550   else {
00551     CreateSubGraphsRandomly();
00552   }
00553 }
00554 
00555 //============================================================================
00559 template<class TSG, class TF>
00560 void EGTGraph<TSG, TF>::CreateSubGraphsRandomly() const
00561 {
00562   //Number of SubGraphs to be generated
00563   const UInt uSubGraphSize = Env::Instance().SubGraphSize();
00564   //Maximum number of iterations
00565   const UInt uSubGraphTrySize = uSubGraphSize * 2; 
00566 
00567   UInt count = 0;
00568   for (UInt i=0; i<uSubGraphTrySize; ++i) {
00569     SubGraph* psg;
00570     if (m_bConstraint) {
00571       psg = CreateSubGraphConstraint();
00572     }
00573     else {
00574       psg = CreateSubGraph();
00575     }
00576     if (psg) { 
00577       if (IsSubGraphRegistered(*psg) == true) {
00578         delete psg;
00579       }
00580       else {
00581         m_tSubGraphs.PushBack(psg);
00582         ++count;
00583         if (uSubGraphSize == count) {
00584           return;
00585         }
00586       }
00587     }
00588   }
00589 }
00590 
00591 //============================================================================
00596 template<class TSG, class TF>
00597 typename EGTGraph<TSG, TF>::SubGraph* 
00598 EGTGraph<TSG, TF>::CreateSubGraphConstraint() const
00599 {
00600   Nodes offNodes = GetCandidates();
00601   Nodes onNodes, fixedNodes;
00602 
00603   UInt uMaxNodeSize = Env::Instance().MaxNodeSize();
00604   //Determines the number of Nodes.
00605   UInt uNumNodes = EGRandom::NextInt(1, offNodes.Size());
00606   if (uNumNodes > uMaxNodeSize) {
00607     uNumNodes = uMaxNodeSize;
00608   }
00609 
00610   UInt n = EGRandom::NextInt(0, offNodes.Size()-1);
00611   NodesIterator it = offNodes.Begin();
00612   NodesIterator itTemp;
00613   std::advance(it, n);
00614   Node* pn = *it;
00615   onNodes.PushBack(pn);
00616   offNodes.Erase(it);
00617 
00618   do {
00619     UInt m = EGRandom::NextInt(0, onNodes.Size()-1);
00620     it = onNodes.Begin();
00621     std::advance(it, m);
00622     pn = *it;
00623     fixedNodes.PushBack(pn);
00624     onNodes.Erase(it);
00625 
00626     for (UInt i=0; i<pn->GetNumInTerms(); ++i) {
00627       Node* destNode = pn->GetInTerm(i)->GetDestNode();
00628       itTemp = std::find(offNodes.Begin(), offNodes.End(), destNode);
00629       if (itTemp != offNodes.End()) {
00630         onNodes.PushBack(*itTemp);
00631         offNodes.Erase(itTemp);
00632       }
00633     }
00634     for (UInt i=0; i<pn->GetNumOutTerms(); ++i) {
00635       Node* destNode = pn->GetOutTerm(i)->GetDestNode();
00636       itTemp = std::find(offNodes.Begin(), offNodes.End(), destNode);
00637       if (itTemp != offNodes.End()) {
00638         onNodes.PushBack(*itTemp);
00639         offNodes.Erase(itTemp);
00640       }
00641     }
00642   } while (fixedNodes.Size() < uNumNodes && onNodes.Size() > 0);
00643   
00644   SubGraph* psg = new SubGraph(fixedNodes, false);
00645   return psg;
00646 }
00647 
00648 //============================================================================
00655 template<class TSG, class TF>
00656 bool EGTGraph<TSG, TF>::IsSubGraphRegistered(const SubGraph& subgraph) const
00657 {
00658   SubGraphsConstIterator it    = m_tSubGraphs.Begin();
00659   SubGraphsConstIterator itEnd = m_tSubGraphs.End();
00660   for (; it != itEnd; ++it) {
00661     if (subgraph == **it) {
00662       return true;
00663     }
00664   }
00665   return false;
00666 }
00667 
00668 //============================================================================
00672 template<class TSG, class TF>
00673 void EGTGraph<TSG, TF>::CreateSubGraphsPowerSet() const
00674 {
00675   //Gets the set of Nodes.
00676   Nodes ptFuncNodes = GetCandidates();
00677 
00678   Nodes ptSelNodes;
00679   const UInt uNumNodes = ptFuncNodes.Size();
00680   const UInt uLimit = (1 << uNumNodes) - 1;
00681   for (UInt i=0; i<uLimit; ++i) {
00682     ptSelNodes.Clear();
00683     EGTVector<UInt>::Iterator it2    = m_tPowerSet[i].Begin();
00684     EGTVector<UInt>::Iterator it2End = m_tPowerSet[i].End();
00685     for (; it2 != it2End; ++it2) {
00686       ptSelNodes.PushBack(ptFuncNodes[*it2]);
00687     }
00688     if ((m_bConstraint == true && IsConnected(ptSelNodes) == true) || 
00689         (m_bConstraint == false)) {
00690       SubGraph* psg = new SubGraph(ptSelNodes, false);
00691       m_tSubGraphs.PushBack(psg);
00692     }
00693   }
00694 }
00695 
00696 //============================================================================
00703 template<class TSG, class TF>
00704 bool EGTGraph<TSG, TF>::IsConnected(Nodes nodes) const
00705 {
00706   if (nodes.Size() == 0) {
00707     return false;
00708   }
00709   else if (nodes.Size() == 1) {
00710     return true;
00711   }
00712   NodesIterator it, itTemp;
00713   Nodes onNodes;
00714   itTemp = nodes.Begin();
00715   onNodes.PushBack(*itTemp);
00716   nodes.Erase(itTemp);
00717 
00718   while (!onNodes.Empty()) {
00719     it = onNodes.Begin();
00720     Node* theNode = *it;
00721     onNodes.Erase(it);
00722 
00723     UInt uNumIn = theNode->GetNumInTerms();
00724     for (UInt i=0; i<uNumIn; ++i) {
00725       if (theNode->GetInTerm(i)->GetTerm()) {
00726         Node* node = theNode->GetInTerm(i)->GetDestNode();
00727         itTemp = std::find(nodes.Begin(), nodes.End(), node);
00728         if (itTemp != nodes.End()) {
00729           onNodes.PushBack(*itTemp);
00730           nodes.Erase(itTemp);
00731           if (nodes.Empty()) {
00732             return true;
00733           }
00734         }
00735       }
00736     }
00737     UInt uNumOut = theNode->GetNumOutTerms();
00738     for (UInt i=0; i<uNumOut; ++i) {
00739       if (theNode->GetOutTerm(i)->GetTerm()) {
00740         Node* node = theNode->GetOutTerm(i)->GetDestNode();
00741         itTemp = std::find(nodes.Begin(), nodes.End(), node);
00742         if (itTemp != nodes.End()) {
00743           onNodes.PushBack(*itTemp);
00744           nodes.Erase(itTemp);
00745           if (nodes.Empty()) {
00746             return true;
00747           }
00748         }
00749       }
00750     }
00751   }
00752   return false;
00753 }
00754 
00755 //============================================================================
00760 template<class TSG, class TF>
00761 void EGTGraph<TSG, TF>::CreatePowerSet(UInt to) const
00762 {
00763   // Generates subsets.
00764   EGTVector<UInt> temp;
00765   temp.PushBack(0);
00766   m_tPowerSet.PushBack(temp);
00767   
00768   if (to <= 1) {
00769     return;
00770   }
00771   for (UInt i=1; i<to; ++i) {
00772     EGTVector<UInt> temp2;
00773     temp2.PushBack(i);
00774     m_tPowerSet.PushBack(temp2);
00775     UInt uLimit = (1 << i) - 1;
00776     for (UInt j=0; j<uLimit; ++j) {
00777       EGTVector<UInt> tVec = m_tPowerSet[j];
00778       tVec.PushBack(i);
00779       m_tPowerSet.PushBack(tVec);
00780     }
00781   }
00782 }
00783 
00784 //============================================================================
00788 template<class TSG, class TF>
00789 void EGTGraph<TSG, TF>::AssignNodeID()
00790 {
00791   UInt i = 0;
00792   NodesIterator it    = m_tNodes.Begin();
00793   NodesIterator itEnd = m_tNodes.End();
00794   for (; it != itEnd; ++it) {
00795     (*it)->SetNodeID(i++);
00796   }
00797 }
00798 
00799 //============================================================================
00807 template<class TSG, class TF>
00808 TSG* EGTGraph<TSG, TF>::CreateComplementarySubGraph(UInt uNum, UInt uMax) const
00809 {
00810   if (uMax == 0) {
00811     uMax = Env::Instance().MaxNodeSize();
00812   }
00813 
00814   const SubGraph* ptSelSubGraph = GetSubGraph(uNum);
00815   if ((ptSelSubGraph == 0) ||
00816       (GetNumNodes() - ptSelSubGraph->GetNumNodes() > uMax)) {
00817     return 0;
00818   }
00819 
00820   Nodes tRestNodes;              //Set of remaining nodes
00821   NodesConstIterator itSel;    //Set of selected nodes
00822   UInt uNodeID;
00823   bool bFind = false;
00824 
00825   const Nodes* ptSelNodes = ptSelSubGraph->GetNodes();
00826   //For each Node of this Graph
00827   NodesConstIterator itEnd = m_tNodes.End();
00828   for (NodesConstIterator it = m_tNodes.Begin(); it != itEnd; ++it) {
00829     //If the Node is not composed of the SubGraph, adds it to "tRestNodes".
00830     uNodeID = (*it)->GetNodeID();
00831     for (itSel = ptSelNodes->Begin(); itSel != ptSelNodes->End(); ++itSel) {
00832       if (uNodeID == (*itSel)->GetNodeID()) {
00833         bFind = true;
00834         break;
00835       }
00836     }
00837     if (bFind == true) {
00838       bFind = false;
00839     }
00840     else {
00841       tRestNodes.PushBack(*it);
00842     }
00843   }
00844   SubGraph* ptNewSubGraph = new SubGraph(tRestNodes, false);
00845   return ptNewSubGraph;
00846 }
00847 
00848 //============================================================================
00856 template<class TSG, class TF>
00857 const TSG* EGTGraph<TSG, TF>::GetSubGraph(const SubGraph& subgraph,
00858   UInt* uNum) const
00859 {
00860   if (m_tSubGraphs.Empty()) {
00861     CreateSubGraphs();
00862   }
00863   
00864 #ifdef DISPLAY_NUM_COMPATIBLE
00865   UInt n = 0;
00866   SubGraphsConstIterator it0    = m_tSubGraphs.Begin();
00867   SubGraphsConstIterator itEnd0 = m_tSubGraphs.End();
00868   for (; it0 != itEnd0; ++it0) {
00869     if (subgraph.IsCompatible(**it0) == true) {
00870       ++n;
00871     }
00872   }
00873   std::cout << n << '/' << m_tSubGraphs.Size() << '\n';
00874 #endif // DISPLAY_NUM_COMPATIBLE
00875 
00876   UInt uFrom = EGRandom::NextInt(0, m_tSubGraphs.Size()-1);
00877   SubGraphsConstIterator itBegin = m_tSubGraphs.Begin();
00878   SubGraphsConstIterator it = itBegin;
00879   std::advance(it, uFrom);
00880   SubGraphsConstIterator itFrom = it;
00881   SubGraphsConstIterator itEnd = m_tSubGraphs.End();
00882   for (; it != itEnd; ++it) {
00883     if (subgraph.IsCompatible(**it) == true) {
00884       *uNum = std::distance(itBegin, it);
00885       return *it;
00886     }
00887   }
00888   for (it = m_tSubGraphs.Begin(); it != itFrom; ++it) {
00889     if (subgraph.IsCompatible(**it) == true) {
00890       *uNum = std::distance(itBegin, it);
00891       return *it;
00892     }
00893   }
00894   return 0;
00895 }
00896 
00897 //============================================================================
00903 template<class TSG, class TF>
00904 TSG* EGTGraph<TSG, TF>::GetSubGraph(UInt uNum)
00905 {
00906   if (m_tSubGraphs.Empty()) {
00907     CreateSubGraphs();
00908   }
00909   if (uNum >= m_tSubGraphs.Size()) {
00910     return 0;
00911   }
00912   else {
00913     return m_tSubGraphs[uNum];
00914   }
00915 }
00916 
00917 //============================================================================
00923 template<class TSG, class TF>
00924 const TSG* EGTGraph<TSG, TF>::GetSubGraph(UInt uNum) const
00925 {
00926   if (m_tSubGraphs.Empty()) {
00927     CreateSubGraphs();
00928   }
00929   if (uNum >= m_tSubGraphs.Size()) {
00930     return 0;
00931   }
00932   else {
00933     return m_tSubGraphs[uNum];
00934   }
00935 }
00936 
00937 //============================================================================
00942 template<class TSG, class TF>
00943 void EGTGraph<TSG, TF>::Display(std::ostream& out) const
00944 {
00945   out << "Fitness  : " << GetFitness() << '\n';
00946   out << "NodeSize : " << m_tNodes.Size() << '\n';
00947   NodesConstIterator itEnd = m_tNodes.End();
00948   for (NodesConstIterator it = m_tNodes.Begin(); it != itEnd; ++it) {
00949     (*it)->Display(out);
00950   }
00951   if (!m_tSubGraphs.Empty()) {
00952     out << "\nSubGraphList\n";
00953     UInt i = 0;
00954     SubGraphsConstIterator itEnd2 = m_tSubGraphs.End();
00955     for (SubGraphsConstIterator it = m_tSubGraphs.Begin(); it!=itEnd2; ++it) {
00956       out << "*****SubGraph " << ++i << "*****\n";
00957       (*it)->Display(out);
00958     }
00959   }
00960   out << '\n';
00961   SaveGraph(out);
00962   m_tFitness.Display(out, true);
00963   out << '\n';
00964   m_tFitness.Display(out, false);
00965 }
00966 
00967 //============================================================================
00975 template<class TSG, class TF>
00976 void EGTGraph<TSG, TF>::DisplayFitness(std::ostream& out, bool title = false) const
00977 {
00978   m_tFitness.Display(out, title);
00979 }
00980 
00981 //============================================================================
00985 template<class TSG, class TF>
00986 void EGTGraph<TSG, TF>::DisplayDot(std::ostream& out) const
00987 {
00988   out << "digraph circuit_graph {\n";
00989   out << "rankdir=\"LR\";\n";
00990   NodesConstIterator itEnd = m_tNodes.End();
00991 
00992   for (NodesConstIterator it = m_tNodes.Begin(); it != itEnd; ++it) {
00993     out << (*it)->GetName() << " [style=filled,fillcolor=";
00994     switch ((*it)->GetOrigin()) {
00995     case ORI_UNDEF:       out << "gray90"; break;      // undef
00996     case ORI_CROSSOVER_A: out << "aquamarine"; break;  // crossover a
00997     case ORI_CROSSOVER_B: out << "yellow"; break;      // crossover b
00998     case ORI_MUTATION_A:  out << "gray90"; break;      // mutation a
00999     case ORI_MUTATION_B:  out << "violet"; break;      // mutation b
01000     case ORI_SELECTION:   out << "gray90"; break;      // selection
01001     default: out << "gray90"; break;
01002     }
01003     out << "];\n";
01004   }
01005 
01006   for (NodesConstIterator it = m_tNodes.Begin(); it != itEnd; ++it) {
01007     UInt uNumOut = (*it)->GetNumOutTerms();
01008     for (UInt i=0; i<uNumOut; ++i) {
01009       out << (*it)->GetName() << " -> ";
01010       out << (*it)->GetOutTerm(i)->GetDestNode()->GetName();
01011 
01012       out << " [label=\"";
01013       out << (*it)->GetOutLabel(i);
01014       UInt in = (*it)->GetOutTerm(i)->GetTerm()->GetTermNum();
01015       out << (*it)->GetOutTerm(i)->GetDestNode()->GetInLabel(in);
01016       out << "\"];\n";
01017     }
01018   }
01019   out << "};\n";
01020 }
01021 
01022 //============================================================================
01023 
01024 template<class TSG, class TF>
01025 typename EGTGraph<TSG, TF>::Node*
01026 EGTGraph<TSG, TF>::m_tCreator = 0;
01027 
01028 template<class TSG, class TF>
01029 EGTVector<EGTVector<UInt> > EGTGraph<TSG, TF>::m_tPowerSet;
01030 
01031 template<class TSG, class TF>
01032 bool EGTGraph<TSG, TF>::m_bConstraint = false;
01033 
01034 #endif //INCLUDE__GRAPH_H__FILE