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

📄 gaalgorithm.h

📁 遗传算法做的排课系统
💻 H
字号:

#ifndef __GA_ALGORITHM_H__
#define __GA_ALGORITHM_H__

#include "..\ExportImport.h"
#include "..\CallConvention.h"
#include "..\Threading\GaThreading.h"
#include "..\Observing\GaObserving.h"
#include "..\Common\GaCatalogue.h"
#include "GaStopCriteria.h"
#include "..\Population\GaPopulation.h"

using namespace Threading;
using namespace Observing;
using namespace Common;
using namespace Population;
using namespace Algorithm;

namespace Algorithm
{
	// Base class for all algorithm parameters
	class GaAlgorithmParams : public GaParameters { }; // END CLASS DEFINITION GaAlgorithmParams

	// Defines possible states of genetic algorithm
	enum GaAlgorithmState
	{
		GAS_UNINITIALIZED = 0x1, 
		GAS_USER_STOPED = 0x2, 
		GAS_CRITERIA_STOPPED = 0x4, 
		GAS_PAUSED = 0x8, 
		GAS_RUNNING = 0x10, 
		/* --- combined states --- */
		GAS_NOT_RUNNING = 0xF, 
		GAS_STOPPED = 0x6
	};

	// Thread types for multithreaded algorithms
	enum GaAlgorithmThreadType
	{
		GATT_CONTROL = 0, 
		GATT_WORK = 1
	};

	// Interface for genetic algorithms
	class GaAlgorithm
	{

	public:

		// Destructor
		virtual ~GaAlgorithm() { };

		// Sends signal to algoritham to start evolution and problem solving.
		// It can be specified if the solving is continued from previosly saved state
		// or evolution can be started from the beginning discharging saved state,
		virtual void GACALL StartSolving(bool continueSolving)=0;

		// Sends signal to algorithm to stop evolution and problem solving.
		// and to discharge current state.
		virtual void GACALL StopSolving()=0;

		// Sends signal to algorithm to temporary stop evolution and problem solving.
		// but to save the current state.
		virtual void GACALL PauseSolving()=0;

		// Every call of BeginParameterChange() must be fallowed by EndParameterChange() call, or deadlock will occure.
		// Should be called before any parameter of any aspect of an algorithm is changed.
		// It will block thread which calls this method until it is save to do the parameter change.
		virtual void GACALL BeginParameterChange()=0;

		// Every call of BeginParameterChange() must be fallowed by EndParameterChange() call, or deadlock will occure.
		// It signals algorithm that paremeter is changed and it can resume of the execution,
		virtual void GACALL EndParameterChange()=0;

		// Returns the pair of operation and parameters which estimates when the algorithm should stop evolution and problem solving.
		virtual const GaStopCriteriaPair& GACALL StopCriteria() const=0;

		// Sets stop criteria and it's parameters
		virtual void GACALL SetStopCriteria(GaStopCriteria* criteria,
			GaStopCriteriaParams* parameters)=0;

		// Set parameters for stop criteria
		virtual void GACALL SetStopCriteriaParams(GaStopCriteriaParams* parameters)=0;

		// Returns current algorithm parameters
		virtual const GaAlgorithmParams& GACALL GetAlgorithmParameters() const=0;

		// Sets new parameters for algorithm
		virtual void GACALL SetAlgorithmParameters(const GaAlgorithmParams& parameters)=0;

		// Retruns statistical information about execution of the algorithm.
		virtual const GaStatistics& GACALL GetAlgorithmStatistics() const=0;

		// Returns referenct to population at given index.
		virtual const GaPopulation& GACALL GetPopulation(int index) const=0;

		// Returns the state of the evolution and problem solving.
		virtual GaAlgorithmState GACALL GetState() const=0;

		// Subscribes algorithm's observer
		virtual void GACALL SubscribeObserver(GaObserver* observer)=0;

		// Subscribes algorithm's observer
		virtual void GACALL UnsubscribeObserver(GaObserver* observer)=0;

	protected:

		// Every BlockParameterChanages() call must be fallowed by ReleaseParameterChanages() call, or deadlock will occure.
		// Should be called when algoritham enters the section in which changing of parameters is not allowed.
		// If at the moment of call to this method another thread called BeginParemeterChangeg() but not yet called EndPrameterChanged(),
		// this call blocks calling  thread until EndParameterChange() call.
		virtual void GACALL BlockParameterChanges()=0;

		// Every BlockParameterChanages() call must be fallowed by ReleaseParameterChanages() call, or deadlock will occure.
		// Should be called when algoritham exits the section in which changing of parameters is not allowed.
		virtual void GACALL ReleaseParameterChanages()=0;

	};// END CLASS DEFINITION GaAlgorithm

