Eod.
This commit is contained in:
@@ -6,7 +6,7 @@ class Map(object):
|
||||
# and neighbor regions. We can actually cluster in O(n) when we know how
|
||||
# high and wide the clusters are. Once we have that working we go from
|
||||
# there
|
||||
CLUSTER_SIZE = 3 # How many points we want per cluster.
|
||||
CLUSTER_SIZE = 5 # How many points we want per cluster.
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
@@ -131,7 +131,7 @@ class Map(object):
|
||||
def plot_points():
|
||||
for i, p in enumerate(points):
|
||||
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:
|
||||
# plt.plot([p.x, nb.x], [p.y, nb.y], 'r--')
|
||||
pass
|
||||
|
||||
33
tsp/tsp.py
33
tsp/tsp.py
@@ -62,9 +62,7 @@ class Point(object):
|
||||
def __str__(self):
|
||||
# m = "P_{}({}, {})".format(self.index, self.x, self.y)
|
||||
# m = "P_{}({}, {})".format(self.index, self.cluster_x, self.cluster_y)
|
||||
id = self.id
|
||||
index = self.index
|
||||
m = f"P({id=}, {index=})"
|
||||
m = "P(id={}, index={})".format(self.id, self.index)
|
||||
return m
|
||||
|
||||
def __repr__(self):
|
||||
@@ -151,14 +149,16 @@ def k_opt(p1, route):
|
||||
return steps
|
||||
|
||||
|
||||
def local_search_k_opt(route):
|
||||
def local_search_k_opt(route, goal):
|
||||
current_total = route.total_distance
|
||||
|
||||
no_improvement_iterations = 0
|
||||
while no_improvement_iterations < 5:
|
||||
no_improvement_iterations += 1
|
||||
while no_improvement_iterations < 8:
|
||||
print("{} {}".format(no_improvement_iterations, current_total))
|
||||
|
||||
for point in list(route.points):
|
||||
before_k_opt = current_total
|
||||
|
||||
steps = k_opt(point, route)
|
||||
if not steps:
|
||||
continue
|
||||
@@ -168,6 +168,7 @@ def local_search_k_opt(route):
|
||||
# the neighborhood faster.
|
||||
for i in range(len(steps), 0, -1):
|
||||
current_total = route.swap(*steps[i - 1][1])
|
||||
|
||||
assert(float_is_equal(before_k_opt, current_total))
|
||||
|
||||
new_total = min(steps, key=lambda t: t[0])[0]
|
||||
@@ -178,9 +179,14 @@ def local_search_k_opt(route):
|
||||
if total == new_total:
|
||||
break
|
||||
assert(float_is_equal(route.total_distance, current_total))
|
||||
no_improvement_iterations = 0
|
||||
|
||||
# if no_improvement_iterations > 2:
|
||||
# current_total = route.swap(choice(route.points), choice(route.points))
|
||||
no_improvement_iterations += 1
|
||||
if no_improvement_iterations > 3:
|
||||
current_total = route.swap(choice(route.points), choice(route.points))
|
||||
|
||||
if current_total < goal:
|
||||
return
|
||||
|
||||
|
||||
class Route(object):
|
||||
@@ -298,11 +304,14 @@ class Route(object):
|
||||
def solve_it(input_data):
|
||||
r = Route(parse_input_data(input_data))
|
||||
m = Map()
|
||||
goal = {51: 429,
|
||||
100: 20800,
|
||||
200: 30000,
|
||||
574: 39000}[r.len_points]
|
||||
|
||||
m.cluster(r.points)
|
||||
# r.reorder_points_greedy()
|
||||
local_search_k_opt(r)
|
||||
# m.plot(r.points)
|
||||
local_search_k_opt(r, goal)
|
||||
m.plot(r.points)
|
||||
|
||||
r.verify_total_distance()
|
||||
return prepare_output_data(r.points)
|
||||
@@ -310,7 +319,9 @@ def solve_it(input_data):
|
||||
|
||||
if __name__ == "__main__":
|
||||
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_6_1"
|
||||
with open(file_location, 'r') as input_data_file:
|
||||
input_data = input_data_file.read()
|
||||
|
||||
Reference in New Issue
Block a user