def crossover(self, route_1, route_2): child = Route(self.cities) start = np.random.randint(len(route_1)) if start < len(route_1) - 1: end = np.random.randint(start + 1,len(route_2)) s = end - start k = 0 while 0 < (end - start): random_city = route_1.get_city(start) child.assign_city(k,random_city) start += 1 k += 1 for i in range(len(route_2)): random_city = route_2.get_city(i) if random_city not in child.route: child.assign_city(s,random_city) s += 1 else: k = 1 random_city = route_1.get_city(start) child.assign_city(0,random_city) for i in range(len(route_2)): random_city = route_2.get_city(i) if random_city not in child.route: child.assign_city(k, random_city) k += 1 return child
def crossover(self, route_1, route_2): # YOUR CODE HERE child = Route(self.cities) initial_State = int(random.random() * len(route_1)) final_State = int(random.random() * len(route_1)) for k in range(0, len(child)): if (initial_State < final_State and k > initial_State and k < final_State): child.assign_city(k, route_1.get_city(k)) elif initial_State > final_State: if not (k < initial_State and k > final_State): child.assign_city(k, route_1.get_city(k)) for k in range(0, len(route_2)): if not child.__contains__(route_2.get_city(k)): for m in range(0, len(child)): if (child.get_city(m) == None): child.assign_city(m, route_2.get_city(k)) break return child