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

📄 gaclass.h

📁 这是一份整理好的遗传算法的类
💻 H
字号:

//遗传算法类头文件 GAClass.h


#ifndef _MY_GA_CLASS_H
#define _MY_GA_CLASS_H

#include "fstream"
using namespace std;

#define TYPE_BIT 0//编码为二进制编码
#define TYPE_GROUP 1//编码为浮点数编码
#define MAXBITSIZE 2//二进制编码的取值个数
#define MAXDOUBLESIZE 100//浮点数编码的取值个数
#define MAX_GENERATION 5000//最大进化代数
#define DEFAULT_SIZE 1000//缺省群体规模
#define DEFAULT_INDIV_LEN 50 //个体编码长度

#define SELECT_P	0.5 //选择概率
#define CROSS_P     0.7 //交叉概率
#define MUTE_P      0.1 //变异概率

#define CODE_VALUE_EPS 0.01  //控制实数编码的浮点数精度

UINT EvovleThreadProc(LPVOID pParam)

//class GAPopulation;
class GAPopulation:public CObject
{
	DECLARE_DYNCREATE(GAPopulation)
	enum{SEL_ROTATE,SEL_DRANK,SEL_RANK};//轮盘赌法、双重排序选择法、单排序选择法
	enum{eProcStart,eProcRunning,eProcEnd};
public:
	GAPopulation(CRuntimeClass* pClass=NULL);
	GAPopulation(CRuntimeClass* pClass,int nSize,int nMaxGen,int nIndivLen,int nCodeType);
	GAPopulation(GAPopulation&);
	 ~GAPopulation();
	GAPopulation operator =(GAPopulation&);

	void SetPopSize(int nSize){m_nSize=nSize;}//设置群体规模
	int GetPopSize(){return m_nSize;}//返回群体规模

	void SetMaxGeneration(int nMaxGen){m_nMaxGen=nMaxGen;}//设置进化代数
	int GetMaxGeneration(){return m_nMaxGen;}//返回进化代数

	void SetIndivLen(int nLen){m_nIndivLen=nLen;}
	int GetIndivLen(){return m_nIndivLen;}

	void SetCodeType(int nCodeType){m_nCodeType=nCodeType;}
	int GetCodeType(){return m_nCodeType;}


	void InitialPop();//初始化种群
	void Evolve();//进化

	virtual class ZDecoder;

	double GetBestResult();
	BOOL IsBroken(){return m_bIsBroken;}//是否中断
	void Break(){m_bIsBroken=TRUE;}//中断进化

	void SetSelP(double p){m_dSelP=p;}
	double GetSelP(){return m_dSelP;}
	
	void SetCrossP(double p){m_dCrossP=p;}
	double GetCrossP(){return m_dCrossP;}
	
	void SetMuteP(double p){m_dMuteP=p;}
	double GetMuteP(){return m_dMuteP;}
	
	CWnd* GetResultWindow(){
		return m_pResultWnd;
	}
	
	void ForceResultWndClose();

	GAIndividual* GetBestIndiv();//返回最优个体
	GAIndividual* GetIndividual(int nIndex){	return m_pIndivArray[nIndex];}

	GAPopulation* Select();
	void Evaluate(BOOL bRecaculateAll=TRUE);

	double GetInflatValue(){return m_dInflat;}
	void SetInflatValue(double dInflat){m_dInflat=dInflat;}

	void Sort();
	GAPopulation* Select2(GAPopulation* p1,GAPopulation* p2);

	int GetSelMethod(){return m_nSelectMethod;}
	void SetSelMethod(int nSelMethod){m_nSelectMethod=nSelMethod;} 

	void WaitingForThreadEnd();

	BOOL IsInvertNeeded();
	void Invert();

	BOOL IsMuteUpdateNeeded();
	void MuteUpdate();//update the pop  by mute the  best indiv bit  by bit

	BOOL IsTemplateUpdateNeeded();
	void TemplateUpdate();
private:
	HANDLE m_hEvolveThread;//计算线程
	
	int m_nSize;//群体规模
	int m_nMaxGen;//进化最大的代数
	double m_dBestResult;//最优结果
	int m_nBestIndex;//最优个体在群体中的位置
	BOOL m_bIsBroken;//是否中断
	
	GAIndividual** m_pIndivArray;//群体数组的指针
	
	double m_dSelP;//选择概率
	double m_dCrossP;//交叉概率
	double m_dMuteP;//变异概率
	int m_nSelectMethod;//选择方法;

	CWnd* m_pResultWnd;//结果显示窗口

	CRuntimeClass* m_pClass;   //个体类的runtimeclass结构,added at 2002.7.3

public:
	double GetMaxValue();
	double GetMinValue();
	void Destroy();
	void Duplicate(GAPopulation* pResource,GAPopulation* pDestation);
	int GetRandomIndiv();
	int* m_pIntValSet;//存储整型编码的取值集合
	double* m_pDoubleValSet;//存储实型编码的取值集合

	GAIndividual* GetBetterIndiv(GAIndividual* p1,GAIndividual* p2);


private:
	int m_nIndivLen;//个体编码长度
	int m_nCodeType;//编码类型
	int m_nValScope;//取值的范围(即同一基因位可取值的离散范围)