	// Implements auxiliary behaviours of an algorithm
	class GaBaseAlgorithm : public GaAlgorithm
	{

	protected:

		// State of the evolution and problem solving.
		GaAlgorithmState _state;

		// Stop ciriteria with parameters
		GaStopCriteriaPair _stopCriteria;

		// Synchronization object for parameter changes
		GaCriticalSection _syncParameterChanges;

		// Synchronization object for state changes
		GaCriticalSection _syncStateChange;

		// Subscribed observer
		GaObserversList _observers;

	public:

		// Initialization of basic features of the algorithm
		DLL_EXPORT
		GaBaseAlgorithm();

		// Sends signal to algoritham to start evolution and problem solving.
		// It can be specified if the solving is continued from previosly saved state
		// or evolution can be started from the beginning discharging saved state,
		DLL_EXPORT
		virtual void GACALL StartSolving(bool continueSolving);

		// Sends signal to algorithm to stop evolution and problem solving.
		// and to discharge current state.
		DLL_EXPORT
		virtual void GACALL StopSolving();

		// Sends signal to algorithm to temporary stop evolution and problem solving.
		// but to save the current state.
		DLL_EXPORT
		virtual void GACALL PauseSolving();

		// Every call of BeginParameterChange() must be fallowed by EndParameterChange() call, or deadlock will occure.
		// Should be called before any parameter of any aspect of an algorithm is changed.
		// It will block thread which calls this method until it is save to do the parameter change.
		DLL_EXPORT
		virtual void GACALL BeginParameterChange();

		// Every call of BeginParameterChange() must be fallowed by EndParameterChange() call, or deadlock will occure.
		// It signals algorithm that paremeter is changed and it can resume of the execution,
		DLL_EXPORT
		virtual void GACALL EndParameterChange();

		// Returns the pair of operation and parameters which estimates when the algorithm should stop evolution and problem solving.
		DLL_EXPORT
		virtual const GaStopCriteriaPair& GACALL StopCriteria() const;

		// Sets stop criteria and it's parameters
		DLL_EXPORT
		virtual void GACALL SetStopCriteria(GaStopCriteria* criteria,
			GaStopCriteriaParams* parameters);

		// Set parameters for stop criteria
		DLL_EXPORT
		virtual void GACALL SetStopCriteriaParams(GaStopCriteriaParams* parameters);

		// Returns the state of the evolution and problem solving.
		DLL_EXPORT
		virtual GaAlgorithmState GACALL GetState() const;

		// Subscribes algorithm's observer
		DLL_EXPORT
		virtual void GACALL SubscribeObserver(GaObserver* observer);

		// Subscribes algorithm's observer
		DLL_EXPORT
		virtual void GACALL UnsubscribeObserver(GaObserver* observer);

	protected:

		// Check to see if stop criteria is reached.
		// if it is reached then sets algorithm state to GAS_CRITERIA_STOPPED and return TRUE.
		DLL_EXPORT
		virtual bool GACALL CheckStopCriteria();

		// Initialize the algorithm
		virtual void GACALL Initialize()=0;

		// Called when user starts the algorithm
		virtual bool GACALL OnStart()=0;

		// Called when user stops the algorithm
		virtual bool GACALL OnStop()=0;

		// Called when user pauses the algorithm
		virtual bool GACALL OnPause()=0;

		// Called when user resumes the paused algorithm
		virtual bool GACALL OnResume()=0;

		// Every BlockParameterChanages() call must be fallowed by ReleaseParameterChanages() call, or deadlock will occure.
		// Should be called when algoritham enters the section in which changing of parameters is not allowed.
		// If at the moment of call to this method another thread called BeginParemeterChangeg() but not yet called EndPrameterChanged(),
		// this call blocks calling  thread until EndParameterChange() call.
		DLL_EXPORT
		virtual void GACALL BlockParameterChanges();

		// Every BlockParameterChanages() call must be fallowed by ReleaseParameterChanages() call, or deadlock will occure.
		// Should be called when algoritham exits the section in which changing of parameters is not allowed.
		DLL_EXPORT
		virtual void GACALL ReleaseParameterChanages();

		// Every BlockStateChange() call must be fallowed by ReleaseStateChange() call, or deadlock will occure.
		// Should be called when algoritham enters the section in which changing of its state is not allowed.
		DLL_EXPORT
		virtual void GACALL BlockStateChange();

		// Every BlockStateChange() call must be fallowed by ReleaseStateChange() call, or deadlock will occure.
		// Should be called when algoritham exits the section in which changing of its state is not allowed.
		DLL_EXPORT
		virtual void GACALL ReleaseStateChange();

	};// END CLASS DEFINITION GaBaseAlgorithm

} // Algorithm

#endif // __GA_ALGORITHM_H__

⌨️ 快捷键说明

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