资源描述:
《蚁群算法(C++版)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//AO.cpp:定义控制台应用程序的入口点。#pragmaonce#include#include#includeconstdoubleALPHA=1.0;//启发因子,信息素的重要程度constdoubleBETA=2.0;//期望因子,城市间距离的重要程度constdoubleROU=0.5;//信息素残留参数constintN_ANT_COUNT=34;//蚂蚁数量constintN_IT_COUNT=1000;//迭代次数constintN_CITY_COUNT=51;//城市数量constdoubl
2、eDBQ=100.0;//总的信息素constdoubleDB_MAX=10e9;//一个标志数,10的9次方doubleg_Trial[N_CITY_COUNT][N_CITY_COUNT];//两两城市间信息素,就是环境信息素doubleg_Distance[N_CITY_COUNT][N_CITY_COUNT];//两两城市间距离//eil51.tsp城市坐标数据doublex_Ary[N_CITY_COUNT]={37,49,52,20,40,21,17,31,52,51,42,31,5,12,36,52,27,17,13,57,62,42,16,8,7
3、,27,30,43,58,58,37,38,46,61,62,63,32,45,59,5,10,21,5,30,39,32,25,25,48,56,30};doubley_Ary[N_CITY_COUNT]={52,49,64,26,30,47,63,62,33,21,41,32,25,42,16,41,23,33,13,58,42,57,57,52,38,68,48,67,48,27,69,46,10,33,63,69,22,35,15,6,17,10,64,15,10,39,32,55,28,37,40};//返回指定范围内的随机整数intrnd(intnL
4、ow,intnUpper){returnnLow+(nUpper-nLow)*rand()/(RAND_MAX+1);}//返回指定范围内的随机浮点数doublernd(doubledbLow,doubledbUpper){doubledbTemp=rand()/((double)RAND_MAX+1.0);returndbLow+dbTemp*(dbUpper-dbLow);}//返回浮点数四舍五入取整后的浮点数doubleROUND(doubledbA){return(double)((int)(dbA+0.5));}//定义蚂蚁类classCAnt{pub
5、lic:CAnt(void);~CAnt(void);public:intm_nPath[N_CITY_COUNT];//蚂蚁走的路径doublem_dbPathLength;//蚂蚁走过的路径长度intm_nAllowedCity[N_CITY_COUNT];//没去过的城市intm_nCurCityNo;//当前所在城市编号intm_nMovedCityCount;//已经去过的城市数量public:intChooseNextCity();//选择下一个城市voidInit();//初始化voidMove();//蚂蚁在城市间移动voidSearch();/
6、/搜索路径voidCalPathLength();//计算蚂蚁走过的路径长度};//构造函数CAnt::CAnt(void){}//析构函数CAnt::~CAnt(void){}//初始化函数,蚂蚁搜索前调用voidCAnt::Init(){for(inti=0;i7、_COUNT);//把出发城市保存入路径数组中m_nPath[0]=m_nCurCityNo;//标识出发城市为已经去过了m_nAllowedCity[m_nCurCityNo]=0;//已经去过的城市数量设置为1m_nMovedCityCount=1;}//选择下一个城市//返回值为城市编号intCAnt::ChooseNextCity(){intnSelectedCity=-1;//返回结果,先暂时把其设置为-1//========================================================================
8、======//计算当前