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

📄 ga.h

📁 VC编写的基本遗传算法,建立遗传算子类
💻 H
字号:
// GA.h: interface for the GA class.
//
//////////////////////////////////////////////////////////////////////
#include <Afxcoll.h>

#if !defined(AFX_GA_H__36707D34_93BC_4503_BBA8_DBFD1DCBA46C__INCLUDED_)
#define AFX_GA_H__36707D34_93BC_4503_BBA8_DBFD1DCBA46C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//////////////////////////////////////////////////////////////////////
//						Code by .御米.
//						DarkThorn@163.com
//						http://blog.donews.com/6lines
//
// MAXPOP: the max individual num of population		MAXSTR: the max chrom length
// nGen: current generation num						nMaxGen: max calculate num of generation
// nMutation: total mutate num						nCross: total crossover num 
// nXcross: crossover point 
// nMaxPop:	individual index with max fitness		nMinPop: individual index with min fitness
//  
// fPc: probability of crossover					fPm: probability of mutation
// fSumFit: sum fitness of population				fAvgFit: average fitness of population
// fMaxFit: max fitness in population				fMinFit: min fitness in population
// coef: pow(2.0,nChromLen)-1.0
//////////////////////////////////////////////////////////////////////
#define MAXPOP 200
#define MAXSTR 64

#define DEFPOPSIZ 50	//default pop size:					[2,200]
#define DEFCHRLEN 22	//default chrom length:				[2,63]
#define DEFMAXGEN 100	//default max generation:			[1,65535]
#define DEFPC 0.6		//default crossover probability:	[0.0,1.0]
#define DEFPM 0.02f		//default mutation probability:		[0.0,1.0]

typedef struct population
{
	unsigned chrom[MAXSTR];
	float x,fitness;				//x: value of chrom
	unsigned parent1,parent2,xsite;
}POP;

class GA:public CWnd  
{
private:

	unsigned nPopSize,nMutation,nCross,nXcross;
	float fPc,fPm,fSumFit,fAvgFit;
	double coef;

public:
	POP *pOldPop,*pNewPop,*pPop;
	unsigned nChromLen,nGen,nMaxGen,nMaxPop,nMinPop;
	float fMaxFit,fMinFit;
	CStringList lsRptData;
	float *MaxFitStat;
	float *AvgFitStat;

	GA();
	virtual ~GA();

	void InitData(unsigned ppsz=DEFPOPSIZ,unsigned chrlen=DEFCHRLEN,unsigned maxgen=DEFMAXGEN,float pc=DEFPC,float pm=DEFPM);

	int Flip(float probabiliby);

	float ObjFunc(float vx);
	float DeCode(unsigned * pChrom);

	void StatPop(POP * pop);
	void InitPop();		//initialize population
	void InitReport();   //initial info report

	unsigned Select();
	int Mutation(unsigned chromval);
	int CrossOver(unsigned * parent1,unsigned * parent2,int popidx);
	void UpdateGen();

	void Report(int gen);

	void RunGA();
};

#endif // !defined(AFX_GA_H__36707D34_93BC_4503_BBA8_DBFD1DCBA46C__INCLUDED_)

⌨️ 快捷键说明

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