def mutate(self, one): one = one.copy() l = [t for t in range(self.num_city)] t = np.random.choice(l, 2) x, y = min(t), max(t) one[x], one[y] = one[y], one[x] l2 = cpl.compute_pathlen(one, self.dis_mat) return one, l2
def cross(self, cur, best): one = cur.copy() l = [t for t in range(self.num_city)] t = np.random.choice(l, 2) x = min(t) y = max(t) cross_part = best[x:y] tmp = [] for t in one: if t in cross_part: continue tmp.append(t) # two methods one = tmp + cross_part l1 = cpl.compute_pathlen(one, self.dis_mat) one2 = cross_part + tmp l2 = cpl.compute_pathlen(one2, self.dis_mat) if l1 < l2: return one, l1 else: return one, l2
def compute_paths(paths, dis_mat): result = [] for one in paths: length = cpl.compute_pathlen(one, dis_mat) result.append(length) return result