⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 population.h

📁 遗传算法做图像的模式匹配
💻 H
📖 第 1 页 / 共 3 页
字号:
			GaScalingParams* scalingParams): _parameters(populationParams),
			_sortingComparator(sortComparator),
			_selection(selectionOperation, *selectionParams),
			_replacement(replacementOperation, *replacementParams),
			_coupling(couplingOperation, *couplingParams),
			_scaling(scalingOperation, *scalingParams) { }

		/// <summary><c>BindPopulation</c> method binds population to this configuration.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="population">pointer to population which should be bound.</param>
		/// <param name="refresh">this parameter should be set to <c>true</c> if population should be notified about change 
		/// and refreshed according to new parameters.</param>
		GAL_API
		void GACALL BindPopulation(GaPopulation* population,
			bool refresh);

		// Unbinds population form the configuration
		/// <summary><c>UnbindPopulation</c> method unbinds population from this configuration.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="population">pointer to population which should be unbound.</param>
		inline void GACALL UnbindPopulation(GaPopulation* population)
		{
			if( population )
				_populations.remove( population );
		}

		// Returns parameter of the population(s)
		/// <summary>This method is not thread-safe.</summary>
		/// <returns></returns>
		inline const GaPopulationParameters& GACALL GetParameters() const { return _parameters; }

		// Sets parameter of the population(s)
		/// <summary></summary>
		/// <param name="parameters"></param>
		GAL_API
		void GACALL SetParameters(const GaPopulationParameters& parameters);

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns reference to object that holds pointer to selection operation which should be executed by the algorithm
		/// and its parameters.</returns>
		inline const GaSelectionPair& GACALL Selection() const { return _selection; }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns reference to object that holds pointer to replacement operation which should be executed by the algorithm
		/// and its parameters.</returns>
		inline const GaReplacementPair& GACALL Replacement() const { return _replacement; }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns reference to object that holds pointer to scaling operation which should be executed by the algorithm
		/// and its parameters.</returns>
		inline const GaScalingPair& GACALL Scaling() const { return _scaling; }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns reference to object that holds pointer to coupling operation which should be executed by the algorithm
		/// and its parameters.</returns>
		inline const GaCouplingPair& GACALL Coupling() const { return _coupling; }

		/// <summary>This <c>SetOperation</c> method sets selection operation and its parameters which should be used by genetic algorithm.
		/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method. The method also notifies all bound populations about the change.
		///
		/// This method is not thread safe.</summary>
		/// <param name="operation">pointer to new selection operation.</param>
		/// <param name="parameters">pointer to parameters of selection operation.</param>
		inline void GACALL SetOperation(GaSelectionOperation* operation,
			GaSelectionParams* parameters) { _selection.SetOperation( operation, *parameters ); }

		/// <summary>This <c>SetOperation</c> method sets replacement operation and its parameters which should be used by genetic algorithm.
		/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method. The method also notifies all bound populations about the change.
		///
		/// This method is not thread safe.</summary>
		/// <param name="operation">pointer to new replacement operation.</param>
		/// <param name="parameters">pointer to parameters of replacement operation.</param>
		inline void GACALL SetOperation(GaReplacementOperation* operation,
			GaReplacementParams* parameters) { _replacement.SetOperation( operation, *parameters ); }

		/// <summary>This <c>SetOperation</c> method sets scaling operation and its parameters which should be used by genetic algorithm.
		/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method. The method also notifies all bound populations about the change.
		///
		/// This method is not thread safe.</summary>
		/// <param name="operation">pointer to new scaling operation.</param>
		/// <param name="parameters">pointer to parameters of scaling operation.</param>
		GAL_API
		void GACALL SetOperation(GaScalingOperation* operation,
			GaScalingParams* parameters);

		/// <summary>This <c>SetOperation</c> method sets coupling operation and its parameters which should be used by genetic algorithm.
		/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method. The method also notifies all bound populations about the change.
		///
		/// This method is not thread safe.</summary>
		/// <param name="operation">pointer to new coupling operation.</param>
		/// <param name="parameters">pointer to parameters of coupling operation.</param>
		inline void GACALL SetOperation(GaCouplingOperation* operation,
			GaCouplingParams* parameters) { _coupling.SetOperation( operation, *parameters ); }

		/// <summary>This <c>SetOperationParams</c> method sets parameters for selection operation.
		/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method. The method also notifies all bound populations about the change.
		///
		/// This method is not thread safe.</summary>
		/// <param name="parameters">pointer to parameters of selection operation.</param>
		inline void GACALL SetOperationParams(GaSelectionParams* parameters) { _selection.SetParameters( *parameters ); }

		/// <summary>This <c>SetOperationParams</c> method sets parameters for replacement operation.
		/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method. The method also notifies all bound populations about the change.
		///
		/// This method is not thread safe.</summary>
		/// <param name="parameters">pointer to parameters of replacement operation.</param>
		inline void GACALL SetOperationParams(GaReplacementParams* parameters) { _replacement.SetParameters( *parameters ); }

		/// <summary>This <c>SetOperationParams</c> method sets parameters for scaling operation.
		/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method. The method also notifies all bound populations about the change.
		///
		/// This method is not thread safe.</summary>
		/// <param name="parameters">pointer to parameters of scaling operation.</param>
		GAL_API
		void GACALL SetOperationParams(GaScalingParams* parameters);

		/// <summary>This <c>SetOperationParams</c> method sets parameters for coupling operation.
		/// It makes copy of parameters object by using <c>GaParameters::Clone</c> method. The method also notifies all bound populations about the change.
		///
		/// This method is not thread safe.</summary>
		/// <param name="parameters">pointer to parameters of coupling operation.</param>
		inline void GACALL SetOperationParams(GaCouplingParams* parameters) { _coupling.SetParameters( *parameters ); }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns pointer to fitness comparator which is used for sorting chromosomes in population.</returns>
		inline const GaFitnessComparator* GACALL GetSortComparator() const { return _sortingComparator; }

		// Sets comparator used for sorting the chromosomes
		/// <summary><c>SetSortComaprator</c> method sets fitness comparator which is used for sorting chromosomes.
		/// The method also notifies all bound populations about the change.</summary>
		/// <param name="comparator">pointer to new fitness comparator.</param>
		GAL_API
		void GACALL SetSortComparator(const GaFitnessComparator* comparator);

	};// END CLASS DEFINITION GaPopulationConfiguration
		
	/// <summary><c>GaPopulation</c> class is used to host chromosomes. Population also provides statistical information about chromosomes in population
	/// by using <see cref="GaStatistics" /> class. Same chromosome can be hosted by multiple populations because they are stored in population using
	/// scaled chromosome object which contains smart pointer to chromosome and value of scaled fitness (because this value depends on population,
	/// but not on chromosome itself and same chromosome can be in multiple populations). Chromosomes in population can be sorted which provides easy way
	/// of tracking best and worst chromosomes, but if chromosomes are not sorted, population provides to sorted groups which stores indices of best and
	/// worst chromosomes. Sorting of chromosomes in population or in sorted groups is done my using provided sorting comparator. Both scaled and non-scaled
	/// fitness values of chromosomes can be used for sorting, which value is going to be used depends on specified parameters, but if scaling operation is not
	/// defined for the population, non-scaled fitness values are used. Maximal number of chromosomes which can be hosted is specified in population's parameters.
	/// Also whether the population is going to be fixed-size (number of chromosomes in population is always equals to maximal number of chromosomes it can store)
	/// or resizable (population can host from 0 to maximal number of chromosomes) is specified in population's parameters. During population initialization,
	/// provided prototype is used to create chromosomes which fill the population.  Fixed-size populations are always filled during initialization,
	/// which is not the case with resizable populations. Besides hosting of chromosomes, population also binds extern genetic operations (selection, coupling,
	/// scaling, and replacement) which are going to be used by genetic algorithm during evolution.
	///
	/// This class has built-in synchronizator so it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros with instances of this class.
	/// No public or private methods are thread-safe.</summary>
	class GaPopulation
	{

		friend class GaPopulationConfiguration;

		DEFINE_SYNC_CLASS;

	private:

		/// <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. Value of this attribute is copied from population's parameters.</summary>
		bool _usingScaledFitness;

		/// <summary>Number of chromosomes currently in population.</summary>
		int _currentSize;

		/// <summary>Statistical information about population.</summary>
		GaStatistics _statistics;

		/// <summary>Pointer to array of scaled chromosome objects. Chromosomes which belong to population are stored in this array.</summary>
		GaScaledChromosome** _chromosomes;

		/// <summary>Pointer to chromosome which represents prototype for initialization of population.</summary>
		GaChromosome* _prototype;

		/// <summary>This sorted group keeps track of best chromosomes in population. If population is sorted, this group is ignored.
		/// Group uses same fitness comparator for sorting as population.</summary>
		GaSortedGroup _best;

		/// <summary>This sorted group keeps track of worst chromosomes in population. If population is sorted, this group is ignored.
		/// Group uses same fitness comparator for sorting as population, but it inverse results of comparisons in order to provide inverse sorting of chromosomes.</summary>
		GaSortedGroup _worst;

		/// <summary></summary>
		GaPopulationConfiguration* _configuration;

		/// <summary>Population's parameters.</summary>
		GaPopulationParameters _parameters;

	public:

		/// <summary>This constructor initializes population with its configuration and prototype chromosomes. It also allocates memory for array
		/// which is used to store chromosomes, and initializes sorted groups which keep track of best and worst chromosomes.</summary>
		/// <param name="prototype">pointer to chromosomes which is prototype for making chromosomes during population initialization.</param>
		/// <param name="configuration">pointer to population's configuration.</param>
		GAL_API
		GaPopulation(GaChromosome* prototype,
			GaPopulationConfiguration* configuration);

		/// <summary>Destructor frees memory used by scaled chromosome objects which are hosted in population and array which stores them.</summary>
		GAL_API
		~GaPopulation();

		/// <summary><c>Clone</c> method makes copy of population's object. It can make copy of whole object (with setup and chromosomes) or it can make empty
		/// copy of population's object (only setup, without coping chromosomes). Copying of chromosomes is done by making new scaled chromosomes object which
		/// holds smart pointers to same chromosomes. Caller is responsible for memory allocated by this method for new population's object.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="copyChromosomes">if this parameter is set to <c>true</c>, both setup and chromosomes are copied to new object,
		/// otherwise new object is empty, but with same setup.</param>
		GAL_API
		GaPopulation* GACALL Clone(bool copyChromosomes) const;

		/// <summary>This method removes all chromosomes from population and clears sorted group and statistical information.
		/// If population is not resizable or user specified so, population is filled with new chromosomes which are built based on provided prototype,
		/// also it sorts new chromosomes if needed and update sorted groups and statistical information.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="fill">if this parameter is set to <c>true</c>, population is filled wit new chromosomes built on specified prototype.
		/// If this parameter is set to <c>false</c>, population is only cleared. This parameter is ignored if population is not resizable, and population
		/// is always filled.</param>
		/// <returns>Method returns pointer to newly created chromosome's object.</returns>
		GAL_API
		void GACALL InitializePopulation(bool fill = true);

		/// <summary>This method fills provided array with indices of best chromosomes in population. If population is not sorted this method can only
		/// provide number of best chromosomes which is equal to number of tracked best 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 first best chromosome which is going to be copied.</param>
		/// <param name="numberOfChromosomes">number of queried chromosomes' indices.</param>
		/// <returns>Method returns number of saved indices in output array.</returns>
		GAL_API
		int GACALL GetBestChromosomes(int* results,
			int start,
			int numberOfChromosomes) const;

		/// <summary>This method fills provided array with smart pointer to best chromosomes in population. If population is not sorted this method can only
		/// provide number of best chromosomes which is equal to number of tracked best 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 first best 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 GetBestChromosomes(GaChromosomePtr* results,
			int start,
			int numberOfChromosomes) const;

		/// <summary>This method fills provided array with smart pointer to 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 indices in output array.</returns>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -