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

//程序在vc++6.0下面同过,对原来的做了一点修改。
//你可以使用本代码,如果感到对你有用的话,请通知作者,作者会很高兴。
//通讯地址:fashionxu@163.com
//by FashionXu
#include "stdafx.h"
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; i〈iCityCount;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〈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数据的时候,将前面的一些字符串信息删除,只留下坐标数据

蚁群算法(Ant Colony Optimization,ACO)是一种模拟蚂蚁在寻找食物过程中的行为规律,从而优化问题求解的启发式算法。 以下是一个简单的蚁群算法的C语言实现,用于求解旅行商问题(TSP): ``` #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define N 10 // 城市数量 #define M 100 // 蚂蚁数量 #define ALPHA 1 // 信息素重要程度因子 #define BETA 5 // 启发函数重要程度因子 #define RHO 0.5 // 信息素挥发因子 #define Q 100 // 常系数 #define MAX_T 1000 // 迭代次数 double distance[N][N]; // 城市距离矩阵 double pheromone[N][N]; // 信息素矩阵 int ants[M][N]; // 蚂蚁走过的路径 double length[M]; // 蚂蚁走过的路径长度 int best_ant[N]; // 最优路径 double best_length = 1e9; // 最优路径长度 double rand01() { return (double)rand() / RAND_MAX; } int rand_int(int n) { return rand() % n; } // 计算两个城市之间的距离 double city_distance(int city1, int city2) { return distance[city1][city2]; } // 初始化城市距离矩阵和信息素矩阵 void init() { srand((unsigned)time(NULL)); int i, j; for (i = 0; i < N; i++) { for (j = i; j < N; j++) { if (i == j) { distance[i][j] = 0; pheromone[i][j] = 0; } else { distance[i][j] = distance[j][i] = rand01(); pheromone[i][j] = pheromone[j][i] = 0.01; } } } } // 蚂蚁选择下一个城市 int select_next_city(int ant, int current_city) { int i; double p[N], sum = 0; for (i = 0; i < N; i++) { if (ants[ant][i] == -1) { double eta = 1.0 / distance[current_city][i]; p[i] = pow(pheromone[current_city][i], ALPHA) * pow(eta, BETA); sum += p[i]; } } if (sum == 0) return -1; double r = rand01() * sum; for (i = 0; i < N; i++) { if (ants[ant][i] == -1) { r -= p[i]; if (r < 0) return i; } } return -1; } // 蚂蚁进行一次完整的旅行 void ant_travel(int ant) { int i; for (i = 0; i < N; i++) { ants[ant][i] = -1; } int current_city = rand_int(N); ants[ant][current_city] = 1; length[ant] = 0; for (i = 1; i < N; i++) { int next_city = select_next_city(ant, current_city); if (next_city == -1) break; ants[ant][next_city] = i + 1; length[ant] += city_distance(current_city, next_city); current_city = next_city; } length[ant] += city_distance(current_city, ants[ant][0] - 1]); if (length[ant] < best_length) { best_length = length[ant]; for (i = 0; i < N; i++) { best_ant[i] = ants[ant][i]; } } } // 更新信息素矩阵 void update_pheromone() { int i, j; for (i = 0; i < N; i++) { for (j = i; j < N; j++) { pheromone[i][j] *= (1 - RHO); pheromone[j][i] = pheromone[i][j]; } } for (i = 0; i < M; i++) { double delta_pheromone = Q / length[i]; for (j = 0; j < N; j++) { int city1 = ants[i][j]; int city2 = ants[i][(j + 1) % N]; pheromone[city1 - 1][city2 - 1] += delta_pheromone; pheromone[city2 - 1][city1 - 1] = pheromone[city1 - 1][city2 - 1]; } } } int main() { init(); int t; for (t = 0; t < MAX_T; t++) { int i; for (i = 0; i < M; i++) { ant_travel(i); } update_pheromone(); } printf("Best length: %lf\n", best_length); printf("Best path: "); int i; for (i = 0; i < N; i++) { printf("%d ", best_ant[i]); } printf("\n"); return 0; } ``` 该实现中,首先定义了几个常数,包括城市数量、蚂蚁数量、信息素重要程度因子、启发函数重要程度因子、信息素挥发因子等。然后定义了一些函数,包括计算两个城市之间的距离、初始化城市距离矩阵和信息素矩阵、蚂蚁选择下一个城市、蚂蚁进行一次完整的旅行、更新信息素矩阵等。最后在主函数中进行迭代,不断让蚂蚁进行旅行,并更新信息素矩阵。迭代结束后输出最优路径最优路径长度。 该实现中的算法并不是最优的,仅供参考。实际应用中需要根据具体问题进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值