最大最小蚁群算法求解TSP的实现在基本算法的基础上做少许更改即可。
1》初始化路段信息素为最大值
2》在增加信息素时只更新最优路径
3》在挥发信息素时,将所有路段的信息素值限制在最大最小值之间
/*最大最小蚁群算法实现 *最大最小蚁群算法通常会出现晚熟现象 *最大最小信息素的值在本算法中很关键,我的选择并不合理,做了几组尝试,效果并不好,可以增加参数选择程序以找到合适的参数 * *本程序写作时参考 重庆大学 黄茜 2008年硕士学位论文《蚁群算法及其在TSP中的应用》 * @author: onezeros@yahoo.cn */ #include <iostream> #include <fstream> #include <vector> #include <math.h> #include <stdlib.h> #include <time.h> #include <assert.h> using namespace std; #include “roulette_select.h” int main() { srand((unsigned)time(NULL)); // data filein fstream filein(“data/berlin52.tsp.processed”,ios::in); fstream fileout(“data/result.txt”,ios::out); if (filein==NULL){ cout<<“cannot open data file”<<endl; return 1; } if (fileout==NULL){ cout<<“cannot open a file to write data”<<endl; } //record how many times the generation is effective int effectiveUpdata=0; //number of cities int cityNum; //standard path int* standardPath; //distance between cities double** distance; //read data from filein filein>>cityNum; cout<<cityNum<<endl; standardPath=new int[cityNum]; for (int i=0;i<cityNum;i++){ filein>>standardPath[i]; } distance=new double* [cityNum]; for (int i=0;i<cityNum;i++){ distance[i]=new double[cityNum]; for (int j=0;j<cityNum;j++){ filein>>distance[i][j]; } } //show standard path and distance fileout<<“standard path:”<<endl; double standardbestlen=0; for (int i=0;i<cityNum;i++){ fileout<<standardPath[i]<<” “; } for (int i=0;i<cityNum-1;i++){ standardbestlen+=distance[standardPath[i]][standardPath[i+1]]; } standardbestlen+=distance[standardPath[cityNum-1]][standardPath[0]]; fileout<<endl<<“length of standard path: “<<endl<<” “<<standardbestlen<<endl; //parameters////////////////////////////////////////////////////////////// //number of ants int antsNum=cityNum; //tabus of ants int** tabus=new int*[antsNum]; for (int i=0;i<antsNum;i++){ tabus[i]=new int[cityNum]; } ////I also record cities which haven’t been reached vector<int>* avaliablePath=new vector<int>[antsNum]; //minimal and maximal quantity of information !!!!!!!!!!!!! double minInfo=0.02; double maxInfo=0.3; //volatility double volaFactor=0.8; //heuristic factor //double heuristicFactor; int alpha=1; int beta=2; //total quantity of information released in a tour!!!!!!!!! double infoQuanTotal=100; //quantity of information on every road double** infoQuanRoad=new double*[cityNum]; for (int i=0;i<cityNum;i++){ infoQuanRoad[i]=new double[cityNum]; for (int j=0;j<cityNum;j++){ //initialize information with maximal information infoQuanRoad[i][j]=maxInfo; } } //number of generations!!!!!!!!!!!! int generation=1500; //record best ant in processing int* best_path=new int[cityNum]; double best_path_len=1<<(8*sizeof(int)-2); //process///////////////////////////////////////////////////////////////// for (int g=0;g<generation;g++){ //record ants’ current position //int* antsPos=new int[antsNum]; //initialize tabus for (int i=0;i<antsNum;i++){ avaliablePath[i].clear(); //uniform distribution for (int j=0;j<cityNum;j++){ if (i!=j){ avaliablePath[i].push_back(j); } } //start from 0 /*for (int j=1;j<antsNum;j++){ avaliablePath[i].push_back(j); }*/ //initialize positions of ants //initialize tabus //uniform distribution tabus[i][0]=i; //start from 0 //tabus[i][0]=0; } //all the ants walk through all the cities for (int step=1;step<cityNum-1;step++){ for (int ant=0;ant<antsNum;ant++){ int avaliableNum=avaliablePath[ant].size(); //probobility of every road to go double* probobility=new double[avaliableNum]; int path_i=tabus[ant][step-1]; int path_j; double* sum=new double[avaliableNum]; double summ=0; for (int s=0;s<avaliableNum;s++){ path_j=avaliablePath[ant][s]; sum[s]=pow(infoQuanRoad[path_i][path_j],alpha)*pow(1/distance[path_i][path_j],beta); summ+=sum[s]; } for (int p=0;p<avaliableNum;p++){ path_j=avaliablePath[ant][p]; probobility[p]=sum[p]/summ; } //select a road to go according to the probobility int randIndex=roulette_select(probobility,avaliableNum); int selected=avaliablePath[ant][randIndex]; tabus[ant][step]=selected; vector<int>::iterator ite; for (ite=avaliablePath[ant].begin();ite!=avaliablePath[ant].end();ite++){ if (*ite==selected){ avaliablePath[ant].erase(ite); break; } } //assert(ite!=avaliablePath[ant].end()); delete[] sum; delete[] probobility; } } //last step for (int ant=0;ant<antsNum;ant++){ assert(avaliablePath[ant].size()==1); tabus[ant][cityNum-1]=avaliablePath[ant][0]; avaliablePath[ant].pop_back(); } //calculate length of paths which all the ants passed double* pathLen=new double[antsNum]; for (int n=0;n<antsNum;n++){ pathLen[n]=0; for (int path=1;path<cityNum;path++){ pathLen[n]+=distance[tabus[n][path-1]][tabus[n][path]]; } } //find global best path int bestIndex=0; //find best in this generation for (int n=1;n<antsNum;n++){ if (pathLen[n]<pathLen[bestIndex]){ bestIndex=n; } } //update global best length if (pathLen[bestIndex]<best_path_len){ best_path_len=pathLen[bestIndex]; effectiveUpdata++; } //record best path of this generation for (int n=0;n<cityNum;n++){ best_path[n]=tabus[bestIndex][n]; } //update quantity of information on best path only double deltaQ=infoQuanTotal/pathLen[bestIndex]; for (int i=1;i<cityNum;i++){ infoQuanRoad[i][i-1]=volaFactor*infoQuanRoad[i][i-1]+deltaQ; infoQuanRoad[i-1][i]=infoQuanRoad[i][i-1]; } //limit quantity of information between minima and maxima for (int i=0;i<cityNum;i++){ for (int j=0;j<cityNum;j++){ if (i<j){ if (infoQuanRoad[i][j]>maxInfo){ infoQuanRoad[i][j]=maxInfo; }else if (infoQuanRoad[i][j]<minInfo){ infoQuanRoad[i][j]=minInfo; } } } } //free antsPos //delete[] antsPos; ////////////////////////////////////////////////////////////////////////// //compare and output result of every generation //to file fileout<<“generation :”<<g<<endl <<“best distance found for the moment: ” <<best_path_len<<endl <<“best distance found in this epoch: ” <<pathLen[bestIndex]<<endl <<“best path found for the moment:”<<endl; for (int i=0;i<cityNum;i++){ fileout<<best_path[i]<<” “; } fileout<<endl; //to console cout<<“generation :”<<g<<endl <<“best distance found for the moment: ” <<best_path_len<<endl <<“best distance found in this epoch: ” <<pathLen[bestIndex]<<endl <<“best path found for the moment:”<<endl; for (int i=0;i<cityNum;i++){ cout<<best_path[i]<<” “; } cout<<endl; //free pathLen delete[] pathLen; } //show standard path and distance cout<<“standard path:”<<endl; for (int i=0;i<cityNum;i++){ cout<<standardPath[i]<<” “; } cout<<endl<<“length of standard path: “<<endl<<” “<<standardbestlen<<endl; cout<<“effective times: “<<effectiveUpdata<<endl; //free memory///////////////////////////////////////////////////////////// filein.close(); fileout.close(); delete[] standardPath; delete[] best_path; for (int i=0;i<cityNum;i++){ delete[] distance[i]; delete[] infoQuanRoad[i]; } delete[] distance; delete[] infoQuanRoad; for (int i=0;i<antsNum;i++){ delete[] tabus[i]; } delete[] tabus; delete[] avaliablePath; return 0; }