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

📄 selectionoperations.h

📁 遗传算法的四个源程序和源代码
💻 H
📖 第 1 页 / 共 2 页
字号:

		/// <summary><c>GaSelectRandomBest</c> selection is randomly selects number of chromosomes and then it cuts off chromosomes with worst fitness values to
		/// fit in selection size. This selection use <see cref="GaSelectRandomBestParams" /> class for parameters. 
		///
		/// 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.
		/// Because this genetic operation is stateless all public method are thread-safe.</summary>
		class GaSelectRandomBest : public GaSelectRandom
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaSelectionOperation::operator ()" /> method.
			///
			/// This method is thread-safe.</summary>
			GAL_API
			virtual void GACALL operator ()(const GaPopulation& population,
				const GaSelectionParams& parameters,
				GaSelectionResultSet& result) const;

			/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			/// <returns>Method returns new instance of <see cref="GaSelectRandomBestParams" /> class.</returns>
			virtual GaParameters* GACALL MakeParameters() const { return new GaSelectRandomBestParams(); }

			/// <summary>Valid parameters must have selection size grater then 0 and group size greater then or equal to selection size.
			///
			/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			virtual bool GACALL CheckParameters(const GaParameters& parameters) const
			{
				const GaSelectRandomBestParams& p = (const GaSelectRandomBestParams&)parameters;
				return p.GetSelectionSize() > 0 && p.GetGroupSize() >= p.GetSelectionSize();
			}

		};// END CLASS DEFINITION GaSelectRandomBest

		/// <summary>When <c>GaSelectRouletteWheel</c> selection is used chromosomes with better fitness values have better chances to be selected.
		/// It virtually transforms fitness value of chromosome to probability of selection. Population on which selection performed must be sorted.
		/// This selection can work with minimization or maximization of fitness values, as well as negative fitness values. This selection use
		/// <see cref="GaSelectDuplicatesParams" /> class for parameters. 
		///
		/// 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.
		/// Because this genetic operation is stateless all public method are thread-safe.</summary>
		class GaSelectRouletteWheel : public GaSelectionOperation
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaSelectionOperation::operator ()" /> method.
			///
			/// This method is thread-safe.</summary>
			GAL_API
			virtual void GACALL operator ()(const GaPopulation& population,
				const GaSelectionParams& parameters,
				GaSelectionResultSet& result) const;

			/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			/// <returns>Method returns new instance of <see cref="GaSelectDuplicatesParams" /> class.</returns>
			virtual GaParameters* GACALL MakeParameters() const { return new GaSelectDuplicatesParams(); }

			/// <summary>Valid parameters must have selection size grater then 0.
			///
			/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			virtual bool GACALL CheckParameters(const GaParameters& parameters) const { return ( (const GaSelectionParams&) parameters ).GetSelectionSize() > 0; }

		protected:

			/// <summary><c>Select</c> method selects only one chromosome using roulette wheel selection algorithm.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="result">reference to selection result set which is used to store selected chromosome.</param>
			/// <param name="population">reference to population on which selection is performed.</param>
			/// <param name="parameters">reference to parameters of population.</param>
			/// <returns>Method returns index of selected chromosome.</returns>
			GAL_API
			virtual int GACALL Select(const GaSelectionResultSet& result,
				const GaPopulation& population,
				const GaSelectionParams& parameters) const;

		};// END CLASS DEFINITION GaSelectRouletteWheel

		/// <summary>This class is used by <c>GaSelectTournament</c> selection operations.
		///
		/// 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 GaSelectTournamentParams : public GaSelectDuplicatesParams
		{

		private:

			/// <summary>Number of roulette wheel selections which are going to be executed before selection chooses one chromosome.</summary>
			int _numberOfSelections;

		public:

			/// <summary>This constructor initializes parameters with user defined values.</summary>
			/// <param name="selectionSize">number of chromosomes which should be selected.</param>
			/// <param name="duplicates">if this parameter is set to <c>true</c>, duplicates of same chromosome in result set are allowed.
			/// If it is set to <c>false</c> duplicates are not allowed.</param>
			/// <param name="numberOfSelections">number of roulette wheel selections which are going to be executed before
			/// selection chooses one chromosome.</param>
			GaSelectTournamentParams(int selectionSize,
				bool duplicates,
				int numberOfSelections) : GaSelectDuplicatesParams(duplicates, selectionSize),
				_numberOfSelections(numberOfSelections) { }

			/// <summary>This constructor initializes parameters with default values. Duplicates are not allowed, and selection size is 2 and
			/// number of roulette wheel selections is 1.</summary>
			GaSelectTournamentParams() : _numberOfSelections(1) { }

			/// <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 GaSelectTournamentParams( *this ); }

			// Returns how many chromosomes are selected using roulette wheel selection
			/// <summary>This method is not thread-safe.</summary>
			/// <returns>Method returns number of roulette wheel selections which are going to be executed before selection chooses one chromosome.</returns>
			inline int GACALL GetNumberOfSelections() const { return _numberOfSelections; }

			// Sets how many chromosomes are selected using roulette wheel selection
			/// <summary><c>SetNumberOfSelection</c> method sets number of roulette wheel selections which are going to be executed before
			/// selection chooses one chromosome.
			///
			/// This method is not thread-safe.</summary>
			/// <param name="number">number of roulette wheel selections.</param>
			inline void GACALL SetNumberOfSelections(int number) { _numberOfSelections = number; }

		};// END CLASS DEFINITION GaSelectTournamentParams

		/// <summary><c>GaSelectTorunament</c> selection is similar to <see cref="GaSelectRouletteWheel" /> selection. For one place in result set
		/// it performs multiple roulette wheel selection before it chooses only one chromosome with best fitness value among selected. This increases
		/// probability of selection of chromosome with better fitness value. This selection use <see cref="GaSelectTorunamentParams" /> class for parameters.
		///
		/// 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.
		/// Because this genetic operation is stateless all public method are thread-safe.</summary>
		class GaSelectTournament : public GaSelectRouletteWheel
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaSelectionOperation::operator ()" /> method.
			///
			/// This method is thread-safe.</summary>
			GAL_API
			virtual void GACALL operator ()(const GaPopulation& population,
				const GaSelectionParams& parameters,
				GaSelectionResultSet& result) const;

			/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			/// <returns>Method returns new instance of <see cref="GaSelectTournamentParams" /> class.</returns>
			virtual GaParameters* GACALL MakeParameters() const { return new GaSelectTournamentParams(); }

			/// <summary>Valid parameters must have selection size and number of selections grater then 0.
			///
			/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			virtual bool GACALL CheckParameters(const GaParameters& parameters) const
			{
				const GaSelectTournamentParams& p = (const GaSelectTournamentParams&)parameters;
				return p.GetSelectionSize() > 0 && p.GetNumberOfSelections() > 0;
			}

		};// END CLASS DEFINITION GaSelectTournament

		/// <summary><c>GaSelectWorst</c> selection is selects defined number of worst chromosomes. If population is not-sorted, this selection can only
		/// select chromosomes which are in worst sorted group. This selection use <see cref="GaSelectionParams" /> class for parameters.
		///
		/// 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.
		/// Because this genetic operation is stateless all public method are thread-safe.</summary>
		class GaSelectWorst : public GaSelectionOperation
		{

		public:

			/// <summary>More details are given in specification of <see cref="GaSelectionOperation::operator ()" /> method.
			///
			/// This method is thread-safe.</summary>
			GAL_API
			virtual void GACALL operator ()(const GaPopulation& population,
				const GaSelectionParams& parameters,
				GaSelectionResultSet& result) const;

			/// <summary>More details are given in specification of <see cref="GaOperation::MakeParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			/// <returns>Method returns new instance of <see cref="GaSelectionParams" /> class.</returns>
			virtual GaParameters* GACALL MakeParameters() const { return new GaSelectionParams(); }

			/// <summary>Valid parameters must have selection size grater then 0.
			///
			/// More details are given in specification of <see cref="GaOperation::CheckParameters" /> method.
			///
			/// This method is thread-safe.</summary>
			virtual bool GACALL CheckParameters(const GaParameters& parameters) const { return ( (const GaSelectionParams&) parameters ).GetSelectionSize() > 0; }

		};// END CLASS DEFINITION GaSelectBest

	} // SelectionOperations
} // Population

#endif // __GA_SELECTION_OPERATIONS_H__

⌨️ 快捷键说明

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