forked from hackboxlive/ant-tsp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ant.cpp
182 lines (167 loc) · 3.78 KB
/
ant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#include "ant.hpp"
#include "graph.hpp"
const int inf = 1e9 + 7;
vector<pii> graph[100100];
vector<edge> edges;
int adj[1004][1004];
int dist[1000][1000];
double pheromone[1001][1001];
double lazy[1001][1001];
int n, m;
void print(vector<int>& v) {
for (auto x: v) {
printf("%d ", x);
}
printf("\n");
}
void singlesource(int u) {
for (int i = 1; i <= n; i++)
dist[u][i] = inf;
priority_queue<pii, vector<pii>, cmp> pq;
dist[u][u] = 0;
pq.push(make_pair(u, 0));
while (!pq.empty()) {
int v = pq.top().first, d = pq.top().second;
pq.pop();
for (auto p: graph[v]) {
int nxt = p.first, w = p.second;
if (dist[u][nxt] > dist[u][v] + w) {
dist[u][nxt] = dist[u][v] + w;
pq.push(make_pair(nxt, dist[u][nxt]));
}
}
}
}
void allpairs() {
for (int u = 1; u <= n; u++) {
singlesource(u);
}
}
int getnext(int from, bool* visited) {
// gets probability distribution of which node to visit
// next
vector<pair<int, double> >prob;
double sum = 0;
for (auto i: graph[from]) {
int vertex = i.first;
if (visited[vertex] == 0) {
double v = pheromone[from][vertex] / (adj[from][vertex] * adj[from][vertex]);
sum += v;
prob.push_back(make_pair(vertex, sum));
}
}
int cnt = 0;
for (auto i: graph[from]) {
if (visited[i.first] == 0) {
prob[cnt].second /= sum;
cnt++;
}
}
if (prob.empty())
return -1;
double r = rand() / (double) RAND_MAX;
int lo = 0, hi = prob.size() - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (prob[mid].second <= r)
hi = mid;
else
lo = mid + 1;
}
return prob[lo].first;
}
int tour(vector<int>& path) {
int src = rand() % n + 1;
bool visited[n + 1];
memset(visited, 0, sizeof(visited));
visited[src] = 1;
int cnt = 1;
int here = src;
int len = 0; // length of tour
while (cnt < n) {
path.push_back(here);
int nxt = getnext(here, visited);
if (nxt == -1) {
path.clear();
return -1;
}
visited[nxt] = 1;
len += adj[here][nxt];
here = nxt;
cnt++;
}
path.push_back(here);
return len;
}
void evaporate() {
double rho = 0.75; // decrease by factor of 0.1
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
pheromone[i][j] = rho * pheromone[i][j];
}
}
}
int main(void) {
srand(time(NULL));
for(int i=0; i<100; i++)
{
for(int j=0; j<100; j++)
pheromone[i][j] = 1;
}
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
graph[u].push_back(make_pair(v, w));
graph[v].push_back(make_pair(u, w));
edges.push_back(edge(u, v, w, 0));
adj[u][v] = w;
adj[v][u] = w;
}
allpairs();
int ants = n / 2 + 1000;
int iter = 500;
int mn = INT_MAX;
vector<int> final_tour;
while (iter--) {
printf("iter = %d\n",500 - iter);
vector<int> paths[ants + 1];
int lengths[ants + 1];
// get every ant to tour
for (int i = 0; i < ants; i++) {
lengths[i] = tour(paths[i]);
if (lengths[i] == -1) // no tour found
continue;
printf("ant no = %d\n", i);
for(int j=0; j<paths[i].size(); j++)
{
printf("%d ",paths[i][j]);
}
printf("lenght of path = %d\n", lengths[i]);
cout<<endl;
if (mn > lengths[i]) {
final_tour.clear();
for (auto x: paths[i])
final_tour.push_back(x);
mn = lengths[i];
}
}
evaporate(); // evaporate pheromone from each edge by factor of 0.25
// increase pheromones according to each ant
for (int i = 0; i < ants; i++) {
if (lengths[i] == -1)
continue;
for (int j = 0; j < paths[i].size() - 1; j++) {
int u = paths[i][j], v = paths[i][j + 1];
pheromone[u][v] += 1000.0 / lengths[i];
}
}
}
for (auto x: final_tour)
printf("%d ", x);
printf("\n");
printf("%d\n", mn);
}