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