



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/basic ant colony algorithm for tsp #include #include #include #include #include #include #include #define n 31 /city size #define m 31 /ant number double inittao=1; double taonn; double detataonn; double distancenn; double yitann; int tabumn; int routemn; double solutionm; int bestrouten; double be
2、stsolution=10000000000; double alfa,beta,rou,q; int ncmax; void initparameter(void); / initialize the parameters of basic aca double evaluesolution(int *a); / evaluate the solution of tsp, and calculate the length of path void incityxy( double x, double y, char *infile ); / input the nodes coordinat
3、es of tsp void initparameter(void) alfa=1; beta=5; rou=0.9; q=100; ncmax=200; void main(void) int nc=0; initparameter(); double xn; double yn; incityxy( x, y, city31.tsp ); for(int i=0;in;i+) for(int j=i+1;jn;j+) distanceji=sqrt(xi-xj)*(xi-xj)+(yi-yj)*(yi-yj); distanceij=distanceji; / calculate the
4、heuristic parameters for(i=0;in;i+) for(int j=0;jn;j+) taoij=inittao; if(j!=i) yitaij=100/distanceij; for(int k=0;km;k+) for(i=0;in;i+) routeki=-1; srand(time(null); for(k=0;km;k+) routek0=k%n; tabukroutek0=1; /each ant try to find the optiamal path do int s=1; double partsum; double pper; double dr
5、and; /ant choose one whole path while(sn) for(k=0;km;k+) int jrand=rand()%3000; drand=jrand/3001.; partsum=0; pper=0; for(int j=0;jn;j+) if(tabukj=0) partsum+=pow(taorouteks-1j,alfa)*pow(yitarouteks-1j,beta); for(j=0;jdrand) break; tabukj=1; routeks=j; s+; / the pheromone is updated for(i=0;in;i+) f
6、or(int j=0;jn;j+) detataoij=0; for(k=0;km;k+) solutionk=evaluesolution(routek); if(solutionkbestsolution) bestsolution=solutionk; for(s=0;sn;s+) bestroutes=routeks; for(k=0;km;k+) for(s=0;sn-1;s+) detataorouteksrouteks+1+=q/solutionk; detataoroutekn-1routek0+=q/solutionk; for(i=0;in;i+) for(int j=0;
7、jn;j+) taoij=rou*taoij+detataoij; if(taoij20) taoij=20; for(k=0;km;k+) for(int j=1;jn;j+) tabukroutekj=0; routekj=-1; nc+; while(ncncmax); /output the calculating results fstream result; result.open(optimal_results.log, ios:app); if(!result) coutcant open the file!n; exit(0); result*-*endl; resultth
8、e initialized parameters of aca are as follows:endl; resultalfa=alfa, beta=beta, rou=rou, q=qendl; resultthe maximum iteration number of aca is:ncmaxendl; resultthe shortest length of the path is:bestsolutionendl; resultthe best route is:endl; for(i=0;in;i+) resultbestroutei ; resultendl; result*-*e
9、ndlendl; result.close(); coutthe shortest length of the path is:bestsolutionendl; double evaluesolution(int *a) double dist=0; for(int i=0;in-1;i+) dist+=distanceaiai+1; dist+=distanceaia0; return dist; void incityxy( double x, double y, char *infile ) fstream inxyfile( infile, ios:in | ios:nocreate ); if(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年电动汽车工程师职业资格考试试卷及答案
- 2025年财务政策与法规考试真题及答案
- 2025年财务报表分析考试题及答案解析
- 安徽省宿州市宿城一中学2025届七年级英语第二学期期中教学质量检测模拟试题含答案
- 2025湖北中考数学试卷
- 中班安全吃健康的食物教案
- 服务礼仪培训方案
- 加氢裂化装置安全培训
- 农民互助互利养殖合作协议
- 直肠手术后肠漏护理查房
- 板鞋竞速竞赛规则
- 灭火器维修与报废规程
- 皮肤病的临床取材及送检指南-修订版
- 机型理论-4c172实用类重量平衡
- 校企合作项目立项申请表(模板)
- 管道工厂化预制推广应用课件
- 海水的淡化精品课件
- 项目工程移交生产验收报告
- 清华大学美术学院陶瓷艺术设计系研究生导师及研究课题
- 计算机控制实验报告初稿(共31页)
- 抗磷脂抗体与抗磷脂综合征.ppt
评论
0/150
提交评论