Change TSP to search closest neighbors. Make lines for plotting thinner and plot neighbor connections.
This commit is contained in:
27
tsp/map.py
27
tsp/map.py
@@ -9,6 +9,7 @@ class Map(object):
|
|||||||
|
|
||||||
def __init__(self, n_clusters):
|
def __init__(self, n_clusters):
|
||||||
self.CLUSTERS_X = n_clusters
|
self.CLUSTERS_X = n_clusters
|
||||||
|
self.plot_n = 0
|
||||||
|
|
||||||
def calc_corners(self, points):
|
def calc_corners(self, points):
|
||||||
x_min, x_max = float("inf"), float("-inf")
|
x_min, x_max = float("inf"), float("-inf")
|
||||||
@@ -118,11 +119,15 @@ class Map(object):
|
|||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
plt.figure(dpi=300)
|
||||||
|
|
||||||
def plot_arrows():
|
def plot_arrows():
|
||||||
for i in range(len_points):
|
for i in range(len_points):
|
||||||
p1 = points[i - 1]
|
p1 = points[i - 1]
|
||||||
p2 = points[i]
|
p2 = points[i]
|
||||||
plot_arrow(p1, p2)
|
plt.plot([p1.x, p2.x], [p1.y, p2.y], 'r',
|
||||||
|
linewidth=0.8)
|
||||||
|
# plot_arrow(p1, p2)
|
||||||
|
|
||||||
def plot_arrow(p1, p2):
|
def plot_arrow(p1, p2):
|
||||||
x = p1.x
|
x = p1.x
|
||||||
@@ -130,7 +135,7 @@ class Map(object):
|
|||||||
dx = p2.x - x
|
dx = p2.x - x
|
||||||
dy = p2.y - y
|
dy = p2.y - y
|
||||||
opt = {'head_width': 0.4, 'head_length': 0.4, 'width': 0.05,
|
opt = {'head_width': 0.4, 'head_length': 0.4, 'width': 0.05,
|
||||||
'length_includes_head': True}
|
'linewidth': 0.4, 'length_includes_head': True}
|
||||||
plt.arrow(x, y, dx, dy, **opt)
|
plt.arrow(x, y, dx, dy, **opt)
|
||||||
|
|
||||||
def plot_points():
|
def plot_points():
|
||||||
@@ -138,11 +143,19 @@ class Map(object):
|
|||||||
plt.plot(p.x, p.y, '')
|
plt.plot(p.x, p.y, '')
|
||||||
# plt.text(p.x, p.y, ' ' + str(p))
|
# plt.text(p.x, p.y, ' ' + str(p))
|
||||||
for nb, _ in p.neighbors:
|
for nb, _ in p.neighbors:
|
||||||
# plt.plot([p.x, nb.x], [p.y, nb.y], 'r--')
|
plt.plot([p.x, nb.x], [p.y, nb.y], 'b--',
|
||||||
pass
|
linewidth=0.2)
|
||||||
|
|
||||||
len_points = len(points)
|
len_points = len(points)
|
||||||
plot_points()
|
|
||||||
self.plot_grid(plt)
|
|
||||||
plot_arrows()
|
plot_arrows()
|
||||||
plt.show()
|
plot_points()
|
||||||
|
try:
|
||||||
|
self.plot_grid(plt)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
plt.axis('off')
|
||||||
|
fig_file = "plots/step_{}.png".format(self.plot_n)
|
||||||
|
plt.savefig(fig_file, bbox_inches='tight')
|
||||||
|
self.plot_n += 1
|
||||||
|
|
||||||
|
|||||||
BIN
tsp/plots/step_0.png
Normal file
BIN
tsp/plots/step_0.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 711 KiB |
30
tsp/tsp.py
30
tsp/tsp.py
@@ -174,9 +174,9 @@ def local_search_k_opt(route, goal, m):
|
|||||||
no_improvement_iterations = 0
|
no_improvement_iterations = 0
|
||||||
|
|
||||||
no_improvement_iterations += 1
|
no_improvement_iterations += 1
|
||||||
if no_improvement_iterations > 10:
|
if no_improvement_iterations > 3:
|
||||||
break
|
break
|
||||||
# print("[random k-opt] current_total={}".format(current_total))
|
print("[random k-opt] current_total={}".format(current_total))
|
||||||
while True:
|
while True:
|
||||||
point = choice(route.points)
|
point = choice(route.points)
|
||||||
try:
|
try:
|
||||||
@@ -189,7 +189,7 @@ def local_search_k_opt(route, goal, m):
|
|||||||
|
|
||||||
if current_total < goal:
|
if current_total < goal:
|
||||||
return
|
return
|
||||||
#
|
|
||||||
|
|
||||||
class Route(object):
|
class Route(object):
|
||||||
def __init__(self, points):
|
def __init__(self, points):
|
||||||
@@ -322,6 +322,13 @@ class Route(object):
|
|||||||
for i, p in enumerate(self.points):
|
for i, p in enumerate(self.points):
|
||||||
p.index = i
|
p.index = i
|
||||||
|
|
||||||
|
def calculate_neighbors(self, n=3):
|
||||||
|
for p in self.points:
|
||||||
|
def d(other_point):
|
||||||
|
return distance(p, other_point)
|
||||||
|
ps = sorted(self.points, key=d)[1:n + 1]
|
||||||
|
p.add_neighbors(ps)
|
||||||
|
|
||||||
|
|
||||||
def solve_tsp(points):
|
def solve_tsp(points):
|
||||||
r = Route(points)
|
r = Route(points)
|
||||||
@@ -334,9 +341,6 @@ def solve_tsp(points):
|
|||||||
|
|
||||||
def solve_it(input_data):
|
def solve_it(input_data):
|
||||||
r = Route(parse_input_data(input_data))
|
r = Route(parse_input_data(input_data))
|
||||||
m = ClusterMap(4)
|
|
||||||
m.cluster(r.points)
|
|
||||||
|
|
||||||
goal = {51: 429, # 4
|
goal = {51: 429, # 4
|
||||||
100: 20800, # 4
|
100: 20800, # 4
|
||||||
200: 30000, # 8
|
200: 30000, # 8
|
||||||
@@ -345,16 +349,18 @@ def solve_it(input_data):
|
|||||||
1889: 378069,
|
1889: 378069,
|
||||||
33810: 78478868,
|
33810: 78478868,
|
||||||
}[r.len_points]
|
}[r.len_points]
|
||||||
|
m = ClusterMap(4)
|
||||||
|
r.calculate_neighbors(8)
|
||||||
|
# m.cluster(r.points)
|
||||||
|
# r.route_from_clusters(m)
|
||||||
|
|
||||||
r.route_from_clusters(m)
|
|
||||||
local_search_k_opt(r, goal, m)
|
local_search_k_opt(r, goal, m)
|
||||||
m.plot(r.points, plt)
|
m.plot(r.points, plt)
|
||||||
|
|
||||||
r.verify_total_distance()
|
r.verify_total_distance()
|
||||||
return prepare_output_data(r.points)
|
return prepare_output_data(r.points)
|
||||||
|
|
||||||
|
|
||||||
def solve_it_(input_data):
|
def solve_it_precomputed(input_data):
|
||||||
r = Route(parse_input_data(input_data))
|
r = Route(parse_input_data(input_data))
|
||||||
n = len(r.points)
|
n = len(r.points)
|
||||||
if n == 51:
|
if n == 51:
|
||||||
@@ -379,9 +385,9 @@ def solve_it_(input_data):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# file_location = "data/tsp_6_1"
|
# file_location = "data/tsp_6_1"
|
||||||
file_location = "data/tsp_51_1"
|
file_location = "data/tsp_51_1"
|
||||||
# file_location = "data/tsp_100_3"
|
file_location = "data/tsp_100_3"
|
||||||
# file_location = "data/tsp_200_2"
|
file_location = "data/tsp_200_2"
|
||||||
# file_location = "data/tsp_574_1"
|
file_location = "data/tsp_574_1"
|
||||||
# file_location = "data/tsp_1889_1"
|
# file_location = "data/tsp_1889_1"
|
||||||
# file_location = "data/tsp_33810_1"
|
# file_location = "data/tsp_33810_1"
|
||||||
with open(file_location, 'r') as input_data_file:
|
with open(file_location, 'r') as input_data_file:
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 672 KiB After Width: | Height: | Size: 275 KiB |
Reference in New Issue
Block a user