社区官方群 QQ群:25166303(忙)  |  飞信群:4541042(空)  |  旺旺群:93237588(空)  |  优酷2009超新星大赛 |   扶持社团计划书(必看)  |    广告联系
发新话题
打印

基本蚁群算法的C++源程序

基本蚁群算法的C++源程序

基本蚁群算法的C++源程序

//基本蚁群算法程序

//程序在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==1)
  {
   addcity(i);
   break;
  }


}


void ant::Clear()
{
m_dLength=0;
int i;
for(i=0; i〈iCityCount;i++)

  prob=0;
  AllowedCity=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=1;
  prob=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==1))
  {
   temp+=pow((1.0/Map.distance[curCity]),beta)*pow((Map.m_dTrial[curCity]),alpha);
  }
}
double sel=0;
for (i=0;i〈iCityCount;i++)

  if((AllowedCity==1))
  {
   prob=pow((1.0/Map.distance[curCity]),beta)*pow((Map.m_dTrial[curCity]),alpha)/temp;
   sel+=prob;
  }
  else
   prob=0;
}
double mRate=rnd(0,sel);
double mSelect=0;

for ( i=0;i〈iCityCount;i++)

  if((AllowedCity==1))
   mSelect+=prob ;
  if (mSelect>=mRate) {j=i;break;}
}

if (j==10000)
{
  temp=-1;
  for (i=0;i〈iCityCount;i++)

   if((AllowedCity==1))
    if (temp    {
     temp=pow((1.0/Map.distance[curCity]),beta)*pow((Map.m_dTrial[curCity]),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][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.tabu[j]][ants.tabu[j+1]]+=Q/ants.m_dLength ;
   Map.m_dDeltTrial[ants.tabu[j+1]][ants.tabu[j]]+=Q/ants.m_dLength;
  }
  Map.m_dDeltTrial[ants.tabu[iCityCount-1]][ants.tabu[0]]+=Q/ants.m_dLength;
  Map.m_dDeltTrial[ants.tabu[0]][ants.tabu[iCityCount-1]]+=Q/ants.m_dLength;
}
for (i=0;i〈iCityCount;i++)

  for (j=0;j〈iCityCount;j++)

  {
   Map.m_dTrial[j]=(rou*Map.m_dTrial[j]+Map.m_dDeltTrial[j] );
   Map.m_dDeltTrial[j]=0;
  }

}


}
void project::initmap()
{
int i;
int j;
for(i=0;i〈iCityCount;i++)
  for (j=0;j〈iCityCount;j++)
  {


   Map.m_dTrial[j]=1;
   Map.m_dDeltTrial[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.num>>cc.x>>cc.y;
  besttour=0;
}
int j;
for(i=0;i〈iCityCount;i++)
  for (j=0;j〈iCityCount;j++)
  {

   {
    Map.distance[j]=sqrt(pow((cc.x-cc[j].x),2)+pow((cc.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.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数据的时候,将前面的一些字符串信息删除,只留下坐标数据。
学习,考研,一切皆有可能!!!

TOP

发新话题

网站web安全
检测认证
公共信息网络
安全备案
电子公告服务
备案网站
信息网络安全
报警服务
网络不良信息
举报中心