声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 4792|回复: 12

[经典算法] 基本蚁群算法程序

[复制链接]
发表于 2006-11-15 21:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
  1. //基本蚁群算法程序

  2. //程序在vc++6.0下面同过,对原来的做了一点修改。
  3. //你可以使用本代码,如果感到对你有用的话,请通知作者,作者会很高兴。
  4. //通讯地址:[email]fashionxu@163.com[/email]
  5. //by FashionXu
  6. #include
  7. #include
  8. #include
  9. #include
  10. using namespace std;


  11. const int iAntCount=34;//ant numbers
  12. const int iCityCount=51;
  13. const int iItCount=2000;
  14. const double Q=100;
  15. const double alpha=1;
  16. const double beta=5;
  17. const double rou=0.5;

  18. int besttour[iCityCount];

  19. double  rnd(int low,double uper)
  20. {
  21. double p=(rand()/(double)RAND_MAX)*((uper)-(low))+(low);

  22. return (p);
  23. };
  24. int rnd(int uper)
  25. {
  26. return (rand()%uper);
  27. };

  28. class GInfo
  29. {
  30. public:
  31. double m_dDeltTrial[iCityCount][iCityCount];//信息素增量
  32. double m_dTrial[iCityCount][iCityCount];//信息素痕迹
  33. double distance[iCityCount][iCityCount];//距离
  34. };


  35. GInfo Map;
  36. class ant
  37. {
  38. private:
  39. int ChooseNextCity();
  40. double prob[iCityCount];//转移概率
  41. int m_iCityCount;
  42. int AllowedCity[iCityCount];
  43. public:
  44. void addcity(int city);
  45. int tabu[iCityCount];//行走路径
  46. void Clear();
  47. void UpdateResult();
  48. double m_dLength;
  49. double m_dShortest;
  50. void move();
  51. ant();
  52. void move2last();
  53. };
  54. void ant::move2last()
  55. {
  56. int i;
  57. for(i=0;i  if (AllowedCity[i]==1)
  58.   {
  59.    addcity(i);
  60.    break;
  61.   }
  62. }
  63. void ant::Clear()
  64. {
  65. m_dLength=0;
  66. int i;
  67. for(i=0;i {
  68.   prob[i]=0;
  69.   AllowedCity[i]=1;
  70. }
  71. i=tabu[iCityCount-1];
  72. m_iCityCount=0;
  73. addcity(i);
  74. }
  75. ant::ant()
  76. {
  77. m_dLength=m_dShortest=0;
  78. m_iCityCount=0;
  79. int i;
  80. for(i=0;i {
  81.   AllowedCity[i]=1;
  82.   prob[i]=0;
  83. }
  84. }
  85. void ant::addcity(int city)
  86. {
  87. //add city to tabu;
  88. tabu[m_iCityCount]=city;
  89. m_iCityCount++;
  90. AllowedCity[city]=0;
  91. }
  92. int ant::ChooseNextCity()
  93. {
  94. //Update the probability of path selection
  95. //select a path from tabu[m_iCityCount-1] to next


  96. int i;
  97. int j=10000;
  98. double temp=0;
  99. int curCity=tabu[m_iCityCount-1];
  100. for (i=0;i
  101. {
  102.   if((AllowedCity[i]==1))
  103.   {
  104.    temp+=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha);
  105.                           //距离                           //残留信息量
  106.   }
  107. }
  108. double sel=0;
  109. for (i=0;i
  110. {  
  111.   if((AllowedCity[i]==1))
  112.   {
  113.    prob[i]=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha)/temp;
  114.    sel+=prob[i];
  115.   }
  116.   else
  117.    prob[i]=0;
  118. }
  119. double mRate=rnd(0,sel);
  120. double mSelect=0;

  121. for ( i=0;i
  122. {  
  123.   if((AllowedCity[i]==1))
  124.    mSelect+=prob[i] ;
  125.   if (mSelect>=mRate)
  126.   {j=i;break;}
  127. }

  128. if (j==10000)
  129. {
  130.   temp=-1;
  131.   for (i=0;i  
  132. {
  133.    if((AllowedCity[i]==1))
  134.     if (temp   
  135.    {
  136.      temp=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha);
  137.      j=i;
  138.     }
  139.   }
  140. }

  141. return j;

  142. }
  143. void ant::UpdateResult()
  144. {
  145. // Update the length of tour
  146. int i;
  147. for(i=0;i  m_dLength+=Map.distance[tabu[i]][tabu[i+1]];
  148. m_dLength+=Map.distance[tabu[iCityCount-1]][tabu[0]];
  149. }
  150. void ant::move()
  151. {
  152. //the ant move to next town and add town ID to tabu.
  153. int j;
  154. j=ChooseNextCity();
  155. addcity(j);
  156. }
  157. class project
  158. {
  159. public:

  160. void UpdateTrial();
  161. double m_dLength;
  162. void initmap();
  163. ant ants[iAntCount];
  164. void GetAnt();
  165. void StartSearch();
  166. project();
  167. };
  168. void project::UpdateTrial()
  169. {
  170. //calculate the changes of trial information
  171. int i;
  172. int j;

  173. for(i=0;i {
  174.   for (j=0;j  {
  175.    Map.m_dDeltTrial[ants[i].tabu[j]][ants[i].tabu[j+1]]+=Q/ants[i].m_dLength ;
  176.    Map.m_dDeltTrial[ants[i].tabu[j+1]][ants[i].tabu[j]]+=Q/ants[i].m_dLength;
  177.   }
  178.   Map.m_dDeltTrial[ants[i].tabu[iCityCount-1]][ants[i].tabu[0]]+=Q/ants[i].m_dLength;
  179.   Map.m_dDeltTrial[ants[i].tabu[0]][ants[i].tabu[iCityCount-1]]+=Q/ants[i].m_dLength;
  180. }
  181. for (i=0;i {
  182.   for (j=0;j  {
  183.    Map.m_dTrial[i][j]=(rou*Map.m_dTrial[i][j]+Map.m_dDeltTrial[i][j] );
  184.    Map.m_dDeltTrial[i][j]=0;
  185.   }

  186. }


  187. }
  188. void project::initmap()
  189. {
  190. int i;
  191. int j;
  192. for(i=0;i  for (j=0;j  {

  193.    Map.m_dTrial[i][j]=1;
  194.    Map.m_dDeltTrial[i][j]=0;
  195.   }
  196. }
  197. project::project()
  198. {
  199. //initial map,read map infomation from file . et.
  200. initmap();
  201. m_dLength=10e9;


  202. ifstream in("eil51.tsp");

  203. struct city
  204. {
  205.   int num;
  206.   int x;
  207.   int  y;
  208. }cc[iCityCount];

  209. for (int i=0;i
  210. {
  211.   in>>cc[i].num>>cc[i].x>>cc[i].y;
  212.   besttour[i]=0;
  213. }
  214. int j;
  215. for(i=0;i  for (j=0;j  
  216. {
  217.    {
  218.     Map.distance[i][j]=sqrt(pow((cc[i].x-cc[j].x),2)+pow((cc[i].y-cc[j].y),2));
  219.    }
  220. }


  221. }
  222. void project::GetAnt()
  223. {
  224. //randomly put ant into map
  225. int i=0;
  226. int city;
  227. srand( (unsigned)time( NULL ) +rand());
  228. for (i=0;i {
  229.   city=rnd(iCityCount);
  230.   ants[i].addcity(city);
  231. }

  232. }
  233. void project::StartSearch()
  234. {
  235. //begin to find best solution
  236. int max=0;//every ant tours times
  237. int i;
  238. int j;
  239. double temp;
  240. int temptour[iCityCount];
  241. while (max
  242. {  
  243.   for(j=0;j

  244.   {
  245.    for (i=0;i    ants[j].move();
  246.   }

  247.   for(j=0;j  
  248. {
  249.    ants[j].move2last();
  250.    ants[j].UpdateResult ();
  251.   }

  252.   //find out the best solution of the step and put it into temp
  253.   int t;
  254.   temp=ants[0].m_dLength;
  255.   for (t=0;t   
  256.   temptour[t]=ants[0].tabu[t];
  257.   for(j=0;j  
  258. {
  259.    if (temp>ants[j].m_dLength)
  260.    {
  261.     temp=ants[j].m_dLength;
  262.     for ( t=0;t     temptour[t]=ants[j].tabu[t];
  263.    }

  264.   }

  265.   if(temp   
  266.   m_dLength=temp;
  267.    for ( t=0;t
  268.   {
  269.    besttour[t]=temptour[t];
  270.   }
  271.   printf("%d : %f\n",max,m_dLength);
  272.   UpdateTrial();

  273.   for(j=0;j   ants[j].Clear();

  274.   max++;

  275. }
  276. printf("The shortest toure is : %f\n",m_dLength);

  277. for ( int t=0;t  printf(" %d ",besttour[t]);

  278. }
  279. int main()
  280. {

  281. project TSP;
  282. TSP.GetAnt();
  283. TSP.StartSearch();
  284. return 0;
  285. }
复制代码

[ 本帖最后由 风花雪月 于 2006-11-20 08:43 编辑 ]

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2007-1-5 11:23 | 显示全部楼层
请问有matlab版的吗?
发表于 2007-1-29 13:04 | 显示全部楼层
原帖由 xingconglan 于 2007-1-5 11:23 发表
请问有matlab版的吗?


去matlab版找找看,这里不讨论matlab问题
发表于 2007-2-6 22:58 | 显示全部楼层

求助

有C版本的吗??
万分感激
发表于 2007-2-28 14:35 | 显示全部楼层
原帖由 mgtong 于 2007-2-6 22:58 发表
有C版本的吗??
万分感激


简单修改一下上面的程序差不多就能运行了,别偷懒了
发表于 2007-5-21 20:58 | 显示全部楼层
谢谢啦
发表于 2007-6-6 21:09 | 显示全部楼层
似乎代码有些问题
发表于 2007-6-8 10:44 | 显示全部楼层
发表于 2007-7-20 19:42 | 显示全部楼层
哥们,你都include什么了啊,怎么都看不到
发表于 2007-7-23 16:35 | 显示全部楼层
原帖由 lijinhan83 于 2007-7-20 19:42 发表
哥们,你都include什么了啊,怎么都看不到


应该是

  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. #include <time.h>
复制代码
发表于 2007-7-26 15:07 | 显示全部楼层
谢谢风花雪月的支持,试试先了。
发表于 2007-7-26 15:17 | 显示全部楼层

错误提示

错误提示,在vc++6.0下运行提示
--------------------Configuration: 基本蚁群算法程序 - Win32 Debug--------------------
Compiling...
基本蚁群算法程序.cpp
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(66) : error C2143: syntax error : missing ';' before 'if'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(66) : error C2143: syntax error : missing ')' before 'if'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(76) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(76) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(89) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(89) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(112) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(112) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(121) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(121) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(134) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(134) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(145) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(145) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(148) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(162) : error C2146: syntax error : missing ';' before identifier 'm_dLength'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(163) : error C2146: syntax error : missing ')' before identifier 'm_dLength'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(163) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(190) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(190) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(191) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(191) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(198) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(198) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(199) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(199) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(212) : error C2143: syntax error : missing ';' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(212) : error C2143: syntax error : missing ')' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(212) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(212) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(235) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(235) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(240) : error C2143: syntax error : missing ';' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(240) : error C2143: syntax error : missing ')' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(241) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(241) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(255) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(255) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(270) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(273) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(273) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(274) : error C2146: syntax error : missing ';' before identifier 'ants'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(275) : error C2143: syntax error : missing ')' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(275) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(275) : error C2143: syntax error : missing ';' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(278) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(278) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(287) : error C2146: syntax error : missing ';' before identifier 'temptour'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(288) : error C2143: syntax error : missing ')' before 'for'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(288) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(289) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(289) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(293) : error C2146: syntax error : missing ';' before identifier 'temptour'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(294) : error C2143: syntax error : missing ')' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(294) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(294) : error C2143: syntax error : missing ';' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(299) : error C2146: syntax error : missing ')' before identifier 'm_dLength'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(301) : error C2143: syntax error : missing ')' before '{'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(301) : error C2143: syntax error : missing ';' before ')'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(307) : error C2146: syntax error : missing ';' before identifier 'ants'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(309) : error C2146: syntax error : missing ')' before identifier 'max'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(309) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(314) : error C2146: syntax error : missing ';' before identifier 'printf'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(316) : error C2143: syntax error : missing ')' before '}'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(316) : error C2059: syntax error : ';'
d:\dev-cpp\小工程\基本蚁群算法程序.cpp(316) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.

基本蚁群算法程序.obj - 66 error(s), 0 warning(s)
发表于 2007-7-29 15:35 | 显示全部楼层
原帖由 lijinhan83 于 2007-7-26 15:17 发表
错误提示,在vc++6.0下运行提示
--------------------Configuration: 基本蚁群算法程序 - Win32 Debug--------------------
Compiling...
基本蚁群算法程序.cpp
d:\dev-cpp\小工程\基本蚁群算法程序.cpp( ...


http://www.chinavib.com/forum/thread-49303-1-1.html

请勿重复发帖
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-6-16 12:38 , Processed in 0.062584 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表