📄 gapopulation.h
字号:
// Makes global instance
static void GACALL MakeInstance();
// Frees memory used by global
static void GACALL FreeInstance();
};// END CLASS DEFINITION GaDefaultPopulationOperations
// #pragma endregion
// #pragma region GaPopulation
// Manages populations
class GaPopulation
{
DEFINE_SYNC_CLASS;
protected:
// Selection operation with its parameters
GaSelectionPair _selection;
// Replacement operation with its parameters
GaReplacementPair _replacement;
// Scaling operation with its parameters
GaScalingPair _scaling;
// Coupling operation with its parameters
GaCouplingPair _coupling;
// TRUE if the scaled fitness value is used to sort the population.
// If nonscaled fitness value is used than this value is set FALSE.
bool _usingScaledFitness;
// Number of chromosomes in population
int _currentSize;
// Parameters of the population
GaPopulationParameters _parameters;
// Statistical information about population
GaStatistics _statistics;
// Chromosomes in the population
GaScaledChromosome** _chromosomes;
// Prototype for making new chromosomes
GaChromosome* _prototype;
// Comparator used for sorting the chromosomes
const GaFitnessComparator* _sortingComparator;
// Group of the best chromosomes in the population
GaSortedGroup _best;
// Group of the worst chromosomes in the population
GaSortedGroup _worst;
public:
// Prepares population for initialization
DLL_EXPORT
GaPopulation(GaChromosome* prototype,
const GaFitnessComparator* sortCopmaration,
const GaPopulationParameters& parameters,
bool setDefaultOperations = true);
// Free resources used by the population
DLL_EXPORT
virtual ~GaPopulation();
// Virtual copy constructor
DLL_EXPORT
virtual GaPopulation* GACALL Clone(bool copyChromosomes) const;
// Initialization of the population, it creates random chromosomes based on provided chromosome prototype.
DLL_EXPORT
virtual void GACALL InitializePopulation(bool fill = true);
// Fills the given array with indices of the best chromosomes in the population
// Returns number of saved chromosomes
DLL_EXPORT
virtual int GACALL GetBestChromosomes(int* results,
int start,
int numberOfChromosomes) const;
// Fills the given array with the best chromosomes in the population
// Returns number of saved chromosomes
DLL_EXPORT
virtual int GACALL GetBestChromosomes(GaChromosomePtr* results,
int start,
int numberOfChromosomes) const;
// Fills the given array with indices of the worst chromosomes in the population
// Returns number of saved chromosomes
DLL_EXPORT
virtual int GACALL GetWorsChromosomes(int* results,
int start,
int numberOfChromosomes) const;
// Fills the given array with the worst chromosomes in the population
// Returns number of saved chromosomes
DLL_EXPORT
virtual int GACALL GetWorsChromosomes(GaChromosomePtr* results,
int start,
int numberOfChromosomes) const;
// Replaces chromosome at given position with new one
DLL_EXPORT
virtual void GACALL Replace(int index,
GaChromosomePtr newChromosome);
// Replaces group chromosomes with new ones
DLL_EXPORT
virtual void GACALL ReplaceGroup(int* indices,
GaChromosomePtr* newChromosomes,
int numberOfChromosomes);
// Inserts chromosome into population
DLL_EXPORT
virtual void GACALL Insert(GaChromosomePtr chromosome);
// Inserts a group of choromosomes in population
DLL_EXPORT
virtual void GACALL InsertGroup(int numberOfChromosomes,
GaChromosomePtr* chromosomes);
// Removes chromosome at given position
DLL_EXPORT
virtual void GACALL Remove(int chromosome);
// Removes chromosomes at given positions
DLL_EXPORT
virtual void GACALL RemoveGroup(int* chromosomes,
int numberOfChromosomes);
// Preapares population for new generation
DLL_EXPORT
virtual void GACALL NextGeneration();
// Notifies population that it inherits evolution from another population
DLL_EXPORT
virtual void GACALL EndOfGeneration(const GaPopulation& previous);
// Notifies population that the evolution for current generation is finished
DLL_EXPORT
virtual void GACALL EndOfGeneration();
// Removes all chromosomes from population and clears statistics
DLL_EXPORT
virtual void GACALL Clear(bool clearStatistics);
// Returns ranking of chromosome.
// If chromosome has no ranking returns -1.
DLL_EXPORT
virtual int GACALL GetChromosomeRanking(int chromosomeIndex,
bool reverse = false);
// Returns chromosome at given position
DLL_EXPORT
virtual GaScaledChromosome& GACALL GetAt(int position) const;
// Returns statistical information about population
DLL_EXPORT
const GaStatistics& GACALL GetStatistics() const;
// Returns parameter of the population
DLL_EXPORT
const GaPopulationParameters& GACALL GetParameters() const;
// Sets parameter of the population
DLL_EXPORT
virtual void GACALL SetParameters(const GaPopulationParameters& parameters);
// Returns TRUE if scaled fitness values are used for sorting the population
DLL_EXPORT
bool GACALL IsScaledFitnessUsed();
// Returns pair of selection operaion and its parameters
DLL_EXPORT
const GaSelectionPair& GACALL Selection() const;
// Returns pair of replacement operaion and its parameters
DLL_EXPORT
const GaReplacementPair& GACALL Replacement() const;
// Returns pair of scaling operaion and its parameters
DLL_EXPORT
const GaScalingPair& GACALL Scaling() const;
// Returns pair of coupling operaion and its parameters
DLL_EXPORT
const GaCouplingPair& GACALL Coupling() const;
// Sets selection operation
DLL_EXPORT
void GACALL SetOperation(GaSelectionOperation* operation,
GaSelectionParams* parameters);
// Sets replacement operation
DLL_EXPORT
void GACALL SetOperation(GaReplacementOperation* operation,
GaReplacementParams* parameters);
// Sets scaling operation
DLL_EXPORT
void GACALL SetOperation(GaScalingOperation* operation,
GaScalingParams* parameters);
// Sets coupling operation
DLL_EXPORT
void GACALL SetOperation(GaCouplingOperation* operation,
GaCouplingParams* parameters);
// Sets parameters for selection operation
DLL_EXPORT
void GACALL SetOperationParams(GaSelectionParams* parameters);
// Sets parameters for replacement operation
DLL_EXPORT
void GACALL SetOperationParams(GaReplacementParams* parameters);
// Sets parameters for scaling operation
DLL_EXPORT
void GACALL SetOperationParams(GaScalingParams* parameters);
// Sets parameters for coupling operation
DLL_EXPORT
void GACALL SetOperationParams(GaCouplingParams* parameters);
// Returns comparator used for sorting the chromosomes
DLL_EXPORT
const GaFitnessComparator* GACALL GetSortComparator() const;
// Sets comparator used for sorting the chromosomes
DLL_EXPORT
void GACALL SetSortComparator(const GaFitnessComparator* comparator);
// Returns reference to an object which represents prototype of all chromosomes in the population
DLL_EXPORT
const GaChromosome& GACALL GetPrototype() const;
// Returns number of chromosomes in population
int GACALL GetCurrentSize() const;
protected:
// Adds chromosomes from the population to the best chromosomes group which sholud be there
DLL_EXPORT
void GACALL RefreshBestGroup();
// Adds chromosomes from the population to the worst chromosomes group which sholud be there
DLL_EXPORT
void GACALL RefreshWorstGroup();
// Updates statistics for population after replacing chromosome(s)
DLL_EXPORT
void GACALL UpdateStatistics(float fitnessChange,
float scaledFitnessChange);
// Should be called when scaling operation is changed
DLL_EXPORT
void GACALL RescaleAll();
// Sorting of the population
virtual void GACALL ResortPopulation(bool refreshFitnesses,
bool scalingChanged,
bool comparatorChanged);
// Comparation of chromosomes for sorting population
DLL_EXPORT
static int APICALL SortComparation(const void* first,
const void* second);
};// END CLASS DEFINITION GaPopulation
// #pragma endregion
} // Population
#endif // __GA_POPULATION_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -