import math import random from model.base_model import Model class MyDPBModel(Model): def __init__(self): super().__init__() def init(self, nodes): """ Put your initialization here. """ super().init(nodes) def getMST(self, node): MST = [] distances = [] for i in range(0, self.N): if i != node: MST.append(i) distances.append(self.dist(node, i)) return [x for _,x in sorted(zip(distances, MST))] def fit(self, max_it=1000): """ Put your iteration process here. """ MST_solutions = [] for i in range(0, self.N): solution = [i] MST_solutions.append(solution) MSTs = [] for i in range(0, self.N): MSTs.append([-1] * self.N) # Breadth First: Set each city as starting point, then go to next city simultaneously for step in range(0, self.N - 1): # print("[step]", step) unvisited_list = list(range(0, self.N)) # For each search path for i in range(0, self.N): cur_city = MST_solutions[i][-1] unvisited_list = list( set(range(0, self.N)) - set(MST_solutions[i]) ) if MSTs[cur_city][0] == -1: MST = self.getMST(cur_city) MSTs[cur_city] = MST for j in MSTs[cur_city]: if(j in unvisited_list): MST_solutions[i].append(j) break for i in range(0, self.N): self.fitness_list.append(self.fitness(MST_solutions[i])) self.best_solution = MST_solutions[ self.fitness_list.index(min(self.fitness_list)) ] self.fitness_list.append(self.fitness(self.best_solution)) return self.best_solution, self.fitness_list