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):
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user