gaobserving.h

来自「遗传算法做的排课系统」· C头文件 代码 · 共 115 行

H
115
字号

#ifndef __GA_OBSERVING_H__
#define __GA_OBSERVING_H__

#include <list>

#include "..\CallConvention.h"
#include "..\ExportImport.h"
#include "..\Chromosome\GaChromosome.h"
#include "..\Common\GaStatistics.h"
#include "..\Threading\GaThreading.h"

namespace Algorithm
{
	enum GaAlgorithmState;
	class GaAlgorithm;
}

using namespace std;
using namespace Common;
using namespace Chromosome;
using namespace Algorithm;

namespace Observing
{
	// Interface for observers of an genetic algorithm
	class GaObserver
	{

	public:

		// Virtual destructor
		virtual ~GaObserver() { }

		// Notifies the observer that new statistical information is available
		virtual void GACALL StatisticUpdate(const GaStatistics& statistics,
			const GaAlgorithm& algorithm)=0;

		// Notifies observer that new best chromosome has found
		virtual void GACALL NewBestChromosome(const GaChromosome& newChromosome,
			const GaAlgorithm& algorithm)=0;

		// Notifies observer that state of evolution (problem sloving) has changed.
		virtual void GACALL EvolutionStateChanged(GaAlgorithmState newState,
			const GaAlgorithm& algorithm)=0;

	};// END INTERFACE DEFINITION GaObserver

	// Adapter calss for GaObserver interface
	class GaObserverAdapter : public GaObserver
	{

	public:

		// Notifies the observer that new statistical information is available
		virtual void GACALL StatisticUpdate(const GaStatistics& statistics,
			const GaAlgorithm& algorithm) {};

		// Notifies observer that new best chromosome has found
		virtual void GACALL NewBestChromosome(const GaChromosome& newChromosome,
			const GaAlgorithm& algorithm) {};

		// Notifies observer that state of evolution (problem sloving) has changed.
		virtual void GACALL EvolutionStateChanged(GaAlgorithmState newState,
			const GaAlgorithm& algorithm) {};

	};// END CLASS DEFINITION GaObserverAdapter

	// Manages list of subscribed observer
	class GaObserversList : public GaObserver
	{
		DEFINE_SYNC_CLASS

	protected:

		typedef list<GaObserver*> GaObserversListType;

		// List of subscribed observers
		GaObserversListType _observers;

	public:

		// Notifies the observer that new statistical information is available
		DLL_EXPORT
		virtual void GACALL StatisticUpdate(const GaStatistics& statistics,
			const GaAlgorithm& algorithm);

		// Notifies observer that new best chromosome has found
		DLL_EXPORT
		virtual void GACALL NewBestChromosome(const GaChromosome& newChromosome,
			const GaAlgorithm& algorithm);

		// Notifies observer that state of evolution (problem sloving) has changed.
		DLL_EXPORT
		virtual void GACALL EvolutionStateChanged(GaAlgorithmState newState,
			const GaAlgorithm& algorithm);

		// Subscribes observer
		DLL_EXPORT
		GaObserversList& GACALL operator +=(GaObserver& observer);

		// Unsubscribes observer
		DLL_EXPORT
		GaObserversList& GACALL operator -=(GaObserver& observer);

		// Returns the number of subscribed observers
		DLL_EXPORT
		int GACALL GetObserverCount() const;

	};// END CLASS DEFINITION GaObserversList

} // Observing

#endif //__GA_OBSERVING_H__

⌨️ 快捷键说明

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