com2014-template/model/base_model.py

36 lines
1.0 KiB
Python

import math
class Model:
def __init__(self):
self.best_solution = []
self.best_fitness = float("Inf")
self.fitness_list = []
def init(self, coords):
self.coords = coords
self.N = len(coords)
self.nodes = [i for i in range(self.N)]
def dist(self, node_0, node_1):
"""
Euclidean distance between two nodes.
"""
coord_0, coord_1 = self.coords[node_0], self.coords[node_1]
return math.sqrt((coord_0[0] - coord_1[0]) ** 2 + (coord_0[1] - coord_1[1]) ** 2)
def fitness(self, solution):
"""
Total distance of the current solution path.
"""
cur_fit = 0
for i in range(self.N):
cur_fit += self.dist(solution[i % self.N], solution[(i + 1) % self.N])
return cur_fit
def fit(self):
raise NotImplementedError("Your fitting method not implemented yet")
def log(self, message):
print('[{name}] {msg}'.format(name=self.__class__.__name__, msg=message))