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

  1. // ===========================================================================
  2. //    CGraphNodes.h        ©1995-97 Timo Eloranta        All rights reserved.
  3. // ===========================================================================
  4. //    This class takes care of storing the nodes of a single graph drawing
  5. //    (i.e. instances of this class are owned by CGraphDrawing objects...).
  6. //    Since almost all of the mutation and crossover operations manipulate
  7. //    the positions of the nodes, you can find the code for those operations
  8. //    from this class.
  9.  
  10. #pragma once
  11.  
  12. #include "CNode.h"
  13.  
  14. #include <iterator.h>        // MSL
  15. #include <vector.h>            // MSL
  16.  
  17. typedef vector<CNode, allocator<CNode> >    NodeVector;
  18.  
  19. class CFitness;
  20.  
  21. class CGraphNodes
  22. {
  23.     friend    class    CGraphDrawing;
  24.  
  25.     private:
  26.         Int16            mNodeCount;
  27.         Int16            mGridSize;
  28.  
  29.         NodeVector    mNodesArray;    // My array of nodes
  30.         
  31.  
  32.         CNodePtr    NodeInSquare(         Int16     inX,    Int16     inY) const;
  33.         Boolean        InsideTheGrid(        Int16    inX,    Int16    inY) const;
  34.         Boolean        InsideAndFree(        Int16    inX,    Int16    inY) const;
  35.                                                                                                         
  36.         void        SetNode(             Int16     inNodeNbr, 
  37.                                         Int16     inX,     Int16 inY);
  38.         void        SetNodePos(         Int16     inNodeNbr, 
  39.                                         Int16     inX,     Int16 inY);
  40.         void        ClearAll();
  41.  
  42.         Int16        GetRdmRowOrColWithNode( Boolean inRowWanted) const;    
  43.         Int16        GetRdmRowOrColWithException( Int16 inNotThis) const;
  44.         Int16        GetRdmRowOrColWithAtLeast2Nodes( Boolean inRowWanted ) const;
  45.         
  46.         void        GetRdmTwoFromRowOrCol(  Boolean inColsWanted,
  47.                                             Int16    inFixedRowOrCol,
  48.                                             Int16    &outNodeNbr1, 
  49.                                             Int16    &outNodeNbr2,
  50.                                             Int16     &outRC1,       
  51.                                             Int16     &outRC2 ) const;
  52.  
  53.     public:
  54.         CGraphNodes();
  55.         
  56.         void     Initialize(     Int16     inNodeCount, 
  57.                                 Int16     inGridSize,
  58.                                 Boolean    inFirstTime);
  59.  
  60.                                     //
  61.         void    SwapRows();            //    Mutation variations
  62.         void    SwapCols();            //    
  63.         Boolean    SwapInRowOrCol( Boolean inRowSwap);
  64.         void    SingleMutate();        //
  65.         Boolean    TinyMutate();        //
  66.         Boolean TinyMoveEdge( CNodePtr inNode1, CNodePtr inNode2 );
  67.         void    SmallMutate();        //
  68.         Boolean    LargeContMutate();    //
  69.         Boolean    InvertRow();        //
  70.         Boolean    InvertPartRow(     Boolean inInvertWholeRow = false);
  71.         Boolean    InvertCol();        //
  72.         Boolean    InvertPartCol(     Boolean inInvertWholeCol = false);
  73.         void    MoveEdge( CNodePtr inNode1, CNodePtr inNode2,
  74.                                 Boolean inPreserveLength );
  75.         void    Move3ConnectedNodes( CNodePtr inNode1, CNodePtr inNode2,
  76.                                 CNodePtr inNode3 );
  77.                                     //
  78.  
  79.         void    RectCrossover(     const Rect             &inSourceRect,
  80.                                 const Rect            &inDestRect, 
  81.                                 const CGraphNodes    &inBaseGraphNodes,
  82.                                 const CGraphNodes    &inRectGraphNodes );
  83.                                 
  84.         Boolean    ThreeNodeCrossover( Int16 inNbr1, Int16    inNbr2, Int16 inNbr3,
  85.                                 Point &inTo1, Point &inTo2, Point &inTo3 );
  86.                                 
  87.         void    BruteForceClosestPairs( CFitness &inFitness) const;
  88.  
  89.         void    DrawAll(         const Rect & inOneOneRect, 
  90.                                 Int16          inSquareSize) const;
  91.         void    ResetAll();
  92.  
  93.         Int16            GetGridSize( ) const
  94.                             { return mGridSize; };
  95.         
  96.         CNodePtr        GetNodePtrByNbr( Int16 inNodeNbr ) const;
  97.         
  98.         CGraphNodes     &operator=(    const CGraphNodes & inGN);
  99.         
  100. };
  101.  
  102. // ===========================================================================
  103. // • Inline Functions
  104. // ===========================================================================
  105.  
  106. // ---------------------------------------------------------------------------
  107. //        • SetNode        (PRIVATE)
  108. //
  109. //          Called by:    CGraphNodes::Initialize
  110. //                        CGraphNodes::RectCrossover
  111. // ---------------------------------------------------------------------------
  112.  
  113. inline void
  114. CGraphNodes::SetNode( Int16 inNodeNbr, Int16 inX, Int16 inY )
  115. {
  116.     mNodesArray[ inNodeNbr ].Set( inNodeNbr, inX, inY );
  117. }
  118.  
  119. // ---------------------------------------------------------------------------
  120. //        • SetNodePos    (PRIVATE)
  121. //
  122. //          Called by:    CGraphNodes::SingleMutate
  123. //                        CGraphNodes::TinyMutate
  124. //                        CGraphNodes::TinyMoveEdge
  125. //                        CGraphNodes::MoveEdge
  126. //                        CGraphNodes::Move3ConnectedNodes
  127. //                        CGraphNodes::SmallMutate
  128. //                        CGraphNodes::ThreeNodeCrossover
  129. // ---------------------------------------------------------------------------
  130.  
  131. inline void
  132. CGraphNodes::SetNodePos( Int16 inNodeNbr, Int16 inX, Int16 inY )
  133. {
  134.     mNodesArray[ inNodeNbr ].SetNodePos( inX, inY );
  135. }
  136.  
  137. // ---------------------------------------------------------------------------
  138. //        • GetNodePtrByNbr
  139. //
  140. //          Called by:    CGraphDrawing::ValidNewEdge
  141. //                        CGraphDrawing::SetNewEdge
  142. //                        CGraphDrawing::ThreeNodeCrossover
  143. //                        CGraphDrawing::CopyEdges
  144. // ---------------------------------------------------------------------------
  145.  
  146. inline CNodePtr
  147. CGraphNodes::GetNodePtrByNbr( Int16 inNodeNbr ) const
  148. {
  149.     return (CNodePtr) &( mNodesArray[ inNodeNbr ] );
  150. }
  151.  
  152. // ---------------------------------------------------------------------------
  153. //        • ResetAll
  154. //
  155. //          Called by:    CGraphDrawing::SmarterCountSect
  156. // ---------------------------------------------------------------------------
  157. //    Set all nodes as non-movers
  158.  
  159. inline void
  160. CGraphNodes::ResetAll( )
  161. {
  162.     NodeVector::iterator    theIter = mNodesArray.begin() + 1;
  163.     
  164.     while ( theIter != mNodesArray.end() ) {
  165.         theIter -> ResetHasMoved();
  166.         ++theIter;
  167.     }
  168. }
  169.  
  170. // ---------------------------------------------------------------------------
  171. //        • InsideTheGrid        (PRIVATE)
  172. //
  173. //          Called by:    CGraphNodes::MoveEdge
  174. //                        CGraphNodes::Move3ConnectedNodes
  175. //                        CGraphNodes::InsideAndFree
  176. // ---------------------------------------------------------------------------
  177.  
  178. inline Boolean
  179. CGraphNodes::InsideTheGrid(    Int16 inX, Int16 inY ) const
  180. {
  181.     return ( inX >= 1 && inX <= mGridSize &&
  182.              inY >= 1 && inY <= mGridSize );
  183. }
  184.  
  185. // ---------------------------------------------------------------------------
  186. //        • InsideAndFree        (PRIVATE)
  187. //
  188. //          Called by:    CGraphNodes::TinyMutate
  189. //                        CGraphNodes::TinyMoveEdge
  190. //                        CGraphNodes::MoveEdge etc.
  191. // ---------------------------------------------------------------------------
  192.  
  193. inline Boolean
  194. CGraphNodes::InsideAndFree(    Int16 inX, Int16 inY ) const
  195. {
  196.     return ( InsideTheGrid(    inX, inY ) && 
  197.              ! NodeInSquare( inX, inY ) );
  198. }
  199.