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

📄 chromosome.h

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

	protected:

		/// <summary>This method calculates chromosome's fitness value, but it doesn't store it.</summary>
		/// <returns>Method returns calculated fitness value.</returns>
		virtual float GACALL CalculateFitness() const=0;

	};// END CLASS DEFINITION GaChromosome

	class GaFitnessComparator;

	/// <summary>This class wraps chromosome class and represents it in population. To reduce memory usage and improve performance
	/// same chromosome can be placed in multiple populations, but some chromosome's values has different values for separate populations.
	/// All values which are specific to the population are extracted from chromosome class to this class. Same object of <c>GaScaledChromosome</c>
	/// cannot be located in multiple populations.
	///
	/// 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 GaScaledChromosome
	{
		DEFINE_SYNC_CLASS

	private:

		/// <summary>Scaled (transformed) fitness value of the chromosome.</summary>
		float _scaledFitness;

		/// <summary>Pointer to population in which the chromosome is located.</summary>
		GaPopulation* _population;

		/// <summary>Smart pointer to chromosome which is bound to population with this object.</summary>
		GaChromosomePtr _chromosome;

		/// <summary>Flags od memberships in sorted groups of population.</summary>
		GaSortedGroupType _groups;

		/// <summary>Index of chromosome in population.</summary>
		int _index;

	public:

		/// <summary>This constructor fully initializes binding object (chromosome, population and index).</summary>
		/// <param name="chromosome">smart pointer to bound chromosome.</param>
		/// <param name="population">pointer to population to which chromosome is bound.</param>
		/// <param name="index">index of chromosome in population.</param>
		GaScaledChromosome(GaChromosomePtr chromosome,
			GaPopulation* population,
			int index);

		/// <summary>This constructor only specifies binding population.</summary>
		/// <param name="population">pointer to population to which chromosome is bound.</param>
		GaScaledChromosome(GaPopulation* population);

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns smart pointer to bound chromosome. Or <c>NULL</c> pointer if object doesn't bind chromosome.</returns>
		inline GaChromosomePtr GACALL GetChromosome() const { return _chromosome; }

		/// <summary><c>SetChromosome</c> sets pointer to bound chromosome.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="chromosome">smart pointer to bound chromosome.</param>
		inline void GACALL SetChromosome(GaChromosomePtr chromosome)
		{
			_chromosome = chromosome;
			Rescale();
		}

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns scaled (transformed) fitness value of bound chromosome.</returns>
		inline float GACALL GetScaledFitness() const { return _scaledFitness; }

		/// <summary><c>SetScaledFitness</c> method sets scaled (transformed) value of bound chromosome.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="fitness">new fitness value.</param>
		inline void GACALL SetScaledFitness(float fitness) { _scaledFitness = fitness; }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns fitness value (scaled or original value) which is used for sorting and comparison of chromosomes in population.</returns>
		GAL_API
		float GACALL GetFitnessForComparison() const;

		/// <summary><c>Rescale</c> method recalculates scaled (transformed) fitness value of chromosome.</summary>
		GAL_API
		void GACALL Rescale();

		/// <summary>This method compares fitness values of two chromosomes (<c>this</c> and <c>c</c>).
		/// Which fitness value (scaled or original) is going to be used depends on flag set in population to which chromosomes are bound.
		/// NOTE: Comparison doesn't have to be arithmetical comparison.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="c">second chromosome for comparison with <c>this</c>.</param>
		/// <returns>a) -1 if fitness value of <c>this</c> is lower then value of <c>c</c>.
		/// <br>b) 0 if fitness values of both chromosomes are equal.
		/// <br>c) 1 if fitness value of <c>this</c> is greater then value of <c>c</c>. </returns>
		inline int GACALL CompareFitnesses(const GaScaledChromosome& c) const { return CompareFitnesses( c.GetFitnessForComparison() ); }

		/// <summary>This method compares fitness values of two chromosomes (<c>this</c> and <c>c</c>).
		/// Which fitness value (scaled or original) is going to be used depends on flag set in population to which chromosomes are bound.
		/// NOTE: Comparison doesn't have to be arithmetical comparison.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="c">second chromosome for comparison with <c>this</c>.</param>
		/// <returns>a) -1 if fitness value of <c>this</c> is lower then value of <c>c</c>.
		/// <br>b) 0 if fitness values of both chromosomes are equal.
		/// <br>c) 1 if fitness value of <c>this</c> is greater then value of <c>c</c>. </returns>
		GAL_API
		int GACALL CompareFitnesses(float c) const;

		/// <summary><c>ClearGroupFlag</c> method clears chromosome's membership flag for specified sorted group of population.
		///
		/// <param name="groups">flags of sorted groups that will be set.</param>
		inline void GACALL ClearGroupFlags(GaSortedGroupType groups) { _groups = (GaSortedGroupType)( _groups & ~groups ); }

		/// <summary><c>SetGroupFlag</c> method sets chromosome's membership flag for specified sorted group of population.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="groups">flags of sorted groups that will be set.</param>
		inline void GACALL SetGroupFlags(GaSortedGroupType groups) { _groups = (GaSortedGroupType)( _groups | groups ); }

		/// <summary>Method returns state of desired membership flag(s).
		///
		/// This method is not thread-safe.</summary>
		/// <param name="groups">desired flags of sorted groups which state is queried. Flags are cmbined with operator |.</param>
		/// <param name="all">parameter tells method how the states of flag are combined into result. It this parameter is set to <c>true</c>,
		/// method returns <c>true</c> only if all flags is set, otherwise if this parameter is set to <c>false</c>,
		/// method returns <c>true</c> if any of flags is set.</param>
		/// <returns>It returns <c>true</c> if any of desired flags is set and all parameter is set to <c>false</c>.
		/// If all parameter is set to <c>true</c>, method returns <c>true</c> only if all desired flags are set.</returns>
		inline bool GACALL GetGroupFlag(GaSortedGroupType groups,
			bool all = false) { return all ? ( _groups & groups ) == groups : ( _groups & groups ) != 0; }

		/// <summary><c>SetGroups</c> sets all flags of sorted group membership.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="groups">new values of flags of sorted groups membership.</param>
		inline void GACALL SetGroups(GaSortedGroupType groups) { _groups = groups; }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns all values of flags of sorted group membership.</returns>
		inline GaSortedGroupType GACALL GetGroups() const { return _groups; }

		/// <summary><c>SetIndex</c> method sets index of bound chromosome in population.</summary>
		/// <param name="index">new index of bound chromosome.</param>
		GAL_API
		void GACALL SetIndex(int index);

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns index of bound chromosome in population.</returns>
		inline int GACALL GetIndex() const { return _index; }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns reference to a population to which chromosome is bound.</returns>
		inline GaPopulation& GACALL GetPopulation() const { return *_population; }

		/// <summary>This operator is not thread-safe.</summary>
		/// <returns>Operator returns smart pointer to bound chromosome. Or <c>NULL</c> pointer if object isn't bound to chromosome.</returns>
		inline GACALL operator GaChromosomePtr() const { return _chromosome; }

		/// <summary>This operator is not thread-safe.</summary>
		/// <returns>Operator returns index of bound chromosome in population.</returns>
		inline GACALL operator float() const { return _scaledFitness; }

	};// END CLASS DEFINITION GaScaledChromosome

	class GaDefaultChromosome;

	/// <summary>This class represent base for chromosome's configuration block (CCB). CCB stores entire setup of chromosome.
	/// <c>GaChromosomeParamsBlock</c> class stores only pointer to chromosomes' 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.
	/// No public or private methods are thread-safe.</summary>
	class GaChromosomeParamsBlock
	{
		friend class GaDefaultChromosome;

	protected:

		/// <summary>Pointer to chromosomes' parameters.</summary>
		GaChromosomeParams* _parameters;

	public:

		/// <summary>This constructor initializes CCB with pointer to chromosomes' parameters.</summary>
		/// <param name="parameters">pointer to chromosomes' parameters.</param>
		GaChromosomeParamsBlock(GaChromosomeParams* parameters) : _parameters(parameters) { }

		/// <summary>This is copy constructor. The constructor doesn't create copy of parameters' object,
		/// it only copies pointer to chromosomes' parameters.</summary>
		/// <param name="rhs">reference to CCB which is copied.</param>
		GaChromosomeParamsBlock(const GaChromosomeParamsBlock& rhs) : _parameters(rhs._parameters) { }

		/// <summary>This constructor initializes empty CCB.</summary>
		GaChromosomeParamsBlock() : _parameters(NULL) { }

		/// <summary>This method is not thread-safe.</summary>
		/// <returns>Method returns reference to chromosomes� parameters.</returns>
		inline const GaChromosomeParams& GACALL GetParameters() const { return *_parameters; }

		/// <summary><c>SetParameters</c> method sets pointer to chromosomes' parameters.
		///
		/// This method is not thread-safe.</summary>
		/// <param name="params">pointer to new parameters.</param>
		inline void GACALL SetParameters(GaChromosomeParams* params) { _parameters = params; }

	};// END CLASS DEFINITION GaChromosomeParamsBlock

	// Implements some behavior concerning fitness value, parameters
	// and preaparing chromosome for crossover and mutation operations.
	/// <summary>GaDefaultChromosome class implements some basic feature of chromosome such as management of fitness value, 
	/// CCB and probability of crossover and mutation 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 GaDefaultChromosome : public GaChromosome
	{

	protected:

		/// <summary>Fitness value of the chromosome.</summary>
		float _fitness;

		/// <summary>Pointer to CCB.</summary>
		GaChromosomeParamsBlock* _configBlock;

	public:

		/// <summary>This constructor initializes chromsome with CCB.</summary>
		/// <param name="configBlock">pointer to CCB.</param>
		GaDefaultChromosome(GaChromosomeParamsBlock* configBlock) : _fitness(0),
			_configBlock(configBlock) { }

		/// <summary>Copy constructor.</summary>
		/// <param name="c">reference to chromosome which is copied.</param>
		/// <param name="setupOnly">if this parameter is <c>true</c>, only pointer to CCB is copied. If this parameter is <c>false</c>,
		/// chromosome's data and CCB is copied. </param>
		GaDefaultChromosome(const GaDefaultChromosome& c,
			bool setupOnly) : _configBlock(c._configBlock)
		{
			if( !setupOnly )
				_fitness = c._fitness;
		}

		/// <summary>This method handles probability of mutation and provides framework for improving-only mutations.
		/// It decides if mutation is going to be preformed and if it is going to be accepted. If improving-only flag is set,
		/// the method calls <see cref="PreapareForMutation" /> method, then <see cref="PerformeMutation" /> is called,
		/// after the mutation is performed it checks (only if improving-only flag is set) if the mutation produced chromosome with better fitness,
		/// if that is the case <see cref="AcceptMutation" /> is called, otherwise <see cref="RejectMutation" /> is called.
		///
		/// This method is not thread-safe.</summary>
		GAL_API
		virtual void GACALL Mutation();

⌨️ 快捷键说明

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