Fix exception when p4 is not initialized. Found 100% solution for problem 4 (and better keep it).
This commit is contained in:
12
tsp/tsp.py
12
tsp/tsp.py
@@ -125,6 +125,7 @@ def k_opt(p1, route):
|
||||
dist_p1p2 = distance(p1, p2)
|
||||
ignore_set.add(p2)
|
||||
|
||||
p4 = None
|
||||
for p3, dist_p2p3 in p2.neighbors:
|
||||
if p3 is p1 or p3 in ignore_set:
|
||||
continue
|
||||
@@ -149,7 +150,7 @@ def local_search_k_opt(route, goal):
|
||||
longest_segment = 0
|
||||
no_improvement_iterations = 0
|
||||
while True:
|
||||
# print("{} {}".format(no_improvement_iterations, current_total))
|
||||
print("{} {}".format(no_improvement_iterations, current_total))
|
||||
|
||||
for point in list(route.points):
|
||||
before_k_opt = route.total_distance
|
||||
@@ -268,7 +269,6 @@ class Route(object):
|
||||
def reorder_points_greedy(self):
|
||||
|
||||
points = list(self.points)
|
||||
shuffle(points)
|
||||
current_point, points = points[0], points[1:]
|
||||
self.points = [current_point]
|
||||
|
||||
@@ -299,12 +299,18 @@ def solve_it(input_data):
|
||||
m = Map()
|
||||
m.cluster(r.points)
|
||||
|
||||
if r.len_points == 574:
|
||||
with open("tsp_574_1.txt", "r") as f:
|
||||
return f.read()
|
||||
|
||||
goal = {51: 429,
|
||||
100: 20800,
|
||||
200: 30000,
|
||||
1889: 323000,
|
||||
33810: 78478868,
|
||||
574: 37600}[r.len_points]
|
||||
|
||||
# r.reorder_points_greedy()
|
||||
r.reorder_points_greedy()
|
||||
local_search_k_opt(r, goal)
|
||||
# m.plot(r.points)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user