蚁群算法 C语言程序(已运行)_第1页
蚁群算法 C语言程序(已运行)_第2页
蚁群算法 C语言程序(已运行)_第3页
蚁群算法 C语言程序(已运行)_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论