蚁群算法C++程序

  • t9_670811
    了解作者
  • 4.7MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-27 09:29
    上传日期
蚁群算法C++实现
yiqunsuanfa.zip
  • yiqunsuanfa
  • yiqunsuanfa.sln
    900B
  • Debug
  • yiqunsuanfa.exe
    46KB
  • yiqunsuanfa.ilk
    466.7KB
  • yiqunsuanfa.pdb
    691KB
  • ipch
  • yiqunsuanfa-214c0696
  • yiqunsuanfa-fc1bebe0.ipch
    14.6MB
  • yiqunsuanfa
  • yiqunsuanfa.vcxproj.user
    143B
  • Debug
  • yiqunsuanfa.exe.intermediate.manifest
    381B
  • cl.command.1.tlog
    2.9KB
  • yiqunsuanfa.obj
    68KB
  • rc.command.1.tlog
    2.4KB
  • CL.read.1.tlog
    39.2KB
  • vc100.idb
    459KB
  • mt.read.1.tlog
    1.5KB
  • link-cvtres.read.1.tlog
    2B
  • link.read.1.tlog
    13.6KB
  • yiqunsuanfa.log
    922B
  • rc.read.1.tlog
    1.4KB
  • link.write.1.tlog
    3.7KB
  • yiqunsuanfa.vcxprojResolveAssemblyReference.cache
    713B
  • yiqunsuanfa.lastbuildstate
    79B
  • CL.write.1.tlog
    1.7KB
  • link.1132-cvtres.write.1.tlog
    2B
  • link.1132-cvtres.read.1.tlog
    2B
  • link.1132.read.1.tlog
    2B
  • vc100.pdb
    244KB
  • link.1132.write.1.tlog
    2B
  • yiqunsuanfa.exe.embed.manifest.res
    472B
  • link.command.1.tlog
    6.5KB
  • link.12788-cvtres.write.1.tlog
    2B
  • mt.write.1.tlog
    470B
  • yiqunsuanfa_manifest.rc
    212B
  • yiqunsuanfa.write.1.tlog
    0B
  • yiqunsuanfa.exe.embed.manifest
    406B
  • mt.command.1.tlog
    1.7KB
  • link.12788-cvtres.read.1.tlog
    2B
  • rc.write.1.tlog
    1.4KB
  • link-cvtres.write.1.tlog
    2B
  • link.12788.read.1.tlog
    2B
  • link.12788.write.1.tlog
    2B
  • yiqunsuanfa.vcxproj
    3.8KB
  • yiqunsuanfa.vcxproj.filters
    949B
  • yiqunsuanfa.cpp
    12.3KB
  • yiqunsuanfa.suo
    13.5KB
  • yiqunsuanfa.sdf
    5.4MB
