基本蚁群算法代码C版
//Basic Ant Colony Algorithm for TSP
#include
#include
#include
#include
#include
#include
#include
#define N 31 //city size
#define M 31 //ant number
double inittao=1; //初始信息量的多少
double tao[N][N]; //每条路径上的信息量
double detatao[N][N]; //Δτ,代
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
相应路径上的信息素增量
double distance[N][N]; //城市距离矩阵
double yita[N][N]; //启发函数,其值yita[i][j]=1/distance[i][j]
int tabu[M][N]; //禁忌表,tabu[i][j]=1表示蚂蚁i已经走过了j城市?
int route[M][N]; //保存蚂蚁k的路径的数组为route[k][N]
double solution[M];
int BestRoute[N];
double BestSolution=10000000000;
double alfa,beta,rou,Q; //Pijk(t)表示t时刻蚂蚁k由城市i转移到城市j的状态转移概率,
//alfa是信息启发式因子,表示轨迹的相对重要性,反映蚂蚁在运动过程中所积累的信息在蚂蚁运动时所起的作用,其值越大,则该蚂蚁越倾向于选择其他蚂蚁经过的路径,蚂蚁之间的协作性越强
//beta是期望启发式因子,表示能见度的相对重要性,反映在运动过程中启发信息在蚂蚁选择路径中的受重视程度,其值越大,则该状态转移概率越接近于贪心规则
//rou是信息残留因子(与书上不同,书上用rou表示信息挥发系数,而用1-rou表示信息残留因子)
//Q为信息素强度,用于计算蚂蚁留在路径上的信息量
int NcMax; //迭代次数
void initparameter(void); // initialize the parameters of basic ACA
double EvalueSolution(int *a); // evaluate the solution of TSP, and calculate the length of path
void InCityXY( double x[], double y[], char *infile ); // input the nodes' coordinates of TSP
void initparameter(void)
{
alfa=1; beta=5; rou=0.9; Q=100;
NcMax=200; //最大迭代次数
}
void main(void)
{
int NC=0;
initparameter();
double x[N];
double y[N];
InCityXY( x, y, "city31.tsp" );
//初始化距离矩阵
for(int i=0;idrand) //当drand落在第j个城市上时,选择j城市
break;
}
tabu[k][j]=1; //禁忌表置访问标志
route[k][s]=j; //保存蚂蚁k的第s步经过的城市
}
s++;
}
//在N次循环后,所有蚂蚁的禁忌表都已填满
//计算每个蚂蚁走过的路径的长度,并找到最短路径保存,记录此路径并更改信息素。// the pheromone is updated
for(i=0;i20) //信息素上界,避免94行和101行中,启发信息被路径信息淹没
tao[i][j]=20;
}
//将蚂蚁的路径再重新置空,为下一次循环做准备,要不然每个蚂蚁的路径都已经满了,则没有办法进行下一次迭代了.
for(k=0;k file!\n";
exit(0);
}
result<<"*-------------------------------------------------------------------------*"< file!\n";
exit(0);
}
int i=0;
while( !inxyfile.eof() )
{
inxyfile>>x[i]>>y[i];
if( ++i >= N ) break;
}
}
/*31个城市坐标:
1304 2312
3639 1315
4177 2244
3712 1399
3488 1535
3326 1556
3238 1229
4196 1004
4312 790
4386 570
3007 1970
2562 1756
2788 1491
2381 1676
1332 695
3715 1678
3918 2179
4061 2370
3780 2212
3676 2578
4029 2838
4263 2931
3429 1908
3507 2367
3394 2643
3439 3201
2935 3240
3140 3550
2545 2357
2778 2826
2370 2975
运行后可得到15602的巡游路径*/