home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-16 | 5.0 KB | 150 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CPopulation.h ©1995-97 Timo Eloranta All rights reserved.
- // ===========================================================================
- // CPopulation.cp (press Command-Tab to open the associated source file)
-
- #pragma once
-
- #include <vector.h> // MSL
- #include <iterator.h> // MSL
-
- #include "MyStructs.h"
- #include "CGraphDrawing.h"
-
- typedef vector<CGraphPtr, allocator<CGraphPtr> > GraphPtrVector;
- typedef vector<UInt16, allocator<UInt16> > UInt16Vector;
- typedef vector<SNodePair, allocator<SNodePair> > SNodePairVector;
-
- class LAnimateCursor;
-
- class CPopulation
- {
- private:
- // === These are set before running ===
- Int16 mPopSize, // Population size
- mMaxRuns, // Maximum nbr of test runs
- mNodeCount, // Nbr of nodes in the graph
- mEdgeCount, // Nbr of edges in the graph
- mGridSize, // The size (N) of the grid (NxN)
- mCrossoverProb, // Crossover probability [0...100]
- mMutationProb; // Mutation probability [0...100]
- Boolean mTermCondExists; // Is there a termination condition?
-
- SSelection mSelection; // Selection variables (see MyStructs.h)
- STermination mTermination; // Termination condition
-
- // === Runtime variables ===
- Int32 mCurGeneration, // Nbr of current generation
- mBestGeneration, // Nbr of the generation when we found
- // the current best ever chromosome
- mLeastCrossings, // Smallest nbr of crossings in this gener.
- mAvgCrossings, // Avg. nbr of crossings in this gener.
- mRunningTime, // Running time in ticks (1s = 60ticks)
- mBestTime; // Time (tickcount) when we found
- // the current best ever chromosome
-
- GraphPtrVector mDrawings; // A list of chromosomes
- SGraphPtrComparator mComparator; // A comparator for this list
-
- UInt16Vector mSelectionArray; // An array used in the
- // selection process
-
- CGraphDrawing mBestEver; // The fittest chromosome
- // we've ever had...
- CGraphDrawing mCrossTemp1, // A pair of chromosomes which
- mCrossTemp2; // are used in crossover phase
-
- // === Private methods ===
- void AddCrossMemories();
- void SetUpEdges( SNodePairVector *inEdgeArray );
- void InitBestEver();
- void InitSelectionArray();
- Boolean DoOneIteration();
- void DoSelection();
- Int16 SelectOne();
- void DoMutate();
- void DoCrossover();
- void DoRectCrossover ( CGraphPtr inCrosser1,
- CGraphPtr inCrosser2 );
-
- Boolean TerminationCondition();
-
- public:
- // === Public methods ===
- CPopulation() {};
- ~CPopulation();
-
- void JunkGraphs();
-
- void Initialize( Boolean inFirstTime,
- SGraphSize *inGraphSize = nil,
- SNodePairVector *inEdgeArray = nil );
-
- // Returns true if new best was found
- Boolean Evaluate( LAnimateCursor *inSpinCursor = nil );
-
- void DoIterate( Boolean &outGameIsOver,
- Boolean &outThereIsANewKing,
- Int16 inMaxSpendTimeOnce );
-
- void SetProbabilities( Int16 inCrossoverProb,
- Int16 inMutationProb );
- void SetSelection( SSelection &inSelection );
- void SetTermination( STermination &inTermination );
- void SetSizes( Int16 inGridSize,
- Int16 inPopSize );
- CGraphPtr GetBestEver() const;
-
- void GetBestEverFitness( CFitness &outFitness );
-
- Int16 GetPopSize() const;
- Int16 GetNodeCount() const;
- Int16 GetEdgeCount() const;
- Int16 GetGridSize() const;
- Int32 GetCurGeneration() const;
- Int32 GetRunningTime() const;
- Int32 GetBestGeneration() const;
- Int32 GetNoChangeGener() const;
- Int32 GetNoChangeTime() const;
- Int32 GetLeastCrossings() const;
- Int32 GetAvgCrossings() const;
- };
-
- Int16 AutomSelectionStep( Int16 inSelectionMinimum, Int16 inPopSize );
-
-
- // ===========================================================================
- // • Inline Functions
- // ===========================================================================
-
- inline CGraphPtr
- CPopulation::GetBestEver() const
- { return (CGraphPtr) &mBestEver; }
- inline void
- CPopulation::GetBestEverFitness( CFitness &outFitness )
- { mBestEver.GetFitness( outFitness ); }
- inline Int16
- CPopulation::GetPopSize() const { return mPopSize; }
- inline Int16
- CPopulation::GetNodeCount() const { return mNodeCount; }
- inline Int16
- CPopulation::GetEdgeCount() const { return mEdgeCount; }
- inline Int16
- CPopulation::GetGridSize() const { return mGridSize; }
- inline Int32
- CPopulation::GetCurGeneration() const { return mCurGeneration; }
- inline Int32
- CPopulation::GetRunningTime() const { return mRunningTime; }
- inline Int32
- CPopulation::GetBestGeneration() const { return mBestGeneration; }
- inline Int32
- CPopulation::GetNoChangeGener() const { return mCurGeneration -
- mBestGeneration; }
- inline Int32
- CPopulation::GetNoChangeTime() const { return mRunningTime -
- mBestTime; }
- inline Int32
- CPopulation::GetLeastCrossings() const { return mLeastCrossings; }
- inline Int32
- CPopulation::GetAvgCrossings() const { return mAvgCrossings; }
-