home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-16 | 5.9 KB | 199 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CGraphNodes.h ©1995-97 Timo Eloranta All rights reserved.
- // ===========================================================================
- // This class takes care of storing the nodes of a single graph drawing
- // (i.e. instances of this class are owned by CGraphDrawing objects...).
- // Since almost all of the mutation and crossover operations manipulate
- // the positions of the nodes, you can find the code for those operations
- // from this class.
-
- #pragma once
-
- #include "CNode.h"
-
- #include <iterator.h> // MSL
- #include <vector.h> // MSL
-
- typedef vector<CNode, allocator<CNode> > NodeVector;
-
- class CFitness;
-
- class CGraphNodes
- {
- friend class CGraphDrawing;
-
- private:
- Int16 mNodeCount;
- Int16 mGridSize;
-
- NodeVector mNodesArray; // My array of nodes
-
-
- CNodePtr NodeInSquare( Int16 inX, Int16 inY) const;
- Boolean InsideTheGrid( Int16 inX, Int16 inY) const;
- Boolean InsideAndFree( Int16 inX, Int16 inY) const;
-
- void SetNode( Int16 inNodeNbr,
- Int16 inX, Int16 inY);
- void SetNodePos( Int16 inNodeNbr,
- Int16 inX, Int16 inY);
- void ClearAll();
-
- Int16 GetRdmRowOrColWithNode( Boolean inRowWanted) const;
- Int16 GetRdmRowOrColWithException( Int16 inNotThis) const;
- Int16 GetRdmRowOrColWithAtLeast2Nodes( Boolean inRowWanted ) const;
-
- void GetRdmTwoFromRowOrCol( Boolean inColsWanted,
- Int16 inFixedRowOrCol,
- Int16 &outNodeNbr1,
- Int16 &outNodeNbr2,
- Int16 &outRC1,
- Int16 &outRC2 ) const;
-
- public:
- CGraphNodes();
-
- void Initialize( Int16 inNodeCount,
- Int16 inGridSize,
- Boolean inFirstTime);
-
- //
- void SwapRows(); // Mutation variations
- void SwapCols(); //
- Boolean SwapInRowOrCol( Boolean inRowSwap);
- void SingleMutate(); //
- Boolean TinyMutate(); //
- Boolean TinyMoveEdge( CNodePtr inNode1, CNodePtr inNode2 );
- void SmallMutate(); //
- Boolean LargeContMutate(); //
- Boolean InvertRow(); //
- Boolean InvertPartRow( Boolean inInvertWholeRow = false);
- Boolean InvertCol(); //
- Boolean InvertPartCol( Boolean inInvertWholeCol = false);
- void MoveEdge( CNodePtr inNode1, CNodePtr inNode2,
- Boolean inPreserveLength );
- void Move3ConnectedNodes( CNodePtr inNode1, CNodePtr inNode2,
- CNodePtr inNode3 );
- //
-
- void RectCrossover( const Rect &inSourceRect,
- const Rect &inDestRect,
- const CGraphNodes &inBaseGraphNodes,
- const CGraphNodes &inRectGraphNodes );
-
- Boolean ThreeNodeCrossover( Int16 inNbr1, Int16 inNbr2, Int16 inNbr3,
- Point &inTo1, Point &inTo2, Point &inTo3 );
-
- void BruteForceClosestPairs( CFitness &inFitness) const;
-
- void DrawAll( const Rect & inOneOneRect,
- Int16 inSquareSize) const;
- void ResetAll();
-
- Int16 GetGridSize( ) const
- { return mGridSize; };
-
- CNodePtr GetNodePtrByNbr( Int16 inNodeNbr ) const;
-
- CGraphNodes &operator=( const CGraphNodes & inGN);
-
- };
-
- // ===========================================================================
- // • Inline Functions
- // ===========================================================================
-
- // ---------------------------------------------------------------------------
- // • SetNode (PRIVATE)
- //
- // Called by: CGraphNodes::Initialize
- // CGraphNodes::RectCrossover
- // ---------------------------------------------------------------------------
-
- inline void
- CGraphNodes::SetNode( Int16 inNodeNbr, Int16 inX, Int16 inY )
- {
- mNodesArray[ inNodeNbr ].Set( inNodeNbr, inX, inY );
- }
-
- // ---------------------------------------------------------------------------
- // • SetNodePos (PRIVATE)
- //
- // Called by: CGraphNodes::SingleMutate
- // CGraphNodes::TinyMutate
- // CGraphNodes::TinyMoveEdge
- // CGraphNodes::MoveEdge
- // CGraphNodes::Move3ConnectedNodes
- // CGraphNodes::SmallMutate
- // CGraphNodes::ThreeNodeCrossover
- // ---------------------------------------------------------------------------
-
- inline void
- CGraphNodes::SetNodePos( Int16 inNodeNbr, Int16 inX, Int16 inY )
- {
- mNodesArray[ inNodeNbr ].SetNodePos( inX, inY );
- }
-
- // ---------------------------------------------------------------------------
- // • GetNodePtrByNbr
- //
- // Called by: CGraphDrawing::ValidNewEdge
- // CGraphDrawing::SetNewEdge
- // CGraphDrawing::ThreeNodeCrossover
- // CGraphDrawing::CopyEdges
- // ---------------------------------------------------------------------------
-
- inline CNodePtr
- CGraphNodes::GetNodePtrByNbr( Int16 inNodeNbr ) const
- {
- return (CNodePtr) &( mNodesArray[ inNodeNbr ] );
- }
-
- // ---------------------------------------------------------------------------
- // • ResetAll
- //
- // Called by: CGraphDrawing::SmarterCountSect
- // ---------------------------------------------------------------------------
- // Set all nodes as non-movers
-
- inline void
- CGraphNodes::ResetAll( )
- {
- NodeVector::iterator theIter = mNodesArray.begin() + 1;
-
- while ( theIter != mNodesArray.end() ) {
- theIter -> ResetHasMoved();
- ++theIter;
- }
- }
-
- // ---------------------------------------------------------------------------
- // • InsideTheGrid (PRIVATE)
- //
- // Called by: CGraphNodes::MoveEdge
- // CGraphNodes::Move3ConnectedNodes
- // CGraphNodes::InsideAndFree
- // ---------------------------------------------------------------------------
-
- inline Boolean
- CGraphNodes::InsideTheGrid( Int16 inX, Int16 inY ) const
- {
- return ( inX >= 1 && inX <= mGridSize &&
- inY >= 1 && inY <= mGridSize );
- }
-
- // ---------------------------------------------------------------------------
- // • InsideAndFree (PRIVATE)
- //
- // Called by: CGraphNodes::TinyMutate
- // CGraphNodes::TinyMoveEdge
- // CGraphNodes::MoveEdge etc.
- // ---------------------------------------------------------------------------
-
- inline Boolean
- CGraphNodes::InsideAndFree( Int16 inX, Int16 inY ) const
- {
- return ( InsideTheGrid( inX, inY ) &&
- ! NodeInSquare( inX, inY ) );
- }
-