📄 population.h
字号:
GAL_API
int GACALL GetWorsChromosomes(int* results,
int start,
int numberOfChromosomes) const;
/// <summary>This method fills provided array with indices of worst chromosomes in population. If population is not sorted this method can only
/// provide number of worst chromosomes which is equal to number of tracked worst chromosomes.
///
/// This method is not thread-safe.</summary>
/// <param name="results">pointer to array which is going to be filled.</param>
/// <param name="start">index of the last worst chromosome which is going to be copied.</param>
/// <param name="numberOfChromosomes">number of queried chromosome' indices.</param>
/// <returns>Method returns number of saved smart pointer to chromosomes in output array.</returns>
GAL_API
int GACALL GetWorsChromosomes(GaChromosomePtr* results,
int start,
int numberOfChromosomes) const;
/// <summary><c>Replace</c> method removes chromosome with specified index from population and insert new chromosome. Scaled fitness value of
/// new chromosome is automatically calculated. This method preserve sorting of chromosomes and update sorted group and statistical information.
///
/// This method is not thread-safe.</summary>
/// <param name="index">index of chromosome which is going to be removed.</param>
/// <param name="newChromosome">smart pointer to chromosome which is going to be inserted into population.</param>
/// <returns>Method returns 1 if chromosome is replaced, otherwise returns 0.</returns>
GAL_API
int GACALL Replace(int index,
GaChromosomePtr newChromosome);
/// <summary><c>Replace</c> method removes group of chromosomes with specified indices from population and insert new chromosomes. Scaled fitness values
/// of new chromosomes are automatically calculated. If index of one chromosome in specified more then once in array of chromosomes which
/// should be removed, results are unexpected and may lead to failures. This method preserve sorting of chromosomes and update sorted group and
/// statistical information.
///
/// This method is not thread-safe.</summary>
/// <param name="indices">pointer to array of indices of chromosomes which are going to be removed.</param>
/// <param name="newChromosomes">pointer to array of smart pointer to chromosomes which are going to be inserted into population.</param>
/// <param name="numberOfChromosomes">number of chromosomes which are going to be replaced.</param>
/// <returns>Method returns number of replaced chromosomes.</returns>
GAL_API
int GACALL ReplaceGroup(int* indices,
GaChromosomePtr* newChromosomes,
int numberOfChromosomes);
/// <summary><c>Insert</c> method inserts new chromosome into population. If population is not sorted new chromosome can be inserted if population
/// is resizable and is not full. If population is sorted new chromosome is inserted at appropriate place (according to sorting policy),
/// but if population is fixed-size or resizable but full, this method virtually extends population size by one before it inserts chromosome,
/// after inserting it removes chromosome with worst fitness value (new chromosome is also included). This method preserve sorting of chromosomes
/// and update sorted group and statistical information.
///
/// This method is not thread-safe.</summary>
/// <param name="chromosome">smart pointer to chromosome which is going to be inserted into population.</param>
/// <returns>Method returns 1 if chromosome is inserted, otherwise returns 0.</returns>
GAL_API
int GACALL Insert(GaChromosomePtr chromosome);
/// <summary><c>Insert</c> method inserts new chromosomes into population. If population is not sorted new chromosomes can be inserted if population
/// is resizable and is not full, if there is more new chromosomes then space in population, only first chromosomes are inserted. If population is
/// sorted new chromosomes are inserted at appropriate places (according to sorting policy), but if population is fixed-size or resizable but full,
/// this method virtually extends population size so it is able to host all chromosome before it inserts chromosomes, after inserting it
/// removes chromosomes with worst fitness values(new chromosomes are also included). This method preserve sorting of chromosomes and update sorted group
/// and statistical information.
///
/// This method is not thread-safe.</summary>
/// <param name="chromosomes">pointer to array of smart pointer to chromosomes which are going to be inserted into population.</param>
/// <param name="numberOfChromosomes">number of chromosomes which should be inserted.</param>
/// <returns>Method returns number of inserted chromosomes.</returns>
GAL_API
int GACALL InsertGroup(GaChromosomePtr* chromosomes,
int numberOfChromosomes);
/// <summary><c>Remove</c> method removes chromosome with specified index from population. To remove chromosome from population, it must be resizable.
/// This method preserve sorting of chromosomes and update sorted group and statistical information.
///
/// This method is not thread-safe.</summary>
/// <param name="chromosome">index of chromosome which is going to be removed.</param>
/// <returns>Method returns 1 if chromosome is removed, otherwise returns 0.</returns>
GAL_API
int GACALL Remove(int chromosome);
/// <summary><c>Remove</c> method removes chromosomes with specified indices from population. To remove chromosomes from population, it must be resizable.
/// If index of one chromosome in specified more then once in array of chromosomes which should be removed, results are unexpected and
/// may lead to failures. This method preserve sorting of chromosomes and update sorted group and statistical information.
///
/// This method is not thread-safe.</summary>
/// <param name="chromosomes">pointer to array of indices of chromosomes which are going to be removed.</param>
/// <param name="numberOfChromosomes">number of chromosomes which are going to be removed.</param>
/// <returns>Method returns number of removed chromosomes.</returns>
GAL_API
int GACALL RemoveGroup(int* chromosomes,
int numberOfChromosomes);
/// <summary><c>NextGeneration</c> method prepares population for new generation. It should be called before new cycle of execution of
/// genetic operations by genetic algorithm.
///
/// This method is not thread-safe.</summary>
inline void GACALL NextGeneration() { _statistics.NextGeneration(); }
/// <summary><c>EndOfGeneration</c> method is used for evolutions with non-overlapping population. It is used to finalize cycle of genetic operations'
/// execution and should be called by genetic algorithm at the end of a generation.
///
/// This method is not thread-safe.</summary>
/// <param name="previous">reference to population which is used in previous generation by the algorithm.</param>
inline void GACALL EndOfGeneration(const GaPopulation& previous)
{
// rescale population if needed
if( _configuration->Scaling().HasOperation() &&
_configuration->Scaling().GetOperation().NeedRescaling( *this, _configuration->Scaling().GetParameters() ) )
RescaleAll();
// copy statistics from previous population
_statistics.CopyFrom( previous._statistics, false, true, true );
}
/// <summary><c>EndOfGeneration</c> method is used for evolutions with overlapping population. It is used to finalize cycle of genetic operations'
/// execution and should be called by genetic algorithm at the end of a generation.
///
/// This method is not thread-safe.</summary>
inline void GACALL EndOfGeneration()
{
// rescale population if needed
if( _configuration->Scaling().HasOperation() &&
_configuration->Scaling().GetOperation().NeedRescaling( *this, _configuration->Scaling().GetParameters() ) )
RescaleAll();
}
/// <summary><c>Clear</c> method removes all chromosomes form population and clears statistical information if specified.
///
/// This method is not thread-safe.</summary>
/// <param name="clearStatistics">if this parameter is set to <c>true</c>, method clears statistics and removes chromosomes,
/// otherwise it keeps statistics.</param>
GAL_API
void GACALL Clear(bool clearStatistics);
/// <summary><c>GetChromosomeRanking</c> method returns ranking of chromosomes with specified index based on its fitness value (scaled or non-scaled).
/// If population is sorted, all chromosomes has ranking. If it is unsorted only chromosomes in best and worst sorted groups has ranking.
/// Also inverse chromosome ranking can be queried.
///
/// This method is not thread-safe.</summary>
/// <param name="chromosomeIndex">index of chromosome which rank is queried.</param>
/// <param name="reverse">if this is set to <c>true</c>, inverse ranking is queried, otherwise normal ranking is queried.</param>
/// <returns>Method returns rnking of the chromosome in the population. If chromosome has no ranking, method returns -1.</returns>
GAL_API
int GACALL GetChromosomeRanking(int chromosomeIndex,
bool reverse = false);
/// <summary>This method is not thread-safe.</summary>
/// <param name="position">index of chromosome.</param>
/// <returns>Method returns reference to scaled chromosome with specified index.</returns>
inline GaScaledChromosome& GACALL GetAt(int position) const { return *_chromosomes[ position ]; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to object which contains statistical information about population.</returns>
inline const GaStatistics& GACALL GetStatistics() const { return _statistics; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to population configuration.</returns>
inline const GaPopulationConfiguration& GetConfiguration() const { return *_configuration; }
/// <summary><c>SetConfiguration</c> method sets population's configuration.
///
/// This method is not thread-safe.</summary>
/// <param name="configuration">pointer to new configuration object.</param>
void SetConfiguration(GaPopulationConfiguration* configuration)
{
if( _configuration != configuration )
{
_configuration->UnbindPopulation( this );
_configuration = configuration;
if( _configuration )
_configuration->BindPopulation( this, true );
}
}
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns <c>true</c> if population use scale fitness values for sorting.
/// If population use non-scaled fitness values, it returns <c>false</c>. </returns>
inline bool GACALL IsScaledFitnessUsed() { return _usingScaledFitness; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns pointer to chromosome which is used as prototype during initialization of population.</returns>
inline const GaChromosome& GACALL GetPrototype() const { return *_prototype; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns number of chromosomes in population. If population is fixed size this method returns maximal number of chromosomes
/// if population is initialized, or 0 if it is not.</returns>
inline int GACALL GetCurrentSize() const { return _currentSize; }
private:
/// <summary><c>SetParameters</c> method sets parameters of population. It copies values of parameters into built-in parameters' object of population
/// and updates population according to new parameters.
///
/// This method is not thread-safe.</summary>
/// <param name="parameters">reference to object of new population's parameters.</param>
GAL_API
void SetParameters(const GaPopulationParameters& parameters);
/// <summary><c>SetSortComaprator</c> method sets fitness comparator which is used for sorting chromosomes. It update sorting of chromosomes,
/// sorted groups and statistical information according to new sorting policy.
///
/// This method is not thread-safe.</summary>
/// <param name="comparator">pointer to new fitness comparator.</param>
GAL_API
void SetSortComparator(const GaFitnessComparator* comparator);
/// <summary><c>RefreshBestGroup</c> method refreshes content of best sorted group.
///
/// This method is not thread-safe.</summary>
inline void GACALL RefreshBestGroup()
{
for( int i = 0; i < _currentSize; i++ )
_best.Add( i );
}
/// <summary><c>RefreshWorstGroup</c> method refreshes content of worst sorted group.
/// This method is not thread-safe.</summary>
inline void GACALL RefreshWorstGroup()
{
for( int i = 0; i < _currentSize; i++ )
_worst.Add( i );
}
/// <summary><c>UpdateStatistics</c> method updates all population statistics.
/// This method is not thread-safe.</summary>
/// <param name="fitnessChange">value by which sum of non-scaled fitness values of all chromosomes in population has change after last update.</param>
/// <param name="scaledFitnessChange">value by which sum of scaled fitness values of all chromosomes in population has change after last update.</param>
GAL_API
void GACALL UpdateStatistics(float fitnessChange,
float scaledFitnessChange);
/// <summary><c>RescaleAll</c> method recalculates scaled fitness values of all chromosomes in population and stores them.
/// It also updates population statistics.</summary>
GAL_API
void GACALL RescaleAll();
/// <summary>ResortPopulation method resorts population when some of part of sorting policy is changed. After resorting, method updates statistics.
/// If population is not sorted, this method updates sorted group.
///
/// This method is not thread-safe.</summary>
/// <param name="refreshFitnesses">if this parameter is set to true, before resorting all fitness values should be recalculated
/// (i.e. after change of fitness operation).</param>
/// <param name="scalingChanged">if this parameter is set to true, before resorting all scaled fitness values should be recalculated
/// (i.e. after change of scaling operation).</param>
/// <param name="comparatorChanged">this parameter indicates that fitness comparator which is used for sorting has changed.</param>
GAL_API
void GACALL ResortPopulation(bool refreshFitnesses,
bool scalingChanged,
bool comparatorChanged);
/// <summary>This method is used by CRT's <c>qsort</c> function for sorting array which stores chromosomes.</summary>
GAL_API
static int APICALL SortComparison(const void* first,
const void* second);
};// END CLASS DEFINITION GaPopulation
} // Population
#endif // __GA_POPULATION_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -