完整程序下载
/*基本蚁群算法实现 *基本蚁群算法很容易出现早熟的现象 *几个参数的选择很关键,我在程序中使用的参数效果并不好 * *本程序写作时参考 重庆大学 黄茜 2008年硕士学位论文《蚁群算法及其在TSP中的应用》 * * @author: [email protected] */ #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)); //test roulette_select /*double testrand[3]={0.2,0.3,0.5}; int count[3]={0,0,0}; for (int i=0;i<100;i++){ int in=roulette_select(testrand,3); count[in]++; cout<< in<<" "; } cout<<endl; for (int i=0;i<3;i++){ cout<<count[i]<<" "; } return 0;*/ // 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; } //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]; //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 a value !!!!!! infoQuanRoad[i][j]=1.1; } } //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(); for (int j=0;j<cityNum;j++){ if (i!=j){ avaliablePath[i].push_back(j); } } //initialize positions of ants //antsPos[i]=i; //initialize tabus tabus[i][0]=i; } //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; for (int n=1;n<antsNum;n++){ if (pathLen[n]<pathLen[bestIndex]){ bestIndex=n; } } if (pathLen[bestIndex]<best_path_len){ best_path_len=pathLen[bestIndex]; } for (int n=0;n<cityNum;n++){ best_path[n]=tabus[bestIndex][n]; } //update quantity of information on every road for (int i=0;i<cityNum;i++){ for (int j=0;j<cityNum;j++){ if (i<j){ //find which ants have passed the road i,j vector<int> passAnts; vector<int>::iterator ite; for (int a=0;a<antsNum;a++){ for (int p=0;p<cityNum;p++){ if (tabus[a][p]==i){ if (j<cityNum-1&&tabus[a][p]==j){ passAnts.push_back(a); }else{ break; } }else if(tabus[a][p]==j){ if (i<cityNum-1&&tabus[a][p]==i){ passAnts.push_back(a); }else{ break; } } } } //increment of quantity of information vector<int>::iterator iter; double deltaQ=0; for (unsigned int d=0;d<passAnts.size();d++){ deltaQ+=1/pathLen[passAnts[d]]; } deltaQ*=infoQuanTotal; infoQuanRoad[i][j]=(1-volaFactor)*infoQuanRoad[i][j]+deltaQ; }else{ infoQuanRoad[i][j]=infoQuanRoad[j][i]; } } } //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; //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; }
开发环境 vc2008
使用vc6.0时,会出现变量 i 重定义的错误,因为vc6对for、while 条件变量的支持不完善。
修改方法:把for中定义的变量,在循环外定义