精品文档,仅供学****与交流,如有侵权请联系网站删除
【精品文档】第 1 页
本程序是涉及8个城市的TSP问题的关于遗传算法的源代码,在Visual C++ 2010上调试运行,输出结果如下截图所示。
另:8个城市的地理位置坐标={{3,4},{1,2},{4,7},{5,3},{1,12},{13,2},{22,21},{30,40}}。
如有另需,只要修改城市的绝对位置坐标和城市数量MAXC就行了。
#include <iostream>
#include ""
#include ""
#include ""
#include ""
#define MAXC 8 //个体大小
#define POPSIZE 10 //种群大小
#define NL 10000 //终止规则大N,最大不优化次数
int parent[POPSIZE+1][MAXC+1]; //二维数组,父代
int child[POPSIZE+1][MAXC+1]; //二维数组,子代
struct city //坐标
{ int x,y;
struct city location[MAXC+1]={{3,4},{1,2},{4,7},{5,3},{1,12},{13,2},{22,21},{30,40}};
float random0_1(); //产生0到1的随机函数
int random_n(int,int); //产生n1到n2的随机函数
void inipop(int pop[POPSIZE+1][MAXC+1]); //初始化群体
float distance(int p1,int p2); //计算两点之间的距离
float fitfun(int pop[POPSIZE+1][MAXC+1],int row); //计算适应函数
void sortpop(int pop[POPSIZE+1][MAXC+1]); //种群排序
void copypop(int pop1[POPSIZE+1][MAXC+1],int pop2[POPSIZE+1][MAXC+1]); //拷贝种群
void copycode(int pop1[POPSIZE+1][MAXC+1],int pop2[POPSIZE+1][MAXC+1],int k,int m); //拷贝个体
void repop(int pop1[POPSIZE+1][MAXC+1],int pop2[POPSIZE+1][MAXC+1]); //产生种群
vo
TSP问题-遗传算法源代码 来自淘豆网www.taodocs.com转载请标明出处.