-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtravelling-salesman-aco.py
More file actions
51 lines (47 loc) · 1.31 KB
/
Copy pathtravelling-salesman-aco.py
File metadata and controls
51 lines (47 loc) · 1.31 KB
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
from antsys import AntWorld, AntSystem
import numpy as np
import operator
def euclidean(p1, p2):
return [
np.sqrt(
np.add(
np.power(
np.subtract(
p1[-2],
p2[-2]
)
, 2
),
np.power(
np.subtract(
p1[-1],
p2[-1]
),
2
)
)
)
]
def cost(path):
return sum(map(operator.attrgetter('info'), path))
cities = [
('a', 20, 52),
('b', 43, 50),
('c', 20, 84),
('d', 70, 65),
('e', 29, 90),
('f', 87, 83),
('g', 83, 23)
]
n = len(cities)
distances = np.zeros((n, n))
for i in range(n):
for j in range(n):
distances[i, j] = euclidean(cities[i], cities[j])[0]
world = AntWorld(cities, euclidean, cost, lambda p, c: c.info)
optimizer = AntSystem(world=world, n_ants=100)
optimizer.optimize(max_iter=100, n_iter_no_change=20, verbose=False)
total_distance, best_path, _ = optimizer.g_best
print('Path:', ''.join(list(map(operator.itemgetter(0), best_path))))
print('Distance:', total_distance)
print(np.round(distances, 2))