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

📄 gachromosome.h

📁 遗传算法做的排课系统
💻 H
📖 第 1 页 / 共 2 页
字号:

#ifndef __GA_CHROMOSOME_H__
#define __GA_CHROMOSOME_H__

#include "..\ExportImport.h"
#include "..\CallConvention.h"
#include "..\Common\GaSmartPtr.h"
#include "..\Common\GaParameters.h"

namespace Population
{
	enum GaSortedGroupType;
	class GaPopulation;
} // Population

using namespace Common;
using namespace Population;

namespace Chromosome
{
	// Parameter for operations which are performed on chromosomes.
	class GaChromosomeParams : public GaParameters
	{

	protected:

		// Probability in percent that mutation will occure.
		float _mutationProbability;

		// Maximum size of chromosome's portion which will be mutated
		int _mutationSize;

		// If it is set to TRUE indicates that the only mutations which lead to improvment of fittness will be commited
		bool _improvingOnlyMutations;

		// Probability in percent that crossover will occure.
		float _crossoverProbability;

		// Number of points on which crossover operation will be performed.
		int _numberOfCrossoverPoints;

	public:

		// Initialize chromosome's parameters
		DLL_EXPORT
		GaChromosomeParams(float mutationProbability,
			int mutationSize,
			bool improvingOnlyMutations,
			float crossoverProbability,
			int numberOfCrossoverPoints);

		// Initialize chromosome's parameters with default values
		DLL_EXPORT
		GaChromosomeParams();

		// Virtual copy constructor
		DLL_EXPORT
		virtual GaParameters* GACALL Clone() const;

		// Sets probability in percent that mutation will occure.
		DLL_EXPORT
		void GACALL SetMutationProbability(float probability);

		// Gets probability in percent that mutation will occure.
		DLL_EXPORT
		float GACALL GetMutationProbability() const;

		// Sets maximum size of chromosome's portion which will be mutated
		DLL_EXPORT
		void GACALL SetMutationSize(int size);

		// Returns maximum size of chromosome's portion which will be mutated
		DLL_EXPORT
		int GetMutationSize() const;

		// If it is set to TRUE indicates that the only mutations which lead to improvment of fittness will be commited
		DLL_EXPORT
		void GACALL SetImprovingMutationsFlag(bool improvingOnly);

		// Returns TRUE if the only mutations which lead to improvment of fitness is commited.
		DLL_EXPORT
		bool GACALL GetImprovingMutationsFlag() const;

		// Sets probability in percent that crossover will occure.
		DLL_EXPORT
		void GACALL SetCrossoverProbability(float probability);

		// Gets probability in percent that crossover will occure.
		DLL_EXPORT
		float GACALL GetCrossoverProbability() const;

		// Sets number of points on which crossover operation will be performed.
		DLL_EXPORT
		void GACALL SetNumberOfCrossoverPoints(int numberOfPoints);

		// Gets number of points on which crossover operation will be performed.
		DLL_EXPORT
		int GACALL GetNumberOfCrossoverPoints() const;

	};// END CLASS DEFINITION GaChromosomeParams

	class GaChromosome;

	// Smart pointers to chromosome
	typedef GaSmartPtr<GaChromosome> GaChromosomePtr;
	typedef GaSmartPtr<const GaChromosome> GaChromosomeConstPtr;

	// Base class for all chromosomes.
	class GaChromosome
	{

	public:

		// Destructor
		virtual ~GaChromosome() { };

		// Performs crossover operation and make new chiled from this and another given chromosome,
		virtual GaChromosomePtr GACALL Crossover(GaChromosomePtr secondParent) const=0;

		// Mutation operation
		virtual void GACALL Mutation()=0;

		// Make new chromosome which is the exact copy of this chromosome
		virtual GaChromosomePtr GACALL MakeCopy(bool setupOnly) const=0;

		// Make new chromosome of this type but with random characteristic
		virtual GaChromosomePtr GACALL MakeNewFromPrototype() const=0;

		// Returns fitness of the chromosome
		virtual float GACALL GetFitness() const=0;

		// Compares fitnesses of two chromosomes. Returns:
		//  a, -1 if the fitness value of this chromosome is lower then the value of the given chromosome
		//  b.  0 if the fitness values of both chromosomes are equal
		//  c.  1 if the fitness value of this chromosome is greater then the value of the given chromosome
		virtual int GACALL CompareFitnesses(GaChromosomePtr c) const=0;

		// Compares fitnesses of two chromosomes. Returns:
		//  a, -1 if the fitness value of this chromosome is lower then the given value
		//  b.  0 if the fitness values of both chromosomes are equal
		//  c.  1 if the fitness value of this chromosome is greater then the given value
		virtual int GACALL CompareFitnesses(float c) const=0;

		// Refresh fitness value of chromosome
		virtual void GACALL RefreshFitness()=0;

		// Returns algorithm's parameters of chromosome
		virtual const GaChromosomeParams& GACALL GetParameters() const=0;

		// Sets algorithm's parameters of chromosome
		virtual void GACALL SetParameters(GaChromosomeParams* p)=0;

		// Returns size of chromosome's code
		virtual int GACALL GetCodeSize() const=0;

		// Copy data and setup from given source chromosome
		virtual GaChromosome& GACALL operator =(const GaChromosome& rhs)=0;

		// Compares two chromosomes and returns how much are they simular in percent
		virtual float GACALL operator ==(const GaChromosome& c) const=0;

	protected:

		// Calculate the fitness of chromosome
		virtual float GACALL CalculateFitness() const=0;

	};// END CLASS DEFINITION GaChromosome

	class GaFitnessComparator;

	// Manages chromosome's scaled fitness value
	class GaScaledChromosome
	{
		DEFINE_SYNC_CLASS

	private:

		// Scaled fitness of the chromsome
		float _scaledFitness;

		// Population in which this chromosome is located
		GaPopulation* _population;

		// Pointer to the chromsome
		GaChromosomePtr _chromosome;

		// Group to which this chromosome belongs
		GaSortedGroupType _groups;

		// Index of chromosome in population
		int _index;

	public:

		// Initialization of scaled value
		DLL_EXPORT
		GaScaledChromosome(GaChromosomePtr chromosome,
			GaPopulation* population,
			int index);

		// Default constructor
		DLL_EXPORT
		GaScaledChromosome(GaPopulation* population);

		// Returns pointer to chromosome
		DLL_EXPORT
		GaChromosomePtr GACALL GetChromosome() const;

		// Sets pointer to chromosome
		DLL_EXPORT
		void GACALL SetChromosome(GaChromosomePtr chromosome);

		// Returns scaled fitness value
		DLL_EXPORT
		float GACALL GetScaledFitness() const;

		// Sets scaled fitness value
		DLL_EXPORT
		void GACALL SetScaledFitness(float fitness);

		// Returns fitness value (scaled or non scaled fitness) for comparation
		DLL_EXPORT
		float GACALL GetFitnessForComparation() const;

		// Rescale scaled fitness value
		DLL_EXPORT
		void GACALL Rescale();

		// Compares fitnesses of two chromosomes. Returns:
		//  a, -1 if the fitness value of this chromosome is lower then the value of the given chromosome
		//  b.  0 if the fitness values of both chromosomes are equal
		//  c.  1 if the fitness value of this chromosome is greater then the value of the given chromosome
		DLL_EXPORT
		int GACALL CompareFitnesses(const GaScaledChromosome& c) const;

		// Compares fitnesses of two chromosomes. Returns:
		//  a, -1 if the fitness value of this chromosome is lower then the given value
		//  b.  0 if the fitness values of both chromosomes are equal
		//  c.  1 if the fitness value of this chromosome is greater then the given value
		DLL_EXPORT
		int GACALL CompareFitnesses(float c) const;

		// Removes chromosome from desired groups
		DLL_EXPORT
		void GACALL ClearGroupFlags(GaSortedGroupType groups);

		// Sets flag that chromosome belongs to specified groups
		DLL_EXPORT
		void GACALL SetGroupFlags(GaSortedGroupType groups);

		// Returns TRUE if the chromosome belongs to specified groups
		DLL_EXPORT
		bool GACALL GetGroupFlag(GaSortedGroupType groups,
			bool all = false);

		// Sets groups to which this chromosome belongs
		DLL_EXPORT
		void GACALL SetGroups(GaSortedGroupType groups);

		// Returns groups to which this chromosome belongs
		DLL_EXPORT
		GaSortedGroupType GACALL GetGroups() const;

		// Sets index of chromosome in pupulation
		DLL_EXPORT
		void GACALL SetIndex(int index);

⌨️ 快捷键说明

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