-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantColony.hpp
454 lines (221 loc) · 9.64 KB
/
antColony.hpp
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "Graph.hpp"
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <map>
//Calculate the cost.
float cost(vector<int> tour,float** mc);
//Generate a tour
std::vector<int>generate_solution(float** pheromone_matrix,float** cost_matrix,
float heuristic_coefficient,float history_coefficient ,float greediness_factor, int n_cities);
//Generate the set of candidates in wich will be chosen the next city
std::map<int,float>generate_candidates(int origin,bool* visited,float** pheromone_matrix,float**cost_matrix,
float heuristic_coefficient, float history_coefficient, int n_cities);
//Upate of the pheromone_matrix per ant.
void update_pheromone_matrix_local(float ** pheromone_matrix,vector<int>local_tour,
float history_coefficient, float local_pheromone,float decay_factor);
//Update to reinforce the best solution(Elitist.)
void update_pheromone_matrix_global(float ** pheromone_matrix,
vector<int>local_tour,float history_coefficient,float decay_factor, float best_cost);
//Main algorithm of ACO
float ant_colony(const Graph& map, int max_it,int num_ants,
float decay_factor, float heuristic_coefficient,
float history_coefficient,float greediness_factor,bool elitist);
float cost(vector<int> tour, float** mc)
{
vector<pair<int,int>> edges{};// tour solution
int n_cities = tour.size();
for(int i = 0 ; i < n_cities; i++)
edges.push_back(pair<int,int>(tour[i],tour[i+1]));
float cost = 0;
for(auto e:edges)
{
// std::cout<<mc[e.first-1][e.second-1]<<std::endl;
cost+= mc[e.first-1][e.second-1];
}
//the cycle
cost+= mc[ tour[n_cities-1] -1 ][ tour[0] -1 ];
return cost;
}
std::vector<int>generate_solution(float** pheromone_matrix,float** cost_matrix,
float heuristic_coefficient,float history_coefficient ,float greediness_factor, int n_cities)
{
vector<int> local_tour;
bool visited [n_cities];
for (size_t i = 0; i < n_cities; i++)
visited[i] = false;
int origin_end = rand()%n_cities + 1; // generate de oring_end city randomly 1...n_cities
local_tour.push_back(origin_end);
visited[origin_end-1] = true;
do
{
map<int,float>candidates = generate_candidates(origin_end-1,visited,pheromone_matrix,cost_matrix,
heuristic_coefficient, history_coefficient,n_cities);
float prob = ((double) rand() / (RAND_MAX));
//selection function
if(prob< greediness_factor) // common 0.9, the randomization and skip second calculation
{
pair<int,float> city;
city.first =0; city.second =0;
for(auto a:candidates)
{
if(a.second> city.second)
{
city.first =a.first;
city.second = a.second;
}
}
int next_city = city.first;
local_tour.push_back(next_city);
visited[next_city-1]= true;
}
else
{
float sumatory = 0;
map<int,float>::iterator it = candidates.begin();
for(auto city:candidates)
sumatory += city.second;
if(sumatory != 0)
{
float p_candidate = 0;
prob = ((float) rand() / (RAND_MAX));
do
{
prob -= it->second/sumatory;
it++;
} while (it!= candidates.end() && prob < 0.0);
it--;
local_tour.push_back(it->first);
visited[it->first-1]= true;
}
else// if else is equal to 0 chose randomly
{
map<int,float>::iterator it= candidates.begin();
int city = rand()%candidates.size();
for (size_t i = 0; i < city; i++)
it++;
it--;
local_tour.push_back((it)->first);
visited[it->first-1]= true;
}
}
//end selection function
}while (local_tour.size() < n_cities);
return local_tour;
}
std::map<int,float>generate_candidates(int origin,bool* visited,float** pheromone_matrix,float**cost_matrix,
float heuristic_coefficient, float history_coefficient, int n_cities)
{
float local_history = 0, local_distance = 0, local_heuristic = 0, partial_cost = 0;
std::map<int,float> candidates{};
for(int i = 0 ; i < n_cities; i++)
{
if(!visited[i])
{
local_history = pow( pheromone_matrix[origin][i], history_coefficient);
local_heuristic = pow( (1.0/cost_matrix[origin][i]) ,heuristic_coefficient);
partial_cost = local_heuristic * local_history;
candidates.insert(make_pair(i+1,partial_cost));
}
}
return candidates;
}
void update_pheromone_matrix_local(float ** pheromone_matrix,vector<int>local_tour,
float history_coefficient, float local_pheromone,float decay_factor)
{
vector<pair<int,int>> edges{};// tour solution
float pheromore_value = 0.0;
int n_cities = local_tour.size();
for(int i =0 ; i<local_tour.size()-1; i++)
edges.push_back(pair<int,int>(local_tour[i],local_tour[i+1]));
int i = 0;
for(auto e:edges)
{
pheromore_value = ( (1.0-decay_factor)*pheromone_matrix[e.first-1][e.second-1])+
(history_coefficient * local_pheromone);
// pheromore_value = (history_coefficient * local_pheromone);
pheromone_matrix[e.first-1][e.second-1] = pheromore_value;
pheromone_matrix[e.second-1][e.first-1] = pheromore_value;
i++;
}
pheromore_value = ( (1.0-decay_factor)*pheromone_matrix[ local_tour[n_cities-1]-1 ][local_tour[0]-1])
+(history_coefficient * local_pheromone);
pheromone_matrix[local_tour[n_cities-1]-1][ local_tour[0]-1] = pheromore_value;
pheromone_matrix[local_tour[0]-1][local_tour[n_cities-1]-1] = pheromore_value;
}
void update_pheromone_matrix_global(float ** pheromone_matrix,vector<int>best_tour,
float history_coefficient,float decay_factor, float best_cost)
{
vector<pair<int,int>> edges;// tour solution
float pheromore_value = 0.0;
int n_cities = best_tour.size();
for(int i =0 ; i<best_tour.size()-1; i++)
edges.push_back(pair<int,int>(best_tour[i],best_tour[i+1]));
for(auto e:edges)
{
pheromore_value = ( (1.0-decay_factor)*pheromone_matrix[e.first-1][e.second-1])
+(history_coefficient*(1.0/best_cost));
pheromone_matrix[e.first-1][e.second-1] = pheromore_value;
pheromone_matrix[e.second-1][e.first-1] = pheromore_value;
}
pheromore_value = ( (1.0-decay_factor)*pheromone_matrix[ best_tour[n_cities-1]-1 ][best_tour[0]-1])
+(history_coefficient*(best_cost));
pheromone_matrix[best_tour[n_cities-1]-1][best_tour[0]-1] = pheromore_value;
pheromone_matrix[best_tour[0]-1][best_tour[n_cities-1]-1] = pheromore_value;
}
float ant_colony(const Graph& map, int max_it,int num_ants,
float decay_factor, float heuristic_coefficient,
float history_coefficient,float greediness_factor,bool elitist)
{
srand(1);
// geneterate a initial tour solution
int n_cities = map.num_vertices();
float** pheromone_matrix = new float* [n_cities];
for(int i = 0; i < n_cities; i++)
{
pheromone_matrix[i] = new float[n_cities];
}
std::vector<int>best_tour{};
for(size_t i = 0; i < n_cities; i++)
best_tour.push_back(i+1);
std::random_shuffle(best_tour.begin(),best_tour.end());
//best_cost = initial cost
float best_cost = cost(best_tour,map.adMat());
std::cout<<"Iteracion: 0 Best : "<<best_cost<<endl;
//get value of pheromone
float local_pheromone = 1.0/(n_cities*best_cost);
//initialize pheromone matrix
for(size_t i = 0; i < n_cities; i++)
{
for (size_t j = 0; j < n_cities; j++)
{
pheromone_matrix[i][j] = local_pheromone;
}
}
std::vector<int> local_tour{};
float local_cost = 0;
for (size_t i = 0; i < max_it; i++)
{
for (size_t j = 0; j < num_ants; j++)// ants
{
//genetare a local solution. // lo suyo seria solo pasarle la matriz ni la euristica ni nada.
local_tour = generate_solution(pheromone_matrix,map.adMat(),
heuristic_coefficient,history_coefficient,greediness_factor,n_cities);
//calculate cost
local_cost = cost(local_tour,map.adMat());
// if improvement, update best_cost and tour
if(local_cost<best_cost)
{
best_cost = local_cost;
best_tour = local_tour;
}
// update pheromone_matrix
update_pheromone_matrix_local(pheromone_matrix,local_tour,history_coefficient, local_pheromone,decay_factor);
}
//update global de la matriz feromonica ( with the best solution and tour) Elitist version
if(elitist)
update_pheromone_matrix_global(pheromone_matrix,best_tour,history_coefficient,decay_factor,best_cost);
std::cout<<"Iteration: "<<i<<" Best : "<<best_cost<<endl;
}
return best_cost;
}