📄 population.h
字号:
/*! \file Population.h
\brief This file declares classes and datatypes of chromosomes population.
*/
/*
*
* website: http://www.coolsoft-sd.com/
* contact: support@coolsoft-sd.com
*
*/
/*
* Genetic Algorithm Library
* Copyright (C) 2007-2008 Coolsoft Software Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef __GA_POPULATION_H__
#define __GA_POPULATION_H__
#include <list>
#include "Platform.h"
#include "Threading.h"
#include "Statistics.h"
#include "Chromosome.h"
#include "SortedGroup.h"
#include "ChromosomeOperations.h"
#include "PopulationOperations.h"
using namespace std;
using namespace Common;
using namespace Population;
using namespace Chromosome;
/// <summary>Contatins interfaces, classes and datatypes used to implement population of chromosomes and genetic operations.</summary>
namespace Population
{
/// <summary><c>GaPopulationParameters</c> class represents parameters of population.
///
/// This class has no built-in synchronizator, so <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros cannot be used with instances of this class.
/// No public or private methods are thread-safe.</summary></summary>
class GaPopulationParameters : public GaParameters
{
private:
/// <summary>Maximal population size (number of chromosomes in population).</summary>
int _populationSize;
/// <summary>This attribute indicate if number of chromosomes in population can change. If it is set to <c>true</c> number of chromosomes can vary.
/// If this attribute is set to <c>false</c>, number of chromosomes in population is equal to maximal number.</summary>
bool _resizable;
/// <summary>If this attribute is set to <c>true</c>, chromosomes in population is sorted by their fitness value (original or scaled).</summary>
bool _sorting;
/// <summary>If this attribute is set to <c>true</c>, scaled fitness values are used for sorting chromosomes in population.
/// If it is set to <c>false</c>, original (non-scaled) fitness values are used.</summary>
bool _usingScaledFitness;
/// <summary>This attribute stores number of best chromosomes of which population keeps track.
/// This attribute is ignored if population is sorted.</summary>
int _bestTrackCount;
/// <summary>This attribute stores number of worst chromosomes of which population keeps track.
/// This attribute is ignored if population is sorted.</summary>
int _worstTrackCount;
public:
/// <summary>This constructor initializes population parameters with user-defined values.</summary>
/// <param name="populationSize">size of population (maximal number of chromosomes in population).</param>
/// <param name="resizable">instructs population that number of chromosomes can change.</param>
/// <param name="sorting">instructs population to sort chromosomes according to their fitness values.</param>
/// <param name="useScaldeFitness">instructs population to use scaled (transformed) fitness value for sorting chromosomes.</param>
/// <param name="bestTrackCount">number of best chromosomes of which track is being kept.</param>
/// <param name="worstTrackCount">number of worst chromosomes of which track is being kept.</param>
GAL_API
GaPopulationParameters(int populationSize,
bool resizable,
bool sorting,
bool useScaldeFitness,
int bestTrackCount,
int worstTrackCount);
/// <summary>More details are given in specification of <see cref="GaParameters::Clone" /> method.
///
/// This method is not thread-safe.</summary>
virtual GaParameters* GACALL Clone() const { return new GaPopulationParameters( *this ); }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns maximal population size (number of chromosomes in population).</returns>
inline int GACALL GetPopulationSize() const { return _populationSize; }
/// <summary><c>SetPopulationSize</c> method sets maximal population size.
///
/// This method is not thread-safe.</summary>
/// <param name="size">size of population.</param>
inline void GACALL SetPopulationSize(int size) { _populationSize = size >= 2 ? size : 2; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns <c>true</c> if number of chromosomes can change, otherwise it returns <c>false</c>.</returns>
inline bool GACALL GetResizable() const { return _resizable; }
/// <summary><c>SetResizable</c> method sets capability of population to accept changes of number of chromosomes in it.
///
/// This method is not thread-safe.</summary>
/// <param name="resizable">instructs population that number of chromosomes can change.</param>
inline void GACALL SetResizable(bool resizable) { _resizable = resizable; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns <c>true</c> if chromosomes in population is sorted by their fitness value (original or scaled),
/// otherwise it returns <c>false</c>.</returns>
inline bool GACALL GetSorting() const { return _sorting; }
/// <summary><c>SetSorting</c> method sets capability of population to sort chromosomes by their fitness value.
///
/// This method is not thread-safe.</summary>
/// <param name="sorting">instructs population to sort chromosomes according to their fitness values.</param>
inline void GACALL SetSorting(bool sorting) { _sorting = sorting; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns <c>true</c> if population use scaled fitness value to sort chromosomes,
/// otherwise if non-scaled fitness values are used it returns <c>false</c>.</returns>
inline bool GACALL GetUsingScaledFitness() const { return _usingScaledFitness; }
/// <summary><c>SetUseScaledFitness</c> method sets capability of population to use scaled fitness values of chromosomes for sorting them.
///
/// This method is not thread-safe.</summary>
/// <param name="useScaledFitness">instructs population to use scaled (transformed) fitness value for sorting chromosomes.</param>
inline void GACALL SetUsingScaledFitness(bool useScaledFitness) { _usingScaledFitness = useScaledFitness; }
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns number of best chromosomes of which population keeps track.</returns>
inline int GACALL GetBestTrackCount() const { return _bestTrackCount; }
/// <summary><c>SetBestTrackCount</c> method sets number of best chromosomes of which population keeps track
///
/// This method is not thread-safe.</summary>
/// <param name="count">number of best chromosomes of which track is being kept.</param>
GAL_API
void GACALL SetBestTrackCount(int count);
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns number of best chromosomes of which population keeps track.</returns>
inline int GACALL GetWorstTrackCount() const { return _worstTrackCount; }
/// <summary><c>SetWorstTrackCount</c> method sets number of worst chromosomes of which population keeps track.
///
/// This method is not thread-safe.</summary>
/// <param name="count">number of worst chromosomes of which track is being kept.</param>
GAL_API
void GACALL SetWorstTrackCount(int count);
};// END CLASS DEFINITION GaPopulationParameters
class GaPopulation;
/// <summary>This class stores parameters and genetic operations of a population or a group of populations. All populations bound to the configuration,
/// are updated and notified every time when configuration is changed. Note that each population copies parameters' of the population and use local copy.
///
/// This class has no built-in synchronizator, so <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros cannot be used with instances of this class.
/// No public or private methods are thread-safe.</summary>
class GaPopulationConfiguration
{
private:
/// <summary>Global instance of default configuration.</summary>
GAL_API
static GaPopulationConfiguration* _default;
/// <summary>Selection operation and its parameters.</summary>
GaSelectionPair _selection;
/// <summary>Replacement operation and its parameters.</summary>
GaReplacementPair _replacement;
/// <summary>Scaling operation and its parameters.</summary>
GaScalingPair _scaling;
/// <summary>Coupling operation and its parameters.</summary>
GaCouplingPair _coupling;
/// <summary>Population's parameters</summary>
GaPopulationParameters _parameters;
/// <summary>List of populations which use this configuration.</summary>
list<GaPopulation*> _populations;
/// <summary>Pointer to comparator used for sorting the chromosomes in population.</summary>
const GaFitnessComparator* _sortingComparator;
public:
/// <summary><c>MakeDefault</c> method makes global instance of default configuration.
///
/// This method is not thread-safe.</summary>
GAL_API
static void GACALL MakeDefault();
/// <summary><c>FreeDefault</c> method frees global instance of default configuration.
///
/// This method is not thread-safe.</summary>
static void GACALL FreeDefault()
{
if( _default )
delete _default;
_default = NULL;
}
/// <summary>This method is not thread-safe.</summary>
/// <returns>Method returns reference to default configuration.</returns>
static GaPopulationConfiguration& GetDefault() { return *_default; }
/// <summary>This constructor initializes configuration by cloning default configuration.</summary>
GaPopulationConfiguration() : _parameters(_default->_parameters),
_sortingComparator(_default->_sortingComparator),
_selection(_default->_selection),
_replacement(_default->_replacement),
_coupling(_default->_coupling),
_scaling(_default->_scaling) { }
/// <summary>This constructor initializes configuration with user defined operations and parameters.</summary>
/// <param name="populationParams">reference to populations' parameters.
/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method.</param>
/// <param name="sortComparator"></param>
/// <param name="selectionOperation">pointer to selection operation.</param>
/// <param name="selectionParams">pointer to parameters of selection operation.</param>
/// <param name="replacementOperation">pointer to replacement operation.</param>
/// <param name="replacementParams">pointer to parameters of replacement operation.</param>
/// <param name="couplingOperation">pointer to coupling operation.</param>
/// <param name="couplingParams">pointer to parameters of coupling operation.</param>
/// <param name="scalingOperation">pointer to scaling operation.</param>
/// <param name="scalingParams">pointer to parameters of scaling operation.</param>
GaPopulationConfiguration(const GaPopulationParameters& populationParams,
const GaFitnessComparator* sortComparator,
GaSelectionOperation* selectionOperation,
GaSelectionParams* selectionParams,
GaReplacementOperation* replacementOperation,
GaReplacementParams* replacementParams,
GaCouplingOperation* couplingOperation,
GaCouplingParams* couplingParams,
GaScalingOperation* scalingOperation,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -