第一范文网 - 专业文章范例文档资料分享平台

蚁群算法C++实现,解决TSP问题

来源:用户分享 时间:2020-06-21 本文由泪依言欣 分享 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

1 C++基本蚁群算法代码 #include #include #include #include using namespace std; const int iAntCount=34;//ant numbers const int iCityCount=51; const int iItCount=2000; const double Q=100; const double alpha=1; const double beta=5; const double rou=0.5; int besttour[iCityCount]; 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 GInfo { public: double m_dDeltTrial[iCityCount][iCityCount]; double m_dTrial[iCityCount][iCityCount]; double distance[iCityCount][iCityCount]; }; GInfo Map; class ant { private: int ChooseNextCity(); double prob[iCityCount]; int m_iCityCount;

int AllowedCity[iCityCount]; public:

void addcity(int city); int tabu[iCityCount]; void Clear();

void UpdateResult(); double m_dLength; double m_dShortest; void move(); ant();

void move2last(); };

void ant::move2last() { int i;

for(i=0;i

addcity(i); break; } }

void ant::Clear() {

m_dLength=0; int i;

for(i=0;i

prob[i]=0;

AllowedCity[i]=1; }

i=tabu[iCityCount-1]; m_iCityCount=0; addcity(i); }

ant::ant() {

m_dLength=m_dShortest=0; m_iCityCount=0; int i;

for(i=0;i

AllowedCity[i]=1; prob[i]=0; } }

void ant::addcity(int city) {

//add city to tabu;

tabu[m_iCityCount]=city; m_iCityCount++; AllowedCity[city]=0; }

int ant::ChooseNextCity() {

//Update the probability of path selection

//select a path from tabu[m_iCityCount-1] to next

int i;

int j=10000; double temp=0;

int curCity=tabu[m_iCityCount-1]; for (i=0;i

if((AllowedCity[i]==1)) {

temp+=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha); } }

double sel=0;

for (i=0;i

if((AllowedCity[i]==1)) {

prob[i]=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha)/temp; sel+=prob[i]; } else

prob[i]=0; }

double mRate=rnd(0,sel); double mSelect=0;

for ( i=0;i

if((AllowedCity[i]==1)) mSelect+=prob[i] ;

if (mSelect>=mRate) {j=i;break;} }

if (j==10000) {

temp=-1;

for (i=0;i

if((AllowedCity[i]==1))

if (temp

temp=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha); j=i; } } }

return j; }

void ant::UpdateResult() {

// Update the length of tour int i;

for(i=0;i

m_dLength+=Map.distance[tabu[i]][tabu[i+1]];

m_dLength+=Map.distance[tabu[iCityCount-1]][tabu[0]]; }

void ant::move() {

//the ant move to next town and add town ID to tabu. int j;

j=ChooseNextCity(); addcity(j); }

class project {

public:

void UpdateTrial(); double m_dLength; void initmap(); 作者:cwyhs001 2007-4-10 18:49 回复此发言 2 C++基本蚁群算法代码 ant ants[iAntCount]; void GetAnt(); void StartSearch(); project(); }; void project::UpdateTrial() { //calculate the changes of trial information int i; int j; for(i=0;i

搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新小学教育蚁群算法C++实现,解决TSP问题 全文阅读和word下载服务。

蚁群算法C++实现,解决TSP问题 .doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/wenku/1089871.html(转载请注明文章来源)
热门推荐
Copyright © 2018-2022 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top