版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、基本蚁群算法的C+源程序 2006-12-01 09:36关键词: 蚁群算法 C+ 源程 序 ACS基本蚁群算法的C+源程序/ 基本蚁群算法程序/ 程序在 vc+6.0 下面同过,对原来的做了一点修改。/ 你可以使用本代码,如果感到对你有用的话,请通知作者,作者会很高兴/ 通讯地址: fashionxu/by FashionXu#include "stdafx.h"#include#include#include#include蚂蚁数量城市数量 最大跌代次数using namespace std;const int iAntCount=34;/ const int iCit
2、yCount=51;/ const int iItCount=2000;/ const double Q=100;const double alpha=1; const double beta=5;const double rou=0.5;int besttouriCityCount;/最有路径列表double rnd(int low,double uper)/获得随机数double p=(rand()/(double)RAND_MAX)*(uper)-(low)+(low); return (p);int rnd(int uper) return (rand()%uper);class GI
3、nfo/tsp 地图信息,包含了信息素,城市距离,和信息素变化矩阵 public:double m_dDeltTrialiCityCountiCityCount;double m_dTrialiCityCountiCityCount;double distanceiCityCountiCityCount;GInfo Map;class antprivate:int ChooseNextCity();/选择城市double probiCityCount;int m_iCityCount;int AllowedCityiCityCount;/没有走过的城市public: void addcity(
4、int city); int tabuiCityCount;void Clear();void UpdateResult(); double m_dLength; double m_dShortest; void move();ant();void move2last();void ant:move2last()int i;for(i=0;i iCityCount;i+)if (AllowedCityi=1)addcity(i);break; void ant:Clear()m_dLength=0;int i;for(i=0; i iCityCount;i+)probi=0;AllowedCi
5、tyi=1;i=tabuiCityCount-1;m_iCityCount=0;addcity(i);ant:ant()m_dLength=m_dShortest=0; m_iCityCount=0;int i;for(i=0;i iCityCount;i+)AllowedCityi=1;probi=0;void ant:addcity(int city)/add city to tabu;tabum_iCityCount=city;m_iCityCount+;AllowedCitycity=0;int ant:ChooseNextCity()/Update the probability o
6、f path selection/select a path from tabum_iCityCount-1 to nextint i;int j=10000;double temp=0;int curCity=tabum_iCityCount-1;for (i=0;i iCityCount;i+)if(AllowedCityi=1)temp+=pow(1.0/Map.distancecurCityi),beta)*pow(Map.m_dTrialc urCityi),alpha);double sel=0;for (i=0;i iCityCount;i+)if(AllowedCityi=1)
7、probi=pow(1.0/Map.distancecurCityi),beta)*pow(Map.m_dTrial curCityi),alpha)/temp;sel+=probi;elseprobi=0;double mRate=rnd(0,sel);double mSelect=0;for ( i=0;i iCityCount;i+)if(AllowedCityi=1)mSelect+=probi ;if (mSelect>=mRate) j=i;break;if (j=10000)temp=-1;for (i=0;i iCityCount;i+)if(AllowedCityi=1
8、)if (temp temp=pow(1.0/Map.distancecurCityi),beta)*pow(Map.m_dTrial curCityi),alpha);j=i;return j;void ant:UpdateResult()/ Update the length of tourint i;for(i=0;i iCityCount-1;i+)m_dLength+=Map.distancetabuitabui+1; m_dLength+=Map.distancetabuiCityCount-1tabu0;void ant:move()/the ant move to next t
9、own and add town ID to j;j=ChooseNextCity();addcity(j);class projectpublic:void UpdateTrial();double m_dLength;void initmap();ant antsiAntCount;void GetAnt();void StartSearch();project();void project:UpdateTrial()/calculate the changes of trial informationint i;int j;for(i=0;i iAntCount;i+)
10、for (j=0;j iCityCount-1;j+) Map.m_dDeltTrialantsi.tabujantsi.tabuj+1+=Q/antsi.m _dLength ;Map.m_dDeltTrialantsi.tabuj+1antsi.tabuj+=Q/antsi.m_ dLength;Map.m_dDeltTrialantsi.tabuiCityCount-1antsi.tabu0+=Q/an tsi.m_dLength;Map.m_dDeltTrialantsi.tabu0antsi.tabuiCityCount-1+=Q/an tsi.m_dLength;for (i=0;
11、i iCityCount;i+)for (j=0;j iCityCount;j+)Map.m_dTrialij=(rou*Map.m_dTrialij+Map.m_dDeltTrialij );Map.m_dDeltTrialij=0;void project:initmap()int i;int j;for(i=0;i iCityCount;i+)for (j=0;j iCityCount;j+)Map.m_dTrialij=1;Map.m_dDeltTrialij=0; project:project()/initial map,read map infomation from file
12、. et. initmap();m_dLength=10e9;ifstream in("eil51.tsp");struct cityint num;int x;int y;cciCityCount;for (int i=0;i iCityCount;i+)in>>cci.num>>cci.x>>cci.y;besttouri=0;int j;for(i=0;i iCityCount;i+)for (j=0;j iCityCount;j+)Map.distanceij=sqrt(pow(cci.x-ccj.x),2)+pow(cci.y-
13、cc j.y),2);void project:GetAnt()/randomly put ant into mapint i=0;int city;srand( (unsigned)time( NULL ) +rand();for (i=0;i iAntCount;i+)city=rnd(iCityCount);antsi.addcity(city);void project:StartSearch()/begin to find best solutionint max=0;/every ant tours timesint i;int j;double temp;int temptour
14、iCityCount;while (max iItCount)for(j=0;j iAntCount;j+)for (i=0;i iCityCount-1;i+)antsj.move();for(j=0;j iAntCount;j+) antsj.move2last();antsj.UpdateResult ();/find out the best solution of the step and put it into temp int t;temp=ants0.m_dLength;for (t=0;t iCityCount;t+)temptourt=ants0.tabut;for(j=0
15、;j iAntCount;j+)if (temp antsj.m_dLength) temp=antsj.m_dLength;for ( t=0;t iCityCount;t+)temptourt=antsj.tabut;if(temp m_dLength)m_dLength=temp;for ( t=0;t iCityCount;t+)besttourt=temptourt;printf("%d : %fn",max,m_dLength); UpdateTrial();for(j=0;j iAntCount;j+) antsj.Clear();max+;printf("The shor
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年沈阳市皇姑区公务员人员招聘笔试备考题库与答案详解
- 岳池县人民医院2027年度公开招聘工作人员(24人)笔试模拟试题及答案解析
- 2026上海复旦大学药学院全球公共政策研究院招聘1名助理教授B笔试备考试题及答案解析
- 2026年江苏省徐州市公务员人员招聘笔试试题与答案详解
- 2026年安阳市文峰区事业单位人员招聘笔试试题与答案详解
- 2026年云南省临沧市事业单位人员招聘笔试试题与答案详解
- 2026年连云港市海州区公务员人员招聘笔试参考试题与答案详解
- 自贡市公交集团有限责任公司吉兴公司新媒体营销管理员招聘(1人)考试备考试题及答案解析
- 2027桂林医科大学第一附属医院公开招聘工作人员考试备考试题及答案解析
- 2026年北海市银海区公务员人员招聘笔试参考试题与答案详解
- 2026哔哩哔哩营销通案
- 广东深圳市华睿欣能投资控股集团有限公司2026届校园招聘考试参考题库及答案解析
- 基于零知识证明的医疗数据共享安全协议
- 老年期痴呆课件
- 20大数据互联网金融电子商务创业项目营销活动推广模板
- 3.1伟大的改革开放 教案高中政治统编版必修一中国特色社会主义
- 羟考酮纳洛酮缓释片-临床用药解读
- DBJT15-60-2019 建筑地基基础检测规范
- 2025年晋江离婚协议书
- 药店医保处方管理制度
- 信息处理与社会责任试题及答案
评论
0/150
提交评论