-
Notifications
You must be signed in to change notification settings - Fork 9
/
show.py
65 lines (57 loc) · 1.67 KB
/
show.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
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
# Libraries
import turtle
import numpy as np
from aco_tsp import SolveTSPUsingACO
from sklearn.preprocessing import MinMaxScaler
from time import sleep
# Reading location dataset
# Filename - location_ll.txt
location = []
lats = []
lons = []
f = open('tsp dataset/location_ll.txt', 'r')
for line in f.readlines():
lat_lon = tuple(map(float, line.split('\t')))
location.append(list(map(float, line.split('\t'))))
location = np.array(location)
# Scaling dataset
scaler = MinMaxScaler(feature_range=(-90, 175))
location[:, 1] = scaler.fit_transform(location[:, 1].reshape(-1, 1)).reshape(1, -1)
scaler = MinMaxScaler(feature_range=(-80, 75))
location[:, 0] = scaler.fit_transform(location[:, 0].reshape(-1, 1)).reshape(1, -1)
# Main
if __name__ == '__main__':
# Parameters
_colony_size = 15
_steps = 50
_nodes = location
# Model
acs = SolveTSPUsingACO(mode='ACS', colony_size=_colony_size, steps=_steps, nodes=_nodes)
time, dist = acs.run()
route = acs.global_best_tour
print('Runtime: ', time, 's')
print('Minimum distance: ', dist)
screen = turtle.Screen()
screen.setup(1253, 755)
screen.setworldcoordinates(-180, -90, 180, 90)
screen.bgpic('./assets/map.png')
loc = turtle.Turtle()
# sleep(10)
loc.shape('circle')
loc.color('green')
loc.penup()
start = _nodes[route[0]]
loc.goto(*start[::-1])
loc.dot(30)
loc.color('blue')
loc.pendown()
for idx in route[1:len(route)-1]:
[lat, lon] = _nodes[idx]
loc.goto(lon, lat)
loc.dot(5)
end = _nodes[route[len(route)-1]]
loc.goto(*end[::-1])
loc.color('red')
loc.dot(30)
loc.hideturtle()
screen.exitonclick()