蚁群算法(ACO)学习笔记
https://blog.csdn.net/fulongxu/article/details/99678958?ops_request_misc=&request_id=&biz_id=102&utm_term=c++%20%E8%9A%81%E7%BE%A4%E7%AE%97%E6%B3%95&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-5-99678958.nonecase&spm=1018.2226.3001.44501 6734 1453
2 2233 10
3 5530 1424
4 401 841
5 3082 1644
6 7608 4458
7 7573 3716
8 7265 1268
9 6898 1885
10 1112 2049
11 5468 2606
12 5989 2873
13 4706 2674
14 4612 2035
15 6347 2683
16 6107 669
17 7611 5184
18 7462 3590
19 7732 4723
20 5900 3561
21 4483 3369
22 6101 1110
23 5199 2182
24 1633 2809
25 4307 2322
26 675 1006
27 7555 4819
28 7541 3981
29 3177 756
30 7352 4506
31 7545 2801
32 3245 3305
33 6426 3173
34 4608 1198
35 23 2216
36 7248 3779
37 7762 4595
38 7392 2244
39 3484 2829
40 6271 2135
41 4985 140
42 1916 1569
43 7280 4899
44 7509 3239
45 10 2676
46 6807 2993
47 5185 3258
48 3023 1942
#include<bits/stdc++.h>
#define Read() freopen("data.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
using namespace std;
const int City_Num = 48;//城市数目
const int Ant_Num = 20;//蚂蚁数目
const int Total_Num = 2000;//迭代次数
double Distance;//保存距离
vector<int> Edge;//保存每条边有哪些蚂蚁走过
double Ant_length;//保存每只蚂蚁一轮后走过的路径长度
double Edge_Info;//保存每条边的信息素
vector<int> Ant;//保存每个蚂蚁的路径
int vis;//标记每个蚂蚁走过哪个城市
double Total_Length;//记录每轮迭代后每只蚂蚁走的总路长
vector<int> Min_Path;
double Alpha = 1.0;
double Beta = 5.0;
double Rho = 0.5;
double Min_Length = 0x3f3f3f3f;
struct node
{
int x, y;
}Node;
double Get_distance(node a, node b)
{
return sqrt((1.0*(a.x-b.x)*(a.x-b.x) + 1.0*(a.y-b.y)*(a.y-b.y))/10.0);
}
void Init()
{
for(int i = 0; i < City_Num; i++)
{
Distance = 0;
Edge_Info = 0;
for(int j = i+1; j < City_Num; j++)
{
Distance = Get_distance(Node, Node);
Distance = Distance;
Edge_Info = 0.1;
Edge_Info = 0.1;
}
}
}
int Rand()
{
return rand()%48;
}
double Rand_Double()
{
return (rand()%10000)/10000.0;
}
void Build_Path()
{
for(int i = 0; i < Ant_Num; i++)
{
while(Ant.size() != City_Num)
{
double sum = 0.0;
double p_go;//计算访问下一个城市的概率
memset(p_go, 0, sizeof(p_go));
int total_pos = Ant.size();
int now_pos = Ant;
for(int j = 0; j < City_Num; j++)
{
if(vis) continue;
p_go = pow(Edge_Info, Alpha)*pow((1.0/Distance), Beta);
sum += p_go;
}
double p = Rand_Double();
double p_total = 0;
for(int j = 0; j < City_Num; j++)
{
if(vis) continue;
if(p_total+(p_go/sum) >= p)//下一个访问的城市是j
{
vis = 1;//标记该城市已经走过
Ant.push_back(j);//把这个城市加到城市列表中
Total_Length += Distance;//计算第i只蚂蚁当前一共走的距离
Edge.push_back(i);//记录走过这条边的蚂蚁,用来更新信息素矩阵
break;
}
else
{
p_total += (p_go/sum);
}
}
}
Total_Length += Distance]];//将终点到起点的路长加入到总路长中
Edge]].push_back(i);//终点指向起点的路的蚂蚁记录一下
if(Min_Length > Total_Length)//寻找全局最短路径,并记录路径
{
Min_Length = Total_Length;
Min_Path.clear();
for(int k = 0; k < City_Num; k++)
Min_Path.push_back(Ant);
Min_Path.push_back(Ant);
}
}
}
double Get_Edge_P(int x, int y)
{
int n = Edge.size();
double sum = 0.0;
for(int i = 0; i < n; i++)
{
int ant = Edge;
sum += 1.0/Total_Length;
}
return sum;
}
void Change_Info()
{
for(int i = 0; i < City_Num; i++)
{
for(int j = i+1; j < City_Num; j++)
{
double edge_p = Get_Edge_P(i, j);
Edge_Info = (1.0-Rho)*Edge_Info + edge_p;
Edge_Info = Edge_Info;
}
}
}
void Print()
{
cout<<Min_Length<<endl;
for(int i = 0; i < City_Num; i++)
cout<<Min_Path<<"-->";
cout<<Min_Path<<endl;
}
int main()
{
srand(time(NULL));
Read();
for(int i = 0; i < City_Num; i++)
{
int num, x, y;
cin >>num>>x>>y;
Node.x = x;
Node.y = y;
}
for(int i = 0; i < 48; i++)
cout<<i+1<<" "<<Node.x<<" "<<Node.y<<endl;
cout<<endl<<endl;
Init();
int T = Total_Num;
while(T--)
{
memset(vis, 0, sizeof(vis));
for(int i = 0; i < Ant_Num; i++)
{
Ant.clear();
Total_Length = 0;
}
for(int i = 0; i < Ant_Num; i++)
{
int start_city = Rand();
vis = 1;
Ant.push_back(start_city);
}
Build_Path();//构建路径
Change_Info();//信息素更新
}
Print();//打印最优路径
return 0;
}
页:
[1]