//基本蚁群算法程序
//程序在vc++6.0下面同过,对原来的做了一点修改。
//你可以使用本代码,如果感到对你有用的话,请通知作者,作者会很高兴。
//通讯地址:fashionxu@163.com
//by FashionXu
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
const int iAntCount=34;//蚂蚁数量
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//tsp地图信息,包含了信息素,城市距离,和信息素变化矩阵
{
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《iCityCount;i++)
if (AllowedCity[i]==1)
{
addcity(i);
break;
}
}
void ant::Clear()
{
m_dLength=0;
int i;
for(i=0;
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〈iCityCount;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〈iCityCount;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〈iCityCount;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〈iCityCount;i++)
if((AllowedCity[i]==1))
mSelect+=prob[i] ;
if (mSelect>=mRate) {j=i;break;}
}
if (j==10000)
{
temp=-1;
for (i=0;i〈iCityCount;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〈iCityCount-1;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();
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〈iAntCount;i++)
for (j=0;j〈iCityCount-1;j++)
{ Map.m_dDeltTrial[ants[i].tabu[j]][ants[i].tabu[j+1]]+=Q/ants[i].m_dLength ;
Map.m_dDeltTrial[ants[i].tabu[j+1]][ants[i].tabu[j]]+=Q/ants[i].m_dLength;
}
Map.m_dDeltTrial[ants[i].tabu[iCityCount-1]][ants[i].tabu[0]]+=Q/ants[i].m_dLength;
Map.m_dDeltTrial[ants[i].tabu[0]][ants[i].tabu[iCityCount-1]]+=Q/ants[i].m_dLength;
}
for (i=0;i〈iCityCount;i++)
for (j=0;j〈iCityCount;j++)
Map.m_dTrial[i][j]=(rou*Map.m_dTrial[i][j]+Map.m_dDeltTrial[i][j] );
Map.m_dDeltTrial[i][j]=0;
}
}
}
void project::initmap()
{
int i;
int j;
for(i=0;i〈iCityCount;i++)
for (j=0;j〈iCityCount;j++)
{
Map.m_dTrial[i][j]=1;
Map.m_dDeltTrial[i][j]=0;
}
}
project::project()
{
//initial map,read map infomation from file . et.
initmap();
m_dLength=10e9;
ifstream in("eil51.tsp");
struct city
{
int num;
int x;
int y;
}cc[iCityCount];
for (int i=0;i〈iCityCount;i++)
{
in>>cc[i].num>>cc[i].x>>cc[i].y;
besttour[i]=0;
}
int j;
for(i=0;i〈iCityCount;i++)
for (j=0;j〈iCityCount;j++)
{
{
Map.distance[i][j]=sqrt(pow((cc[i].x-cc[j].x),2)+pow((cc[i].y-cc[j].y),2));
}
}
}
void project::GetAnt()
{
//randomly put ant into map
int i=0;
int city;
srand( (unsigned)time( NULL ) +rand());
for (i=0;i〈iAntCount;i++)
{
city=rnd(iCityCount);
ants[i].addcity(city);
}
}
void project::StartSearch()
{
//begin to find best solution
int max=0;//every ant tours times
int i;
int j;
double temp;
int temptour[iCityCount];
while ((max〈iItCount)
{
for(j=0;j〈iAntCount;j++)
{
for (i=0;i〈iCityCount-1;i++)
ants[j].move();
}
for(j=0;j〈iAntCount;j++)
{ ants[j].move2last();
ants[j].UpdateResult ();
}
//find out the best solution of the step and put it into temp
int t;
temp=ants[0].m_dLength;
for (t=0;t〈iCityCount;t++)
temptour[t]=ants[0].tabu[t];
for(j=0;j〈iAntCount;j++)
{
if (temp〉ants[j].m_dLength) {
temp=ants[j].m_dLength;
for ( t=0;t〈iCityCount;t++)
temptour[t]=ants[j].tabu[t];
}
}
if(temp〈m_dLength){
m_dLength=temp;
for ( t=0;t〈iCityCount;t++)
besttour[t]=temptour[t];
}
printf("%d : %f\n",max,m_dLength);
UpdateTrial();
for(j=0;j〈iAntCount;j++)
ants[j].Clear();
max++;
}
printf("The shortest toure is : %f\n",m_dLength);
for ( int t=0;t〈iCityCount;t++)
printf(" %d ",besttour[t]);
}
int main()
{
project TSP;
TSP.GetAnt();
TSP.StartSearch();
return 0;
}
求eil51最优到了438,可以修改循环次数和其他参数。以得到更好的解。使用TSP数据的时候,将前面的一些字符串信息删除,只留下坐标数据。
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=4673640
|
- 评论人:laushineyee
2008-05-24 07:28:56
|
|||
eil51.tsp是一个调用文件,相当于各个城市的坐标值,所以你可以试着自己做个类似的数据,城市坐标x,y。本人蚁群算法硕士,可以联系 |
||||
|
- 评论人:laushineyee
2008-05-24 07:28:31
|
|||
eil51.tsp是一个调用文件,相当于各个城市的坐标值,所以你可以试着自己做个类似的数据,城市坐标x,y。本人蚁群算法硕士,可以联系 |
||||
|
- 评论人:laushineyee
2008-05-24 07:28:31
|
|||
eil51.tsp是一个调用文件,相当于各个城市的坐标值,所以你可以试着自己做个类似的数据,城市坐标x,y。本人蚁群算法硕士,可以联系 |
||||
|
- 评论人:laushineyee
2008-05-24 07:28:31
|
|||
eil51.tsp是一个调用文件,相当于各个城市的坐标值,所以你可以试着自己做个类似的数据,城市坐标x,y。本人蚁群算法硕士,可以联系 |
||||
|
- 评论人:laushineyee
2008-05-24 07:28:21
|
|||
eil51.tsp是一个调用文件,相当于各个城市的坐标值,所以你可以试着自己做个类似的数据,城市坐标x,y。本人蚁群算法硕士,可以联系 |
||||
|
- 评论人:wang
2007-06-05 17:12:38
|
|||
你的"stdafx.h"头文件呢? |
||||
|
- 评论人:王
2007-04-01 23:13:42
|
|||
大哥,头文件给下啊,弄不明白啊,能给个QQ啊 |
||||
|
- 评论人:klbbydd
2007-03-29 10:22:37
|
|||
谢谢你,你提供的蚁群算法对我很重要。万分感激!我这次的毕业设计就是这个但还多了些蚁群算法在聚类问题上的研究。希望你的帮助。我的QQ是276841831。 |
||||
|
- 评论人:anonymous
2006-11-27 21:11:35
|
|||
函数中怎么是这么实现的,我始终理解不了如果按照这个选择准则的话,下一个城市的位置不是和初始化的顺序有关了吗?因为一直在自加,其对应的数组值被加到中,当到达一定值后,就停止了。原本的论文上好像没有这么一个公式?楼主能解释下吗? |
||||
|
- 评论人:anonymous
2006-11-25 21:23:54
|
|||
大虾,你已经毕业了吧?毕业了,就不博了啊。我的论文也是蚁群。有空加我的吧:85823840。 |
||||
|
- 评论人:anonymous
2006-09-30 13:45:25
|
|||
谢谢。 |
||||
|
- 评论人:我爱蚁群
2006-09-30 11:28:07
|
|||
你好我是一大四学生前几天选了毕业设计的题目是基于蚁群算法的路由选择我在这方面实在是不怎么懂能指导一下吗谢谢邮件联系吧 |
||||
|
- 评论人:哲
2006-09-25 10:02:40
|
|||
不错,做的很好。我在用蚁群算法做机器人的路径规划,已经作出一些成果了。看看你的源码对我的编程肯定有很好的帮助谢谢 |
||||
|
- 评论人:小亮
2006-06-07 13:59:33
|
|||
一样的问题.你的eil51.tsp文件保存在那? |
||||
|
- 评论人:helensteele
2006-06-07 08:21:49
|
|||
我没太仔细看,就把你的程序弄下来但运行不了,问题出在哪呢?麻烦告之(邮箱联系) |
||||
|
- 评论人:开会打开计划
2006-05-31 19:56:21
|
|||
谁会做蚁群算法的论文和程序,我马上就要交,大哥大姐帮个忙,有酬谢 |
||||
|
- 评论人:陈
2006-05-29 10:40:19
|
|||
程序有问题运行不了 |
||||
|
- 评论人:mona
2006-05-12 12:03:12
|
|||
你好,我现在研究基于蚁群算法在并行机调度上应用,看了你的代码,有些启发,不知这方面的应用你有什么心得?代码怎么改写? 谢谢!!邮件联系!! |
||||
|
- 评论人:小允
2006-05-10 22:41:30
|
|||
你好,我最近在做基于蚁群算法在Qos路由优化,看到你的代码,我下了,谢谢!!!
|
||||
|
- 评论人:Kevin L Li
2006-05-08 11:14:27
|
|||
可以把TSP文件发给我吗?
|
||||
|
- 评论人:fashionxu
2006-04-26 16:30:05
|
|||
指标一致,可能是程序有问题。
|
||||
|
- 评论人:yeni
2006-04-26 14:57:35
|
|||
你好,请问一下你对并行的蚁群算法有没有研究?不知道并行的时候那些参数的调整有没有经验值的?我的毕业设计做的这个如果参数一点点的调的话估计要做不完的了~如果你有这方面的研究的话能跟我分享一下吗?我的qq23799501 |
||||
|
- 评论人:mmemo
2006-04-25 20:20:51
|
|||
指标值始终一样,那是怎么回事?是不是陷入局部最优了? |
||||
|
- 评论人:fashionxu
2006-04-24 22:23:05
|
|||
参数alpha beta rou Q等很重要,一般有一些经验值可用。用此处的值,一般会收敛。如果不收敛,可能程序有问题。 |
||||
|
- 评论人:mmemo
2006-04-24 22:20:13
|
|||
参数alpha beta rou Q等的调整是不是很重要啊?不收敛调这些参数有用吗? |
||||
|
- 评论人:PPT
2006-04-21 10:44:20
|
|||
学习ing |
||||
|
- 评论人:yeni
2006-04-20 14:15:49
|
|||
请问你做并行吗? |
||||
|
- 评论人:ymmona
2006-04-20 10:12:43
|
|||
能否把eil51.tsp文件挂上来!谢谢! |
||||
|
- 评论人:fashionxu
2006-03-18 19:39:43
|
|||
跟原程序放到一块。 |
||||
|
- 评论人:王
2006-03-18 16:45:52
|
|||
你用了eil51.tsp这个文件保存在哪里面? |
||||
|
- 评论人:fashionxu
2006-03-17 12:14:26
|
|||
谢谢你啊。呵呵,有机会多讨论哦。 |
||||
|
- 评论人:王
2006-03-17 00:40:11
|
|||
很好 |
||||