[fix] error handling

This commit is contained in:
2021-01-07 22:52:00 +00:00
parent 0a10febfd7
commit ff8de454a6
18 changed files with 4581 additions and 4939 deletions

View File

@@ -19,6 +19,8 @@ def dist(node_0, node_1, coords):
def fitness(solution, coords):
N = len(coords)
if(len(solution) != N):
return math.inf
cur_fit = 0
for i in range(len(solution)):
cur_fit += dist(solution[i % N], solution[(i + 1) % N], coords)
@@ -64,6 +66,7 @@ def TSP(tsp_file, model, *args, max_it=1000, timeout=60):
nodes = load_data(tsp_file)
model = model()
# Set timeout
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(timeout)
@@ -80,27 +83,32 @@ def TSP(tsp_file, model, *args, max_it=1000, timeout=60):
log("Timeout -3")
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
outfile.write("-3")
best_solution = [-1] * len(nodes)
elif (len(best_solution) == 0):
print(exec)
log("No Answer -1")
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
outfile.write("-1")
else:
print(exc)
log("Unkown -4")
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
outfile.write("-4")
best_solution = [-1] * len(nodes)
return best_solution, fitness_list
signal.alarm(0)
# Collect results
if(len(fitness_list) > 0):
log("[Node] " + str(len(best_solution)) + ", [Best] " + str(fitness_list[fitness_list.index(min(fitness_list))]))
if (len(best_solution) == 0):
log("No Answer -1")
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
outfile.write("-1")
elif (len(best_solution) != len(nodes)):
if (len(best_solution) != len(nodes)):
print(len(best_solution))
log("Invalid -2")
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
outfile.write("-2")
best_solution = [-1] * len(nodes)
else:
# log("Writing the best solution to file" )
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile: