home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / TimGA 1.2.1 / .h / CGraphDrawing.h < prev    next >
Encoding:
Text File  |  1997-07-16  |  4.7 KB  |  167 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CGraphDrawing.h        ©1995-97 Timo Eloranta        All rights reserved.
  3. // ===========================================================================
  4. //    An instance of the CGraphDrawing class represents a single possibility of 
  5. //    drawing the given graph. The population consists of these objects.
  6.  
  7. #pragma once
  8.  
  9. #include <PP_Constants.h>
  10.  
  11. #include "CEdge.h"
  12. #include "CFitness.h"
  13. #include "CGraphNodes.h"
  14. #include "MyStructs.h"
  15.  
  16. #include <vector.h>            // MSL
  17. #include <iterator.h>        // MSL
  18.  
  19. typedef CGraphDrawing*                        CGraphPtr;
  20. typedef vector<CEdge, allocator<CEdge> >    EdgeVector;
  21.  
  22. class CCrossMemory;
  23.  
  24. class CGraphDrawing
  25. {
  26.     private:
  27.         Int16        mEdgeCount;
  28.         Int16        mSelectionOriginal;
  29.         Boolean        mEvaluationNeeded;
  30.         Boolean        mMemoryIsGarbage;
  31.         CFitness    mFitness;
  32.  
  33.         CGraphNodes        mNodes;            // My nodes storage
  34.         EdgeVector        mEdges;            // My array of edges
  35.         
  36.         CCrossMemory    *mCrossMemo;    // Structure which holds the
  37.                                         // info on edge crossings
  38.         
  39.         Boolean        EdgeMutation( Boolean inPreserveLength, Boolean inTiny );
  40.         Boolean        TwoEdgeMutation();
  41.         
  42.         void        Evaluate( Int32 inCurLeastCrossings );
  43.                               
  44.         Int32        SimpleCountSect();
  45.         Int32        SmarterCountSect();
  46.         void        CountEdgeLengths();
  47.         void        CopyEdges( const CGraphDrawing & inGD);
  48.         
  49.         Boolean        ThreeConnectedNodes(     CNodePtr    &outNode1,
  50.                                             CNodePtr    &outNode2,
  51.                                             CNodePtr    &outNode3 );
  52.  
  53.     public:
  54.         CGraphDrawing();
  55.         ~CGraphDrawing();
  56.         
  57.         void     Initialize(     Int16     inNodeCount, 
  58.                                 Int16     inEdgeCount,
  59.                                 Int16     inGridSize,
  60.                                 Boolean    inFirstTime);
  61.                                 
  62.         void    AddCrossMemory();
  63.         void    JunkCrossMemory();
  64.  
  65.         Boolean ValidNewEdge(     Int16     inNodeNbr1, 
  66.                                 Int16     inNodeNbr2) const;
  67.         
  68.         void    SetNewEdge(     Int16     inEdgeNbr, 
  69.                                 Int16     inNodeNbr1,
  70.                                 Int16     inNodeNbr2);
  71.                                                                 
  72.         void    Mutate();
  73.         void    RectCrossover(     const Rect             &inSourceRect,
  74.                                 const Rect            &inDestRect, 
  75.                                 const CGraphDrawing &inBaseGraph,
  76.                                 const CGraphDrawing &inRectGraph );
  77.         void    ThreeNodeCrossover( CGraphPtr    inCrosser2 );
  78.  
  79.         void    GetFitness(     CFitness &outFitness,
  80.                                 Int32    inCurLeastCrossings = max_Int32 );
  81.  
  82.         Int16    GetGridSize( ) const
  83.                         { return mNodes.GetGridSize(); };
  84.  
  85.         CGraphNodes *    GetNodes( ) const
  86.                         { return (CGraphNodes *) &mNodes; };
  87.         
  88.         void    SetSelectionOriginal( Int16 inGraphNbr )
  89.                         { mSelectionOriginal = inGraphNbr; };
  90.                     
  91.         Int16    GetSelectionOriginal( ) const
  92.                         { return mSelectionOriginal; };
  93.         
  94.         void    DrawEdges(         Int16     inOneOneXY,
  95.                                 Int16     inSquareSize) const;
  96.         void    DrawNodes(         const Rect & inOneOneRect, 
  97.                                 Int16     inSquareSize) const;
  98.  
  99.         void     BestCopy(        const CGraphDrawing & inGD,
  100.                                 Boolean    inFirstTime );
  101.         void    RuntimeCopy(    const CGraphDrawing & inGD,
  102.                                 Boolean inCopyCrossMemory );
  103.  
  104.         Boolean        operator==( const CGraphDrawing & inGD) const
  105.                         { return ( mFitness == inGD.mFitness ); };
  106.         Boolean        operator<(     const CGraphDrawing & inGD) const
  107.                         { return ( mFitness < inGD.mFitness ); };
  108. };
  109.  
  110. struct SGraphPtrComparator {
  111.     Boolean operator()( const CGraphPtr& in1st,
  112.                         const CGraphPtr& in2nd ) {
  113.         return (*in1st) < (*in2nd);
  114.     }
  115. };
  116.  
  117. null_template
  118. struct iterator_trait <CGraphDrawing*const*> {
  119.     typedef ptrdiff_t                    distance_type;
  120.     typedef CGraphDrawing*const          value_type;
  121.     typedef random_access_iterator_tag   iterator_category;
  122. };
  123.  
  124. null_template
  125. struct iterator_trait <CGraphDrawing**> {
  126.     typedef ptrdiff_t                    distance_type;
  127.     typedef CGraphDrawing*               value_type;
  128.     typedef random_access_iterator_tag   iterator_category;
  129. };
  130.  
  131. // ===========================================================================
  132. // • Inline Functions
  133. // ===========================================================================
  134.  
  135. // ---------------------------------------------------------------------------
  136. //        • DrawNodes
  137. //
  138. //          Called by:    CGraphPane::DrawNodes
  139. // ---------------------------------------------------------------------------
  140.  
  141. inline void
  142. CGraphDrawing::DrawNodes( const Rect &inOneOneRect, Int16 inSquareSize) const
  143. {
  144.     mNodes.DrawAll( inOneOneRect, inSquareSize);
  145. }
  146.  
  147. // ---------------------------------------------------------------------------
  148. //        • GetFitness
  149. //
  150. //          Called by:    CPopulation::InitBestEver
  151. //                        CPopulation::Evaluate
  152. //                        CPopulation::GetBestEverFitness
  153. // ---------------------------------------------------------------------------
  154.  
  155. inline void 
  156. CGraphDrawing::GetFitness( 
  157.     CFitness     &outFitness, 
  158.     Int32         inCurLeastCrossings )
  159. {
  160.     // Evaluate only if necessary (if the chromosome has changed)
  161.     
  162.     if ( mEvaluationNeeded )
  163.         Evaluate( inCurLeastCrossings );
  164.         
  165.     outFitness = mFitness;
  166. }
  167.