




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实用文档遗传算法解决TSP问题(C+版)遗传算法流程:交叉,编译,计算适应度,保存最优个体。其中交叉过程是选择最优的两个染色体进行交叉操作,本文采用的是轮盘赌算法。#include #include #include using namespace std;#define population 200/种群数量#define pc 0.9/交叉的概率#define pm 0.1/变异的概率#define count 200/迭代的次数#define num 10/城市的数量int* city;/存放每个个体的访问顺序int path1010 = /0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0, 23, 93, 18, 40, 34, 13, 75, 50, 35 ,/0 23, 0, 75, 4, 72, 74, 36, 57, 36, 22 ,/1 93, 75, 0, 64, 21, 73, 51, 25, 74, 89 ,/2 18, 4, 64, 0, 55, 52, 8, 10, 67, 1 , /3 40, 72, 21, 55, 0, 43, 64, 6, 99, 74 , /4 34, 74, 73, 52, 43, 0, 43, 66, 52, 39 ,/5 13, 36, 51, 8, 64, 43, 0, 16, 57, 94 ,/6 75, 57, 25, 10, 6, 66, 16, 0, 23, 11 , /7 50, 36, 74, 67, 99, 52, 57, 23, 0, 42 ,/8 35, 22, 89, 1, 74, 39, 94, 11, 42, 0 /9;int* dis;/存放每个个体的访问顺序下的路径长度double* fitness;/存放灭个个体的适应度int min_dis = 1000000;int min_index = -1;int* min_path;/初始化种群void init()int *a = new intnum;for (int i = 0; inum; i+)ai = i;city = new int*population;for (int i = 0; ipopulation; i+)cityi = new intnum;for (int i = 0; i= 0; j-)int n = rand() % (j + 1);/产出的数是0-j,保证交换的后面的数不会再被交换swap(aj, an);/保证a里面全是0-(num-1)的数,无重复的数,只是顺序颠倒cityij = aj;deletea;dis = new intpopulation;fitness = new doublepopulation;min_path = new intnum;/计算适应度void compute()/cout do compute now. endl;double total = 0;for (int i = 0; ipopulation; i+)/计算每种情况下,路径的长度disi = 0;int a = cityi0, b;for (int j = 1; jnum; j+)b = cityij;disi += pathab;a = b;disi += pathbcityi0;fitnessi = 1.0 / disi;/以距离的倒数作为适应度函数值total += fitnessi;/选择适应度高的物种,采用轮盘赌算法int select()double total = 0;for (int i = 0; ipopulation; i+)total += fitnessi;double size = rand() / (double)RAND_MAX * total;/保证不产生0/cout size size endl;double sum = 0;int i = 0;while (sum = size&ipopulation)sum += fitness+i;return -i;/返回被选中的个体int getMinDis()int result = dis0;int index = 0;for (int i = 1; i disi)result = disi;index = i;return index;int getMaxDis()int result = dis0;int index = 0;for (int i = 1; ipopulation; i+)if (result disi)result = disi;index = i;return index;void save()int current_min_index = getMinDis();int current_max_index = getMaxDis();if (discurrent_min_index min_dis)min_dis = discurrent_min_index;for (int i = 0; i num; i+)min_pathi =citycurrent_min_indexi;/cout current min dis is: min_dis endl;elsefor (int i = 0; inum; i+)citycurrent_max_indexi = min_pathi;discurrent_max_index = min_dis;fitnesscurrent_max_index = 1.0 / min_dis;/最优保存算法bool isExist(int value, int* array, int len)for (int i = 0; ilen; i+)if (value = arrayi)return true;return false;void convert(int p1, int p2, int* src, int* dst)int len = p2 - p1 + 1;int* temp = new intlen;for (int i = p1; i = p2; i+)tempi - p1 = srci;int j = (p2 + 1) % num;for (int i = 1; i = num; i+)int index = (i + p2) % num;if (!isExist(dstindex, temp, len)dstj = dstindex;j = (j + 1) % num;for (int i = p1; i = p2; i+)dsti = srci;deletetemp;/交叉,采用次序交叉算法void cross()/cout do cross now. endl;for (int k = 0; kpopulation; k += 2)int a = select();int b = select();while (a = b)b = select();/保证被选中的个体不是一样的/cout same b endl;/cout choose popuilation a b endl;double p = rand() / double(RAND_MAX);/cout cross rate is p endl;int* a1 = new intnum;int* a2 = new intnum;int* b1 = new intnum;int* b2 = new intnum;for (int i = 0; inum; i+)a1i = cityai;a2i = citybi;b1i = a2i;b2i = a1i;if (ppc)/满足交叉条件/选择交叉的两点,并保证p1p2)swap(p1, p2);/cout choose pos p1 p2 endl;/开始交叉convert(p1, p2, a1, b1);convert(p1, p2, a2, b2);for (int i = 0; inum; i+)cityki = b1i;cityk + 1i = b2i;elsefor (int i = 0; inum; i+)cityki = a1i;cityk + 1i = a2i;deletea1;deletea2;deleteb1;deleteb2;/变异,采用对换操作进行变异void morphis()/cout do morphis now. endl;for (int i = 0; ipopulation; i+)double p = rand() / double(RAND_MAX);/cout morphis rate is p endl;if (ppm)/执行变异int a = -1, b = -1;while (a = b)a = rand() % num;b = rand() % num;swap(cityia, cityib);int getdis()/compute();int result = dis0;int index = 0;for (int i = 1; i disi)result = disi;index = i;return result;/释放申请的数组的空间void dispose()for (int i = 0; ipopulation; i+)deletecityi;deletecity;deletedis;deletefitness;int main()init();/初始化种群int i = 0;srand(time(0);compute();while (icount)cross();/交叉morphis();/变异compute();/计算适应度save()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 养护安全培训会议课件
- 餐饮培训课件怎么写
- 氨制冷安全培训课件
- 2025年高精度铝板带箔行业研究报告及未来发展趋势预测
- 二级护理质量管理
- 2025年高模低缩丝行业研究报告及未来发展趋势预测
- 2025年高精度铜合金板行业研究报告及未来发展趋势预测
- 2025年高端运动鞋行业研究报告及未来发展趋势预测
- 医护关系有什么危害
- 水电施工日志培训课件
- 《维生素及图片》课件
- 天然气开采业的生产流程与技术要点
- 英语专业大学生职业生涯规划书
- 非物质文化遗产概论:第四章-非物质文化遗产的保课件
- FLUENT 15 0流场分析实战指南
- 弱电维护保养合同
- GB/T 41972-2022铸铁件铸造缺陷分类及命名
- YY/T 0471.3-2004接触性创面敷料试验方法 第3部分:阻水性
- PEP小学英语五年级上册第四单元全国优质课赛课一等奖《思维导图在小学英语复习课的应用》精品课件
- 新闻传播中的媒介素养课件
- 超疏水材料课件
评论
0/150
提交评论