	double m_dInflat;//退火因子
	GAIndividual* m_pTemplateIndiv;

	int  m_nProcState;
public:
	// replace the ith indiv with the new indiv --pNew
	void ReplaceIndiv(int nIndex, GAIndividual* pNew);
	// Create the new Individual
	GAIndividual* CreateNewIndiv(void);
	GAIndividual* CreateTemplateIndiv(void);
	void ReplaceIndiv(GAIndividual* pOld, GAIndividual* pNew);
	int GetBestIndivIndex(void);
	
	double GetWorstResult();
	double GetMeanResult();

	// 显示计算结果
	virtual void ShowBestResult(ofstream& pf);
	virtual void WriteBestResultToFile(fstream& pf){};

	int  GetProcState(){return m_nProcState;}
	void SetProcState(int nStat){m_nProcState=nStat;}

	//for guosiyuan caculation
	void SetStationQH(double dQ,double dH){m_dQ=dQ;m_dH=dH;}
	double m_dQ;
	double m_dH;

};


class GANode:public CObject
{
	DECLARE_DYNCREATE(GANode)
public:
	BOOL Compare(GANode* pNode);
	BOOL Mute();
	GANode();
	virtual ~GANode();
	GANode* Create(GAPopulation* pPop,int nType,double dMin=0,double dMax=1.0,double dEps=CODE_VALUE_EPS);
	GANode(GANode&);
	GANode operator =(GANode&);

	int GetValueType(){return m_nValueType;}
	void SetValueType(int nType){m_nValueType=nType;}
	
	int GetIntValue(){	return m_nBit;	}
	double GetDoubleValue(){return m_dGroup;	}
	
	//	int GetValue(){	GetIntValue();}
	//	double GetValue(){GetDoubleValue();}
	
	
	void SetValue(double dVal){m_dGroup=dVal;}
	void SetValue(int nVal){m_nBit=nVal;}
	
	void CopyNode(GANode* pNode){
		m_nValueType=pNode->m_nValueType;
		m_nBit=pNode->m_nBit;
		m_dGroup=pNode->m_dGroup;
		m_pParent=pNode->m_pParent;
		m_dMinVal=pNode->m_dMinVal;
		m_dMaxVal=pNode->m_dMaxVal;
		m_dValEps=pNode->m_dValEps;
	}
	
	void SetValueExt(double dMin,double dMax){m_dMinVal=dMin;m_dMaxVal=dMax;}
	double GetMinVal(){return m_dMinVal;}
	double GetMaxVal(){return m_dMaxVal;}
	void SetValueEps(double dEps){m_dValEps=dEps;}
	double GetValueEps(){return m_dValEps;}
private:
	int m_nValueType;//值的类型
	int m_nBit;
	double m_dGroup;
	GAPopulation* m_pParent;

	double m_dMaxVal;
	double m_dMinVal;
	double m_dValEps;
};
class GAIndividual:public CObject
{
	DECLARE_DYNCREATE(GAIndividual)
public:
	BOOL IsObjValCaculated();
	GAIndividual();
	virtual ~GAIndividual();
	virtual GAIndividual* Create(GAPopulation* pPop,int nLength);
	virtual GAIndividual* Create(GAIndividual* pDup);

	GAIndividual(GAIndividual&);
	GAIndividual operator =(GAIndividual&);
	
	virtual double Evaluate();//计算目标函数
	virtual double GetFitness();//计算适应度
	
	virtual void Duplicate(GAIndividual* pDup);//复制
	virtual BOOL CrossOver(GAIndividual* pCross);//交叉
	virtual void Mute();//变异
	
	virtual BOOL Compare(GAIndividual* pCompare);//比较
	
	void SetLength(int nLength){m_nLength=nLength;}
	int GetLength(){return m_nLength;}
	
	GANode* GetNode(int nIndex){
		//GANode node=m_pNodeArray[nIndex];
		//return node;
		if(nIndex>=m_nLength)return NULL;
		return &(m_pNodeArray[nIndex]);
	}
	void SetNode(int nIndex,GANode node){
		m_pNodeArray[nIndex]=node;
	}

	int GetRandomBit();
	void Invert();

	void UpdateFromTemplate(GAIndividual* pTemp);
	virtual void ShowBestResult(){};//added by kunlun.x at 2004.12.20
protected:
	BOOL m_bIsObjValCaculated;
	int m_nLength;//个体编码长度
	double m_dObjVal;//目标函数值
	double m_dFitness;//适应度
	GANode* m_pNodeArray;
	GAPopulation* m_pParent;

public:
	// 设置个体的目标函数值(此值可能来自其他已进行评价过的相同个体)
	void SetObjVal(double dVal);
	double GetObjVal(){	return m_dObjVal;}
	CString GetIndivString(void);
	void SetParent(GAPopulation* pPop);
};

