[fix] error handling
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user