蚁群算法 C语言程序(已运行).doc

上传人:土8路 文档编号:10118317 上传时间:2021-04-20 格式:DOC 页数:5 大小:20.50KB
返回 下载 相关 举报
蚁群算法 C语言程序(已运行).doc_第1页
第1页 / 共5页
蚁群算法 C语言程序(已运行).doc_第2页
第2页 / 共5页
蚁群算法 C语言程序(已运行).doc_第3页
第3页 / 共5页
蚁群算法 C语言程序(已运行).doc_第4页
第4页 / 共5页
蚁群算法 C语言程序(已运行).doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《蚁群算法 C语言程序(已运行).doc》由会员分享,可在线阅读,更多相关《蚁群算法 C语言程序(已运行).doc(5页珍藏版)》请在三一文库上搜索。

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( !inxyfile ) coutcant open the infile file!n; exit(0); int i=0; while( !inxyfile.eof() ) inxyfilexiyi; if( +i = N ) break; 本文来自CSDN博客,转载请标明出处:http:/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1