/*

class GAPopulation:public CObject
{
	DECLARE_DYNCREATE(GAPopulation)
	enum{SEL_ROTATE,SEL_DRANK,SEL_RANK};//轮盘赌法、双重排序选择法、单排序选择法
	enum{eProcStart,eProcRunning,eProcEnd};
public:
	GAPopulation(CRuntimeClass* pClass=NULL);
	GAPopulation(CRuntimeClass* pClass,int nSize,int nMaxGen,int nIndivLen,int nCodeType);
	GAPopulation(GAPopulation&);
	 ~GAPopulation();
	GAPopulation operator =(GAPopulation&);

	void SetPopSize(int nSize){m_nSize=nSize;}//设置群体规模
	int GetPopSize(){return m_nSize;}//返回群体规模

	void SetMaxGeneration(int nMaxGen){m_nMaxGen=nMaxGen;}//设置进化代数
	int GetMaxGeneration(){return m_nMaxGen;}//返回进化代数

	void SetIndivLen(int nLen){m_nIndivLen=nLen;}
	int GetIndivLen(){return m_nIndivLen;}

	void SetCodeType(int nCodeType){m_nCodeType=nCodeType;}
	int GetCodeType(){return m_nCodeType;}


	void InitialPop();//初始化种群
	void Evolve();//进化

	virtual class ZDecoder;

	double GetBestResult();
	BOOL IsBroken(){return m_bIsBroken;}//是否中断
	void Break(){m_bIsBroken=TRUE;}//中断进化

	void SetSelP(double p){m_dSelP=p;}
	double GetSelP(){return m_dSelP;}
	
	void SetCrossP(double p){m_dCrossP=p;}
	double GetCrossP(){return m_dCrossP;}
	
	void SetMuteP(double p){m_dMuteP=p;}
	double GetMuteP(){return m_dMuteP;}
	
	CWnd* GetResultWindow(){
		return m_pResultWnd;
	}
	
	void ForceResultWndClose();

	GAIndividual* GetBestIndiv();//返回最优个体
	GAIndividual* GetIndividual(int nIndex){	return m_pIndivArray[nIndex];}

	GAPopulation* Select();
	void Evaluate(BOOL bRecaculateAll=TRUE);

	double GetInflatValue(){return m_dInflat;}
	void SetInflatValue(double dInflat){m_dInflat=dInflat;}

	void Sort();
	GAPopulation* Select2(GAPopulation* p1,GAPopulation* p2);

	int GetSelMethod(){return m_nSelectMethod;}
	void SetSelMethod(int nSelMethod){m_nSelectMethod=nSelMethod;} 

	void WaitingForThreadEnd();

	BOOL IsInvertNeeded();
	void Invert();

	BOOL IsMuteUpdateNeeded();
	void MuteUpdate();//update the pop  by mute the  best indiv bit  by bit

	BOOL IsTemplateUpdateNeeded();
	void TemplateUpdate();
private:
	HANDLE m_hEvolveThread;//计算线程
	
	int m_nSize;//群体规模
	int m_nMaxGen;//进化最大的代数
	double m_dBestResult;//最优结果
	int m_nBestIndex;//最优个体在群体中的位置
	BOOL m_bIsBroken;//是否中断
	
	GAIndividual** m_pIndivArray;//群体数组的指针
	
	double m_dSelP;//选择概率
	double m_dCrossP;//交叉概率
	double m_dMuteP;//变异概率
	int m_nSelectMethod;//选择方法;

	CWnd* m_pResultWnd;//结果显示窗口

	CRuntimeClass* m_pClass;   //个体类的runtimeclass结构,added at 2002.7.3

public:
	double GetMaxValue();
	double GetMinValue();
	void Destroy();
	void Duplicate(GAPopulation* pResource,GAPopulation* pDestation);
	int GetRandomIndiv();
	int* m_pIntValSet;//存储整型编码的取值集合
	double* m_pDoubleValSet;//存储实型编码的取值集合

	GAIndividual* GetBetterIndiv(GAIndividual* p1,GAIndividual* p2);


private:
	int m_nIndivLen;//个体编码长度
	int m_nCodeType;//编码类型
	int m_nValScope;//取值的范围(即同一基因位可取值的离散范围)

	double m_dInflat;//退火因子
	GAIndividual* m_pTemplateIndiv;

	int  m_nProcState;
public:
	// replace the ith indiv with the new indiv --pNew
	void ReplaceIndiv(int nIndex, GAIndividual* pNew);
	// Create the new Individual
	GAIndividual* CreateNewIndiv(void);
	GAIndividual* CreateTemplateIndiv(void);
	void ReplaceIndiv(GAIndividual* pOld, GAIndividual* pNew);
	int GetBestIndivIndex(void);
	
	double GetWorstResult();
	double GetMeanResult();

	// 显示计算结果
	virtual void ShowBestResult(ofstream& pf);
	virtual void WriteBestResultToFile(fstream& pf){};

	int  GetProcState(){return m_nProcState;}
	void SetProcState(int nStat){m_nProcState=nStat;}

	//for guosiyuan caculation
	void SetStationQH(double dQ,double dH){m_dQ=dQ;m_dH=dH;}
	double m_dQ;
	double m_dH;

};*/





#endif

⌨️ 快捷键说明

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