-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutil.py
150 lines (125 loc) · 4.34 KB
/
util.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
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
import numpy as np
import matplotlib.pyplot as plt
import sqlite3
import json
from matplotlib.ticker import FormatStrFormatter
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
def export2json(filename, sol_best):
coord = []
for line in open(filename,"r").readlines():
x=line.strip("\r\n").split(",")
coord.append({'lat':x[0],'lng':x[1]})
export_data = []
for i in range(len(sol_best)):
export_data.append(coord[ int(sol_best[i]) ])
file = open("path.json", 'w')
file.write(json.dumps(export_data))
file.close()
def sum_distmat(p, distmat):
dist = 0
num_location = p.shape[0]
for i in range(num_location-1):
dist += distmat[p[i]][p[i+1]]
dist += distmat[p[0]][p[num_location-1]]
return dist
def get_distmat(p):
num_location = p.shape[0]
# 1 degree of lat/lon ~ 111km = 111000m in Taiwan
p *= 111000
distmat = np.zeros((num_location, num_location))
for i in range(num_location):
for j in range(i, num_location):
distmat[i][j] = distmat[j][i] = np.linalg.norm(p[i] - p[j])
return distmat
def swap(sol_new):
while True:
n1 = np.int(np.floor(np.random.uniform(0, sol_new.shape[0])))
n2 = np.int(np.floor(np.random.uniform(0, sol_new.shape[0])))
if n1 != n2:
break
sol_new[n1], sol_new[n2] = sol_new[n2], sol_new[n1]
return sol_new
def reverse(sol_new):
while True:
n1 = np.int(np.floor(np.random.uniform(0, sol_new.shape[0])))
n2 = np.int(np.floor(np.random.uniform(0, sol_new.shape[0])))
if n1 != n2:
break
sol_new[n1:n2] = sol_new[n1:n2][::-1]
return sol_new
def transpose(sol_new):
while True:
n1 = np.int(np.floor(np.random.uniform(0, sol_new.shape[0])))
n2 = np.int(np.floor(np.random.uniform(0, sol_new.shape[0])))
n3 = np.int(np.floor(np.random.uniform(0, sol_new.shape[0])))
if n1 != n2 != n3 != n1:
break
#Let n1 < n2 < n3
n1, n2, n3 = sorted([n1, n2, n3])
#Insert data between [n1,n2) after n3
tmplist = sol_new[n1:n2].copy()
sol_new[n1 : n1+n3-n2+1] = sol_new[n2 : n3+1].copy()
sol_new[n3-n2+1+n1 : n3+1] = tmplist.copy()
return sol_new
def accept(cost_new, cost_current, T):
# If new cost better than current, accept it
# If new cost not better than current, accept it by probability P(dE)
# P(dE) = exp(dE/(kT)), defined by Metropolis
return ( cost_new < cost_current or
np.random.rand() < np.exp(-(cost_new - cost_current) / T) )
def save_sqlite(payloads):
conn = sqlite3.connect('data/tsp.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS TSP (costs REAL, route TEXT, markov_step INTEGER) ")
c.execute('INSERT INTO TSP VALUES (?,?,?)' , payloads)
conn.commit()
conn.close()
def plot(path, points, costs):
'''
path: List of the different orders in which the nodes are visited
points: coordinates for the different nodes
costs: Cost of each iteration
'''
# Change figure size
plt.figure(figsize=(15,6))
'''
Plot Cost Function
'''
plt.subplot(121)
curve, = plt.plot(np.array(costs), label='Distance(m)')
plt.ylabel("Distance")
plt.xlabel("Iteration")
plt.grid(True)
plt.legend()
cost = str("%.2f" % round(costs[-1], 2))
plt.title("Final Distance: " + cost)
'''
Plot TSP Route
'''
plt.subplot(122)
# Transform back to longitude/latitude
points = (points / 111000).tolist()
# Unpack the primary path and transform it into a list of ordered coordinates
x = []; y = []
for i in path:
x.append(points[i][1])
y.append(points[i][0])
x.append(points[path[0]][1])
y.append(points[path[0]][0])
# Plot line
plt.plot(x, y, 'c-', label='Route')
# Plot dot
plt.plot(x, y, 'bo', label='Location')
# Avoid scientific notation
ax = plt.gca()
ax.xaxis.set_major_formatter(FormatStrFormatter('%.3f'))
ax.yaxis.set_major_formatter(FormatStrFormatter('%.3f'))
# Set axis too slightly larger than the set of x and y
plt.xlim(min(x)*0.99999, max(x)*1.00001)
plt.ylim(min(y)*0.99999, max(y)*1.00001)
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.title("TSP Route Visualization")
plt.grid(True)
plt.show()