-
Notifications
You must be signed in to change notification settings - Fork 1
/
antsystem.cpp
399 lines (360 loc) · 10.1 KB
/
antsystem.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* AcoPath: Shortest path calculation using Ant Colony Optimization
* Copyright (C) 2014-2021 by Constantine Kyriakopoulos
* zfox@users.sourceforge.net
* @version 0.9.1
*
* @section LICENSE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "antsystem.h"
/**
* Constructor initialising the topology from external file.
*
* @param filename A file containing the topology in JSON format
* @param ants Number of ants to unlease in each iteration
* @param iterations Number of iterations
*/
AntSystem::AntSystem(const std::string& filename, int ants, int iterations)
{
try
{
initTopo(filename);
for(auto& edge : edges)
edge2phero.insert(std::make_pair(edge, static_cast<double>(PHERO_QUANTITY)));
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
init(ants, iterations);
}
/**
* Constructor w/out initialising the topology.
*
* @param ants Number of ants to unlease in each iteration
* @param iterations Number of iterations
*/
AntSystem::AntSystem(int ants, int iterations)
{
init(ants, iterations);
}
/**
* Empty destructor.
*/
AntSystem::~AntSystem() { }
/**
* Initialiser for ants, iterations and random numner generator.
*
* @param ants Number of ants to unlease in each iteration
* @param iterations Number of iterations
*/
void AntSystem::init(int ants, int iterations)
{
if(ants > 0 && iterations > 0)
{
this->ants = ants;
this->iterations = iterations;
}
else
{
this->ants = ANTS;
this->iterations = ITERATIONS;
}
std::random_device rd;
gen = std::mt19937_64(rd());
}
/**
* Finds the best path from a source node to a destination using the Ant System.
*
* @param start Path's starting point
* @param end Path's end point
* @return std::vector<int> The best path
*/
std::vector<int> AntSystem::path(int start, int end)
{
std::vector<int> bestPath;
double shortest = std::numeric_limits<double>::max();
// For the predefined number of iterations
for(int i = 0; i < iterations; ++i)
{
std::map<int, std::vector<int> > antTraces;
std::map<int, double> tourLengths;
// Release ants from source node and let them traverse the graph
// structure to reach a destination
for(int j = 1; j <= ants; ++j)
{
// This trace will be used by this ant to store its node sequence
std::vector<int> antTrace;
goAnt(start, end, antTrace);
if(antTrace.size() > 1 && antTrace.front() == start
&& antTrace.back() == end)
{
// Destination reached, so calculate tour length and keep the shortest one
antTraces.insert(std::make_pair(j, antTrace));
tourLengths[j] = calcTourLength(antTrace);
if(tourLengths[j] > 0 && tourLengths[j] < shortest)
{
shortest = tourLengths[j];
bestPath = antTrace;
}
}
else
{
// Well, this ant failed to reach its destination
antTraces.insert(std::make_pair(j, std::vector<int>()));
tourLengths[j] = 0;
}
}
// Update pheromone trails upon the correct node sequences
updateTrails(antTraces, tourLengths);
}
return bestPath;
}
/**
* Clears instance's state
*/
void AntSystem::clear()
{
edge2phero.clear();
edges.clear();
}
/**
* Returns the amount of pheromone difference according to tour's length.
*
* @param length Tour's length produced by an ant
* @return double Amount of pheromone
*/
double AntSystem::diffPheromone(double length)
{
return PHERO_QUANTITY / length;
}
/**
* Updates pheromone levels upon all graph edges.
*
* @param antTraces Created traces by ants
* @param tourLengths The length of traces
*/
void AntSystem::updateTrails(std::map<int, std::vector<int>>& antTraces,
std::map<int, double>& tourLengths)
{
// First, evaporate all existing pheromone levels
for(auto& pair : edge2phero)
pair.second *= (1 - EVAPO_RATE);
// Then, increase pheromone level upon correct paths
for(auto& pair : edge2phero)
{
int edgeStart = pair.first.edgeStart;
int edgeEnd = pair.first.edgeEnd;
std::map<int, std::vector<int>>::iterator ait = antTraces.begin();
while(ait != antTraces.end())
{
// For every ant trace
std::vector<int> trace = (*ait).second;
if(trace.size() <= 1)
{
ait++;
continue;
}
// In case it's valid, add an amount of pheromone that depends on
// each tour length
for(unsigned int i = 0; i < trace.size() - 1; ++i)
if(trace.at(i) == edgeStart && trace.at(i + 1) == edgeEnd)
edge2phero[pair.first] += diffPheromone(tourLengths[(*ait).first]);
ait++;
}
}
}
/**
* Recursive method that finds a suitable trace from a starting
* point to a specific destination by unleashing an ant.
*
* @param start Path's starting point
* @param end Path's destination
* @param trace Container where path's nodes will be stored
*/
void AntSystem::goAnt(int start, int end, std::vector<int>& trace)
{
// Detect cycles and give up this attempt
if(isCyclic(start, trace))
{
trace.clear();
return;
}
// Destination reached
if(start == end && trace.size() > 0)
{
trace.push_back(start);
return;
}
// Get available physical neighbours
std::vector<int> neighs = availNeighbours(start);
double probs[neighs.size()];
int index = 0;
// Produce a transition probability to each one
for(int neigh : neighs)
probs[index++] = prob(start, neigh);
std::uniform_real_distribution<> distro(0, 1);
double value = distro(gen);
// Sort probabilities in range [0, 1] and use a uniform dice to
// pick up an index domain
index = 0; double sum = 0;
for(; index < (int)neighs.size(); ++index)
{
sum += probs[index];
if(value <= sum)
break;
}
// This index belongs to the chosen neighbour
int chosenNeighbour = (neighs.size() > 0) ? neighs[index] : -1;
if(chosenNeighbour == -1)
{
// No available neighbour found, so give up
trace.clear();
return;
}
// Recurse to the next neighbour
trace.push_back(start);
goAnt(chosenNeighbour, end, trace);
}
/**
* Calculates path's length.
*
* @param tour Container with path's nodes
* @return double Tour's length
*/
double AntSystem::calcTourLength(std::vector<int>& tour)
{
if(tour.size() <= 1)
return 0;
double weightSum = 0;
for(unsigned int i = 0; i < tour.size() - 1; ++i)
{
// Find the edge that starts with current trace node
auto it = std::find_if(edge2phero.cbegin(), edge2phero.cend(),
[&tour, i](std::pair<Edge, double> pair)
{
return pair.first.edgeStart == tour[i]
&& pair.first.edgeEnd == tour[i + 1];
});
if(it != edge2phero.cend())
weightSum += (*it).first.weight;
}
return weightSum;
}
/**
* Returns the probability of selecting the second node as destination, from
* the first one.
*
* @param edgeStart The edge's starting point
* @param edgeEnd The edge's end point
* @return double The probability
*/
double AntSystem::prob(int edgeStart, int edgeEnd)
{
double numerator = std::pow(pheromone(edgeStart, edgeEnd), A_PAR)
* std::pow(heuInfo(edgeStart, edgeEnd), B_PAR);
double denumerator = 0;
std::vector<int> neighs = availNeighbours(edgeStart);
for(int neigh : neighs)
denumerator += std::pow(pheromone(edgeStart, neigh), A_PAR)
* std::pow(heuInfo(edgeStart, neigh), B_PAR);
return numerator / denumerator;
}
/**
* Returns the 'amount' of heuristic information from first input node to
* the second.
*
* @param edgeStart The edge's starting point
* @param edgeEnd The edge's end point
* @return double The amount of heuristic information
*/
double AntSystem::heuInfo(int edgeStart, int edgeEnd)
{
// Find the edge with this lambda function and use its weight
auto it = std::find_if(edge2phero.cbegin(), edge2phero.cend(),
[edgeStart, edgeEnd](std::pair<Edge, double> pair)
{
return pair.first.edgeStart == edgeStart
&& pair.first.edgeEnd == edgeEnd;
});
return it != edge2phero.cend() ? 1 / (*it).first.weight : 0;
}
/**
* Returns the amount of pheromone from first input node to the second.
*
* @param edgeStart The edge's starting point
* @param edgeEnd The edge's end point
* @return double The amount of pheromone
*/
double AntSystem::pheromone(int edgeStart, int edgeEnd)
{
// Find the edge with this lambda function and return its pheromone level
auto it = std::find_if(edge2phero.cbegin(), edge2phero.cend(),
[edgeStart, edgeEnd](std::pair<Edge, double> pair)
{
return pair.first.edgeStart == edgeStart
&& pair.first.edgeEnd == edgeEnd;
});
return it != edge2phero.cend() ? (*it).second : 0;
}
/**
* Finds all available neighbours of input node.
*
* @param node The input node
* @return std::vector<int> Container with nodes
*/
std::vector<int> AntSystem::availNeighbours(int node)
{
std::vector<int> neighbours;
// Find all edges that start from the input node and return its
// other endpoints
std::for_each(edge2phero.cbegin(), edge2phero.cend(),
[&neighbours, node](std::pair<Edge, double> pair)
{
if(pair.first.edgeStart == node)
neighbours.push_back(pair.first.edgeEnd);
});
return neighbours;
}
/**
* Detects if a cycle is formed inside the sequence of nodes.
*
* @param nodes The sequence of nodes
* @return bool The indication of a cyclic sequence
*/
bool AntSystem::isCyclic(int nd, const std::vector<int>& nodes)
{
std::set<int> uniqueNodes;
uniqueNodes.insert(nd);
for(int node : nodes)
uniqueNodes.insert(node);
return nodes.size() + 1 != uniqueNodes.size();
}
/**
* Inserts an edge.
*
* @param src Source node
* @param dest Destination node
* @param weight Weight for the edge
*/
void AntSystem::insertEdge(int src, int dest, double weight)
{
AdaptiveSystem::insertEdge(src, dest, weight);
edge2phero.clear();
for(auto& edge : edges)
edge2phero.insert({edge, static_cast<double>(PHERO_QUANTITY)});
}