Change TSP to search closest neighbors. Make lines for plotting thinner and plot neighbor connections.

This commit is contained in:
Felix Martin 2020-01-31 14:30:24 -05:00
parent fb2953bc6f
commit fb4005b74f
4 changed files with 38 additions and 19 deletions

View File

@ -9,6 +9,7 @@ class Map(object):
def __init__(self, n_clusters):
self.CLUSTERS_X = n_clusters
self.plot_n = 0
def calc_corners(self, points):
x_min, x_max = float("inf"), float("-inf")
@ -118,11 +119,15 @@ class Map(object):
except ModuleNotFoundError:
return
plt.figure(dpi=300)
def plot_arrows():
for i in range(len_points):
p1 = points[i - 1]
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):
x = p1.x
@ -130,7 +135,7 @@ class Map(object):
dx = p2.x - x
dy = p2.y - y
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)
def plot_points():
@ -138,11 +143,19 @@ class Map(object):
plt.plot(p.x, p.y, '')
# plt.text(p.x, p.y, ' ' + str(p))
for nb, _ in p.neighbors:
# plt.plot([p.x, nb.x], [p.y, nb.y], 'r--')
pass
plt.plot([p.x, nb.x], [p.y, nb.y], 'b--',
linewidth=0.2)
len_points = len(points)
plot_points()
self.plot_grid(plt)
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

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 KiB

View File

@ -174,9 +174,9 @@ def local_search_k_opt(route, goal, m):
no_improvement_iterations = 0
no_improvement_iterations += 1
if no_improvement_iterations > 10:
if no_improvement_iterations > 3:
break
# print("[random k-opt] current_total={}".format(current_total))
print("[random k-opt] current_total={}".format(current_total))
while True:
point = choice(route.points)
try:
@ -189,7 +189,7 @@ def local_search_k_opt(route, goal, m):
if current_total < goal:
return
#
class Route(object):
def __init__(self, points):
@ -322,6 +322,13 @@ class Route(object):
for i, p in enumerate(self.points):
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):
r = Route(points)
@ -334,9 +341,6 @@ def solve_tsp(points):
def solve_it(input_data):
r = Route(parse_input_data(input_data))
m = ClusterMap(4)
m.cluster(r.points)
goal = {51: 429, # 4
100: 20800, # 4
200: 30000, # 8
@ -345,16 +349,18 @@ def solve_it(input_data):
1889: 378069,
33810: 78478868,
}[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)
m.plot(r.points, plt)
r.verify_total_distance()
return prepare_output_data(r.points)
def solve_it_(input_data):
def solve_it_precomputed(input_data):
r = Route(parse_input_data(input_data))
n = len(r.points)
if n == 51:
@ -379,9 +385,9 @@ def solve_it_(input_data):
if __name__ == "__main__":
# file_location = "data/tsp_6_1"
file_location = "data/tsp_51_1"
# file_location = "data/tsp_100_3"
# file_location = "data/tsp_200_2"
# file_location = "data/tsp_574_1"
file_location = "data/tsp_100_3"
file_location = "data/tsp_200_2"
file_location = "data/tsp_574_1"
# file_location = "data/tsp_1889_1"
# file_location = "data/tsp_33810_1"
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