//程序在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数据的时候,将前面的一些字符串信息删除,只留下坐标数据

基本蚁群算法的C++源程序相关推荐

  1. 蚁群算法优化神经网络matlab源程序,粒子群优化神经网络的程序大集合

    粒子群程序集合 866867259psobp psobp.m pso(粒子群算法)优化神经网络 粒子群算法(PSO)应用于神经网络优化[matlab] PSOt A Particle Swarm Op ...

  2. 蚁群算法小结及算法实例(附Matlab代码)

    目录 1.基本蚁群算法 2.基本蚁群算法的流程 3.关键参数说明 3.1 信息素启发式因子 α 3.2 期望启发因子 β 3.3 信息素蒸发系数 ρ 3.4 蚂蚁数目 m 3.5 信息素强度 Q 对算 ...

  3. 2018/-4-11遗传,差分进化,免疫,蚁群算法中的公式

    <智能优化算法以及matlab实现>包子阳中的智能算法公式 1.遗传算法中的轮盘赌公式 2.差分进化中的公式 (1)初始化 (2)变异 (3)交叉 自适应差分进化算法中的自适应变异算子: ...

  4. 2018-4-8蚁群算法---包子阳《智能优化算法以及Matlab实现》第五章

    资料来源: <智能优化算法以及matlab实现>包子阳  余继周 编著 第五章-----蚁群算法 是一种元启发式优化算法(自己理解:就是作为群体的单位个体也就是元,在里面充当着随机的选择搜 ...

  5. 智能优化算法之蚁群算法(1)

    蚁群算法(ant colony algorithm) : 一种模拟进化算法 蚂蚁在觅食过程中能够在其经过的路径留下一种称为信息素的物质,并在觅食的过程中能感知这种物质的强度,并指导自己的行动方向,他们 ...

  6. matlab蚁群算法 路径规划,基于蚁群算法的机器人路径规划MATLAB源码

    基于蚁群算法的机器人路径规划MA TLAB源码 使用网格离散化的方法对带有障碍物的环境建模,使用邻接矩阵存储该环境,使得问题转化为蚁群算法寻找最短路径. function [ROUTES,PL,Tau ...

  7. 机器学习(MACHINE LEARNING)MATLAB蚁群算法解决TSP问题

    文章目录 1 蚁群算法 2 蚁群算法与TSP问题的关系 3 代码实现 1 蚁群算法 基本原理: (1)蚂蚁在携带等量的信息素一路释放 (2)信息素浓度会和路径的长度成反比 (3)下次蚂蚁来到该路口会选 ...

  8. vrp车辆路径问题 php,蚁群算法在车辆路径问题(VRP)中的应用.ppt

    蚁群算法在车辆路径问题(VRP)中的应用 ◆割平面法(Cutting Planes Approach)[6] 割平面法求解VRP问题(A)的基本思想是,在求解相应的不含整数约束的VRP问题(B)上,增 ...

  9. matlab蚁群算法 降维,基于蚁群算法的路由问题研究

    在组播路由中,用matlab画图,组播总费用提前收敛,到达各个节点的费用后收敛,甚至有些节点费用一直没有收敛.不应该是每一个节点收敛后,组播树总费用才收敛吗? 是不是我的代码有问题 以下是我的代码: ...

最新文章

  1. Unity从头到尾无代码游戏制作学习教程
  2. 【笔记】大数定理证明
  3. [小程序]微信小程序获取input并发送网络请求
  4. java小数点默认定义_java求两个数后小数点默认无限长度
  5. java解决错误经验_在Java错误进入生产之前的新处理方式
  6. 计算机课件知识,计算机基础知识1认识计算机课件.ppt
  7. Protocol handler start failedCaused by: java.net.SocketException: Permission denied
  8. 数组中的对象的特征值提取生成新对象实现方法
  9. PyTorch载入图片后ToTensor解读(含PIL和OpenCV读取图片对比)
  10. C#设计模式---模板方法模式(Template Method Pattern)
  11. python可选参数定义_Python中函数的参数定义和可变参数用法实例分析
  12. 如何正确使用 Flink Connector?
  13. webshell检测方式深度剖析---RASP(taint扩展)
  14. Android中的事件处理总结
  15. 被称为偏执的企业家,他成功跻身中国民企500强
  16. 中台:业务中台、数据中台、技术中台
  17. RestfulCRUD 规范
  18. 计算机毕业设计springboot社区志愿者管理系统的设计与实现【前后端分离·新项目】
  19. web实验5 圣斗士网页
  20. ALIENTEK MINIS TM32 给定程序keil5大量错误问题办法

热门文章

  1. 【OpenGL】二十四、OpenGL 纹理贴图 ( 读取文件内容 | 桌面程序添加控制台窗口 | ‘fopen‘: This function may be unsafe 错误处理 )
  2. python 获取子目录下的所有文件的路径
  3. 云+网+端 技术架构
  4. windows下pycharm远程调试pyspark
  5. Cloud Native workshop
  6. AC日记——行程长度编码 openjudge 1.7 32
  7. img标签使用默认图片的一种方式
  8. IOS笔记本----读写.plist文件
  9. PHP通过header实现文本文件的下载
  10. main函数第3个参数envp装的是什么(envp:环境变量)