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

📄 schedule.h

📁 遗传算法的四个源程序和源代码
💻 H
字号:

/*
 * 
 * website: http://www.coolsoft-sd.com/
 * contact: support@coolsoft-sd.com
 *
 */

/*
 * Genetic Algorithm Library
 * Copyright (C) 2007-2008 Coolsoft Software Development
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */


#pragma once

#include <list>

#ifdef WIN32

#include <hash_map>
using namespace stdext;

#else

#include <ext/hash_map>
using namespace __gnu_cxx;

#endif

#include "CourseClass.h"

#include "../../GeneticLibrary/source/Initialization.h"

#include "../../GeneticLibrary/source/MultiValueChromosome.h"
#include "../../GeneticLibrary/source/ChromosomeOperations.h"

#include "../../GeneticLibrary/source/Population.h"
#include "../../GeneticLibrary/source/SelectionOperations.h"
#include "../../GeneticLibrary/source/ReplacementOperations.h"

#include "../../GeneticLibrary/source/StopCriterias.h"
#include "../../GeneticLibrary/source/IncrementalAlgorithm.h"

using namespace Threading;
using namespace Chromosome;
using namespace Chromosome::Representation;

using namespace Population;
using namespace Population::ReplacementOperations;
using namespace Population::SelectionOperations;

using namespace Algorithm;
using namespace Algorithm::SimpleAlgorithms;
using namespace Algorithm::StopCriterias;

#if defined(GAL_STL_EXT_MSVC)

	typedef hash_map<CourseClass*, int> CourseClassHashMap;

#else

	class CourseClassHash
	{

	public:

		// Parameters for hash table
		enum
		{
			bucket_size = 4,
			min_buckets = 8
		};	

		// Construct with default comparator
		CourseClassHash() { }

		// Hash function
		size_t GACALL operator()(CourseClass* key) const
		{
			size_t t = (size_t)key;

			ldiv_t rem = ldiv((long)t, 127773);
			rem.rem = 16807 * rem.rem - 2836 * rem.quot;
			if (rem.rem < 0)
				rem.rem += 2147483647;

			return ((size_t)rem.rem);
		}

		// Comparison
		inline bool GACALL operator()(CourseClass* value1, CourseClass* value2) const { return value1 == value2; }

	};

	typedef hash_map<CourseClass*, int, CourseClassHash, CourseClassHash> CourseClassHashMap;

#endif

#define DAY_HOURS	12
#define DAYS_NUM	5

class ScheduleObserver : public GaObserverAdapter
{

private:

	CChildView* _window;

	SysEventObject _event;


	void ReleaseEvent() { SignalEvent( _event ); }

public:

	ScheduleObserver() : _window(NULL) { MakeEvent( _event, 0 ); }

	virtual ~ScheduleObserver() { DeleteEvent( _event ); }

	void WaitEvent() { WaitForEvent( _event ); }
	
	virtual void GACALL NewBestChromosome(const GaChromosome& newChromosome, const GaAlgorithm& algorithm);

	virtual void GACALL EvolutionStateChanged(GaAlgorithmState newState, const GaAlgorithm& algorithm);

	inline void SetWindow(CChildView* window) { _window = window; }

};

class ScheduleCrossover : public GaCrossoverOperation
{

public:

	virtual GaChromosomePtr GACALL operator ()(const GaChromosome* parent1,
									  const GaChromosome* parent2) const;

	virtual GaParameters* GACALL MakeParameters() const { return NULL; }

	virtual bool GACALL CheckParameters(const GaParameters& parameters) const { return true; }

};

class ScheduleMutation : public GaMutationOperation
{

public:

	virtual void GACALL operator ()(GaChromosome* chromosome) const;

	virtual GaParameters* GACALL MakeParameters() const { return NULL; }

	virtual bool GACALL CheckParameters(const GaParameters& parameters) const { return true; }

};

class ScheduleFitness : public GaFitnessOperation
{

public:

	virtual float GACALL operator ()(const GaChromosome* chromosome) const;

	virtual GaParameters* GACALL MakeParameters() const { return NULL; }

	virtual bool GACALL CheckParameters(const GaParameters& parameters) const { return true; }
};

class Schedule : public GaMultiValueChromosome<list<CourseClass*> >
{

	friend class ScheduleCrossover;
	friend class ScheduleMutation;
	friend class ScheduleFitness;
	friend class ScheduleObserver;

private:

	CourseClassHashMap _classes;

	CourseClassHashMap _backupClasses;

	// Flags of class requiroments satisfaction
	mutable vector<bool> _criteria;

public:

	Schedule(GaChromosomeDomainBlock<list<CourseClass*> >* configBlock);

	Schedule(const Schedule& c, bool setupOnly);

	virtual ~Schedule() { }

	virtual GaChromosomePtr GACALL MakeCopy(bool setupOnly) const { return new Schedule( *this, setupOnly ); }

	virtual GaChromosomePtr GACALL MakeNewFromPrototype() const;

	virtual void GACALL PreapareForMutation();

	virtual void GACALL AcceptMutation();

	virtual void GACALL RejectMutation();

	// Returns reference to table of classes
	inline const hash_map<CourseClass*, int>& GetClasses() const { return _classes; }

	// Returns array of flags of class requiroments satisfaction
	inline const vector<bool>& GetCriteria() const { return _criteria; }

	// Return reference to array of time-space slots
	inline const vector<list<CourseClass*> >& GetSlots() const { return _values; }

};

class ScheduleTest
{

private:

	static ScheduleTest _instance;

	GaChromosomeParams* _chromosomeParams;

	ScheduleCrossover _crossoverOperation;

	ScheduleMutation _mutationOperation;

	ScheduleFitness _fitnessOperation;

	GaChromosomeDomainBlock<list<CourseClass*> >* _ccb;

	Schedule* _prototype;

	GaPopulationConfiguration* _populationConfig;

	GaPopulation* _population;

	GaAlgorithm* _algorithm;

	ScheduleObserver _observer;

public:

	inline static ScheduleTest& GetInstance() { return _instance; }

	ScheduleTest();

	~ScheduleTest();

	inline GaAlgorithm* GetAlgorithm() { return _algorithm; }

	inline const GaAlgorithm* GetAlgorithm() const { return _algorithm; }

	inline ScheduleObserver& GetObserver() { return _observer; }

	inline const ScheduleObserver& GetObserver() const { return _observer; }

};

⌨️ 快捷键说明

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