-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
33 lines (27 loc) · 1.05 KB
/
main.py
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
import math
from AntColonyOptimization import AntColonyOptimization, Graph
from plot import plot
def distance(landmark1: dict, landmark2: dict):
return math.sqrt((landmark1['x'] - landmark2['x']) ** 2 + (landmark1['y'] - landmark2['y']) ** 2)
def main():
landmarks = []
points = []
with open('./data/chn31.txt') as f:
for line in f.readlines():
landmark = line.split(' ')
landmarks.append(dict(index=int(landmark[0]), x=int(landmark[1]), y=int(landmark[2])))
points.append((int(landmark[1]), int(landmark[2])))
cost_matrix = []
rank = len(landmarks)
for i in range(rank):
row = []
for j in range(rank):
row.append(distance(landmarks[i], landmarks[j]))
cost_matrix.append(row)
AntColonyOptimization = AntColonyOptimization(10, 100, 1.0, 10.0, 0.5, 10, 2)
graph = Graph(cost_matrix, rank)
path, cost = AntColonyOptimization.solve(graph)
print('cost: {}, path: {}'.format(cost, path))
plot(points, path)
if __name__ == '__main__':
main()