欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > DOC文档下载  

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

    • 资源ID:10118317       资源大小:20.50KB        全文页数:5页
    • 资源格式: DOC        下载积分:2
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录   微博登录  
    二维码
    微信扫一扫登录
    下载资源需要2
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    /Basic Ant Colony Algorithm for TSP #include <iostream.h> #include <fstream.h> #include <math.h> #include <time.h> #include <conio.h> #include <stdlib.h> #include <iomanip.h> #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 BestSolution=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 coordinates 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;i<N;i+) for(int j=i+1;j<N;j+) distanceji=sqrt(xi-xj)*(xi-xj)+(yi-yj)*(yi-yj); distanceij=distanceji; / calculate the heuristic parameters for(i=0;i<N;i+) for(int j=0;j<N;j+) taoij=inittao; if(j!=i) yitaij=100/distanceij; for(int k=0;k<M;k+) for(i=0;i<N;i+) routeki=-1; srand(time(NULL); for(k=0;k<M;k+) routek0=k%N; tabukroutek0=1; /each ant try to find the optiamal path do int s=1; double partsum; double pper; double drand; /ant choose one whole path while(s<N) for(k=0;k<M;k+) int jrand=rand()%3000; drand=jrand/3001.; partsum=0; pper=0; for(int j=0;j<N;j+) if(tabukj=0) partsum+=pow(taorouteks-1j,alfa)*pow(yitarouteks-1j,beta); for(j=0;j<N;j+) if(tabukj=0) pper+=pow(taorouteks-1j,alfa)*pow(yitarouteks-1j,beta)/partsum; if(pper>drand) break; tabukj=1; routeks=j; s+; / the pheromone is updated for(i=0;i<N;i+) for(int j=0;j<N;j+) detataoij=0; for(k=0;k<M;k+) solutionk=EvalueSolution(routek); if(solutionk<BestSolution) BestSolution=solutionk; for(s=0;s<N;s+) BestRoutes=routeks; for(k=0;k<M;k+) for(s=0;s<N-1;s+) detataorouteksrouteks+1+=Q/solutionk; detataoroutekN-1routek0+=Q/solutionk; for(i=0;i<N;i+) for(int j=0;j<N;j+) taoij=rou*taoij+detataoij; if(taoij<0.00001) taoij=0.00001; if(taoij>20) taoij=20; for(k=0;k<M;k+) for(int j=1;j<N;j+) tabukroutekj=0; routekj=-1; NC+; while(NC<NcMax); /output the calculating results fstream result; result.open("optimal_results.log", ios:app); if(!result) cout<<"cant open the <optimal_results.log> file!n" exit(0); result<<"*-*"<<endl; result<<"the initialized parameters of ACA are as follows:"<<endl; result<<"alfa="<<alfa<<", beta="<<beta<<", rou="<<rou<<", Q="<<Q<<endl; result<<"the maximum iteration number of ACA is:"<<NcMax<<endl; result<<"the shortest length of the path is:"<<BestSolution<<endl; result<<"the best route is:"<<endl; for(i=0;i<N;i+) result<<BestRoutei<<" " result<<endl; result<<"*-*"<<endl<<endl; result.close(); cout<<"the shortest length of the path is:"<<BestSolution<<endl; double EvalueSolution(int *a) double dist=0; for(int i=0;i<N-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 ) cout<<"cant open the <"<<infile<<"> file!n" exit(0); int i=0; while( !inxyfile.eof() ) inxyfile>>xi>>yi; if( +i >= N ) break; 本文来自CSDN博客,转载请标明出处:http:/blog.csdn.net/lyp2003ok/archive/2009/01/04/3706252.aspx

    注意事项

    本文(蚁群算法 C语言程序(已运行).doc)为本站会员(土8路)主动上传,三一文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




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

    三一文库
    收起
    展开