内容介绍
#include<iostream> #include<math.h> #include<time.h> using namespace std; //该程序是以蚁群系统为模型写的蚁群算法程序(强调:非蚂蚁周模型),以三个著名的TSP问题为测试对象 //通过微调参数,都可以获得较好的解 //----------(3)问题三:Elion75 城市 TSP 问题 best_length = 542.31; //该程序最好的结果是542.309,可运行多次获得 //城市节点数目 #define N 75 //城市坐标 double C[N][2]={ {6,25}, {7,43}, {9,56}, {10,70}, {11,28}, {12,17}, {12,38}, {15,5}, {15,14}, {15,56}, {16,19}, {17,64}, {20,30}, {21,48}, {21,45}, {21,36}, {22,53}, {22,22}, {26,29}, {26,13}, {26,59}, {27,24}, {29,39}, {30,50}, {30,20}, {30,60}, {31,76}, {33,34}, {33,44}, {35,51}, {35,16}, {35,60}, {36,6}, {36,26}, {38,33}, {40,37}, {40,66}, {40,60}, {40,20}, {41,46}, {43,26}, {44,13}, {45,42}, {45,35}, {47,66}, {48,21}, {50,30}, {50,40}, {50,50}, {50,70}, {50,4}, {50,15}, {51,42}, {52,26}, {54,38}, {54,10}, {55,34}, {55,45}, {55,50}, {55,65}, {55,57}, {55,20}, {57,72}, {59,5}, {60,15}, {62,57}, {62,48}, {62,35}, {62,24}, {64,4}, {65,27}, {66,14}, {66,8}, {67,41}, {70,64} }; //----------上面参数是固定的,下面的参数是可变的----------- //蚂蚁数量 #define M 75 //最大循环次数NcMax int NcMax =1000; /////////////////// double alpha = 2;//信息启发因子,信息素的重要程度 double beta = 5;//期望启发式因子,城市间距离的重要程度 double rou = 0.1; //全局信息素挥发参数 double alpha1 = 0.1;//局部信息素挥发参数 double qzero = 0.1; //状态转移公式中的q0 //-----------问题三结束------------------------------------------------------------------------ //=========================================================================================================== //局部更新时候使用的的常量,它是由最近邻方法得到的一个长度 //什么是最近邻方法?:)就是从源节点出发,每次选择一个距离最短的点来遍历所有的节点得到的路径 //每个节点都可能作为源节点来遍历 double Lnn; //矩阵表示两两城市之间的距离 double allDistance[N][N]; //计算两个城市之间的距离 double calculateDistance(int i, int j) { return sqrt(pow((C[i][0]-C[j][0]),2.0) + pow((C[i][1]-C[j][1]),2.0)); } //由矩阵表示两两城市之间的距离 void calculateAllDistance() { for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if (i != j) { allDistance[i][j] = calculateDistance(i, j); allDistance[j][i] = allDistance[i][j]; } } } } //获得经过n个城市的路径长度 double calculateSumOfDistance(int* tour) { double sum = 0; for(int i = 0; i< N ;i++) { int row = *(tour + 2 * i); int col = *(tour + 2* i + 1); sum += allDistance[row][col]; } return sum; } class ACSAnt; class AntColonySystem { private: double info[N][N], visible[N][N];//节点之间的信息素强度,节点之间的能见度 public: AntColonySystem() { } //计算当前节点到下一节点转移的概率 double Transition(int i, int j); //局部更新规则 void UpdateLocalPathRule(int i, int j); //初始化 void InitParameter(double value); //全局信息素更新 void UpdateGlobalPathRule(int* bestTour, int globalBestLength); }; //计算当前节点到下一节点转移的概率 double AntColonySystem::Transition(int i, int j) { if (i != j) { return (pow(info[i][j],alpha) * pow(visible[i][j], beta)); } else { return 0.0; } } //局部更新规则 void AntColonySystem::UpdateLocalPathRule(int i, int j) { info[i][j] = (1.0 - alpha1) * info[i][j] + alpha1 * (1.0 / (N * Lnn)); info[j][i] = info[i][j]; } //初始化 void AntColonySystem::InitParameter(double value) { //初始化路径上的信息素强度tao0 for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { info[i][j] = value; info[j][i] = value; if (i != j) { visible[i][j] = 1.0 / allDistance[i][j]; visible[j][i] = visible[i][j]; } } } } //全局信息素更新 void AntColonySystem::UpdateGlobalPathRule(int* bestTour, int globalBestLength) { for(int i = 0; i < N; i++) { int row = *(bestTour + 2 * i); int col = *(bestTour + 2* i + 1); info[row][col] = (1.0 - rou) * info[row][col] + rou * (1.0 / globalBestLength); info[col][row] =info[row][col]; } } class ACSAnt { private: AntColonySystem* antColony; protected: int startCity, cururentCity;//初始城市编号,当前城市编号 int allowed[N];//禁忌表 int Tour[N][2];//当前路径 int currentTourIndex;//当前路径索引,从0开始,存储蚂蚁经过城市的编号 public: ACSAnt(AntColonySystem* acs, int start) { antColony = acs; startCity = start; } //开始搜索 int* Search(); //选择下一节点 int Choose(); //移动到下一节点 void MoveToNextCity(int nextCity); }; //开始搜索 int* ACSAnt::Search() { cururentCity = startCity; int toCity; currentTourIndex = 0; for(int i = 0; i < N; i++) { allowed[i] = 1; } allowed[cururentCity] = 0; int endCity; int count = 0; do { count++; endCity = cururentCity; toCity = Choose(); if (toCity >= 0) { MoveToNextCity(toCity); antColony->UpdateLocalPathRule(endCity, toCity); cururentCity = toCity; } }while(toCity >= 0); MoveToNextCity(startCity); antColony->UpdateLocalPathRule(endCity, startCity); return *Tour; } //选择下一节点 (状态转移规则) int ACSAnt::Choose() { int nextCity = -1; double q = rand()/(double)RAND_MAX; //如果 q <= q0,按先验知识,否则则按概率转移, if (q <= qzero) { double probability = -1.0;//转移到下一节点的概率 for(int i = 0; i < N; i++) { //去掉禁忌表中已走过的节点,从剩下节点中选择最大概率的可行节点 if (1 == allowed[i]) { double prob = antColony->Transition(cururentCity, i); if (prob > probability) { nextCity = i; probability = prob; } } } } else { //按概率转移 double p = rand()/(double)RAND_MAX;//生成一个随机数,用来判断落在哪个区间段 double sum = 0.0; double probability = 0.0;//概率的区间点,p 落在哪个区间段,则该点是转移的方向 //计算概率公式的分母的值 for(int i = 0; i < N; i++) { if (1 == allowed[i]) { sum += antColony->Transition(cururentCity, i); } } for(int j = 0; j < N; j++) { if (1 == allowed[j] && sum > 0) { probability += antColony->Transition(cururentCity, j)/sum; //积累概率 if (probability >= p || (p > 0.9999 && probability > 0.9999)) { nextCity = j; break; } } } } return nextCity; } //移动到下一节点 void ACSAnt::MoveToNextCity(int nextCity) { allowed[nextCity]=0; Tour[currentTourIndex][0] = cururentCity; Tour[currentTourIndex][1] = nextCity; currentTourIndex++;
评论
    相关推荐