gp.h

来自「遗传算法求解TSP问题的源码.遗传代数和种群规模比较大,所以整个求解过程比较长」· C头文件 代码 · 共 49 行

H
49
字号
#ifndef  LiuYongJin_XMU_CSD_2007_12_11_GP
#define	 LiuYongJin_XMU_CSD_2007_12_11_GP

#include "../Type.h"

typedef struct _individual
{
	double fitness;
	Path	path;
}Individual;


const int NGene=500;			//遗传代数
const int PEOPLE_SIZE=300;		//种群大小
const double P_CROSS = 0.8;		//交叉概率
const double P_MUTATION = 0.05;	//变异概率

class CGpTsp
{
public:
	CGpTsp(int nCitys, Position *position);
	~CGpTsp(void);

	void GetBestPath(Path *bestpath);

private:
	int	m_nCity;		//城市个数
	Position *m_xyPosition;	//每个城市的坐标

	Individual *m_Generation; //当前代
	
	void InitGene(Individual *ind);
	void InitCurGeneration();

	void Mutation(Individual *ind);				//变异
	void Copy(Individual * dst, Individual * src);			//复制

	void Turn(int *path, int index, int n);
	double Distance(int *path, int index1, int index2);
	int Find(int *path, int value, int n);
	void Crossover(Individual *ind1, Individual *ind2);		//交叉
	
	void Sort();
	int  Select();
	void Evaluate();
	void Reproduce();
};

#endif

⌨️ 快捷键说明

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