[fix] error handling
This commit is contained in:
1
template/.gitignore
vendored
1
template/.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
.vscode/
|
||||
__pycache__/
|
||||
.ipynb_checkpoints/
|
||||
|
||||
@@ -4,4 +4,5 @@ ADD ./ /tsp
|
||||
|
||||
WORKDIR /tsp
|
||||
|
||||
CMD ["/bin/bash", "-c", "pip install -r requirements.txt && python main.py; mkdir -p /output && cp -r output/* /output"]
|
||||
RUN pip install -r requirements.txt
|
||||
CMD ["/bin/bash", "-c", "python main.py; mkdir -p /output && cp -r output/* /output"]
|
||||
|
||||
@@ -7,5 +7,4 @@ from utils.tsp import TSP_Bench_ALL
|
||||
from model.my_model import MyModel
|
||||
|
||||
if __name__ == "__main__":
|
||||
model = MyModel()
|
||||
TSP_Bench_ALL("./data", model)
|
||||
TSP_Bench_ALL("./data", MyModel)
|
||||
|
||||
@@ -23,6 +23,8 @@ class Model:
|
||||
"""
|
||||
Total distance of the current solution path.
|
||||
"""
|
||||
if(len(solution) != self.N):
|
||||
return math.inf
|
||||
cur_fit = 0
|
||||
for i in range(self.N):
|
||||
cur_fit += self.dist(solution[i % self.N], solution[(i + 1) % self.N])
|
||||
|
||||
@@ -1,22 +1,27 @@
|
||||
import math
|
||||
import random
|
||||
from model.base_model import Model
|
||||
import numpy as np
|
||||
|
||||
class MyModel(Model):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def init(self, coords):
|
||||
def init(self, nodes):
|
||||
"""
|
||||
Put your initialization here.
|
||||
"""
|
||||
super().init(coords)
|
||||
self.log("Nothing to initialize in your model now")
|
||||
super().init(nodes)
|
||||
|
||||
def fit(self, max_it=1000, visualize=False):
|
||||
def fit(self, max_it):
|
||||
"""
|
||||
Put your iteration process here.
|
||||
"""
|
||||
self.log("Nothing happens in your model now")
|
||||
random_solutions = []
|
||||
for i in range(0, max_it):
|
||||
solution = np.random.permutation(self.N).tolist()
|
||||
random_solutions.append(solution)
|
||||
self.fitness_list.append(self.fitness(solution))
|
||||
|
||||
return self.best_solution, self.fitness_list
|
||||
self.best_solution = random_solutions[self.fitness_list.index(min(self.fitness_list))]
|
||||
return self.best_solution, self.fitness_list
|
||||
@@ -0,0 +1 @@
|
||||
numpy
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user