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

📄 gapopulation.h

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

#ifndef __GA_POPULATION_H__
#define __GA_POPULATION_H__

#include <vector>

#include "..\CallConvention.h"
#include "..\ExportImport.h"
#include "..\Threading\GaThreading.h"
#include "..\Common\GaStatistics.h"
#include "..\Chromosome\GaChromosome.h"
#include "..\Chromosome\GaChromosomeOperations.h"
#include "GaSelection.h"
#include "GaReplacement.h"
#include "GaCoupling.h"
#include "GaScaling.h"

using namespace std;

using namespace Common;
using namespace Threading;
using namespace Population;
using namespace Chromosome;

namespace Population
{

	// #pragma region GaSortedGroupType

	// Defines types of sorted groups
	enum GaSortedGroupType
	{
		GASGT_NONE = 0x0, 
		GASGT_BEST = 0x1, 
		GASGT_WORST = 0x2,
		GASGT_OTHER = 0x4 
	};

	// #pragma endregion

	// #pragma region GaSortedGroup

	// Sorted group of chromosomes
	class GaSortedGroup
	{

		DEFINE_SYNC_CLASS

	private:

		// Population in which chromosomes of the group is suited
		const GaPopulation* _population;

		// Indecies of chromosomes in population
		int* _chromosomesIndecies;

		// Fitness comparator used to sort group
		const GaFitnessComparator* _comparator;

		// Maximum number of chromosomes in the group
		int _maxSize;

		// Current number of chromosomes in the group
		int _currentSize;

		// Type of this group
		GaSortedGroupType _type;

	public:

		// Initialization of the group
		DLL_EXPORT
		GaSortedGroup(const GaPopulation* population,
			int maxSize,
			GaSortedGroupType type,
			const GaFitnessComparator* comparator);

		// Empty group
		DLL_EXPORT
		GaSortedGroup(const GaPopulation* population,
			GaSortedGroupType type);

		// Frees aquired resources
		DLL_EXPORT
		~GaSortedGroup();

		// Copy chromosome indicies to given sorted group
		DLL_EXPORT
		void GACALL CopyTo(GaSortedGroup& destination,
			  bool sameSorting) const;

		// Adds chromosome to the group.
		// Returns position at which this chromosome has been inserted
		// or -1 if chromosome hasn't been inserted.
		DLL_EXPORT
		int GACALL Add(int chromosomeIndex);

		// Removes chromosome index from the group.
		// Returns TRUE if the chromosome is removed.
		DLL_EXPORT
		bool GACALL Remove(int chromosomeIndex);

		// Removes all chromosomes from the group
		DLL_EXPORT
		void GACALL Clear();

		// Returns ranking of chromosome.
		// If chromosomi is not in group returns -1;
		DLL_EXPORT
		int GACALL GetRanking(int chromosomeIndex);

		// Returns chromosome's index in population at given position
		DLL_EXPORT
		int GACALL GetAt(int pos) const;

		// Returns refernece to scaled chromosome at given position
		DLL_EXPORT
		GaScaledChromosome& GACALL GetScaledChromosomeAt(int pos) const;

		// Returns pointer to chromosome at given position
		DLL_EXPORT
		GaChromosomePtr GACALL GetChromosomeAt(int pos) const;

		// Returns maximum size of the group
		DLL_EXPORT
		int GACALL GetMaxSize() const;

		// Sets maximum size of the group
		DLL_EXPORT
		void GACALL SetMaxSize(int size);

		// Returns current number of chromosomes in the group
		DLL_EXPORT
		int GACALL GetCurrentSize() const;

		// Returns type of this group
		DLL_EXPORT
		GaSortedGroupType GACALL GetType() const;

		// Returns fitnes comparator used to sort the group
		DLL_EXPORT
		const GaFitnessComparator* GACALL GetComparator() const;

		// Returns fitnes comparator used to sort the group
		DLL_EXPORT
		void GACALL SetComparator(const GaFitnessComparator* comparator);

		// Population in which chromosomes of the group is suited
		DLL_EXPORT
		const GaPopulation* GACALL GetPopulation() const;

		// Return chromosome's index in population at given position
		DLL_EXPORT
		int GACALL operator [](int pos) const;

	};// END CLASS DEFINITION GaSortedGroup

	// #pragma endregion

	// #pragma region GaPopulationParameters

	// Parameters of the population
	class GaPopulationParameters : public GaParameters
	{

	private:

		// Number of chromosomes in population
		int _populationSize;

		// Inidicate that population can change it size during evolution
		bool _resizable;

		// Sorting of the population
		bool _sorting;

		// If set to TRUE scaled fitness value is used to sort population otherwise nonscaled fitness value is used
		bool _usingScaledFitness;

		// How many best chromosomes in the population are cached
		int _bestTrackCount;

		// How many worst chromosomes in the population are cached
		int _worstTrackCount;

	public:

		// Initialization of the parameters
		DLL_EXPORT
		GaPopulationParameters(int populationSize,
			bool resizable,
			bool sorting,
			bool useScaldeFitness,
			int bestTrackCount,
			int worstTrackCount);

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

		// Returns number of chromosomes in the population
		DLL_EXPORT
		int GACALL GetPopulationSize() const;

		// Sets  number of chromosomes in the population
		DLL_EXPORT
		void GACALL SetPopulationSize(int size);

		// Returns TRUE of the population can change it size during evolution
		DLL_EXPORT
		bool GACALL GetResizable() const;

		// Set to TRUE if the population will change it size during evolution
		DLL_EXPORT
		void GACALL SetResizable(bool resizable);

		// Returns TRUE if the population should be sorted
		DLL_EXPORT
		bool GACALL GetSorting() const;

		// Sets sorting of the population
		DLL_EXPORT
		void GACALL SetSorting(bool sorting);

		// Returns TRUE it the scaled fitness value is used to sort population otherwise nonscaled fitness value is used
		DLL_EXPORT
		bool GACALL GetUsingScaledFitness() const;

		// Set to TRUE it the scaled fitness value is used to sort population and for nonscaled fitness value set to FALSE
		DLL_EXPORT
		void GACALL SetUsingScaledFitness(bool useScaledFitness);

		// Returns how many best chromosomes in the population are cached
		DLL_EXPORT
		int GACALL GetBestTrackCount() const;

		// Sets how many best chromosomes in the population are cached
		DLL_EXPORT
		void GACALL SetBestTrackCount(int count);

		// Returns how many worst chromosomes in the population are cached
		DLL_EXPORT
		int GACALL GetWorstTrackCount() const;

		// Sets how many worst chromosomes in the population are cached
		DLL_EXPORT
		void GACALL SetWorstTrackCount(int count);

	};// END CLASS DEFINITION GaPopulationParameters

	// #pragma endregion

	// #pragma region GaDefaultPopulationOperations

	// Contains default operations which belonogs to population and theirs parameters
	class GaDefaultPopulationOperations
	{

	public:

		// Default selection operation
		GaSelectionPair _selection;

		// Default replacement operation
		GaReplacementPair _replacement;

		// Default scaling operation
		GaScalingPair _scaling;

		// Default coupling operation
		GaCouplingPair _coupling;

	private:

		// Global instance of the catalogue
		static GaDefaultPopulationOperations* _instance;

	public:

		// Initialize the catalogue
		GaDefaultPopulationOperations();

		// Returns instance of global catalogue
		static GaDefaultPopulationOperations& GACALL Instance();

⌨️ 快捷键说明

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